Spaces:
Sleeping
Sleeping
File size: 3,884 Bytes
c6c68ff b37e4bb 4d132d8 c6c68ff ccc9eb4 b37e4bb 4d132d8 c6c68ff 110d33a c6c68ff 110d33a 4d132d8 ccc9eb4 110d33a c6c68ff 4d132d8 110d33a 4d132d8 110d33a 4d132d8 110d33a 4d132d8 110d33a 81d1cfc 110d33a 4d132d8 110d33a c6c68ff ccc9eb4 110d33a 4d132d8 110d33a 4d132d8 110d33a 4d132d8 ccc9eb4 4d132d8 110d33a 4d132d8 110d33a 4d132d8 110d33a 4d132d8 c6c68ff ccc9eb4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | import gradio as gr
import os
import requests
import json
# Configuration
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
MODEL = "llama3-70b-8192"
API_URL = "https://api.groq.com/openai/v1/chat/completions"
def analyze_flood_risk(country, city, area, water_level, rainfall):
"""Detailed flood analysis based on location"""
prompt = f"""
As a flood risk expert, analyze:
- Country: {country}
- City: {city}
- Area: {area}
- Water Level: {water_level} meters
- 24h Rainfall: {rainfall} mm
Respond in JSON format with:
- risk_level: <HIGH/MEDIUM/LOW>
- summary: <1 sentence>
- affected_areas: <list>
- precautions: <3 bullet points>
- emergency_contacts: <local authorities>
"""
try:
response = requests.post(
API_URL,
headers={"Authorization": f"Bearer {GROQ_API_KEY}"},
json={
"model": MODEL,
"messages": [
{
"role": "system",
"content": "You are a local flood analysis expert. Consider regional geography."
},
{"role": "user", "content": prompt}
],
"response_format": {"type": "json_object"}
},
timeout=10
)
response.raise_for_status()
result = json.loads(response.json()["choices"][0]["message"]["content"])
# Determine color based on risk
color = "#ff0000" if result["risk_level"] == "HIGH" else "#ff9900" if result["risk_level"] == "MEDIUM" else "#00aa00"
# Format HTML output
return f"""
<div style='border-left: 5px solid {color}; padding-left: 10px'>
<h3>{area}, {city}, {country}</h3>
<p><b>Risk Level:</b> <span style='color:{color}'>{result['risk_level']}</span></p>
<p><b>Summary:</b> {result['summary']}</p>
<h4>Affected Areas:</h4>
<ul>
{"".join([f"<li>{area}</li>" for area in result['affected_areas']])}
</ul>
<h4>Precautions:</h4>
<ol>
{"".join([f"<li>{action}</li>" for action in result['precautions']])}
</ol>
<h4>Emergency Contacts:</h4>
<ul>
{"".join([f"<li>{contact}</li>" for contact in result['emergency_contacts']])}
</ul>
</div>
"""
except Exception as e:
return f"<div style='color:red'>Error: {str(e)}</div>"
# Gradio Interface
with gr.Blocks(theme=gr.themes.Soft(), title="Flood Risk Analyzer") as app:
gr.Markdown("# 🌊 Flood Risk Assessment")
gr.Markdown("Enter location details to analyze flood risk")
with gr.Row():
with gr.Column():
country = gr.Textbox(label="Country", placeholder="e.g. Pakistan")
city = gr.Textbox(label="City", placeholder="e.g. Karachi")
area = gr.Textbox(label="Area/Neighborhood", placeholder="e.g. Clifton")
water_level = gr.Slider(0, 10, step=0.1, label="Water Level (meters)")
rainfall = gr.Slider(0, 500, label="24h Rainfall (mm)")
submit_btn = gr.Button("Analyze Risk", variant="primary")
with gr.Column():
output_html = gr.HTML(label="Analysis Results")
# Examples
gr.Examples(
examples=[
["Pakistan", "Karachi", "Clifton", 3.5, 200],
["Pakistan", "Lahore", "Iqbal Town", 2.0, 80],
["India", "Mumbai", "Nariman Point", 4.0, 300]
],
inputs=[country, city, area, water_level, rainfall]
)
submit_btn.click(
fn=analyze_flood_risk,
inputs=[country, city, area, water_level, rainfall],
outputs=output_html
)
app.launch() |