Spaces:
Runtime error
Runtime error
Commit
·
a579209
1
Parent(s):
3bc729f
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import requests
|
| 4 |
+
import datetime
|
| 5 |
+
|
| 6 |
+
openai.api_key = "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"
|
| 7 |
+
weather_api_key = "1aafc3163909c1493596da9340e00aee"
|
| 8 |
+
|
| 9 |
+
def get_weather_data(city):
|
| 10 |
+
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={weather_api_key}&units=metric"
|
| 11 |
+
response = requests.get(url)
|
| 12 |
+
data = response.json()
|
| 13 |
+
|
| 14 |
+
if data["cod"] == "404":
|
| 15 |
+
return "City not found."
|
| 16 |
+
|
| 17 |
+
weather = data["weather"][0]["description"]
|
| 18 |
+
temperature = data["main"]["temp"]
|
| 19 |
+
location = data["name"]
|
| 20 |
+
time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 21 |
+
|
| 22 |
+
return f"Location: {location}\nTime: {time}\nWeather: {weather}\nTemperature: {temperature}°C"
|
| 23 |
+
|
| 24 |
+
def get_weather_feedback(user_input):
|
| 25 |
+
response = openai.Completion.create(
|
| 26 |
+
engine="text-davinci-003",
|
| 27 |
+
prompt=f"The weather today is {user_input}.",
|
| 28 |
+
max_tokens=50,
|
| 29 |
+
n=1,
|
| 30 |
+
stop=None,
|
| 31 |
+
temperature=0.6
|
| 32 |
+
)
|
| 33 |
+
feedback = response.choices[0].text.strip()
|
| 34 |
+
return feedback
|
| 35 |
+
|
| 36 |
+
iface = gr.Interface(
|
| 37 |
+
fn=get_weather_data,
|
| 38 |
+
inputs="text",
|
| 39 |
+
outputs="text",
|
| 40 |
+
title="Weather Forecast",
|
| 41 |
+
description="Enter the name of a city to get the weather forecast.",
|
| 42 |
+
examples=[["New York"], ["London"], ["Tokyo"]]
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
feedback_iface = gr.Interface(
|
| 46 |
+
fn=get_weather_feedback,
|
| 47 |
+
inputs="text",
|
| 48 |
+
outputs="text",
|
| 49 |
+
title="Feedback",
|
| 50 |
+
description="Enter a weather description to get feedback.",
|
| 51 |
+
examples=[["sunny"], ["rainy"], ["cloudy"]]
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
iface.launch()
|