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.' )