|
|
|
|
|
import streamlit as st |
|
from transformers import pipeline, AutoTokenizer |
|
import pandas as pd |
|
import sqlite3 |
|
import os |
|
|
|
|
|
st.set_page_config(page_title="Détection de Fake News", layout="wide") |
|
MODEL = "jy46604790/Fake-News-Bert-Detect" |
|
DB_FILE = "historique.db" |
|
|
|
|
|
@st.cache_resource |
|
def load_pipeline(): |
|
return pipeline("text-classification", model=MODEL, tokenizer=MODEL), AutoTokenizer.from_pretrained(MODEL) |
|
|
|
clf, tokenizer = load_pipeline() |
|
|
|
|
|
def init_db(): |
|
if not os.path.exists(DB_FILE): |
|
conn = sqlite3.connect(DB_FILE) |
|
c = conn.cursor() |
|
c.execute(''' |
|
CREATE TABLE IF NOT EXISTS historique ( |
|
id INTEGER PRIMARY KEY AUTOINCREMENT, |
|
texte TEXT, |
|
prediction TEXT, |
|
score REAL |
|
) |
|
''') |
|
conn.commit() |
|
conn.close() |
|
|
|
init_db() |
|
|
|
|
|
def detect(text): |
|
tokens = tokenizer.encode(text, truncation=True, max_length=512, add_special_tokens=True) |
|
text = tokenizer.decode(tokens, skip_special_tokens=True) |
|
result = clf(text)[0] |
|
label = "Fake news" if result["label"] == "LABEL_0" else "Real news" |
|
return label, result["score"] |
|
|
|
def save_to_db(texte, prediction, score): |
|
conn = sqlite3.connect(DB_FILE) |
|
c = conn.cursor() |
|
c.execute('INSERT INTO historique (texte, prediction, score) VALUES (?, ?, ?)', (texte, prediction, score)) |
|
conn.commit() |
|
conn.close() |
|
|
|
def load_history(): |
|
conn = sqlite3.connect(DB_FILE) |
|
df = pd.read_sql_query('SELECT * FROM historique ORDER BY id DESC', conn) |
|
conn.close() |
|
return df |
|
|
|
|
|
st.title("Détection de Fake News") |
|
|
|
|
|
with st.sidebar: |
|
st.header("Historique des analyses") |
|
history = load_history() |
|
if not history.empty: |
|
options = [f"{row['id']}: {row['prediction']} (Score: {row['score']:.2f})" for idx, row in history.iterrows()] |
|
selection = st.selectbox("Sélectionner une analyse :", options) |
|
selected_id = int(selection.split(":")[0]) |
|
selected_row = history[history['id'] == selected_id].iloc[0] |
|
|
|
st.subheader("Détail de l'analyse") |
|
color = "red" if selected_row["prediction"] == "Fake news" else "green" |
|
st.markdown(f"<h4 style='color:{color};'>{selected_row['prediction']} (Confiance: {selected_row['score']:.2f})</h4>", unsafe_allow_html=True) |
|
st.markdown(f"<div style='border:1px solid #ccc; padding:10px; border-radius:5px;'>{selected_row['texte']}</div>", unsafe_allow_html=True) |
|
else: |
|
st.info("Aucune analyse enregistrée.") |
|
|
|
|
|
text = st.text_area("Entrez un nouveau texte à analyser :", height=150) |
|
|
|
if st.button("Analyser") and text.strip(): |
|
label, score = detect(text) |
|
color = "red" if label == "Fake news" else "green" |
|
st.markdown(f"<h3 style='color:{color};'>{label} (Confiance: {score:.2f})</h3>", unsafe_allow_html=True) |
|
save_to_db(text, label, score) |
|
|