Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,22 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def kelvin_to_celsius(temp_kelvin):
|
| 5 |
return temp_kelvin - 273.15
|
| 6 |
|
| 7 |
def get_weather(city_name, api_key):
|
| 8 |
-
|
| 9 |
-
complete_url = base_url + "q=" + city_name + "&appid=" + api_key
|
| 10 |
-
response = requests.get(complete_url)
|
| 11 |
-
data = response.json()
|
| 12 |
-
|
| 13 |
-
if data.get("cod") == 200:
|
| 14 |
-
main_data = data.get("main", {})
|
| 15 |
-
weather_data = data.get("weather", [{}])[0]
|
| 16 |
-
temperature = kelvin_to_celsius(main_data.get("temp", 0))
|
| 17 |
-
pressure = main_data.get("pressure", "N/A")
|
| 18 |
-
humidity = main_data.get("humidity", "N/A")
|
| 19 |
-
weather_description = weather_data.get("description", "N/A")
|
| 20 |
-
return temperature, pressure, humidity, weather_description
|
| 21 |
-
else:
|
| 22 |
-
return None
|
| 23 |
|
| 24 |
def summarize_text(text):
|
| 25 |
-
"""Summarize the provided text using OpenAI
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
function_name="Completion.create",
|
| 29 |
-
api_key='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'
|
| 30 |
-
)
|
| 31 |
-
response = completion_function(engine="davinci", prompt=f"Summarize the following weather comparison:\n\n{text}", max_tokens=150)
|
| 32 |
-
return response['choices'][0]['text'].strip()
|
| 33 |
|
| 34 |
def compare_weather(city1, city2):
|
| 35 |
-
|
| 36 |
-
weather1 = get_weather(city1, api_key)
|
| 37 |
-
weather2 = get_weather(city2, api_key)
|
| 38 |
-
|
| 39 |
-
if weather1 and weather2:
|
| 40 |
-
comparison = f"""
|
| 41 |
-
{city1}:
|
| 42 |
-
Temperature: {weather1[0]:.2f}°C
|
| 43 |
-
Pressure: {weather1[1]} hPa
|
| 44 |
-
Humidity: {weather1[2]}%
|
| 45 |
-
Condition: {weather1[3]}
|
| 46 |
-
|
| 47 |
-
{city2}:
|
| 48 |
-
Temperature: {weather2[0]:.2f}°C
|
| 49 |
-
Pressure: {weather2[1]} hPa
|
| 50 |
-
Humidity: {weather2[2]}%
|
| 51 |
-
Condition: {weather2[3]}
|
| 52 |
-
"""
|
| 53 |
-
summary = summarize_text(comparison)
|
| 54 |
-
return summary
|
| 55 |
-
else:
|
| 56 |
-
return "Error fetching weather data for one or both cities."
|
| 57 |
|
| 58 |
# Sample cities for dropdown
|
| 59 |
cities = ["New York", "Los Angeles", "London", "Paris", "Tokyo", "Sydney", "Moscow", "Mumbai", "Cape Town", "Rio de Janeiro"]
|
|
@@ -61,11 +24,10 @@ cities = ["New York", "Los Angeles", "London", "Paris", "Tokyo", "Sydney", "Mosc
|
|
| 61 |
interface = gr.Interface(
|
| 62 |
fn=compare_weather,
|
| 63 |
inputs=[
|
| 64 |
-
gr.
|
| 65 |
-
gr.
|
| 66 |
],
|
| 67 |
outputs="text"
|
| 68 |
)
|
| 69 |
|
| 70 |
-
if
|
| 71 |
-
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
+
import openai
|
| 4 |
+
|
| 5 |
+
openai.api_key = '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'
|
| 6 |
|
| 7 |
def kelvin_to_celsius(temp_kelvin):
|
| 8 |
return temp_kelvin - 273.15
|
| 9 |
|
| 10 |
def get_weather(city_name, api_key):
|
| 11 |
+
# ... (same as before)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def summarize_text(text):
|
| 14 |
+
"""Summarize the provided text using OpenAI."""
|
| 15 |
+
response = openai.Completion.create(engine="davinci", prompt=f"Summarize the following weather comparison:\n\n{text}", max_tokens=150)
|
| 16 |
+
return response.choices[0].text.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def compare_weather(city1, city2):
|
| 19 |
+
# ... (same as before)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Sample cities for dropdown
|
| 22 |
cities = ["New York", "Los Angeles", "London", "Paris", "Tokyo", "Sydney", "Moscow", "Mumbai", "Cape Town", "Rio de Janeiro"]
|
|
|
|
| 24 |
interface = gr.Interface(
|
| 25 |
fn=compare_weather,
|
| 26 |
inputs=[
|
| 27 |
+
gr.components.Dropdown(choices=cities, label="City 1"),
|
| 28 |
+
gr.components.Dropdown(choices=cities, label="City 2")
|
| 29 |
],
|
| 30 |
outputs="text"
|
| 31 |
)
|
| 32 |
|
| 33 |
+
if __name
|
|
|