Update app.py
Browse files
app.py
CHANGED
|
@@ -2,15 +2,10 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
from datetime import datetime, timedelta
|
| 4 |
|
| 5 |
-
# Weather API Key
|
| 6 |
API_KEY = "91b23cab82ee530b2052c8757e343b0d"
|
| 7 |
|
| 8 |
-
# OpenAI API Key and Endpoint
|
| 9 |
-
OPENAI_API_KEY = "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"
|
| 10 |
-
OPENAI_ENDPOINT = "https://api.openai.com/v1/engines/davinci-completion/completions"
|
| 11 |
-
|
| 12 |
def kelvin_to_celsius(temp_kelvin):
|
| 13 |
-
return int(temp_kelvin - 273.15)
|
| 14 |
|
| 15 |
def get_weather(city_name):
|
| 16 |
base_url = "http://api.openweathermap.org/data/2.5/weather?"
|
|
@@ -24,34 +19,14 @@ def get_weather(city_name):
|
|
| 24 |
temperature = kelvin_to_celsius(main_data.get("temp", 0))
|
| 25 |
weather_description = weather_data.get("description", "N/A")
|
| 26 |
timezone = data.get("timezone", 0)
|
| 27 |
-
local_time = (datetime.utcnow() + timedelta(seconds=timezone)).strftime('%H:%M')
|
| 28 |
return temperature, weather_description, local_time
|
| 29 |
else:
|
| 30 |
return None
|
| 31 |
|
| 32 |
-
def translate_to_neapolitan(text):
|
| 33 |
-
headers = {
|
| 34 |
-
'Authorization': f'Bearer {OPENAI_API_KEY}',
|
| 35 |
-
'Content-Type': 'application/json',
|
| 36 |
-
'User-Agent': 'OpenAI-Gradio-App'
|
| 37 |
-
}
|
| 38 |
-
|
| 39 |
-
data = {
|
| 40 |
-
'prompt': f'Translate this to Neapolitan dialect: {text}',
|
| 41 |
-
'max_tokens': 150
|
| 42 |
-
}
|
| 43 |
-
|
| 44 |
-
response = requests.post(OPENAI_ENDPOINT, headers=headers, json=data)
|
| 45 |
-
response_data = response.json()
|
| 46 |
-
|
| 47 |
-
if response.status_code == 200:
|
| 48 |
-
return response_data['choices'][0]['text'].strip()
|
| 49 |
-
else:
|
| 50 |
-
return "Error in translation."
|
| 51 |
-
|
| 52 |
def summarize_weather(first_city, second_city):
|
| 53 |
-
first_city =
|
| 54 |
-
second_city =
|
| 55 |
|
| 56 |
weather1 = get_weather(first_city)
|
| 57 |
weather2 = get_weather(second_city)
|
|
@@ -59,21 +34,23 @@ def summarize_weather(first_city, second_city):
|
|
| 59 |
if weather1 and weather2:
|
| 60 |
summary = (f"In {first_city} it's {weather1[2]} and {weather1[1]} with {weather1[0]}°C, "
|
| 61 |
f"while in {second_city} it's {weather2[2]} and {weather2[1]} with {weather2[0]}°C.")
|
| 62 |
-
|
| 63 |
-
return translated_summary
|
| 64 |
else:
|
| 65 |
return "Error fetching weather data for one or both cities."
|
| 66 |
|
|
|
|
|
|
|
|
|
|
| 67 |
interface = gr.Interface(
|
| 68 |
fn=summarize_weather,
|
| 69 |
inputs=[
|
| 70 |
-
gr.Textbox(placeholder="Enter First City"),
|
| 71 |
-
gr.Textbox(placeholder="Enter Second City")
|
| 72 |
],
|
| 73 |
outputs="text",
|
|
|
|
| 74 |
title="Weather Comparison",
|
| 75 |
-
description="Compare local time and temperature in Celsius between two cities."
|
| 76 |
-
theme="monochrome"
|
| 77 |
)
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import requests
|
| 3 |
from datetime import datetime, timedelta
|
| 4 |
|
|
|
|
| 5 |
API_KEY = "91b23cab82ee530b2052c8757e343b0d"
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def kelvin_to_celsius(temp_kelvin):
|
| 8 |
+
return int(temp_kelvin - 273.15) # Convert to int to remove decimals
|
| 9 |
|
| 10 |
def get_weather(city_name):
|
| 11 |
base_url = "http://api.openweathermap.org/data/2.5/weather?"
|
|
|
|
| 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') # Removed seconds
|
| 23 |
return temperature, weather_description, local_time
|
| 24 |
else:
|
| 25 |
return None
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
def summarize_weather(first_city, second_city):
|
| 28 |
+
first_city = ' '.join(word.capitalize() for word in first_city.split())
|
| 29 |
+
second_city = ' '.join(word.capitalize() for word in second_city.split())
|
| 30 |
|
| 31 |
weather1 = get_weather(first_city)
|
| 32 |
weather2 = get_weather(second_city)
|
|
|
|
| 34 |
if weather1 and weather2:
|
| 35 |
summary = (f"In {first_city} it's {weather1[2]} and {weather1[1]} with {weather1[0]}°C, "
|
| 36 |
f"while in {second_city} it's {weather2[2]} and {weather2[1]} with {weather2[0]}°C.")
|
| 37 |
+
return summary
|
|
|
|
| 38 |
else:
|
| 39 |
return "Error fetching weather data for one or both cities."
|
| 40 |
|
| 41 |
+
# Fetch the "Seafoam" theme from the Gradio Theme Gallery
|
| 42 |
+
theme = gr.Theme.from_hub("gradio/Monochrome")
|
| 43 |
+
|
| 44 |
interface = gr.Interface(
|
| 45 |
fn=summarize_weather,
|
| 46 |
inputs=[
|
| 47 |
+
gr.Textbox(placeholder="Enter First City", label="First City"),
|
| 48 |
+
gr.Textbox(placeholder="Enter Second City", label="Second City")
|
| 49 |
],
|
| 50 |
outputs="text",
|
| 51 |
+
theme=theme,
|
| 52 |
title="Weather Comparison",
|
| 53 |
+
description="Compare local time and temperature in Celsius between two cities."
|
|
|
|
| 54 |
)
|
| 55 |
|
| 56 |
if __name__ == "__main__":
|