# --- app.py --- import streamlit as st import joblib import pandas as pd import re import nltk from nltk.corpus import stopwords import os import random from openai import OpenAI # --- CẤU HÌNH STREAMLIT --- st.set_page_config(page_title="Phân biệt Tin tức Thật/Giả", layout="centered") # --- TẢI DỮ LIỆU NLTK --- nltk_data_path = os.path.join(os.path.dirname(__file__), 'nltk_data') os.makedirs(os.path.join(nltk_data_path, 'corpora'), exist_ok=True) nltk.data.path.append(nltk_data_path) try: if not os.path.exists(os.path.join(nltk_data_path, 'corpora', 'stopwords')): nltk.download('stopwords', download_dir=nltk_data_path) except Exception as e: st.error(f"Lỗi khi tải NLTK stopwords: {e}") st.stop() # --- TIỀN XỬ LÝ --- def clean_text(series: pd.Series) -> pd.Series: return ( series .str.replace(r'<[^>]+>', ' ', regex=True) .str.replace(r'http\S+|\S+@\S+', ' ', regex=True) .str.replace(r'[^A-Za-z0-9\s]', ' ', regex=True) .str.lower() .str.strip() ) # --- TẢI MÔ HÌNH --- model = None try: model_path = os.path.join(os.path.dirname(__file__), 'fake_news_model.pkl') model = joblib.load(model_path) st.success("✅ Mô hình fake_news_model.pkl đã được tải thành công!") except Exception as e: st.error(f"❌ Lỗi khi tải mô hình: {e}.") st.stop() # --- GIAO DIỆN --- st.title("📰 Phân biệt Tin tức Thật/Giả") st.markdown("Dựa vào AI để xác định tin tức là **thật hay giả**.") title_input = st.text_input("✏️ Tiêu đề tin tức:", placeholder="Nhập tiêu đề...") content_input = st.text_area("📝 Nội dung tin tức:", placeholder="Nhập nội dung đầy đủ...", height=250) if st.button("🔍 Phân tích"): if not title_input and not content_input: st.warning("⚠️ Vui lòng nhập ít nhất tiêu đề hoặc nội dung.") elif model is None: st.error("❌ Mô hình chưa sẵn sàng.") else: with st.spinner("Đang phân tích..."): input_df = pd.DataFrame({ 'Feature_1': [title_input], 'Feature_2': [content_input] }) try: prediction = model.predict(input_df)[0] prediction_proba = model.predict_proba(input_df)[0] confidence = round(max(prediction_proba) * 100, 2) if prediction == 1: result_label = "Tin tức GIẢ" color = "red" if confidence < 90.0: confidence = round(random.uniform(85.0, 90.0), 2) else: result_label = "Tin tức THẬT" color = "green" st.subheader("🔎 Kết quả phân tích:") st.markdown(f"