Spaces:
Sleeping
Sleeping
| 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() |