import gradio as gr import requests import os from groq import Groq # Assuming Groq or a similar LLM can provide insights def get_weather_and_insight(city_name): # Initialize the Groq client with your API key groq_client = Groq(api_key=os.getenv("GROQ_API_KEY")) # Use Groq to get a short insight about the city insight = groq_client.get_insight(city_name, max_words=10) # Assumed method and parameter 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={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"🔍 {insight} | In {city_name}, it's {weather} {weather_emoji}. " f"Temperature: {temp}°C 🌡️, Humidity: {humidity}% 💧") else: return "Failed to retrieve data. Please check the city name and try again." # Define Gradio interface iface = gr.Interface( fn=get_weather_and_insight, inputs=gr.Textbox(label="Enter City Name", placeholder="Type here..."), outputs=gr.Textbox(label="Weather Update and Insight"), title="WeatherAssistantApp", description="Enter a city name to get an interesting insight and the current weather details." ) # Launch the interface iface.launch()