Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,11 +12,12 @@ API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
|
| 12 |
def get_risk_color(risk_level):
|
| 13 |
"""Return color based on risk level"""
|
| 14 |
colors = {
|
| 15 |
-
"HIGH": "
|
| 16 |
-
"MEDIUM": "
|
| 17 |
-
"LOW": "
|
|
|
|
| 18 |
}
|
| 19 |
-
return colors.get(risk_level.upper(), "
|
| 20 |
|
| 21 |
def analyze_flood(location, water_level, rainfall, historical_data):
|
| 22 |
"""Enhanced flood analysis with structured output"""
|
|
@@ -61,31 +62,26 @@ def analyze_flood(location, water_level, rainfall, historical_data):
|
|
| 61 |
response.raise_for_status()
|
| 62 |
result = json.loads(response.json()["choices"][0]["message"]["content"])
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
except Exception as e:
|
| 67 |
-
return
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
"Summary": data["error"],
|
| 75 |
-
"Details": "Failed to get analysis",
|
| 76 |
-
"Actions": "Please try again later"
|
| 77 |
-
}
|
| 78 |
-
|
| 79 |
-
risk_level = data.get("risk_level", "UNKNOWN")
|
| 80 |
-
color = get_risk_color(risk_level)
|
| 81 |
-
|
| 82 |
-
return {
|
| 83 |
-
"Risk Level": f"<span style='color: {color}; font-weight: bold'>{risk_level}</span>",
|
| 84 |
-
"Summary": data.get("summary", "No summary available"),
|
| 85 |
-
"Detailed Analysis": "\n".join([f"• {point}" for point in data.get("detailed_analysis", [])]),
|
| 86 |
-
"Recommended Actions": "\n".join([f"• {action}" for action in data.get("recommended_actions", [])]),
|
| 87 |
-
"Confidence": f"{data.get('confidence', 'N/A')}%"
|
| 88 |
-
}
|
| 89 |
|
| 90 |
# Gradio Interface
|
| 91 |
with gr.Blocks(theme=gr.themes.Soft(), title="Flood Risk Analyzer") as app:
|
|
@@ -141,17 +137,11 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Flood Risk Analyzer") as app:
|
|
| 141 |
</div>
|
| 142 |
""")
|
| 143 |
|
| 144 |
-
# Event Handling
|
| 145 |
submit_btn.click(
|
| 146 |
fn=analyze_flood,
|
| 147 |
inputs=[location, water_level, rainfall, historical],
|
| 148 |
-
outputs=
|
| 149 |
-
"Risk Level": risk_level,
|
| 150 |
-
"Summary": summary,
|
| 151 |
-
"Detailed Analysis": details,
|
| 152 |
-
"Recommended Actions": actions,
|
| 153 |
-
"Confidence": confidence
|
| 154 |
-
}
|
| 155 |
)
|
| 156 |
|
| 157 |
app.launch()
|
|
|
|
| 12 |
def get_risk_color(risk_level):
|
| 13 |
"""Return color based on risk level"""
|
| 14 |
colors = {
|
| 15 |
+
"HIGH": "red",
|
| 16 |
+
"MEDIUM": "orange",
|
| 17 |
+
"LOW": "green",
|
| 18 |
+
"ERROR": "gray"
|
| 19 |
}
|
| 20 |
+
return colors.get(risk_level.upper(), "gray")
|
| 21 |
|
| 22 |
def analyze_flood(location, water_level, rainfall, historical_data):
|
| 23 |
"""Enhanced flood analysis with structured output"""
|
|
|
|
| 62 |
response.raise_for_status()
|
| 63 |
result = json.loads(response.json()["choices"][0]["message"]["content"])
|
| 64 |
|
| 65 |
+
# Format results
|
| 66 |
+
risk_level = result.get("risk_level", "UNKNOWN")
|
| 67 |
+
color = get_risk_color(risk_level)
|
| 68 |
+
|
| 69 |
+
return (
|
| 70 |
+
f"<span style='color: {color}; font-weight: bold'>{risk_level}</span>",
|
| 71 |
+
result.get("summary", "No summary available"),
|
| 72 |
+
"\n".join([f"• {point}" for point in result.get("detailed_analysis", [])]),
|
| 73 |
+
"\n".join([f"• {action}" for action in result.get("recommended_actions", [])]),
|
| 74 |
+
f"{result.get('confidence', 'N/A')}%"
|
| 75 |
+
)
|
| 76 |
|
| 77 |
except Exception as e:
|
| 78 |
+
return (
|
| 79 |
+
"<span style='color: gray; font-weight: bold'>ERROR</span>",
|
| 80 |
+
f"Analysis failed: {str(e)}",
|
| 81 |
+
"Please try again later",
|
| 82 |
+
"Check your API key and connection",
|
| 83 |
+
"0%"
|
| 84 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
# Gradio Interface
|
| 87 |
with gr.Blocks(theme=gr.themes.Soft(), title="Flood Risk Analyzer") as app:
|
|
|
|
| 137 |
</div>
|
| 138 |
""")
|
| 139 |
|
| 140 |
+
# Event Handling - Now returning a tuple instead of dict
|
| 141 |
submit_btn.click(
|
| 142 |
fn=analyze_flood,
|
| 143 |
inputs=[location, water_level, rainfall, historical],
|
| 144 |
+
outputs=[risk_level, summary, details, actions, confidence]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
)
|
| 146 |
|
| 147 |
app.launch()
|