import streamlit as st import torch from transformers import AutoTokenizer, AutoModelForSequenceClassification import numpy as np import os st.set_page_config( page_title="TruthLens โ Fake News Detector", page_icon="๐", layout="wide", initial_sidebar_state="collapsed" ) st.markdown(""" """, unsafe_allow_html=True) # โโ Load model โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ @st.cache_resource def load_model(): from transformers import pipeline # Using a well-trained public fake news detection model classifier = pipeline( "text-classification", model="hamzab/roberta-fake-news-classification", device=-1 ) return classifier classifier = load_model() # โโ Predict โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ def predict(text): result = classifier(text, truncation=True, max_length=512)[0] label_raw = result['label'].upper() score = result['score'] if 'FAKE' in label_raw or label_raw == 'LABEL_0': label = "FAKE" fake_prob = round(score * 100, 1) real_prob = round((1 - score) * 100, 1) else: label = "REAL" real_prob = round(score * 100, 1) fake_prob = round((1 - score) * 100, 1) confidence = round(score * 100, 1) risk_score = int(fake_prob) return label, confidence, fake_prob, real_prob, risk_score return label, confidence, round(fake_prob*100,1), round(real_prob*100,1), risk_score def get_signals(text): lower = text.lower() signals = [] cap = sum(1 for c in text if c.isupper()) / max(len(text),1) signals.append(("Capitalization", f"{cap*100:.0f}%", "HIGH" if cap>0.2 else "LOW")) excl = text.count('!') signals.append(("Exclamation marks", str(excl), "HIGH" if excl>=3 else ("MED" if excl else "LOW"))) sens = ['shocking','breaking','secret','hidden','suppressed','miracle','exposed','bombshell'] found = [w for w in sens if w in lower] signals.append(("Sensational words", str(len(found)), "HIGH" if len(found)>=2 else ("MED" if found else "LOW"))) factual = ['according to','study','research','published','reported','announced'] fc = [w for w in factual if w in lower] signals.append(("Factual language", f"{len(fc)} found", "LOW" if len(fc)>=2 else "HIGH")) return signals # โโ Samples โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ SAMPLES = { "fake1": "SHOCKING: Scientists CONFIRM that drinking lemon juice cures ALL cancers overnight! Big Pharma desperately hiding this miracle cure! Share before it gets deleted!!!", "fake2": "BREAKING: The moon landing was STAGED in Hollywood! Leaked NASA documents CONFIRM what conspiracy theorists have known. Government LYING to us for decades!!!", "real1": "The Intergovernmental Panel on Climate Change released its assessment report indicating global temperatures have risen 1.1 degrees Celsius above pre-industrial levels, compiled by 230 scientists.", "real2": "Apple reported quarterly revenue of $89.5 billion, with its services segment growing 16 percent year-over-year according to the company's official earnings call." } # โโ UI โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ # Header st.markdown("""
Fine-tuned DistilBERT model trained on 70,000+ articles. Paste any headline or article to instantly analyze it.
Try a sample โ
', unsafe_allow_html=True) c1, c2, c3, c4 = st.columns(4) with c1: st.markdown('{len(text_input)} CHARACTERS
", unsafe_allow_html=True) st.markdown("", unsafe_allow_html=True) col_btn, _ = st.columns([1, 2]) with col_btn: st.markdown('