Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import os | |
| # Initialize Groq API | |
| GROQ_API_KEY = os.environ.get("GROQ_API_KEY") | |
| if not GROQ_API_KEY: | |
| raise ValueError("β Groq API Key not found! Please set it in Hugging Face Secrets") | |
| def get_flood_warning(location, water_level, rainfall): | |
| """Get flood prediction from Groq AI""" | |
| prompt = f""" | |
| Analyze flood risk based on: | |
| - Location: {location} | |
| - Current water level: {water_level} meters | |
| - 24-hour rainfall: {rainfall} mm | |
| - Historical flood data for area | |
| Provide output in this exact format: | |
| "RISK: <HIGH/MEDIUM/LOW> | ALERT: <Warning message> | ACTION: <Recommended action>" | |
| """ | |
| try: | |
| headers = { | |
| "Authorization": f"Bearer {GROQ_API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| payload = { | |
| "messages": [{"role": "user", "content": prompt}], | |
| "model": "mixtral-8x7b-32768" | |
| } | |
| response = requests.post( | |
| "https://api.groq.com/openai/v1/chat/completions", | |
| headers=headers, | |
| json=payload | |
| ) | |
| return response.json()["choices"][0]["message"]["content"] | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| # Create Gradio Interface | |
| with gr.Blocks(theme=gr.themes.Soft(), title="Flood Prediction System") as app: | |
| gr.Markdown("# π AI Flood Warning System") | |
| gr.Markdown("Predict flood risks using Groq AI and real-time data") | |
| with gr.Row(): | |
| with gr.Column(): | |
| location = gr.Textbox(label="π Location Name", placeholder="Enter city/village name") | |
| water_level = gr.Slider(label="π§ Water Level (meters)", minimum=0, maximum=15, step=0.1) | |
| rainfall = gr.Slider(label="π§οΈ 24h Rainfall (mm)", minimum=0, maximum=300) | |
| submit_btn = gr.Button("Analyze Flood Risk", variant="primary") | |
| with gr.Column(): | |
| output = gr.Textbox(label="β οΈ Flood Alert", interactive=False) | |
| gr.Examples( | |
| examples=[ | |
| ["Delhi", 4.5, 120], | |
| ["Mumbai", 2.1, 250], | |
| ["Chennai", 1.8, 80] | |
| ], | |
| inputs=[location, water_level, rainfall] | |
| ) | |
| submit_btn.click( | |
| fn=get_flood_warning, | |
| inputs=[location, water_level, rainfall], | |
| outputs=output | |
| ) | |
| app.launch() |