|
|
import os |
|
|
import gradio as gr |
|
|
import requests |
|
|
import openai |
|
|
|
|
|
|
|
|
openai_api_key = os.environ.get("OPENAI_API_KEY") |
|
|
if not openai_api_key: |
|
|
raise ValueError("OpenAI API key not found!") |
|
|
|
|
|
|
|
|
openai.api_key = openai_api_key |
|
|
|
|
|
def kelvin_to_celsius(temp_kelvin): |
|
|
return temp_kelvin - 273.15 |
|
|
|
|
|
def get_weather(city_name, api_key): |
|
|
|
|
|
|
|
|
def summarize_text(text): |
|
|
"""Summarize the provided text using OpenAI.""" |
|
|
response = openai.Completion.create(engine="davinci", prompt=f"Summarize the following weather comparison:\n\n{text}", max_tokens=150) |
|
|
return response.choices[0].text.strip() |
|
|
|
|
|
def compare_weather(city1, city2): |
|
|
api_key = "91b23cab82ee530b2052c8757e343b0d" |
|
|
weather1 = get_weather(city1, api_key) |
|
|
weather2 = get_weather(city2, api_key) |
|
|
|
|
|
if weather1 and weather2: |
|
|
comparison = f""" |
|
|
{city1}: |
|
|
Temperature: {weather1[0]:.2f}°C |
|
|
Pressure: {weather1[1]} hPa |
|
|
Humidity: {weather1[2]}% |
|
|
Condition: {weather1[3]} |
|
|
|
|
|
{city2}: |
|
|
Temperature: {weather2[0]:.2f}°C |
|
|
Pressure: {weather2[1]} hPa |
|
|
Humidity: {weather2[2]}% |
|
|
Condition: {weather2[3]} |
|
|
""" |
|
|
summary = summarize_text(comparison) |
|
|
return summary |
|
|
else: |
|
|
return "Error fetching weather data for one or both cities." |
|
|
|
|
|
|
|
|
cities = ["New York", "Los Angeles", "London", "Paris", "Tokyo", "Sydney", "Moscow", "Mumbai", "Cape Town", "Rio de Janeiro"] |
|
|
|
|
|
interface = gr.Interface( |
|
|
fn=compare_weather, |
|
|
inputs=[ |
|
|
gr.inputs.Dropdown(choices=cities, label="City 1"), |
|
|
gr.inputs.Dropdown(choices=cities, label="City 2") |
|
|
], |
|
|
outputs="text" |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
interface.launch() |