Download app.py from berkeruveyik/Disaster_Tweets_classification: direct link, hf CLI and curl.
- Browser
- Download file 6.04 kB
-
https://huggingface.co/spaces/berkeruveyik/Disaster_Tweets_classification/resolve/main/app.py
- Command line
-
hf download hf://spaces/berkeruveyik/Disaster_Tweets_classification/app.py
-
curl -L -o app.py https://huggingface.co/spaces/berkeruveyik/Disaster_Tweets_classification/resolve/main/app.py
6.04 kB
| import gradio as gr | |
| import numpy as np | |
| import os | |
| # Lazy import - sadece model yΓΌklerken import et | |
| def load_model(): | |
| """Model ve gerekli kΓΌtΓΌphaneleri lazy loading ile yΓΌkle""" | |
| print("Loading TensorFlow...") | |
| os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # TensorFlow loglarΔ±nΔ± azalt | |
| import tensorflow as tf | |
| print(f"TensorFlow version: {tf.__version__}") | |
| print("Loading Keras Hub...") | |
| import keras_hub | |
| print(f"Keras Hub version: {keras_hub.__version__}") | |
| print("Loading BERT model...") | |
| try: | |
| model = tf.keras.models.load_model('model_4.keras') | |
| print("β Model loaded successfully!") | |
| return model | |
| except Exception as e: | |
| print(f"β Error loading model: {e}") | |
| raise | |
| # Model yΓΌkleme - global deΔiΕken | |
| print("Initializing application...") | |
| model = load_model() | |
| print("Application ready!") | |
| def predict_disaster(text): | |
| """Predict if a tweet is about a disaster or not""" | |
| if not text.strip(): | |
| return { | |
| "Disaster": 0.0, | |
| "Not Disaster": 0.0 | |
| }, "β οΈ Please enter a tweet to classify" | |
| try: | |
| # BERT model directly accepts raw text (has built-in preprocessing) | |
| prediction = model.predict([text], verbose=0)[0][0] | |
| # Calculate probabilities | |
| disaster_prob = float(prediction) | |
| not_disaster_prob = 1 - disaster_prob | |
| # Result message | |
| if disaster_prob > 0.5: | |
| result = f"π¨ **DISASTER** (Confidence: {disaster_prob*100:.1f}%)" | |
| else: | |
| result = f"β **NOT DISASTER** (Confidence: {not_disaster_prob*100:.1f}%)" | |
| return { | |
| "Disaster": disaster_prob, | |
| "Not Disaster": not_disaster_prob | |
| }, result | |
| except Exception as e: | |
| return { | |
| "Disaster": 0.0, | |
| "Not Disaster": 0.0 | |
| }, f"β Error during prediction: {str(e)}" | |
| # Example tweets for testing | |
| examples = [ | |
| ["Our Deeds are the Reason of this #earthquake May ALLAH Forgive us all"], | |
| ["Forest fire near La Ronge Sask. Canada"], | |
| ["13,000 people receive #wildfires evacuation orders in California"], | |
| ["Just happened a terrible car crash"], | |
| ["I love summer days at the beach with friends"], | |
| ["The sunset today is absolutely beautiful"], | |
| ["Residents asked to shelter in place are being notified by officers. No other evacuation or shelter in place orders are expected"], | |
| ["This is so awesome! Best day ever!"], | |
| ["Heard loud noises from downtown, seems like an explosion"], | |
| ["I'm making dinner tonight, trying a new recipe"], | |
| ["Buildings are collapsing after the earthquake"], | |
| ["Had a great time at the party last night!"], | |
| ["Emergency services responding to massive flooding in the area"], | |
| ["Can't wait for the weekend to start"], | |
| ["Tornado warning issued for our county, take shelter immediately"] | |
| ] | |
| # Create Gradio interface | |
| with gr.Blocks(theme=gr.themes.Soft(), title="Disaster Tweet Classifier") as demo: | |
| gr.Markdown(""" | |
| # π¨ Disaster Tweet Classification | |
| ### AI-Powered BERT Model to Identify Real Disaster Reports | |
| This application uses a fine-tuned **BERT** (Bidirectional Encoder Representations from Transformers) model | |
| to analyze tweets and classify them as either referring to a **real disaster** or **not a disaster**. | |
| Perfect for emergency response teams, news organizations, and disaster management agencies! ππ₯π | |
| """) | |
| with gr.Row(): | |
| with gr.Column(scale=2): | |
| input_text = gr.Textbox( | |
| label="π Enter Tweet Text", | |
| placeholder="Type or paste a tweet here... (e.g., 'Earthquake hits California')", | |
| lines=4 | |
| ) | |
| with gr.Row(): | |
| clear_btn = gr.Button("ποΈ Clear", variant="secondary") | |
| predict_btn = gr.Button("π Classify Tweet", variant="primary", size="lg") | |
| with gr.Column(scale=1): | |
| output_label = gr.Label( | |
| label="π Prediction Confidence", | |
| num_top_classes=2 | |
| ) | |
| output_text = gr.Markdown(label="Result") | |
| # Examples section | |
| gr.Markdown(""" | |
| --- | |
| ### π Try These Examples: | |
| Click on any example below to automatically classify it | |
| """) | |
| gr.Examples( | |
| examples=examples, | |
| inputs=input_text, | |
| outputs=[output_label, output_text], | |
| fn=predict_disaster, | |
| cache_examples=False, | |
| label="Sample Tweets" | |
| ) | |
| gr.Markdown(""" | |
| --- | |
| ### βΉοΈ About This Model | |
| **Model Architecture**: BERT Tiny (English, Uncased) | |
| - **Parameters**: ~4.4M parameters | |
| - **Training**: Fine-tuned on disaster tweet dataset | |
| - **Accuracy**: Optimized for real-time disaster detection | |
| **Use Cases**: | |
| - π¨ Emergency response monitoring | |
| - π° News verification | |
| - π Social media analysis | |
| - π Crisis management | |
| **How it Works**: | |
| The model uses contextual understanding to distinguish between: | |
| - Real disaster reports (earthquakes, fires, accidents, floods, etc.) | |
| - Casual language or metaphorical usage of disaster-related words | |
| **Limitations**: | |
| - Optimized for English tweets only | |
| - May require context for ambiguous cases | |
| - Should be used as a support tool, not sole decision-maker | |
| """) | |
| # Event handlers | |
| predict_btn.click( | |
| fn=predict_disaster, | |
| inputs=input_text, | |
| outputs=[output_label, output_text] | |
| ) | |
| input_text.submit( | |
| fn=predict_disaster, | |
| inputs=input_text, | |
| outputs=[output_label, output_text] | |
| ) | |
| clear_btn.click( | |
| fn=lambda: ("", {"Disaster": 0.0, "Not Disaster": 0.0}, ""), | |
| outputs=[input_text, output_label, output_text] | |
| ) | |
| # Launch the app | |
| if __name__ == "__main__": | |
| demo.launch( | |
| share=False, | |
| debug=False | |
| ) |