Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,41 +3,14 @@ import joblib
|
|
| 3 |
import pandas as pd
|
| 4 |
import re
|
| 5 |
import nltk
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
# from nltk.stem.porter import PorterStemmer
|
| 9 |
import os
|
| 10 |
|
| 11 |
# --- CẤU HÌNH TRANG STREAMLIT (PHẢI LÀ LỆNH STREAMLIT ĐẦU TIÊN) ---
|
| 12 |
st.set_page_config(page_title="Phân biệt Tin tức Thật/Giả", layout="centered")
|
| 13 |
|
| 14 |
-
# ---
|
| 15 |
-
# PHẢI CHÍNH XÁC NHƯ LỚP BẠN ĐÃ DÙNG KHI HUẤN LUYỆN
|
| 16 |
-
from sklearn.base import BaseEstimator, TransformerMixin
|
| 17 |
-
|
| 18 |
-
class TextCleaner(BaseEstimator, TransformerMixin):
|
| 19 |
-
def fit(self, X, y=None):
|
| 20 |
-
return self
|
| 21 |
-
|
| 22 |
-
def transform(self, X: pd.Series, y=None):
|
| 23 |
-
if not isinstance(X, pd.Series):
|
| 24 |
-
X = pd.Series(X)
|
| 25 |
-
|
| 26 |
-
return (
|
| 27 |
-
X
|
| 28 |
-
.str.replace(r'<[^>]+>', ' ', regex=True)
|
| 29 |
-
.str.replace(r'http\S+|\S+@\S+', ' ', regex=True)
|
| 30 |
-
.str.replace(r'[^A-Za-z0-9\s]', ' ', regex=True)
|
| 31 |
-
.str.replace(r'\s+', ' ', regex=True)
|
| 32 |
-
.str.lower()
|
| 33 |
-
.str.strip()
|
| 34 |
-
)
|
| 35 |
-
|
| 36 |
-
# --- Cài đặt và tải tài nguyên NLTK (có thể không cần nữa cho hàm preprocess_text cũ,
|
| 37 |
-
# nhưng giữ lại nếu có bất kỳ phần nào khác trong ứng dụng cần NLTK) ---
|
| 38 |
-
# Nếu bạn không sử dụng NLTK trong app.py sau khi model được tải,
|
| 39 |
-
# bạn có thể loại bỏ toàn bộ phần NLTK này để giảm kích thước và thời gian build.
|
| 40 |
-
# Tuy nhiên, nếu bạn dùng nltk.download() trong app.py, giữ lại nó.
|
| 41 |
nltk_data_path = os.path.join(os.path.dirname(__file__), 'nltk_data')
|
| 42 |
os.makedirs(os.path.join(nltk_data_path, 'corpora'), exist_ok=True)
|
| 43 |
nltk.data.path.append(nltk_data_path)
|
|
@@ -49,19 +22,35 @@ except Exception as e:
|
|
| 49 |
st.error(f"Lỗi khi tải NLTK stopwords: {e}")
|
| 50 |
st.stop()
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
# --- Tải mô hình ---
|
| 54 |
model = None
|
| 55 |
try:
|
| 56 |
model_path = os.path.join(os.path.dirname(__file__), 'fake_news_model.pkl')
|
| 57 |
-
# joblib.load() sẽ cần tìm thấy lớp TextCleaner TRƯỚC khi nó tải model
|
| 58 |
model = joblib.load(model_path)
|
| 59 |
-
st.success
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
-
st.error(f"Lỗi khi tải mô hình: {e}. Vui lòng đảm bảo
|
| 62 |
-
st.stop()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
# --- Giao diện Streamlit ---
|
|
|
|
| 65 |
st.title("📰 Phân biệt Tin tức Thật/Giả")
|
| 66 |
st.markdown("Sử dụng mô hình AI để phân tích và xác định liệu một tin tức là thật hay giả.")
|
| 67 |
|
|
@@ -79,7 +68,6 @@ if st.button("Phân tích"):
|
|
| 79 |
st.error("Mô hình chưa sẵn sàng để phân tích. Vui lòng kiểm tra lại lỗi tải mô hình.")
|
| 80 |
else:
|
| 81 |
with st.spinner("Đang phân tích tin tức..."):
|
| 82 |
-
# Tạo DataFrame đầu vào giống với X_train (có 2 cột 'Feature_1', 'Feature_2')
|
| 83 |
input_df = pd.DataFrame({
|
| 84 |
'Feature_1': [title_input],
|
| 85 |
'Feature_2': [content_input]
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import re
|
| 5 |
import nltk
|
| 6 |
+
from nltk.corpus import stopwords
|
| 7 |
+
from nltk.stem.porter import PorterStemmer
|
|
|
|
| 8 |
import os
|
| 9 |
|
| 10 |
# --- CẤU HÌNH TRANG STREAMLIT (PHẢI LÀ LỆNH STREAMLIT ĐẦU TIÊN) ---
|
| 11 |
st.set_page_config(page_title="Phân biệt Tin tức Thật/Giả", layout="centered")
|
| 12 |
|
| 13 |
+
# --- Cài đặt và tải tài nguyên NLTK (quan trọng cho môi trường Docker) ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
nltk_data_path = os.path.join(os.path.dirname(__file__), 'nltk_data')
|
| 15 |
os.makedirs(os.path.join(nltk_data_path, 'corpora'), exist_ok=True)
|
| 16 |
nltk.data.path.append(nltk_data_path)
|
|
|
|
| 22 |
st.error(f"Lỗi khi tải NLTK stopwords: {e}")
|
| 23 |
st.stop()
|
| 24 |
|
| 25 |
+
# --- Định nghĩa hàm clean_text CHÍNH XÁC NHƯ TRONG CODE HUẤN LUYỆN ---
|
| 26 |
+
def clean_text(series: pd.Series) -> pd.Series:
|
| 27 |
+
return (
|
| 28 |
+
series
|
| 29 |
+
.str.replace(r'<[^>]+>', ' ', regex=True)
|
| 30 |
+
.str.replace(r'http\S+|\S+@\S+', ' ', regex=True)
|
| 31 |
+
.str.replace(r'[^A-Za-z0-9\s]', ' ', regex=True)
|
| 32 |
+
.str.lower()
|
| 33 |
+
.str.strip()
|
| 34 |
+
)
|
| 35 |
|
| 36 |
# --- Tải mô hình ---
|
| 37 |
model = None
|
| 38 |
try:
|
| 39 |
model_path = os.path.join(os.path.dirname(__file__), 'fake_news_model.pkl')
|
|
|
|
| 40 |
model = joblib.load(model_path)
|
| 41 |
+
# Không dùng st.success/st.error ở đây nữa, vì chúng phải được gọi sau set_page_config
|
| 42 |
+
# st.success("Mô hình fake_news_model.pkl đã được tải thành công!")
|
| 43 |
except Exception as e:
|
| 44 |
+
st.error(f"Lỗi khi tải mô hình: {e}. Vui lòng đảm bảo hàm clean_text đã được định nghĩa chính xác và file mô hình nằm đúng vị trí.")
|
| 45 |
+
st.stop() # Dừng ứng dụng nếu mô hình không tải được
|
| 46 |
+
|
| 47 |
+
# Chỉ hiển thị thông báo tải thành công nếu không có lỗi nào trước đó
|
| 48 |
+
if model is not None:
|
| 49 |
+
st.success("Mô hình fake_news_model.pkl đã được tải thành công!")
|
| 50 |
+
|
| 51 |
|
| 52 |
# --- Giao diện Streamlit ---
|
| 53 |
+
# Các lệnh Streamlit khác sẽ bắt đầu từ đây trở đi
|
| 54 |
st.title("📰 Phân biệt Tin tức Thật/Giả")
|
| 55 |
st.markdown("Sử dụng mô hình AI để phân tích và xác định liệu một tin tức là thật hay giả.")
|
| 56 |
|
|
|
|
| 68 |
st.error("Mô hình chưa sẵn sàng để phân tích. Vui lòng kiểm tra lại lỗi tải mô hình.")
|
| 69 |
else:
|
| 70 |
with st.spinner("Đang phân tích tin tức..."):
|
|
|
|
| 71 |
input_df = pd.DataFrame({
|
| 72 |
'Feature_1': [title_input],
|
| 73 |
'Feature_2': [content_input]
|