CognitiveScience/FakeNewsDetector
0
1import streamlit as st2import transformers3import torch4import requests5from PIL import Image6from transformers import AutoTokenizer, AutoModelForSequenceClassification7 8# Setting the page configurations9st.set_page_config(10 page_title="Fake News Detection App", 11 page_icon="fas fa-exclamation-triangle", 12 layout="wide", 13 initial_sidebar_state="auto")14 15# Load the model and tokenizer16model_name = AutoModelForSequenceClassification.from_pretrained("ikoghoemmanuell/finetuned_fake_news_roberta")17tokenizer_name = AutoTokenizer.from_pretrained("ikoghoemmanuell/finetuned_fake_news_roberta")18 19 20# Define the CSS style for the app21st.markdown(22"""23<style>24body {25 background-color: #f5f5f5;26}27h1 {28 color: #4e79a7;29}30</style>31""",32unsafe_allow_html=True33)34 35# Set up sidebar36st.sidebar.header('Navigation')37menu = ['Home', 'About']38choice = st.sidebar.selectbox(39 "Select an option", 40 menu)41 42# Define the function for detecting fake news43@st.cache_resource44def detect_fake_news(text):45 # Load the pipeline.46 pipeline = transformers.pipeline("text-classification", 47 model=model_name, 48 tokenizer=tokenizer_name)49 50 # Predict the sentiment.51 prediction = pipeline(text)52 sentiment = prediction[0]["label"]53 score = prediction[0]["score"]54 55 return sentiment, score56 57 58# Home section59if choice == 'Home':60 st.markdown("<h1 style='text-align: center;margin-top:0px;'>TRUTH- A fake news detection app</h1>", 61 unsafe_allow_html=True)62 63 # Loading GIF64 gif_url = "https://thumbs.gfycat.com/AnchoredWeeklyGreatwhiteshark-size_restricted.gif"65 st.image(gif_url, 66 use_column_width=True, 67 width=400)68 69 st.markdown("<h1 style='text-align: center;'>Welcome</h1>", 70 unsafe_allow_html=True)71 st.markdown("<p style='text-align: center;'>This is a Fake News Detection App.</p>", 72 unsafe_allow_html=True)73 74 # Get user input75 text = st.text_input("Enter some text and we'll tell you if it's likely to be fake news or not!")76 77 if st.button('Predict'):78 # Show fake news detection output79 if text:80 with st.spinner('Checking if news is Fake...'):81 label, score = detect_fake_news(text)82 if label == "LABEL_1":83 st.error(f"The text is likely to be fake news with a confidence score of {score*100:.2f}%!")84 else:85 st.success(f"The text is likely to be genuine with a confidence score of {score*100:.2f}%!")86 else:87 with st.spinner('Checking if news is Fake...'):88 st.warning("Please enter some text to detect fake news.")89 90 91# About section92if choice == 'About':93 # Load the banner image94 banner_image_url = "https://docs.gato.txst.edu/78660/w/2000/a_1dzGZrL3bG/fake-fact.jpg"95 96 # Display the banner image97 st.image(98 banner_image_url, 99 use_column_width=True, 100 width=400)101 st.markdown('''102 <p style='font-size: 20px; font-style: italic;font-style: bold;'>103 104 TRUTH is a cutting-edge application specifically designed to combat the spread of fake 105 news. Using state-of-the-art algorithms and advanced deep learning techniques, our app 106 empowers users to detect and verify the authenticity of news articles. TRUTH provides 107 accurate assessments of the reliability of news content. With its user-friendly 108 interface and intuitive design, the app enables users to easily navigate and obtain 109 trustworthy information in real-time. With TRUTH, you can take control of the news you110 consume and make informed decisions based on verified facts.111 112 </p>113 ''', 114 unsafe_allow_html=True)