Update app.py
Browse files
app.py
CHANGED
|
@@ -2,22 +2,32 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
print("
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
print("Model loaded successfully!")
|
| 17 |
-
print(f"Model type: {type(model)}")
|
| 18 |
-
except Exception as e:
|
| 19 |
-
print(f"Error loading model: {e}")
|
| 20 |
-
raise
|
| 21 |
|
| 22 |
def predict_disaster(text):
|
| 23 |
"""Predict if a tweet is about a disaster or not"""
|
|
@@ -29,14 +39,13 @@ def predict_disaster(text):
|
|
| 29 |
|
| 30 |
try:
|
| 31 |
# BERT model directly accepts raw text (has built-in preprocessing)
|
| 32 |
-
# Make prediction
|
| 33 |
prediction = model.predict([text], verbose=0)[0][0]
|
| 34 |
|
| 35 |
-
#
|
| 36 |
disaster_prob = float(prediction)
|
| 37 |
not_disaster_prob = 1 - disaster_prob
|
| 38 |
|
| 39 |
-
#
|
| 40 |
if disaster_prob > 0.5:
|
| 41 |
result = f"🚨 **DISASTER** (Confidence: {disaster_prob*100:.1f}%)"
|
| 42 |
else:
|
|
@@ -74,17 +83,15 @@ examples = [
|
|
| 74 |
|
| 75 |
# Create Gradio interface
|
| 76 |
with gr.Blocks(theme=gr.themes.Soft(), title="Disaster Tweet Classifier") as demo:
|
| 77 |
-
gr.Markdown(
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
"""
|
| 87 |
-
)
|
| 88 |
|
| 89 |
with gr.Row():
|
| 90 |
with gr.Column(scale=2):
|
|
@@ -94,8 +101,9 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Disaster Tweet Classifier") as dem
|
|
| 94 |
lines=4
|
| 95 |
)
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
|
|
|
| 99 |
|
| 100 |
with gr.Column(scale=1):
|
| 101 |
output_label = gr.Label(
|
|
@@ -106,8 +114,11 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Disaster Tweet Classifier") as dem
|
|
| 106 |
output_text = gr.Markdown(label="Result")
|
| 107 |
|
| 108 |
# Examples section
|
| 109 |
-
gr.Markdown("
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
gr.Examples(
|
| 113 |
examples=examples,
|
|
@@ -118,6 +129,36 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Disaster Tweet Classifier") as dem
|
|
| 118 |
label="Sample Tweets"
|
| 119 |
)
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
# Event handlers
|
| 122 |
predict_btn.click(
|
| 123 |
fn=predict_disaster,
|
|
@@ -135,39 +176,10 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Disaster Tweet Classifier") as dem
|
|
| 135 |
fn=lambda: ("", {"Disaster": 0.0, "Not Disaster": 0.0}, ""),
|
| 136 |
outputs=[input_text, output_label, output_text]
|
| 137 |
)
|
| 138 |
-
|
| 139 |
-
gr.Markdown(
|
| 140 |
-
"""
|
| 141 |
-
---
|
| 142 |
-
### ℹ️ About This Model
|
| 143 |
-
|
| 144 |
-
**Model Architecture**: BERT Tiny (English, Uncased)
|
| 145 |
-
- **Parameters**: ~4.4M parameters
|
| 146 |
-
- **Training**: Fine-tuned on disaster tweet dataset
|
| 147 |
-
- **Accuracy**: Optimized for real-time disaster detection
|
| 148 |
-
|
| 149 |
-
**Use Cases**:
|
| 150 |
-
- 🚨 Emergency response monitoring
|
| 151 |
-
- 📰 News verification
|
| 152 |
-
- 🌐 Social media analysis
|
| 153 |
-
- 🔍 Crisis management
|
| 154 |
-
|
| 155 |
-
**How it Works**:
|
| 156 |
-
The model uses contextual understanding to distinguish between:
|
| 157 |
-
- Real disaster reports (earthquakes, fires, accidents, floods, etc.)
|
| 158 |
-
- Casual language or metaphorical usage of disaster-related words
|
| 159 |
-
|
| 160 |
-
**Limitations**:
|
| 161 |
-
- Optimized for English tweets only
|
| 162 |
-
- May require context for ambiguous cases
|
| 163 |
-
- Should be used as a support tool, not sole decision-maker
|
| 164 |
-
|
| 165 |
-
---
|
| 166 |
-
|
| 167 |
-
**Created by**: berkeruveyik | **Model**: BERT Tiny | **Framework**: TensorFlow + Keras Hub
|
| 168 |
-
"""
|
| 169 |
-
)
|
| 170 |
|
| 171 |
# Launch the app
|
| 172 |
if __name__ == "__main__":
|
| 173 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Lazy import - sadece model yüklerken import et
|
| 6 |
+
def load_model():
|
| 7 |
+
"""Model ve gerekli kütüphaneleri lazy loading ile yükle"""
|
| 8 |
+
print("Loading TensorFlow...")
|
| 9 |
+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # TensorFlow loglarını azalt
|
| 10 |
+
|
| 11 |
+
import tensorflow as tf
|
| 12 |
+
print(f"TensorFlow version: {tf.__version__}")
|
| 13 |
+
|
| 14 |
+
print("Loading Keras Hub...")
|
| 15 |
+
import keras_hub
|
| 16 |
+
print(f"Keras Hub version: {keras_hub.__version__}")
|
| 17 |
+
|
| 18 |
+
print("Loading BERT model...")
|
| 19 |
+
try:
|
| 20 |
+
model = tf.keras.models.load_model('model_4.keras')
|
| 21 |
+
print("✅ Model loaded successfully!")
|
| 22 |
+
return model
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print(f"❌ Error loading model: {e}")
|
| 25 |
+
raise
|
| 26 |
|
| 27 |
+
# Model yükleme - global değişken
|
| 28 |
+
print("Initializing application...")
|
| 29 |
+
model = load_model()
|
| 30 |
+
print("Application ready!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
def predict_disaster(text):
|
| 33 |
"""Predict if a tweet is about a disaster or not"""
|
|
|
|
| 39 |
|
| 40 |
try:
|
| 41 |
# BERT model directly accepts raw text (has built-in preprocessing)
|
|
|
|
| 42 |
prediction = model.predict([text], verbose=0)[0][0]
|
| 43 |
|
| 44 |
+
# Calculate probabilities
|
| 45 |
disaster_prob = float(prediction)
|
| 46 |
not_disaster_prob = 1 - disaster_prob
|
| 47 |
|
| 48 |
+
# Result message
|
| 49 |
if disaster_prob > 0.5:
|
| 50 |
result = f"🚨 **DISASTER** (Confidence: {disaster_prob*100:.1f}%)"
|
| 51 |
else:
|
|
|
|
| 83 |
|
| 84 |
# Create Gradio interface
|
| 85 |
with gr.Blocks(theme=gr.themes.Soft(), title="Disaster Tweet Classifier") as demo:
|
| 86 |
+
gr.Markdown("""
|
| 87 |
+
# 🚨 Disaster Tweet Classification
|
| 88 |
+
### AI-Powered BERT Model to Identify Real Disaster Reports
|
| 89 |
+
|
| 90 |
+
This application uses a fine-tuned **BERT** (Bidirectional Encoder Representations from Transformers) model
|
| 91 |
+
to analyze tweets and classify them as either referring to a **real disaster** or **not a disaster**.
|
| 92 |
+
|
| 93 |
+
Perfect for emergency response teams, news organizations, and disaster management agencies! 🚑🔥🌊
|
| 94 |
+
""")
|
|
|
|
|
|
|
| 95 |
|
| 96 |
with gr.Row():
|
| 97 |
with gr.Column(scale=2):
|
|
|
|
| 101 |
lines=4
|
| 102 |
)
|
| 103 |
|
| 104 |
+
with gr.Row():
|
| 105 |
+
clear_btn = gr.Button("🗑️ Clear", variant="secondary")
|
| 106 |
+
predict_btn = gr.Button("🔍 Classify Tweet", variant="primary", size="lg")
|
| 107 |
|
| 108 |
with gr.Column(scale=1):
|
| 109 |
output_label = gr.Label(
|
|
|
|
| 114 |
output_text = gr.Markdown(label="Result")
|
| 115 |
|
| 116 |
# Examples section
|
| 117 |
+
gr.Markdown("""
|
| 118 |
+
---
|
| 119 |
+
### 📝 Try These Examples:
|
| 120 |
+
Click on any example below to automatically classify it
|
| 121 |
+
""")
|
| 122 |
|
| 123 |
gr.Examples(
|
| 124 |
examples=examples,
|
|
|
|
| 129 |
label="Sample Tweets"
|
| 130 |
)
|
| 131 |
|
| 132 |
+
gr.Markdown("""
|
| 133 |
+
---
|
| 134 |
+
### ℹ️ About This Model
|
| 135 |
+
|
| 136 |
+
**Model Architecture**: BERT Tiny (English, Uncased)
|
| 137 |
+
- **Parameters**: ~4.4M parameters
|
| 138 |
+
- **Training**: Fine-tuned on disaster tweet dataset
|
| 139 |
+
- **Accuracy**: Optimized for real-time disaster detection
|
| 140 |
+
|
| 141 |
+
**Use Cases**:
|
| 142 |
+
- 🚨 Emergency response monitoring
|
| 143 |
+
- 📰 News verification
|
| 144 |
+
- 🌐 Social media analysis
|
| 145 |
+
- 🔍 Crisis management
|
| 146 |
+
|
| 147 |
+
**How it Works**:
|
| 148 |
+
The model uses contextual understanding to distinguish between:
|
| 149 |
+
- Real disaster reports (earthquakes, fires, accidents, floods, etc.)
|
| 150 |
+
- Casual language or metaphorical usage of disaster-related words
|
| 151 |
+
|
| 152 |
+
**Limitations**:
|
| 153 |
+
- Optimized for English tweets only
|
| 154 |
+
- May require context for ambiguous cases
|
| 155 |
+
- Should be used as a support tool, not sole decision-maker
|
| 156 |
+
|
| 157 |
+
---
|
| 158 |
+
|
| 159 |
+
**Created by**: berkeruveyik | **Model**: BERT Tiny | **Framework**: TensorFlow + Keras Hub
|
| 160 |
+
""")
|
| 161 |
+
|
| 162 |
# Event handlers
|
| 163 |
predict_btn.click(
|
| 164 |
fn=predict_disaster,
|
|
|
|
| 176 |
fn=lambda: ("", {"Disaster": 0.0, "Not Disaster": 0.0}, ""),
|
| 177 |
outputs=[input_text, output_label, output_text]
|
| 178 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
# Launch the app
|
| 181 |
if __name__ == "__main__":
|
| 182 |
+
demo.launch(
|
| 183 |
+
share=False,
|
| 184 |
+
debug=False
|
| 185 |
+
)
|