Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import pipeline | |
from deep_translator import GoogleTranslator | |
import requests | |
############ SETTING UP THE PAGE LAYOUT AND TITLE ############ | |
# `st.set_page_config` is used to display the default layout width, the title of the app, and the emoticon in the browser tab. | |
st.set_page_config(layout="centered", page_title="FND Fake News") | |
############ CREATE THE LOGO AND HEADING ############ | |
# We create a set of columns to display the logo and the heading next to each other. | |
c1, c2 = st.columns([0.32, 2]) | |
# The snowflake logo will be displayed in the first column, on the left. | |
with c1: | |
st.caption("") | |
st.title("📑") | |
# The heading will be on the right. | |
with c2: | |
st.caption("") | |
st.title("FND Detection") | |
# We need to set up session state via st.session_state so that app interactions don't reset the app. | |
if "valid_inputs_received" not in st.session_state: | |
st.session_state["valid_inputs_received"] = False | |
############ SIDEBAR CONTENT ############ | |
st.sidebar.subheader("Model Options") | |
st.sidebar.write("") | |
SELECTED_MODEL = st.sidebar.selectbox("Choose a model", ("XLm-Roberta", "Lstm")) | |
LANGUAGE = st.sidebar.selectbox("Language", ("en", "ar"), format_func=lambda x: "English" if x == "en" else "Arabic") | |
# Model selection | |
if SELECTED_MODEL: | |
st.session_state.valid_inputs_received = False | |
MODEL_INFO = { | |
"XLm-Roberta": """ | |
This model is trained by over 400,000 arabic news and 70000 english news from different medias based on the 'XLm-roberta-base'. It can give result by simply entering the text of the news less than 90000 words(the excess will be truncated automatically). | |
""", | |
"Lstm": """ | |
It is trained on the provided datasets\n | |
""", | |
None: "NO MODEL SELECTED", | |
} | |
model_info_container = st.sidebar.container(border=True) | |
model_info_container.markdown(MODEL_INFO[SELECTED_MODEL]) | |
copyright_container = st.sidebar.container(border=True) | |
copyright_container.markdown("Copyright ©️ 2024 [Mohamed Ed Deryouch](https://huggingface.co/edderyouch)") | |
############ TABBED NAVIGATION ############ | |
MainTab , InfoTab = st.tabs(["Main","Info"]) | |
############ TRANSLATION FUNCTIONS ############ | |
def translate_to_english(text): | |
try: | |
if text.strip(): # Only translate if there's actual text | |
return GoogleTranslator(source="auto", target="en").translate(text) | |
return text | |
except Exception as e: | |
st.error(f"Translation error: {str(e)}") | |
return text | |
def translate_from_english(text, target_language): | |
if target_language == "en" or not text.strip(): | |
return text | |
try: | |
return GoogleTranslator(source="en", target=target_language).translate(text) | |
except Exception as e: | |
st.error(f"Translation error: {str(e)}") | |
return text | |
############ API VERIFICATION FUNCTIONS ############ | |
NEWS_API_KEY = 'f939987352f04e96b798ca181910c2f0' | |
NEWS_API_URL = 'https://newsapi.org/v2/everything' | |
FACT_CHECK_API_KEY = "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" | |
def verify_with_news_api(query): | |
if not NEWS_API_KEY: | |
return None | |
url = f"https://factchecktools.googleapis.com/v1alpha1/claims:search?query={query}&key={FACT_CHECK_API_KEY}" | |
response = requests.get(url) | |
print(response) | |
if response.status_code == 200: | |
claims = response.json().get("claims", []) | |
return any(claim["claimReview"] for claim in claims) | |
return False | |
############ MODEL FUNCTION ############ | |
def MODEL_RESULT(model: str, news: str) -> str | None: | |
if model == "XLm-Roberta": | |
MODEL_jy46604790 = "jy46604790/Fake-News-Bert-Detect" | |
classifier = pipeline( | |
"text-classification", model=MODEL_jy46604790, tokenizer=MODEL_jy46604790 | |
) | |
result = classifier(news) | |
if result[0]["label"] == "LABEL_1": | |
return "REAL NEWS" | |
else: | |
return "FAKE NEWS" | |
if model == "Lstm": | |
from utils import modelx | |
return modelx(arch=model, model_path="models/lstm/x_g85_lstm.keras", text=news) | |
with MainTab: | |
st.write("") | |
st.markdown("Classify News based on the selected ml model.") | |
st.write("") | |
container = st.container(border=True) | |
container.write(f"Selected model: {SELECTED_MODEL}") | |
container.write(f"Selected language: {'English' if LANGUAGE == 'en' else 'Arabic'}") | |
with st.form(key="main_form"): | |
# Predefined news examples | |
pre_defined_news = { | |
"en": "SCIENTISTS DISCOVER TALKING ELEPHANTS: Researchers at Harvard", | |
"ar": " هبطت مركبة ناسا الجوالة 'بيرسيفيرانس' بنجاح على سطح المريخ " | |
} | |
news = st.text_area( | |
"Enter news to classify", | |
pre_defined_news[LANGUAGE], | |
height=200, | |
help="Please provide the news that you need to verify for its truthfulness.", | |
key="news", | |
) | |
submit_button = st.form_submit_button(label="Analyze News") | |
if submit_button and not news.strip(): | |
st.warning("📑 Please enter some news text to analyze") | |
st.stop() | |
elif submit_button or st.session_state.valid_inputs_received: | |
if submit_button: | |
st.session_state.valid_inputs_received = True | |
# Translation process | |
processed_news = news | |
processed_news = translate_to_english(news) if LANGUAGE == "ar" else news | |
# Get model prediction | |
with st.spinner("Analyzing..."): | |
prediction = MODEL_RESULT(SELECTED_MODEL, processed_news) | |
query = " ".join(processed_news.split()[:5]) # Use first 5 words as query | |
api_result = verify_with_news_api(query) | |
print(prediction) | |
st.subheader("Result") | |
if prediction == "REAL NEWS": | |
st.success("✅ Model Prediction: REAL NEWS") | |
else: | |
st.error("❌ Model Prediction: FAKE NEWS") | |