CodeNine commited on
Commit
b37e4bb
Β·
verified Β·
1 Parent(s): 601e24a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -55
app.py CHANGED
@@ -1,70 +1,58 @@
1
  import gradio as gr
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
- )
 
 
 
 
 
 
69
 
70
- app.launch()
 
 
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()