import streamlit as st import joblib import re from related_news import fetch_related_articles # Load model and vectorizer model = joblib.load("model.pkl") vectorizer = joblib.load("vectorizer.pkl") # ๐Ÿงผ Text cleaning function def clean_text(text): text = re.sub(r"http\S+", "", text) text = re.sub(r"[^a-zA-Z\s]", "", text) text = text.lower() return text # ๐Ÿ’… Page setup st.set_page_config( page_title="TruthRadar ๐Ÿง ", page_icon="๐Ÿ›ฐ๏ธ", layout="centered", initial_sidebar_state="auto" ) # ๐ŸŽจ Custom styles for light theme st.markdown(""" """, unsafe_allow_html=True) # ๐Ÿ›ฐ๏ธ Header st.markdown("

๐Ÿ›ฐ๏ธ TruthRadar

", unsafe_allow_html=True) st.markdown("

Detect fake news in a flash โ€” headlines or full articles ๐Ÿ”

", unsafe_allow_html=True) st.markdown("---") st.markdown("

Currently optimised for US-based news articles.

", unsafe_allow_html=True) #input st.markdown("", unsafe_allow_html=True) user_input = st.text_area("", height=200) # ๐Ÿš€ Analyze button if st.button("๐Ÿš€ Analyze"): if not user_input.strip(): st.warning("Bruhhh paste *something* to analyze ๐Ÿ˜…") else: with st.spinner("๐Ÿง  Scanning for truth..."): try: cleaned = clean_text(user_input) transformed = vectorizer.transform([cleaned]) prediction = model.predict(transformed)[0] proba = model.predict_proba(transformed)[0] confidence = max(proba) * 100 label = prediction.upper() articles = fetch_related_articles(user_input) # โœจ Show prediction result using markdown for HTML formatting if label == "REAL": st.markdown("
โœ… Prediction: REAL
", unsafe_allow_html=True) else: st.markdown("
โŒ Prediction: FAKE
", unsafe_allow_html=True) st.markdown(f"
๐Ÿ”Ž Confidence: {confidence:.2f}%
", unsafe_allow_html=True) # ๐Ÿง Improved keyword matching for warning if label == "FAKE": input_keywords = set(re.findall(r'\b\w{4,}\b', user_input.lower())) similar_found = any( any(word in (article.get("title", "") + article.get("description", "")).lower() for word in input_keywords) for article in articles ) if similar_found: st.markdown( "
๐Ÿง Warning: Similar stories were found from trusted sources. Cross-check below.
", unsafe_allow_html=True ) # ๐Ÿ“ฐ Related News Display st.markdown("---") st.markdown("

๐Ÿ“ฐ Related News from Trusted Sources

", unsafe_allow_html=True) if not articles: st.write("No related articles found.") else: for article in articles: title = article.get("title", "No title") url = article.get("url", "#") desc = article.get("description", "No description available.") st.markdown(f"**[{title}]({url})**") st.caption(desc) except Exception as e: st.error(f"๐Ÿšจ Error: {e}") # ๐Ÿ“Ž Footer st.markdown("---") st.markdown("

Made with ๐Ÿ’ป by Sai Srikar โ€ข TruthRadar AI

", unsafe_allow_html=True)