CoolFace
Apppublic

Igniteit/TruthGuardAIDev

sourceHugging Faceupdated 8mo agoView on Hugging Face
0likes
streamlit_app.py102 linesDownload Raw Back to src
1import streamlit as st2from transformers import pipeline3 4# -----------------------------5# Page Config6# -----------------------------7st.set_page_config(8    page_title="TruthGuard AI",9    page_icon="๐Ÿ›ก๏ธ",10    layout="centered"11)12 13st.title("๐Ÿ›ก๏ธ TruthGuard AI")14st.subheader("AI-Powered Fake News & Misinformation Detector")15 16st.markdown("""17TruthGuard AI analyzes news articles, social media posts,18and public statements to estimate whether content is19**likely real or misinformation**.20 21โš ๏ธ This tool is for educational purposes only.22""")23 24# -----------------------------25# Load Models (Public & Safe)26# -----------------------------27@st.cache_resource(show_spinner=True)28def load_models():29    return {30        "zero_shot": pipeline(31            "zero-shot-classification",32            model="facebook/bart-large-mnli"  # public, no token needed33        )34    }35 36models = load_models()37 38# -----------------------------39# User Input40# -----------------------------41text = st.text_area(42    "Paste content to analyze:",43    height=220,44    placeholder="Paste news, tweet, or statement here..."45)46 47def valid_input(txt):48    return len(txt.strip()) > 20  # allow shorter claims too49 50# -----------------------------51# Explainability Function52# -----------------------------53def explain(text):54    reasons = []55    if "!" in text or "โš ๏ธ" in text or "Breaking" in text:56        reasons.append("Sensational or emotional language detected")57    if len(text.split()) < 30:58        reasons.append("Short claim with limited context")59    if not reasons:60        reasons.append("Patterns similar to misinformation found")61    return reasons62 63# -----------------------------64# Analyze Button65# -----------------------------66if st.button("๐Ÿ” Analyze"):67    if not valid_input(text):68        st.warning("Please enter a meaningful statement to analyze.")69    else:70        with st.spinner("Analyzing with AI models (first run may take 20-30s)..."):71            res = models["zero_shot"](text, candidate_labels=["FAKE", "REAL"])72        73        final = res['labels'][0]       # top predicted label74        confidence = round(res['scores'][0]*100, 2)  # confidence %75 76        # Display results77        if final == "FAKE":78            st.error(f"๐Ÿšจ Likely Misinformation ({confidence}%)")79        else:80            st.success(f"โœ… Likely Real Content ({confidence}%)")81 82        # Explain reasoning83        st.markdown("### ๐Ÿง  Why this result?")84        for r in explain(text):85            st.write("โ€ข", r)86 87        # Fact-check sources88        st.markdown("### ๐Ÿ”Ž Recommended Fact-Check Sources")89        for src in ["Reuters", "BBC", "AP News", "Snopes", "PolitiFact"]:90            st.write("โ€ข", src)91 92        st.caption(93            "โš ๏ธ This AI does not verify facts against live sources. "94            "Always cross-check information."95        )96 97# -----------------------------98# Footer99# -----------------------------100st.markdown("---")101st.caption("Built by a Senior Software Engineer | Responsible AI โ€ข NLP")102