Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,58 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
GROQ_API_KEY = os.
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
-
def
|
| 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 |
-
|
| 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 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import os
|
| 3 |
+
import requests
|
| 4 |
|
| 5 |
+
# Groq API configuration
|
| 6 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 7 |
+
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 8 |
+
MODEL = "mixtral-8x7b-32768" # Use a supported model if this one is deprecated
|
| 9 |
|
| 10 |
+
def get_flood_risk(location, water_level, rainfall):
|
|
|
|
| 11 |
prompt = f"""
|
| 12 |
Analyze flood risk based on:
|
| 13 |
- Location: {location}
|
| 14 |
- Current water level: {water_level} meters
|
| 15 |
- 24-hour rainfall: {rainfall} mm
|
| 16 |
- Historical flood data for area
|
| 17 |
+
|
| 18 |
Provide output in this exact format:
|
| 19 |
"RISK: <HIGH/MEDIUM/LOW> | ALERT: <Warning message> | ACTION: <Recommended action>"
|
| 20 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
headers = {
|
| 23 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 24 |
+
"Content-Type": "application/json"
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
data = {
|
| 28 |
+
"model": MODEL,
|
| 29 |
+
"messages": [
|
| 30 |
+
{"role": "system", "content": "You are a flood risk analysis expert."},
|
| 31 |
+
{"role": "user", "content": prompt}
|
| 32 |
+
]
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
response = requests.post(GROQ_API_URL, headers=headers, json=data)
|
| 36 |
+
|
| 37 |
+
if response.status_code == 200:
|
| 38 |
+
result = response.json()
|
| 39 |
+
message = result["choices"][0]["message"]["content"].strip()
|
| 40 |
+
return message
|
| 41 |
+
else:
|
| 42 |
+
return f"β Error: {response.status_code} - {response.text}"
|
| 43 |
+
|
| 44 |
+
# Gradio UI
|
| 45 |
+
iface = gr.Interface(
|
| 46 |
+
fn=get_flood_risk,
|
| 47 |
+
inputs=[
|
| 48 |
+
gr.Textbox(label="Location"),
|
| 49 |
+
gr.Number(label="Current Water Level (m)"),
|
| 50 |
+
gr.Number(label="24-hour Rainfall (mm)")
|
| 51 |
+
],
|
| 52 |
+
outputs="text",
|
| 53 |
+
title="π Flood Risk Predictor",
|
| 54 |
+
description="Enter location, water level and rainfall to get flood risk prediction using Groq AI"
|
| 55 |
+
)
|
| 56 |
|
| 57 |
+
if __name__ == "__main__":
|
| 58 |
+
iface.launch()
|