Weather Forecast Text
A few weeks ago, I was delighted to receive beta access to OpenAI’s groundbreaking new model, GPT-3. The model, which for the foreseeable future will only be accessible via API and not open source, is a truly potent tool for implementing various text-generation applications with an almost human-like syntax. OpenAI has reasoned that its potential for misuse requires stringent access control.
OpenAI and GPT-3: A Brief Overview
OpenAI, co-founded by tech visionary Elon Musk, AI researchers and Sam Altman, is a leading artificial intelligence research institution. GPT-3 (Generative Pre-trained Transformer 3) is its latest masterpiece. This third iteration, as the name suggests, is a tool proficient in generating text using a pre-trained neural network. Currently hailed as one of the most significant and practical advances in AI for years, GPT-3 has been trained on approximately 570GB of text data sourced from the public internet (like Wikipedia, for instance). This blog post will not delve too deeply into the mechanics of GPT-3 but is intended to stimulate interest and demonstrate how seamlessly pre-trained neural networks can be employed.
Text generation for the weather domain
After some initial experimentation with horoscopes and code generation (yes, GPT-3 can actually write programming code), I was keen to find a practical application closer to my work. For media companies and newspapers, we currently use a rule-based algorithm to generate texts. This algorithm, honed over the years, is quite potent. Curiosity piqued, I wanted to gauge the effort needed to implement a Minimum Viable Product (MVP) using GPT-3. Surprisingly, it took all of 30 minutes! Programming GPT-3 is akin to tutoring an exceptionally sharp student. All it takes is a clear task explanation, a demonstration, and the ‘student’ should be able to transfer the learning even when parameters are slightly changed.
Turning JSON into Multilingual Weather Forecasts
To generate a weather forecast, we need certain data about the current weather. Both the weather data and the output are provided in JSON format. The task description and relevant information were relayed to the network, followed by an example. This example comprises nine different input fields and one output field for each language.
Input:
{
"date":"today",
"location":"Wiesbaden",
"temperature":17.3,
"weather":"windy",
"windspeed":"24 m/s",
"winddirection":"west",
"humidity":56.4,
"languages":[
"english",
"german"
]
}
Output:
{
"english": "Today, the weather in Wiesbaden is windy, the temperature is 17.3 degree Celsius and the humidity is 56.4 percent. The wind blows from west with a speed of 24 m/s.",
"german": "Heute ist das Wetter in Wiesbaden recht windig, die Temperaturen bewegen sich bei 17.3 Grad Celsius und die Luftfeuchtigkeit beträgt 56.4 Prozent. Der Wind weht mit etwa 24 m/s von Westen."
}
Next, I tasked the network with generating a similar report on its own, making the task more challenging by modifying all the fields and introducing “french” and “spanish” to the list of languages.
Example Task:
{
"date":"today",
"location":"Wiesbaden",
"temperature":17.3,
"weather":"windy",
"windspeed":"24 m/s",
"winddirection":"west",
"humidity":56.4,
"languages":[
"english",
"german"
]
}
Test Task
{
"date":"tomorrow",
"location":"New York",
"temperature":14.3,
"weather":"clear sky",
"windspeed":"14 m/s",
"winddirection":"east",
"humidity":46.3,
"languages":[
"english",
"german",
"french",
"spain"
]
}
The output was astonishing. The model accomplished exactly what we intended, even incorporating the two additional languages.
{
"english": "Tomorrow, the weather in New York is clear sky, the temperature is 14.3 degree Celsius and the humidity is 46.3 percent. The wind blows from east with a speed of 14 m/s.",
"german":"Morgen ist das Wetter in New York klar, die Temperaturen bewegen sich bei 14.3 Grad Celsius und die Luftfeuchtigkeit beträgt 46.3 Prozent. Der Wind weht mit etwa 14 m/s von Ost.",
"french": "Demain, le temps à New York est clair, la température est de 14.3 degrés Celsius et la humidité est de 46.3 pour cent. Le vent souffle d'est avec une vitesse de 14 m/s.",
"spain": "Mañana, el tiempo en New York es claro, la temperatura es de 14.3 grados Celsius y la humedad es de 46.3 por ciento. El viento sopla de este con una velocidad de 14 m/s."
}
The following video showcases the model in action, recorded from the OpenAI playground:
Let’s take the next step in automating text generation and create a simple user interface form.
'''
GPT3 app for weather text generation
(c) Marvin Gabler 2020
'''
import openai
import streamlit as st
import json
import datetime
from config import openai_key
openai.api_key = openai_key
prompt_template = '''We will create a weather forecast text from a weather forecast json document.
The document contains the date, location, the weather parameters and the languages to generate.
The text should be between 20 and 30 words.
In:
{
"date":"15. November 2020",
"location":"Wiesbaden",
"temperature":17.3,
"weather":"windy",
"windspeed":24,
"winddirection":"west",
"humidity":56.4,
"languages":
[
"english",
"german"
]
}
{
"english": "On 15th November the weather in Wiesbaden is windy,
the temperature is 17.3 degree Celsius and the humidity
is 56.4 percent. The wind blows from west with a speed of 24 m/s.",
"german":"Am 15. November ist das Wetter in Wiesbaden recht windig,
die Temperaturen bewegen sich bei 17.3 Grad Celsius und die
Luftfeuchtigkeit beträgt 56.4 Prozent. Der Wind weht mit etwa 24 m/s
von Westen."
}
In: '''
def get_weathertext(date,location,temperature,weather,humidity,windspeed, winddirection, languages):
'''
Creates a weathertext from given parameters
Params:
date: str, human understandable (e.g. 11 November 2020 / tomorrow / 1.11.2020)
location: str
temperature: float
weather: str
humidity: float
windspeed: float
winddirection: str
languages: list of str
'''
weather_document = {"date":date,"location":location,
"temperature":temperature,"weather":weather,"windspeed":windspeed,
"winddirection":winddirection,"humidity":humidity,"languages":languages}
prompt = prompt_template + json.dumps(weather_document)
response = openai.Completion.create(engine="davinci",
prompt=prompt,
max_tokens=(len(languages)*80),
temperature=0.7,
stop="In:")
try:
desired_text = response["choices"][0]["text"]
print(desired_text)
desired_text = desired_text.replace("\nOut: ", "").replace(" Out: ","").replace("Out:","")
print(desired_text)
except Exception as e:
print(str(e))
desired_text = response
return weather_document, desired_text
def main():
'''
Main function that also creates the frontend
'''
# Hide the streamlit elements
hide_footer_style = """
<style>
.reportview-container .main footer {visibility: hidden;}
//.stApp .toolbar {visibility: hidden;}
</style>
"""
st.markdown(hide_footer_style, unsafe_allow_html=True)
st.write("Q.met GmbH © Marvin Gabler 2020")
st.title('Weather forecast text generator')
st.write("Using GPT3, the worlds most advanced AI.")
col1_1, col1_2 = st.beta_columns(2)
date = col1_1.date_input("Date")
location = col1_2.text_input("Location (city name)")
col2_1, col2_2, col2_3 = st.beta_columns(3)
temperature = col2_1.number_input("Temperature in Celsius")
weather = col2_2.text_input("Weather condition")
humidity = col2_3.number_input("Humidity in %")
col3_1, col3_2, col3_3 = st.beta_columns(3)
windspeed = col2_1.number_input("Windspeed in m/s")
winddirection = col2_2.selectbox("Winddirection",["North","West","East","South",
"Northwest","Northeast","Southwest","Southeast"])
languages = col2_3.multiselect("Language",["English","German","French","Spain",
"Dutch","Russian","Chinese","Italian"])
if st.button("Generate forecast text"):
with st.spinner(text="Generating forecast text"):
weather_document, forecast_text = get_weathertext(date.strftime("%-d. %B %Y"), location, temperature, weather, humidity,
windspeed, winddirection, languages)
st.title("Input")
st.json(weather_document)
st.title("Output")
st.json(forecast_text)
if __name__ == "__main__":
main()
$ streamlit run app.py
And we get:
I hope you enjoyed the article, have a great day.