Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Groq API Setup (Replace with your API Key)
|
| 6 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Hugging Face Secrets se set karo
|
| 7 |
+
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 8 |
+
|
| 9 |
+
# Flood Prediction via Groq AI
|
| 10 |
+
def predict_flood_risk(location, water_level, rainfall):
|
| 11 |
+
prompt = f"""
|
| 12 |
+
Flood Risk Assessment:
|
| 13 |
+
- Location: {location}
|
| 14 |
+
- Current Water Level: {water_level} meters
|
| 15 |
+
- Last 24h Rainfall: {rainfall} mm
|
| 16 |
+
- Historical Data: Moderate flood risk zone
|
| 17 |
+
|
| 18 |
+
Question: Should we issue a flood warning?
|
| 19 |
+
Reply format: "VERDICT: [HIGH/MEDIUM/LOW] | REASON: [1-line explanation]"
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
headers = {
|
| 23 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 24 |
+
"Content-Type": "application/json"
|
| 25 |
+
}
|
| 26 |
+
payload = {
|
| 27 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 28 |
+
"model": "mixtral-8x7b-32768" # Groq's fast model
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
response = requests.post(GROQ_API_URL, headers=headers, json=payload)
|
| 32 |
+
if response.status_code == 200:
|
| 33 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 34 |
+
else:
|
| 35 |
+
return f"Error: {response.text}"
|
| 36 |
+
|
| 37 |
+
# Gradio UI
|
| 38 |
+
with gr.Blocks(title="Flood Alert AI") as app:
|
| 39 |
+
gr.Markdown("## 🌊 Flood Warning System (Powered by Groq AI)")
|
| 40 |
+
with gr.Row():
|
| 41 |
+
location = gr.Textbox(label="📍 Location (e.g., Delhi, India)")
|
| 42 |
+
water_level = gr.Slider(label="💧 Water Level (meters)", minimum=0, maximum=10)
|
| 43 |
+
rainfall = gr.Slider(label="🌧️ 24h Rainfall (mm)", minimum=0, maximum=500)
|
| 44 |
+
submit_btn = gr.Button("🚨 Check Flood Risk")
|
| 45 |
+
|
| 46 |
+
output = gr.Textbox(label="⚠️ AI Alert", interactive=False)
|
| 47 |
+
|
| 48 |
+
submit_btn.click(
|
| 49 |
+
fn=predict_flood_risk,
|
| 50 |
+
inputs=[location, water_level, rainfall],
|
| 51 |
+
outputs=output
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
app.launch()
|