import gradio as gr import requests def get_weather(city): API_URL = "https://api-inference.huggingface.co/models/YOUR_HF_MODEL" API_KEY = "your_huggingface_api_key_here" # Replace with your valid Hugging Face API key if not API_KEY or API_KEY == "your_huggingface_api_key_here": return "Error: API key is missing or invalid. Please check your Hugging Face API key." headers = {"Authorization": f"Bearer {API_KEY}"} payload = {"inputs": city} response = requests.post(API_URL, headers=headers, json=payload) if response.status_code == 200: try: data = response.json() if isinstance(data, list) and len(data) > 0 and "generated_text" in data[0]: forecast = data[0]["generated_text"] return f"Weather forecast for {city}, Pakistan: {forecast}" else: return "Invalid response received from API. Please try again." except Exception as e: return f"Error processing response: {str(e)}" elif response.status_code == 400: return "Bad request. Please check the input format and try again." elif response.status_code == 401: return "Unauthorized request. Check your API key and ensure it is correct and activated." elif response.status_code == 404: return "Model not found. Verify the model ID on Hugging Face." else: return f"Error fetching weather data. Status code: {response.status_code}, Message: {response.text}" iface = gr.Interface( fn=get_weather, inputs=gr.Textbox(placeholder="Enter city name in Pakistan"), outputs="text", title="Weather Forecast for Pakistan", description="Enter a city name in Pakistan to get the weather forecast using a Hugging Face model." ) if __name__ == "__main__": iface.launch()