File size: 3,093 Bytes
fbd840b ce843b3 8d40710 ce843b3 4b68ee3 ce843b3 a08d08a 49d47b9 4b68ee3 ce843b3 4b68ee3 ce843b3 49d47b9 ce843b3 49d47b9 fbd840b 4b68ee3 49d47b9 ce843b3 4b68ee3 a08d08a 49d47b9 8d40710 a08d08a 4b68ee3 a08d08a 4b68ee3 a08d08a ce843b3 49d47b9 4b68ee3 |
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 |
# app.py
import streamlit as st
from transformers import pipeline, AutoTokenizer
import pandas as pd
import sqlite3
import os
# --- CONFIGURATION
st.set_page_config(page_title="Détection de Fake News", layout="wide")
MODEL = "jy46604790/Fake-News-Bert-Detect"
DB_FILE = "historique.db"
# --- Chargement modèle et tokenizer
@st.cache_resource
def load_pipeline():
return pipeline("text-classification", model=MODEL, tokenizer=MODEL), AutoTokenizer.from_pretrained(MODEL)
clf, tokenizer = load_pipeline()
# --- Initialisation base de données
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()
# --- Fonctions principales
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
# --- Interface principale
st.title("Détection de Fake News")
# Barre latérale : historique
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.")
# Champ d'analyse
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)
|