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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -36
app.py CHANGED
@@ -2,51 +2,67 @@ import gradio as gr
2
  import requests
3
  import os
4
 
5
- # Groq API Setup (Replace with your API Key)
6
- GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Hugging Face Secrets se set karo
7
- GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
 
8
 
9
- # Flood Prediction via Groq AI
10
- def predict_flood_risk(location, water_level, rainfall):
11
  prompt = f"""
12
- Flood Risk Assessment:
13
  - Location: {location}
14
- - Current Water Level: {water_level} meters
15
- - Last 24h Rainfall: {rainfall} mm
16
- - Historical Data: Moderate flood risk zone
17
-
18
- Question: Should we issue a flood warning?
19
- Reply format: "VERDICT: [HIGH/MEDIUM/LOW] | REASON: [1-line explanation]"
20
  """
21
-
22
- headers = {
23
- "Authorization": f"Bearer {GROQ_API_KEY}",
24
- "Content-Type": "application/json"
25
- }
26
- payload = {
27
- "messages": [{"role": "user", "content": prompt}],
28
- "model": "mixtral-8x7b-32768" # Groq's fast model
29
- }
30
-
31
- response = requests.post(GROQ_API_URL, headers=headers, json=payload)
32
- if response.status_code == 200:
 
 
 
33
  return response.json()["choices"][0]["message"]["content"]
34
- else:
35
- return f"Error: {response.text}"
36
 
37
- # Gradio UI
38
- with gr.Blocks(title="Flood Alert AI") as app:
39
- gr.Markdown("## 🌊 Flood Warning System (Powered by Groq AI)")
40
- with gr.Row():
41
- location = gr.Textbox(label="πŸ“ Location (e.g., Delhi, India)")
42
- water_level = gr.Slider(label="πŸ’§ Water Level (meters)", minimum=0, maximum=10)
43
- rainfall = gr.Slider(label="🌧️ 24h Rainfall (mm)", minimum=0, maximum=500)
44
- submit_btn = gr.Button("🚨 Check Flood Risk")
45
 
46
- output = gr.Textbox(label="⚠️ AI Alert", interactive=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  submit_btn.click(
49
- fn=predict_flood_risk,
50
  inputs=[location, water_level, rainfall],
51
  outputs=output
52
  )
 
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
  )