mbalvi's picture
Create app.py
0b88d2d verified
"""
app.py - Simple Weather Forecast Web App (Demo)
------------------------------------------------
This is a beginner-friendly Gradio app for Hugging Face Spaces.
⚠️ NOTE:
This demo does not use real-time weather APIs (so no API key needed).
It generates a mock forecast based on simple rules.
You can later connect it to real APIs like OpenWeatherMap.
How to run (in Hugging Face Spaces or Colab):
1. pip install -r requirements.txt
2. python app.py
3. Open the public link.
"""
import gradio as gr
import random
from datetime import datetime, timedelta
# --- Simple mock forecast generator ---
def get_mock_forecast(city, days):
if not city or days <= 0:
return "❌ Please enter a city and valid number of days."
forecasts = []
conditions = ["☀️ Sunny", "🌤️ Partly Cloudy", "🌧️ Rainy", "🌩️ Thunderstorm", "❄️ Snow"]
today = datetime.now()
for i in range(days):
date = (today + timedelta(days=i)).strftime("%A, %d %b %Y")
temp = random.randint(10, 40) # fake temperature in Celsius
condition = random.choice(conditions)
forecasts.append(f"{date}{condition}, {temp}°C")
return "\n".join(forecasts)
# --- Gradio UI ---
with gr.Blocks() as demo:
gr.Markdown("## 🌦️ Simple Weather Forecast App (Demo)")
with gr.Row():
city = gr.Textbox(label="Enter City", placeholder="e.g. London")
days = gr.Slider(1, 7, value=3, step=1, label="Forecast Days")
forecast_btn = gr.Button("Get Forecast")
output = gr.Textbox(label="Forecast", lines=8)
forecast_btn.click(get_mock_forecast, inputs=[city, days], outputs=output)
# Launch app
if __name__ == "__main__":
demo.launch()