File size: 3,978 Bytes
ca4f1f1
 
 
 
67c1024
 
 
ca4f1f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import re
from pathlib import Path

import joblib
import numpy as np
import streamlit as st


# -------------------------
# Page config
# -------------------------
st.set_page_config(
    page_title='Disaster Tweet Classifier (TF-IDF + LogReg)',
    page_icon='🚨',
    layout='centered'
)

st.title('🚨 Disaster Tweet Classifier')
st.write('Classify tweets as **Disaster (1)** or **Non-Disaster (0)** using a TF-IDF + Logistic Regression model.')


# -------------------------
# Paths (HF friendly)
# -------------------------
BASE_DIR = Path(__file__).resolve().parent
MODEL_PATH = BASE_DIR / 'final_clf.pkl'
THRESH_PATH = BASE_DIR / 'threshold.pkl'


# -------------------------
# Helpers (same logic as notebook)
# -------------------------
def build_text(text: str, keyword: str, location: str) -> str:
    text = '' if text is None else str(text)
    keyword = '' if keyword is None else str(keyword)
    location = '' if location is None else str(location)

    # same markers as training
    return text + ' [KEY] ' + keyword + ' [LOC] ' + location


def clean_text(s: str) -> str:
    s = '' if s is None else str(s)
    s = s.lower()
    s = re.sub(r'http\S+|www\.\S+', ' URL ', s)
    s = re.sub(r'@\w+', ' USER ', s)
    s = re.sub(r'\n|\r', ' ', s)
    s = re.sub(r'\s+', ' ', s)
    return s.strip()


@st.cache_resource
def load_artifacts():
    if not MODEL_PATH.exists():
        raise FileNotFoundError(
            f'Model not found: {MODEL_PATH.name}. Put it in the same folder as app.py.'
        )
    if not THRESH_PATH.exists():
        raise FileNotFoundError(
            f'Threshold not found: {THRESH_PATH.name}. Put it in the same folder as app.py.'
        )

    model = joblib.load(MODEL_PATH)
    threshold = joblib.load(THRESH_PATH)

    # make sure threshold is float
    threshold = float(threshold)
    return model, threshold


# -------------------------
# Load model
# -------------------------
try:
    model, threshold = load_artifacts()
except Exception as e:
    st.error(str(e))
    st.stop()


# -------------------------
# UI
# -------------------------
with st.form('predict_form'):
    text = st.text_area('Tweet text', height=140, placeholder='Paste the tweet text here...')
    c1, c2 = st.columns(2)
    with c1:
        keyword = st.text_input('Keyword (optional)', placeholder='e.g., earthquake')
    with c2:
        location = st.text_input('Location (optional)', placeholder='e.g., California')

    submitted = st.form_submit_button('Predict')

st.caption(f'Using threshold = **{threshold:.2f}** (tuned on validation).')

# Example buttons
with st.expander('Examples (click to copy)'):
    st.code('Just happened a terrible car crash', language='text')
    st.code('Forest fire near La Ronge Sask. Canada', language='text')
    st.code('What a beautiful sunset, the sky is ablaze!', language='text')


# -------------------------
# Prediction
# -------------------------
if submitted:
    if not text or str(text).strip() == '':
        st.warning('Please enter a tweet text.')
        st.stop()

    full_text = build_text(text, keyword, location)
    full_text = clean_text(full_text)

    try:
        proba = float(model.predict_proba([full_text])[:, 1][0])
    except Exception as e:
        st.error(f'Prediction failed: {e}')
        st.stop()

    pred = int(proba >= threshold)

    if pred == 1:
        st.success('Prediction: **Disaster (1)**')
    else:
        st.info('Prediction: **Non-Disaster (0)**')

    st.write(f'**Disaster probability:** {proba:.3f}')

    # Simple explanation
    st.markdown('---')
    st.subheader('What was sent to the model?')
    st.code(full_text, language='text')

    st.markdown('---')
    st.subheader('Notes')
    st.write(
        '- The model uses TF-IDF features (word and bigram features).\n'
        '- The threshold was tuned on a validation split to improve F1-score.\n'
        '- Keyword and location are appended to the text using special markers.'
    )