Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from groq import Groq
|
2 |
+
import os
|
3 |
+
from PIL import Image
|
4 |
+
import torch
|
5 |
+
from torchvision import transforms
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
# Replace 'your_api_key_here' with your actual Groq API key
|
9 |
+
api_key = "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"
|
10 |
+
|
11 |
+
# Initialize the Groq client with the API key
|
12 |
+
client = Groq(api_key=api_key)
|
13 |
+
|
14 |
+
# Placeholder for flood prediction model (open-source PyTorch model)
|
15 |
+
class FloodPredictionModel:
|
16 |
+
def __init__(self):
|
17 |
+
# Example: Load a pre-trained model (you should replace this with a flood prediction model)
|
18 |
+
self.model = torch.hub.load("pytorch/vision:v0.10.0", "resnet18", pretrained=True)
|
19 |
+
self.model.eval()
|
20 |
+
|
21 |
+
def predict(self, image):
|
22 |
+
# Preprocess the image
|
23 |
+
preprocess = transforms.Compose([
|
24 |
+
transforms.Resize((224, 224)),
|
25 |
+
transforms.ToTensor(),
|
26 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
27 |
+
])
|
28 |
+
input_tensor = preprocess(image).unsqueeze(0)
|
29 |
+
|
30 |
+
# Make prediction
|
31 |
+
with torch.no_grad():
|
32 |
+
outputs = self.model(input_tensor)
|
33 |
+
_, predicted = outputs.max(1)
|
34 |
+
return predicted.item() # Placeholder: Replace with actual prediction logic
|
35 |
+
|
36 |
+
|
37 |
+
# Instantiate the flood prediction model
|
38 |
+
flood_model = FloodPredictionModel()
|
39 |
+
|
40 |
+
|
41 |
+
# Function to process user input and predict
|
42 |
+
def flood_prediction(image, user_query):
|
43 |
+
# Debugging: Check if image is received correctly
|
44 |
+
print("Received Image:", image)
|
45 |
+
|
46 |
+
# Analyze the image with the flood prediction model
|
47 |
+
try:
|
48 |
+
prediction = flood_model.predict(image)
|
49 |
+
prediction_text = (
|
50 |
+
"Flood risk detected in the area!" if prediction == 1 else "No immediate flood risk detected."
|
51 |
+
)
|
52 |
+
except Exception as e:
|
53 |
+
prediction_text = f"Error in flood prediction: {str(e)}"
|
54 |
+
|
55 |
+
# Debugging: Check prediction result
|
56 |
+
print("Flood Prediction Result:", prediction_text)
|
57 |
+
|
58 |
+
# Use Groq's API for query-based interaction
|
59 |
+
try:
|
60 |
+
chat_completion = client.chat.completions.create(
|
61 |
+
messages=[{"role": "user", "content": user_query}],
|
62 |
+
model="llama-3.3-70b-versatile",
|
63 |
+
)
|
64 |
+
ai_response = chat_completion.choices[0].message.content
|
65 |
+
except Exception as e:
|
66 |
+
ai_response = f"Error with Groq API: {str(e)}"
|
67 |
+
|
68 |
+
# Debugging: Check AI response
|
69 |
+
print("AI Response:", ai_response)
|
70 |
+
|
71 |
+
return prediction_text, ai_response
|
72 |
+
|
73 |
+
|
74 |
+
# Define the Gradio interface
|
75 |
+
with gr.Blocks() as flood_app:
|
76 |
+
gr.Markdown("## 🌊 Flood Prediction App")
|
77 |
+
gr.Markdown(
|
78 |
+
"""
|
79 |
+
Welcome to the Flood Prediction App! This tool helps you analyze uploaded images
|
80 |
+
to predict potential flood risks in the area. You can also interact with a powerful
|
81 |
+
language model for further insights.
|
82 |
+
|
83 |
+
### Instructions:
|
84 |
+
1. Upload an image of the area you want to analyze.
|
85 |
+
2. Optionally, enter a query (e.g., "What are the risks of flooding in coastal areas?").
|
86 |
+
3. Click **Predict Flood Risk** to get the results.
|
87 |
+
"""
|
88 |
+
)
|
89 |
+
|
90 |
+
with gr.Row():
|
91 |
+
image_input = gr.Image(label="Upload Image", type="pil")
|
92 |
+
user_query = gr.Textbox(label="Your Query (Optional)", placeholder="Ask about flood risks...")
|
93 |
+
|
94 |
+
predict_button = gr.Button("Predict Flood Risk")
|
95 |
+
|
96 |
+
with gr.Row():
|
97 |
+
prediction_output = gr.Textbox(label="Flood Prediction")
|
98 |
+
ai_response_output = gr.Textbox(label="AI Response")
|
99 |
+
|
100 |
+
predict_button.click(
|
101 |
+
flood_prediction,
|
102 |
+
inputs=[image_input, user_query],
|
103 |
+
outputs=[prediction_output, ai_response_output]
|
104 |
+
)
|
105 |
+
|
106 |
+
gr.Markdown(
|
107 |
+
"""
|
108 |
+
### Output Description:
|
109 |
+
- **Flood Prediction**: Indicates whether there is a flood risk based on the uploaded image.
|
110 |
+
- **AI Response**: Provides detailed insights or answers based on your query.
|
111 |
+
"""
|
112 |
+
)
|
113 |
+
|
114 |
+
# Launch the app (for local testing or Google Colab)
|
115 |
+
flood_app.launch()
|