File size: 1,527 Bytes
31acc0b
 
6552ecd
 
31acc0b
 
 
 
 
43f0891
 
31acc0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import joblib
import streamlit as st

st.set_page_config(page_title="Fake News Detector", page_icon="📰", layout="centered")

st.title("📰 Fake News Detector")
st.write("Paste a news text (or headline) and the model will predict **FAKE** or **REAL**.")

MODEL_PATH = os.path.join("src/fake_news_svc_tfidf.pkl")


@st.cache_resource
def load_model():
    if not os.path.exists(MODEL_PATH):
        raise FileNotFoundError(
            f"Model not found at: {MODEL_PATH}\n"
            "Make sure the file is in the repo under models/."
        )
    return joblib.load(MODEL_PATH)

model = load_model()

text_input = st.text_area("News text / headline:", height=200, placeholder="Paste text here...")

col1, col2 = st.columns([1, 1])
with col1:
    predict_btn = st.button("Predict", type="primary")
with col2:
    clear_btn = st.button("Clear")

if clear_btn:
    st.rerun()

if predict_btn:
    if not text_input.strip():
        st.warning("Please enter some text first.")
    else:
        pred = model.predict([text_input])[0]  # 0/1

        label = "REAL ✅" if pred == 1 else "FAKE 🚨"
        st.subheader("Prediction")
        st.success(label)

        # Optional: show a score (LinearSVC has decision_function)
        try:
            score = float(model.decision_function([text_input])[0])
            st.caption(f"Decision score (higher → REAL, lower → FAKE): {score:.4f}")
        except Exception:
            pass

st.divider()
st.caption("Model: TF-IDF + Linear SVC (saved with joblib).")