berkeruveyik commited on
Commit
80e0f4f
·
verified ·
1 Parent(s): 314b1fe

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +153 -0
app.py ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import tensorflow as tf
4
+ from tensorflow import keras
5
+ import pickle
6
+ import re
7
+
8
+ # Load the model
9
+ model = keras.models.load_model('model_4.keras')
10
+
11
+ # Load tokenizer (you need to upload tokenizer.pickle to your Space)
12
+ try:
13
+ with open('tokenizer.pickle', 'rb') as handle:
14
+ tokenizer = pickle.load(handle)
15
+ except:
16
+ print("Warning: Tokenizer not found. Creating a basic one.")
17
+ from tensorflow.keras.preprocessing.text import Tokenizer
18
+ tokenizer = Tokenizer(num_words=10000, oov_token="<OOV>")
19
+
20
+ # Get max sequence length from model input shape
21
+ max_length = model.input_shape[1] if len(model.input_shape) > 1 else 100
22
+
23
+ def preprocess_text(text):
24
+ """Clean and preprocess the tweet text"""
25
+ # Convert to lowercase
26
+ text = text.lower()
27
+ # Remove URLs
28
+ text = re.sub(r'http\S+|www\S+|https\S+', '', text, flags=re.MULTILINE)
29
+ # Remove user mentions and hashtags
30
+ text = re.sub(r'\@\w+|\#','', text)
31
+ # Remove extra spaces
32
+ text = re.sub(r'\s+', ' ', text).strip()
33
+ return text
34
+
35
+ def predict_disaster(text):
36
+ """Predict if a tweet is about a disaster or not"""
37
+ if not text.strip():
38
+ return {
39
+ "Disaster": 0.0,
40
+ "Not Disaster": 0.0
41
+ }, "⚠️ Please enter a tweet to classify"
42
+
43
+ # Preprocess the text
44
+ processed_text = preprocess_text(text)
45
+
46
+ # Tokenize and pad
47
+ sequences = tokenizer.texts_to_sequences([processed_text])
48
+ padded = keras.preprocessing.sequence.pad_sequences(
49
+ sequences,
50
+ maxlen=max_length,
51
+ padding='post',
52
+ truncating='post'
53
+ )
54
+
55
+ # Make prediction
56
+ prediction = model.predict(padded, verbose=0)[0][0]
57
+
58
+ # Create confidence scores
59
+ disaster_prob = float(prediction)
60
+ not_disaster_prob = 1 - disaster_prob
61
+
62
+ # Determine result message
63
+ if disaster_prob > 0.5:
64
+ result = f"🚨 **DISASTER** (Confidence: {disaster_prob*100:.1f}%)"
65
+ emoji = "🚨"
66
+ else:
67
+ result = f"✅ **NOT DISASTER** (Confidence: {not_disaster_prob*100:.1f}%)"
68
+ emoji = "✅"
69
+
70
+ return {
71
+ "Disaster": disaster_prob,
72
+ "Not Disaster": not_disaster_prob
73
+ }, result
74
+
75
+ # Example tweets for testing
76
+ examples = [
77
+ ["Our Deeds are the Reason of this #earthquake May ALLAH Forgive us all"],
78
+ ["Forest fire near La Ronge Sask. Canada"],
79
+ ["13,000 people receive #wildfires evacuation orders in California"],
80
+ ["Just happened a terrible car crash"],
81
+ ["I love summer days at the beach with friends"],
82
+ ["The sunset today is absolutely beautiful"],
83
+ ["Residents asked to shelter in place are being notified by officers. No other evacuation or shelter in place orders are expected"],
84
+ ["This is so awesome! Best day ever!"],
85
+ ["Bombing at the airport, many casualties reported"],
86
+ ["I'm making dinner tonight, trying a new recipe"]
87
+ ]
88
+
89
+ # Create Gradio interface
90
+ with gr.Blocks(theme=gr.themes.Soft(), title="Disaster Tweet Classifier") as demo:
91
+ gr.Markdown(
92
+ """
93
+ # 🚨 Disaster Tweet Classification
94
+ ### Determine whether a tweet is about a real disaster or not
95
+
96
+ This AI model analyzes tweets and classifies them as either referring to a **real disaster** (earthquake, fire, accident, etc.)
97
+ or **not a disaster** (regular conversation, metaphorical usage).
98
+ """
99
+ )
100
+
101
+ with gr.Row():
102
+ with gr.Column(scale=2):
103
+ input_text = gr.Textbox(
104
+ label="Enter Tweet Text",
105
+ placeholder="Type or paste a tweet here...",
106
+ lines=3
107
+ )
108
+
109
+ predict_btn = gr.Button("🔍 Classify Tweet", variant="primary", size="lg")
110
+
111
+ with gr.Column(scale=1):
112
+ output_label = gr.Label(
113
+ label="Prediction Confidence",
114
+ num_top_classes=2
115
+ )
116
+
117
+ output_text = gr.Markdown(label="Result")
118
+
119
+ # Examples section
120
+ gr.Markdown("### 📝 Try These Examples:")
121
+ gr.Examples(
122
+ examples=examples,
123
+ inputs=input_text,
124
+ outputs=[output_label, output_text],
125
+ fn=predict_disaster,
126
+ cache_examples=False
127
+ )
128
+
129
+ # Event handlers
130
+ predict_btn.click(
131
+ fn=predict_disaster,
132
+ inputs=input_text,
133
+ outputs=[output_label, output_text]
134
+ )
135
+
136
+ input_text.submit(
137
+ fn=predict_disaster,
138
+ inputs=input_text,
139
+ outputs=[output_label, output_text]
140
+ )
141
+
142
+ gr.Markdown(
143
+ """
144
+ ---
145
+ ### ℹ️ About
146
+ This model uses Natural Language Processing to classify disaster-related tweets.
147
+ It can help emergency services and news organizations quickly identify real disaster reports.
148
+ """
149
+ )
150
+
151
+ # Launch the app
152
+ if __name__ == "__main__":
153
+ demo.launch()