Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
app.py - Simple Weather Forecast Web App (Demo)
|
| 3 |
+
------------------------------------------------
|
| 4 |
+
This is a beginner-friendly Gradio app for Hugging Face Spaces.
|
| 5 |
+
|
| 6 |
+
⚠️ NOTE:
|
| 7 |
+
This demo does not use real-time weather APIs (so no API key needed).
|
| 8 |
+
It generates a mock forecast based on simple rules.
|
| 9 |
+
You can later connect it to real APIs like OpenWeatherMap.
|
| 10 |
+
|
| 11 |
+
How to run (in Hugging Face Spaces or Colab):
|
| 12 |
+
1. pip install -r requirements.txt
|
| 13 |
+
2. python app.py
|
| 14 |
+
3. Open the public link.
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
import gradio as gr
|
| 18 |
+
import random
|
| 19 |
+
from datetime import datetime, timedelta
|
| 20 |
+
|
| 21 |
+
# --- Simple mock forecast generator ---
|
| 22 |
+
def get_mock_forecast(city, days):
|
| 23 |
+
if not city or days <= 0:
|
| 24 |
+
return "❌ Please enter a city and valid number of days."
|
| 25 |
+
|
| 26 |
+
forecasts = []
|
| 27 |
+
conditions = ["☀️ Sunny", "🌤️ Partly Cloudy", "🌧️ Rainy", "🌩️ Thunderstorm", "❄️ Snow"]
|
| 28 |
+
|
| 29 |
+
today = datetime.now()
|
| 30 |
+
for i in range(days):
|
| 31 |
+
date = (today + timedelta(days=i)).strftime("%A, %d %b %Y")
|
| 32 |
+
temp = random.randint(10, 40) # fake temperature in Celsius
|
| 33 |
+
condition = random.choice(conditions)
|
| 34 |
+
forecasts.append(f"{date} — {condition}, {temp}°C")
|
| 35 |
+
|
| 36 |
+
return "\n".join(forecasts)
|
| 37 |
+
|
| 38 |
+
# --- Gradio UI ---
|
| 39 |
+
with gr.Blocks() as demo:
|
| 40 |
+
gr.Markdown("## 🌦️ Simple Weather Forecast App (Demo)")
|
| 41 |
+
|
| 42 |
+
with gr.Row():
|
| 43 |
+
city = gr.Textbox(label="Enter City", placeholder="e.g. London")
|
| 44 |
+
days = gr.Slider(1, 7, value=3, step=1, label="Forecast Days")
|
| 45 |
+
|
| 46 |
+
forecast_btn = gr.Button("Get Forecast")
|
| 47 |
+
output = gr.Textbox(label="Forecast", lines=8)
|
| 48 |
+
|
| 49 |
+
forecast_btn.click(get_mock_forecast, inputs=[city, days], outputs=output)
|
| 50 |
+
|
| 51 |
+
# Launch app
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
demo.launch()
|