Update app.py
Browse files
app.py
CHANGED
|
@@ -2,16 +2,14 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
from datetime import datetime, timedelta
|
| 4 |
|
| 5 |
-
|
| 6 |
-
API_KEY = '91b23cab82ee530b2052c8757e343b0d'
|
| 7 |
-
OPENAI_API_KEY = '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'
|
| 8 |
|
| 9 |
def kelvin_to_celsius(temp_kelvin):
|
| 10 |
return temp_kelvin - 273.15
|
| 11 |
|
| 12 |
-
def get_weather(city_name
|
| 13 |
base_url = "http://api.openweathermap.org/data/2.5/weather?"
|
| 14 |
-
complete_url = base_url + "q=" + city_name + "&appid=" +
|
| 15 |
response = requests.get(complete_url)
|
| 16 |
data = response.json()
|
| 17 |
|
|
@@ -20,59 +18,31 @@ def get_weather(city_name, api_key):
|
|
| 20 |
weather_data = data.get("weather", [{}])[0]
|
| 21 |
temperature = kelvin_to_celsius(main_data.get("temp", 0))
|
| 22 |
weather_description = weather_data.get("description", "N/A")
|
| 23 |
-
timezone = data.get("timezone", 0)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
local_time = datetime.utcnow() + timedelta(seconds=timezone)
|
| 27 |
-
formatted_time = local_time.strftime('%H:%M:%S') # 24-hour format
|
| 28 |
-
|
| 29 |
-
return temperature, weather_description, formatted_time
|
| 30 |
else:
|
| 31 |
return None
|
| 32 |
|
| 33 |
-
def openai_create(prompt):
|
| 34 |
-
openai_url = "https://api.openai.com/v1/engines/text-davinci-003/completions"
|
| 35 |
-
headers = {
|
| 36 |
-
"Authorization": f"Bearer {OPENAI_API_KEY}",
|
| 37 |
-
"Content-Type": "application/json"
|
| 38 |
-
}
|
| 39 |
-
data = {
|
| 40 |
-
"prompt": prompt,
|
| 41 |
-
"temperature": 0.7,
|
| 42 |
-
"max_tokens": 256,
|
| 43 |
-
"top_p": 1,
|
| 44 |
-
"frequency_penalty": 0,
|
| 45 |
-
"presence_penalty": 0.6,
|
| 46 |
-
"stop": ["\n"]
|
| 47 |
-
}
|
| 48 |
-
response = requests.post(openai_url, headers=headers, json=data)
|
| 49 |
-
response_data = response.json()
|
| 50 |
-
summary = response_data['choices'][0]['text'].strip()
|
| 51 |
-
return summary
|
| 52 |
-
|
| 53 |
def summarize_weather(city1, city2):
|
| 54 |
-
weather1 = get_weather(city1
|
| 55 |
-
weather2 = get_weather(city2
|
| 56 |
|
| 57 |
if weather1 and weather2:
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
In {city1}, the current weather is {weather1[1]} with a temperature of {weather1[0]:.2f}°C. The local time is {weather1[2]}.
|
| 61 |
-
Meanwhile, in {city2}, it's {weather2[1]} and the temperature is {weather2[0]:.2f}°C. The local time there is {weather2[2]}.
|
| 62 |
-
"""
|
| 63 |
-
summary = openai_create(comparison)
|
| 64 |
return summary
|
| 65 |
else:
|
| 66 |
return "Error fetching weather data for one or both cities."
|
| 67 |
|
| 68 |
-
# Gradio interface
|
| 69 |
interface = gr.Interface(
|
| 70 |
fn=summarize_weather,
|
| 71 |
inputs=[
|
| 72 |
-
gr.Textbox(placeholder="Enter City 1
|
| 73 |
-
gr.Textbox(placeholder="Enter City 2
|
| 74 |
],
|
| 75 |
outputs="text"
|
| 76 |
)
|
| 77 |
|
| 78 |
-
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
from datetime import datetime, timedelta
|
| 4 |
|
| 5 |
+
API_KEY = "91b23cab82ee530b2052c8757e343b0d"
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def kelvin_to_celsius(temp_kelvin):
|
| 8 |
return temp_kelvin - 273.15
|
| 9 |
|
| 10 |
+
def get_weather(city_name):
|
| 11 |
base_url = "http://api.openweathermap.org/data/2.5/weather?"
|
| 12 |
+
complete_url = base_url + "q=" + city_name + "&appid=" + API_KEY
|
| 13 |
response = requests.get(complete_url)
|
| 14 |
data = response.json()
|
| 15 |
|
|
|
|
| 18 |
weather_data = data.get("weather", [{}])[0]
|
| 19 |
temperature = kelvin_to_celsius(main_data.get("temp", 0))
|
| 20 |
weather_description = weather_data.get("description", "N/A")
|
| 21 |
+
timezone = data.get("timezone", 0)
|
| 22 |
+
local_time = (datetime.utcnow() + timedelta(seconds=timezone)).strftime('%H:%M:%S')
|
| 23 |
+
return temperature, weather_description, local_time
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
else:
|
| 25 |
return None
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
def summarize_weather(city1, city2):
|
| 28 |
+
weather1 = get_weather(city1)
|
| 29 |
+
weather2 = get_weather(city2)
|
| 30 |
|
| 31 |
if weather1 and weather2:
|
| 32 |
+
summary = (f"In {city1} it's {weather1[2]} and {weather1[1]} with {weather1[0]:.2f}°C, "
|
| 33 |
+
f"while in {city2} it's {weather2[2]} and {weather2[1]} with {weather2[0]:.2f}°C.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
return summary
|
| 35 |
else:
|
| 36 |
return "Error fetching weather data for one or both cities."
|
| 37 |
|
|
|
|
| 38 |
interface = gr.Interface(
|
| 39 |
fn=summarize_weather,
|
| 40 |
inputs=[
|
| 41 |
+
gr.Textbox(placeholder="Enter City 1"),
|
| 42 |
+
gr.Textbox(placeholder="Enter City 2")
|
| 43 |
],
|
| 44 |
outputs="text"
|
| 45 |
)
|
| 46 |
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
interface.launch()
|