File size: 2,358 Bytes
4ce246c
156235b
41b14c2
707fafa
989864f
bd817ed
dffd221
 
41b14c2
16ce4ec
41b14c2
707fafa
 
 
 
 
 
8af5b38
9fc5504
8af5b38
41b14c2
bd817ed
4ce246c
 
bd817ed
b72ccd7
4ce246c
b72ccd7
8af5b38
989864f
4ce246c
 
 
 
 
b72ccd7
 
 
989864f
b72ccd7
bd817ed
 
4ce246c
b72ccd7
4ce246c
9d3c1c5
4ce246c
 
b72ccd7
 
8af5b38
 
4ce246c
 
9d3c1c5
dffd221
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
import gradio as gr
import os
import requests
from groq import Groq 

def preprocess_city_name_and_generate_insight(city_name):
    api_key = os.getenv("GROQ_API_KEY")
    if not api_key:
        return city_name, "GROQ API key is not set in the environment variables."

    try:
        client = Groq(api_key=api_key)

        prompt = f"Generate a short, interesting fact about {city_name} in less than 10 words."
        result = client.query(prompt=prompt, model_id="llama3-70b-8192")  # Adjust parameters as necessary

        insight = result.get('generated_text', "No insight generated.").strip()
    except Exception as e:
        print(f"Error generating insight: {str(e)}")
        insight = "Could not generate insight due to an error."

    return city_name, insight

def get_weather(city_name):
    corrected_city_name, city_insight = preprocess_city_name_and_generate_insight(city_name)
    API_Key = os.getenv("OPENWEATHER_API_KEY")
    if not API_Key:
        return "API Key is not set. Please set the OPENWEATHER_API_KEY environment variable."
    
    url = f'https://api.openweathermap.org/data/2.5/weather?q={corrected_city_name}&appid={API_Key}&units=metric'
    response = requests.get(url)
    
    if response.status_code == 200:
        data = response.json()
        weather = data['weather'][0]['description']
        temp = data['main']['temp']
        humidity = data['main']['humidity']
        weather_emoji = "☀️" if "clear" in weather else "☁️" if "cloud" in weather else "🌧️" if "rain" in weather else "❄️" if "snow" in weather else "🌫️"
        return (f"In {corrected_city_name}, the weather is currently {weather} {weather_emoji}. "
                f"The temperature is a comfy {temp}°C 🌡️. "
                f"And the humidity levels are at {humidity}% 💧. "
                f"Did you know? {city_insight}")
    else:
        return "Failed to retrieve data. Please check the city name and try again."

# Defining Gradio interface
iface = gr.Interface(
    fn=get_weather,
    inputs=gr.Textbox(label="Enter City Name", placeholder="Type here..."),
    outputs=gr.Textbox(label="Weather Update"),
    title="WeatherAssistantApp",
    description="Enter a city name to get a lively description of the current weather, temperature, and humidity."
)

# Launching the interface
iface.launch()