File size: 1,861 Bytes
0f785db f61a667 9abb26a 0f785db 9abb26a 379b7ce 16ddbf2 0f785db 9abb26a 16ddbf2 9abb26a 16ddbf2 379b7ce 16ddbf2 379b7ce 16ddbf2 379b7ce 16ddbf2 379b7ce 16ddbf2 0f785db 16ddbf2 9abb26a 379b7ce 9abb26a 16ddbf2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
import os
import gradio as gr
import requests
import openai
# Get OpenAI API key from environment variable
openai_api_key = os.environ.get("OPENAI_API_KEY")
if not openai_api_key:
raise ValueError("OpenAI API key not found!")
# Authenticate with OpenAI using your API key
openai.api_key = openai_api_key
def kelvin_to_celsius(temp_kelvin):
return temp_kelvin - 273.15
def get_weather(city_name, api_key):
# ... [same as your previous code] ...
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."
# Sample cities for dropdown
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() |