typesdigital's picture
Update app.py
77db2fa
raw
history blame
1.46 kB
import gradio as gr
import openai
import requests
import datetime
def get_weather_data(city):
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={weather_api_key}&units=metric"
response = requests.get(url)
data = response.json()
if data["cod"] == "404":
return "City not found."
weather = data["weather"][0]["description"]
temperature = data["main"]["temp"]
location = data["name"]
time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
return f"Location: {location}\nTime: {time}\nWeather: {weather}\nTemperature: {temperature}°C"
def get_weather_feedback(user_input):
response = openai.Completion.create(
engine="text-davinci-003",
prompt=f"The weather today is {user_input}.",
max_tokens=50,
n=1,
stop=None,
temperature=0.6
)
feedback = response.choices[0].text.strip()
return feedback
iface = gr.Interface(
fn=get_weather_data,
inputs="text",
outputs="text",
title="Weather Forecast",
description="Enter the name of a city to get the weather forecast.",
examples=[["New York"], ["London"], ["Tokyo"]]
)
feedback_iface = gr.Interface(
fn=get_weather_feedback,
inputs="text",
outputs="text",
title="Feedback",
description="Enter a weather description to get feedback.",
examples=[["sunny"], ["rainy"], ["cloudy"]]
)
if __name__ == "__main__":
iface.launch()