typesdigital commited on
Commit
a84bdff
·
1 Parent(s): d860ba6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -32
app.py CHANGED
@@ -1,36 +1,55 @@
 
 
1
  import requests
2
- import streamlit as st
3
- from datetime import datetime
4
 
5
- # Define function to fetch weather data
6
- def fetch_weather_data(api_key, city):
7
- url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
 
 
8
  response = requests.get(url)
9
  data = response.json()
10
- return data
11
-
12
- # Define function to format time
13
- def format_time(timestamp):
14
- dt_object = datetime.fromtimestamp(timestamp)
15
- return dt_object.strftime("%Y-%m-%d %H:%M:%S")
16
-
17
- # Set title
18
- st.title("Weather App")
19
-
20
- # Get API key and city from user
21
- api_key = st.text_input("1aafc3163909c1493596da9340e00aee")
22
- city = st.text_input("Enter the name of a city")
23
-
24
- # Fetch weather data and display it
25
- if api_key and city:
26
- data = fetch_weather_data(api_key, city)
27
- if data["cod"] == 200:
28
- st.write(f"Weather in {city}: {data['weather'][0]['description']}")
29
- st.write(f"Temperature: {data['main']['temp']}°C")
30
- st.write(f"Feels like: {data['main']['feels_like']}°C")
31
- st.write(f"Humidity: {data['main']['humidity']}%")
32
- st.write(f"Wind speed: {data['wind']['speed']} m/s")
33
- st.write(f"Sunrise: {format_time(data['sys']['sunrise'])}")
34
- st.write(f"Sunset: {format_time(data['sys']['sunset'])}")
35
- else:
36
- st.write("Invalid API key or city name")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()