Spaces:
Sleeping
Sleeping
File size: 6,032 Bytes
39c1ac4 3b60e00 1fd52db 39c1ac4 8aa9f64 39c1ac4 fe86b7f 1fd52db 39c1ac4 c1810f7 39c1ac4 8aa9f64 3b60e00 39c1ac4 c1810f7 39c1ac4 3b60e00 39c1ac4 fe86b7f 8aa9f64 39c1ac4 3b60e00 39c1ac4 fe86b7f 39c1ac4 fe86b7f 39c1ac4 8aa9f64 39c1ac4 2b98400 39c1ac4 3b60e00 fc269f8 3b60e00 fc269f8 3b60e00 39c1ac4 8aa9f64 39c1ac4 fe86b7f 39c1ac4 fe86b7f 39c1ac4 c1810f7 fe86b7f 39c1ac4 fe86b7f 3b60e00 39c1ac4 fe86b7f 39c1ac4 fe86b7f 39c1ac4 3b60e00 39c1ac4 3b60e00 2058299 3b60e00 fe86b7f 3b60e00 fe86b7f 39c1ac4 3b60e00 202b83d 3b60e00 8dc3e08 3b60e00 39c1ac4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
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")
|