Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +102 -0
- requirements.txt +34 -0
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
DisasterSense | Gradio Demo Interface
|
| 3 |
+
Interactive UI for multimodal disaster severity prediction.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import sys
|
| 7 |
+
import requests
|
| 8 |
+
import gradio as gr
|
| 9 |
+
from pathlib import Path
|
| 10 |
+
|
| 11 |
+
API_URL = "http://127.0.0.1:8000/predict"
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def predict(image, tweet_text):
|
| 15 |
+
if image is None:
|
| 16 |
+
return "Please upload an image.", "", "", "", ""
|
| 17 |
+
|
| 18 |
+
if not tweet_text.strip():
|
| 19 |
+
return "Please enter tweet text.", "", "", "", ""
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
with open(image, "rb") as f:
|
| 23 |
+
response = requests.post(
|
| 24 |
+
API_URL,
|
| 25 |
+
files={"image": ("image.jpg", f, "image/jpeg")},
|
| 26 |
+
data={"text": tweet_text},
|
| 27 |
+
timeout=30,
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
if response.status_code != 200:
|
| 31 |
+
return f"API Error: {response.status_code}", "", "", "", ""
|
| 32 |
+
|
| 33 |
+
data = response.json()
|
| 34 |
+
|
| 35 |
+
severity_score = f"{data['severity_score']}/100"
|
| 36 |
+
severity_level = data["severity_level"]
|
| 37 |
+
image_pred = data["image_prediction"].replace("_", " ").title()
|
| 38 |
+
text_pred = data["text_prediction"].replace("_", " ").title()
|
| 39 |
+
damage_score = f"{data['damage_score']:.2f}"
|
| 40 |
+
|
| 41 |
+
level_colors = {
|
| 42 |
+
"LOW" : "π’ LOW",
|
| 43 |
+
"MODERATE": "π‘ MODERATE",
|
| 44 |
+
"HIGH" : "π HIGH",
|
| 45 |
+
"CRITICAL": "π΄ CRITICAL",
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
return (
|
| 49 |
+
level_colors.get(severity_level, severity_level),
|
| 50 |
+
severity_score,
|
| 51 |
+
image_pred,
|
| 52 |
+
text_pred,
|
| 53 |
+
damage_score,
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
except requests.exceptions.ConnectionError:
|
| 57 |
+
return "API not running. Start with: uvicorn api.main:app --reload", "", "", "", ""
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return f"Error: {str(e)}", "", "", "", ""
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
with gr.Blocks(title="DisasterSense", theme=gr.themes.Soft()) as demo:
|
| 63 |
+
gr.Markdown("""
|
| 64 |
+
# π DisasterSense
|
| 65 |
+
### Multimodal Disaster Severity Detection
|
| 66 |
+
Upload a disaster image and paste a related tweet to get a real-time crisis severity score.
|
| 67 |
+
""")
|
| 68 |
+
|
| 69 |
+
with gr.Row():
|
| 70 |
+
with gr.Column(scale=1):
|
| 71 |
+
image_input = gr.Image(type="filepath", label="Disaster Image")
|
| 72 |
+
text_input = gr.Textbox(
|
| 73 |
+
lines=3,
|
| 74 |
+
placeholder="Paste a disaster-related tweet here...",
|
| 75 |
+
label="Tweet Text"
|
| 76 |
+
)
|
| 77 |
+
submit_btn = gr.Button("Analyze", variant="primary", size="lg")
|
| 78 |
+
|
| 79 |
+
with gr.Column(scale=1):
|
| 80 |
+
severity_level = gr.Textbox(label="Severity Level", interactive=False)
|
| 81 |
+
severity_score = gr.Textbox(label="Severity Score (0-100)", interactive=False)
|
| 82 |
+
image_pred = gr.Textbox(label="Image Prediction", interactive=False)
|
| 83 |
+
text_pred = gr.Textbox(label="Text Prediction", interactive=False)
|
| 84 |
+
damage_score = gr.Textbox(label="Damage Score", interactive=False)
|
| 85 |
+
|
| 86 |
+
gr.Markdown("""
|
| 87 |
+
---
|
| 88 |
+
**Model Details:**
|
| 89 |
+
- Image Classifier: EfficientNet-B0 fine-tuned on CrisisMMD v2.0 (64% accuracy)
|
| 90 |
+
- NLP Classifier: twitter-roberta-base fine-tuned on CrisisMMD v2.0 (75% accuracy)
|
| 91 |
+
- Fusion: Weighted combination (60% image, 40% text)
|
| 92 |
+
- Dataset: 7 real disaster events β Harvey, Irma, Maria, California Wildfires, and more
|
| 93 |
+
""")
|
| 94 |
+
|
| 95 |
+
submit_btn.click(
|
| 96 |
+
fn=predict,
|
| 97 |
+
inputs=[image_input, text_input],
|
| 98 |
+
outputs=[severity_level, severity_score, image_pred, text_pred, damage_score],
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
if __name__ == "__main__":
|
| 102 |
+
demo.launch(share=False)
|
requirements.txt
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Core ML β CPU only for deployment (smaller memory footprint)
|
| 2 |
+
--extra-index-url https://download.pytorch.org/whl/cpu
|
| 3 |
+
torch>=2.0.0
|
| 4 |
+
torchvision>=0.15.0
|
| 5 |
+
transformers>=4.35.0
|
| 6 |
+
accelerate>=0.24.0
|
| 7 |
+
|
| 8 |
+
# Data & EDA
|
| 9 |
+
numpy>=1.24.0
|
| 10 |
+
pandas>=2.0.0
|
| 11 |
+
matplotlib>=3.7.0
|
| 12 |
+
seaborn>=0.12.0
|
| 13 |
+
Pillow>=9.5.0
|
| 14 |
+
scikit-learn>=1.3.0
|
| 15 |
+
|
| 16 |
+
# API & Deployment
|
| 17 |
+
fastapi>=0.104.0
|
| 18 |
+
uvicorn>=0.24.0
|
| 19 |
+
python-multipart>=0.0.6
|
| 20 |
+
pydantic>=2.0.0
|
| 21 |
+
|
| 22 |
+
# Database
|
| 23 |
+
psycopg2-binary>=2.9.0
|
| 24 |
+
sqlalchemy>=2.0.0
|
| 25 |
+
|
| 26 |
+
# UI
|
| 27 |
+
gradio>=4.0.0
|
| 28 |
+
|
| 29 |
+
# Utilities
|
| 30 |
+
python-dotenv>=1.0.0
|
| 31 |
+
requests>=2.31.0
|
| 32 |
+
tqdm>=4.65.0
|
| 33 |
+
huggingface_hub>=0.19.0
|
| 34 |
+
datasets>=2.14.0
|