Nolist commited on
Commit
201eb52
·
verified ·
1 Parent(s): 48dc57c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -14
app.py CHANGED
@@ -11,14 +11,19 @@ import os
11
  # Tải stopwords vào thư mục con 'nltk_data'
12
  # Điều này giúp Dockerfile dễ dàng sao chép và NLTK tìm thấy chúng
13
  nltk_data_path = os.path.join(os.path.dirname(__file__), 'nltk_data')
14
- if not os.path.exists(os.path.join(nltk_data_path, 'corpora', 'stopwords')):
15
- os.makedirs(os.path.join(nltk_data_path, 'corpora'), exist_ok=True)
16
- nltk.data.path.append(nltk_data_path) # Thêm đường dẫn này để NLTK tìm kiếm
17
- try:
 
 
 
 
 
18
  nltk.download('stopwords', download_dir=nltk_data_path)
19
- except Exception as e:
20
- st.error(f"Lỗi khi tải NLTK stopwords: {e}")
21
- st.stop() # Dừng ứng dụng nếu không tải được
22
 
23
  # --- Tải mô hình ---
24
  model = None
@@ -78,16 +83,20 @@ if st.button("Phân tích"):
78
  prediction = model.predict([processed_text])[0]
79
  prediction_proba = model.predict_proba([processed_text])[0]
80
 
81
- # Điều chỉnh nhãn tùy thuộc vào cách bạn mã hóa 0 và 1 trong quá trình huấn luyện
82
-
83
- if prediction == 0:
84
- result_label = "True"
85
  color = "red"
86
- else:
87
- result_label = "Fake"
88
  color = "green"
 
 
 
 
 
 
89
 
90
- confidence = round(max(prediction_proba) * 100, 2)
91
 
92
  st.subheader("Kết quả phân tích:")
93
  st.markdown(f"<h3 style='color:{color};'>{result_label}</h3>", unsafe_allow_html=True)
 
11
  # Tải stopwords vào thư mục con 'nltk_data'
12
  # Điều này giúp Dockerfile dễ dàng sao chép và NLTK tìm thấy chúng
13
  nltk_data_path = os.path.join(os.path.dirname(__file__), 'nltk_data')
14
+
15
+ # Đảm bảo thư mục tồn tại
16
+ os.makedirs(os.path.join(nltk_data_path, 'corpora'), exist_ok=True)
17
+ # Thêm đường dẫn này để NLTK tìm kiếm
18
+ nltk.data.path.append(nltk_data_path)
19
+
20
+ # Cố gắng tải stopwords
21
+ try:
22
+ if not os.path.exists(os.path.join(nltk_data_path, 'corpora', 'stopwords')):
23
  nltk.download('stopwords', download_dir=nltk_data_path)
24
+ except Exception as e:
25
+ st.error(f"Lỗi khi tải NLTK stopwords: {e}")
26
+ st.stop() # Dừng ứng dụng nếu không tải được
27
 
28
  # --- Tải mô hình ---
29
  model = None
 
83
  prediction = model.predict([processed_text])[0]
84
  prediction_proba = model.predict_proba([processed_text])[0]
85
 
86
+ # === ĐIỀU CHỈNH LOGIC Ở ĐÂY DỰA TRÊN 1=FAKE, 0=TRUE ===
87
+ if prediction == 1: # Nếu prediction là 1, đó là TIN GIẢ
88
+ result_label = "Tin tức GIẢ"
 
89
  color = "red"
90
+ else: # Nếu prediction là 0, đó là TIN THẬT
91
+ result_label = "Tin tức THẬT"
92
  color = "green"
93
+ # =======================================================
94
+
95
+ # Lấy độ tin cậy của lớp được dự đoán
96
+ # Nếu prediction là 1 (FAKE), lấy xác suất của lớp 1
97
+ # Nếu prediction là 0 (TRUE), lấy xác suất của lớp 0
98
+ confidence = round(prediction_proba[prediction] * 100, 2)
99
 
 
100
 
101
  st.subheader("Kết quả phân tích:")
102
  st.markdown(f"<h3 style='color:{color};'>{result_label}</h3>", unsafe_allow_html=True)