Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,51 +2,67 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# Groq API
|
| 6 |
-
GROQ_API_KEY = os.
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
prompt = f"""
|
| 12 |
-
|
| 13 |
- Location: {location}
|
| 14 |
-
- Current
|
| 15 |
-
-
|
| 16 |
-
- Historical
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
"""
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
return response.json()["choices"][0]["message"]["content"]
|
| 34 |
-
|
| 35 |
-
return f"Error: {
|
| 36 |
|
| 37 |
-
# Gradio
|
| 38 |
-
with gr.Blocks(title="Flood
|
| 39 |
-
gr.Markdown("#
|
| 40 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
submit_btn.click(
|
| 49 |
-
fn=
|
| 50 |
inputs=[location, water_level, rainfall],
|
| 51 |
outputs=output
|
| 52 |
)
|
|
|
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Initialize Groq API
|
| 6 |
+
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 7 |
+
if not GROQ_API_KEY:
|
| 8 |
+
raise ValueError("β Groq API Key not found! Please set it in Hugging Face Secrets")
|
| 9 |
|
| 10 |
+
def get_flood_warning(location, water_level, rainfall):
|
| 11 |
+
"""Get flood prediction from Groq AI"""
|
| 12 |
prompt = f"""
|
| 13 |
+
Analyze flood risk based on:
|
| 14 |
- Location: {location}
|
| 15 |
+
- Current water level: {water_level} meters
|
| 16 |
+
- 24-hour rainfall: {rainfall} mm
|
| 17 |
+
- Historical flood data for area
|
| 18 |
+
|
| 19 |
+
Provide output in this exact format:
|
| 20 |
+
"RISK: <HIGH/MEDIUM/LOW> | ALERT: <Warning message> | ACTION: <Recommended action>"
|
| 21 |
"""
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
headers = {
|
| 25 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 26 |
+
"Content-Type": "application/json"
|
| 27 |
+
}
|
| 28 |
+
payload = {
|
| 29 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 30 |
+
"model": "mixtral-8x7b-32768"
|
| 31 |
+
}
|
| 32 |
+
response = requests.post(
|
| 33 |
+
"https://api.groq.com/openai/v1/chat/completions",
|
| 34 |
+
headers=headers,
|
| 35 |
+
json=payload
|
| 36 |
+
)
|
| 37 |
return response.json()["choices"][0]["message"]["content"]
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return f"Error: {str(e)}"
|
| 40 |
|
| 41 |
+
# Create Gradio Interface
|
| 42 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Flood Prediction System") as app:
|
| 43 |
+
gr.Markdown("# π AI Flood Warning System")
|
| 44 |
+
gr.Markdown("Predict flood risks using Groq AI and real-time data")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
with gr.Row():
|
| 47 |
+
with gr.Column():
|
| 48 |
+
location = gr.Textbox(label="π Location Name", placeholder="Enter city/village name")
|
| 49 |
+
water_level = gr.Slider(label="π§ Water Level (meters)", minimum=0, maximum=15, step=0.1)
|
| 50 |
+
rainfall = gr.Slider(label="π§οΈ 24h Rainfall (mm)", minimum=0, maximum=300)
|
| 51 |
+
submit_btn = gr.Button("Analyze Flood Risk", variant="primary")
|
| 52 |
+
|
| 53 |
+
with gr.Column():
|
| 54 |
+
output = gr.Textbox(label="β οΈ Flood Alert", interactive=False)
|
| 55 |
+
gr.Examples(
|
| 56 |
+
examples=[
|
| 57 |
+
["Delhi", 4.5, 120],
|
| 58 |
+
["Mumbai", 2.1, 250],
|
| 59 |
+
["Chennai", 1.8, 80]
|
| 60 |
+
],
|
| 61 |
+
inputs=[location, water_level, rainfall]
|
| 62 |
+
)
|
| 63 |
|
| 64 |
submit_btn.click(
|
| 65 |
+
fn=get_flood_warning,
|
| 66 |
inputs=[location, water_level, rainfall],
|
| 67 |
outputs=output
|
| 68 |
)
|