typesdigital commited on
Commit
1eab79c
·
1 Parent(s): dc60bd7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -52
app.py CHANGED
@@ -1,55 +1,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()
 
 
 
1
  import requests
2
+
3
+ def get_weather_data(api_key, city):
4
+ base_url = "http://api.openweathermap.org/data/2.5/weather"
5
+ params = {"q": city, "appid": api_key, "units": "metric"}
6
+
7
+ response = requests.get(base_url, params=params)
8
+
9
+ if response.status_code == 200:
10
+ return response.json()
11
+ else:
12
+ print("Failed to fetch weather data.")
13
+ return None
14
+
15
+ def display_weather_info(weather_data):
16
+ if weather_data:
17
+ city = weather_data["name"]
18
+ weather = weather_data["weather"][0]["description"]
19
+ temperature = weather_data["main"]["temp"]
20
+ wind_speed = weather_data["wind"]["speed"]
21
+
22
+ print(f"Weather in {city}: {weather}")
23
+ print(f"Temperature: {temperature}°C")
24
+ print(f"Wind Speed: {wind_speed} m/s")
25
+ else:
26
+ print("Weather data is unavailable.")
27
+
28
+ def main():
29
+ api_key = "1aafc3163909c1493596da9340e00aee" # Replace with your OpenWeatherMap API key
30
+ city = input("Enter a city name: ")
31
+
32
+ weather_data = get_weather_data(api_key, city)
33
+ display_weather_info(weather_data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  if __name__ == "__main__":
36
+ main()