Spaces:
Runtime error
Runtime error
Commit
·
a84bdff
1
Parent(s):
d860ba6
Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,55 @@
|
|
|
|
|
|
1 |
import requests
|
2 |
-
import
|
3 |
-
from datetime import datetime
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
8 |
response = requests.get(url)
|
9 |
data = response.json()
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|