Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
|
@@ -411,7 +411,7 @@ def analyze_with_gemini(news_text, search_results, distilbert_prediction, distil
|
|
| 411 |
distilbert_analysis = "DistilBERT: Không thể phân tích"
|
| 412 |
|
| 413 |
prompt = f"""
|
| 414 |
-
Hãy phân tích tin tức sau và đánh giá độ tin cậy của
|
| 415 |
|
| 416 |
"{news_text}"
|
| 417 |
|
|
@@ -419,15 +419,13 @@ Hãy phân tích tin tức sau và đánh giá độ tin cậy của nó:
|
|
| 419 |
|
| 420 |
{distilbert_analysis}
|
| 421 |
|
| 422 |
-
|
| 423 |
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
3. So sánh với kết quả DistilBERT
|
| 428 |
-
4. Khuyến nghị cho người đọc
|
| 429 |
|
| 430 |
-
|
| 431 |
"""
|
| 432 |
|
| 433 |
print("Calling Gemini API...")
|
|
@@ -455,11 +453,15 @@ Phân tích bằng tiếng Việt, ngắn gọn và dễ hiểu.
|
|
| 455 |
if "429" in str(e) or "quota" in str(e).lower():
|
| 456 |
print("Gemini API quota exceeded, providing fallback analysis...")
|
| 457 |
fallback_analysis = f"""
|
| 458 |
-
Phân tích
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 463 |
"""
|
| 464 |
return fallback_analysis
|
| 465 |
|
|
@@ -542,7 +544,29 @@ def analyze_news(news_text):
|
|
| 542 |
"""Main analysis function combining all three tools"""
|
| 543 |
try:
|
| 544 |
if not news_text.strip():
|
| 545 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 546 |
|
| 547 |
print(f"Analyzing: {news_text[:50]}...")
|
| 548 |
|
|
@@ -600,38 +624,90 @@ def analyze_news(news_text):
|
|
| 600 |
real_confidence = combined_confidence
|
| 601 |
fake_confidence = 1 - combined_confidence
|
| 602 |
|
| 603 |
-
# Build the detailed report
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 604 |
detailed_analysis = f"""
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
##
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
**
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 622 |
{gemini_analysis}
|
|
|
|
| 623 |
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
|
|
|
|
|
|
|
|
|
| 628 |
"""
|
| 629 |
|
| 630 |
return gr.update(value=detailed_analysis, visible=True), f"Độ chắc chắn là tin thật: {real_confidence:.1%}", f"Độ chắc chắn là tin giả: {fake_confidence:.1%}", gr.update(visible=True)
|
| 631 |
|
| 632 |
except Exception as e:
|
| 633 |
-
error_message = f"
|
| 634 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 635 |
return gr.update(value=error_message, visible=True), "Độ chắc chắn là tin thật: 0%", "Độ chắc chắn là tin giả: 0%", gr.update(visible=True)
|
| 636 |
|
| 637 |
# --- GRADIO INTERFACE ---
|
|
|
|
| 411 |
distilbert_analysis = "DistilBERT: Không thể phân tích"
|
| 412 |
|
| 413 |
prompt = f"""
|
| 414 |
+
Hãy phân tích tin tức sau và đánh giá độ tin cậy của nó một cách đơn giản, dễ hiểu:
|
| 415 |
|
| 416 |
"{news_text}"
|
| 417 |
|
|
|
|
| 419 |
|
| 420 |
{distilbert_analysis}
|
| 421 |
|
| 422 |
+
Hãy trả lời bằng tiếng Việt, ngắn gọn và dễ hiểu cho người dùng bình thường:
|
| 423 |
|
| 424 |
+
1. Tin tức này có vẻ THẬT hay GIẢ? (Chỉ trả lời THẬT hoặc GIẢ)
|
| 425 |
+
2. Tại sao bạn nghĩ vậy? (Giải thích ngắn gọn, dễ hiểu)
|
| 426 |
+
3. Người đọc nên làm gì? (Lời khuyên đơn giản)
|
|
|
|
|
|
|
| 427 |
|
| 428 |
+
Tránh dùng thuật ngữ kỹ thuật, hãy viết như đang nói chuyện với bạn bè.
|
| 429 |
"""
|
| 430 |
|
| 431 |
print("Calling Gemini API...")
|
|
|
|
| 453 |
if "429" in str(e) or "quota" in str(e).lower():
|
| 454 |
print("Gemini API quota exceeded, providing fallback analysis...")
|
| 455 |
fallback_analysis = f"""
|
| 456 |
+
**Phân tích cơ bản (do giới hạn API):**
|
| 457 |
+
|
| 458 |
+
🤖 **Kết quả AI:** {'Tin tức này có vẻ THẬT' if distilbert_prediction == 'REAL' else 'Tin tức này có vẻ GIẢ' if distilbert_prediction == 'FAKE' else 'Không thể xác định'}
|
| 459 |
+
|
| 460 |
+
📊 **Độ tin cậy:** {f"{distilbert_confidence:.0%}" if distilbert_confidence else 'Không có'}
|
| 461 |
+
|
| 462 |
+
🌐 **Nguồn tin:** {len(search_results) if search_results else 0} nguồn được tìm thấy
|
| 463 |
+
|
| 464 |
+
💡 **Khuyến nghị:** Hãy kiểm tra thêm từ các nguồn tin chính thống trước khi tin tưởng hoàn toàn.
|
| 465 |
"""
|
| 466 |
return fallback_analysis
|
| 467 |
|
|
|
|
| 544 |
"""Main analysis function combining all three tools"""
|
| 545 |
try:
|
| 546 |
if not news_text.strip():
|
| 547 |
+
empty_message = """
|
| 548 |
+
<div style="font-family: 'Segoe UI', Arial, sans-serif; line-height: 1.6; color: #333;">
|
| 549 |
+
|
| 550 |
+
## 📝 **HƯỚNG DẪN SỬ DỤNG**
|
| 551 |
+
|
| 552 |
+
<div style="background: linear-gradient(135deg, #74b9ff 0%, #0984e3 100%); color: white; padding: 20px; border-radius: 10px; margin: 20px 0; text-align: center;">
|
| 553 |
+
<h2 style="margin: 0; font-size: 24px;">💡 Vui lòng nhập tin tức</h2>
|
| 554 |
+
<p style="margin: 10px 0 0 0; font-size: 16px; opacity: 0.9;">Để bắt đầu phân tích</p>
|
| 555 |
+
</div>
|
| 556 |
+
|
| 557 |
+
<div style="background: #f8f9fa; padding: 15px; border-radius: 8px; border-left: 4px solid #17a2b8; margin: 10px 0;">
|
| 558 |
+
<p><strong>Hướng dẫn:</strong></p>
|
| 559 |
+
<ul>
|
| 560 |
+
<li>Nhập tin tức tiếng Việt cần kiểm tra vào ô trên</li>
|
| 561 |
+
<li>Nhấn nút "Phân tích với AI nâng cao"</li>
|
| 562 |
+
<li>Chờ hệ thống phân tích (có thể mất 10-30 giây)</li>
|
| 563 |
+
<li>Xem kết quả phân tích chi tiết</li>
|
| 564 |
+
</ul>
|
| 565 |
+
</div>
|
| 566 |
+
|
| 567 |
+
</div>
|
| 568 |
+
"""
|
| 569 |
+
return gr.update(value=empty_message, visible=True), "Độ chắc chắn là tin thật: 0%", "Độ chắc chắn là tin giả: 0%", gr.update(visible=False)
|
| 570 |
|
| 571 |
print(f"Analyzing: {news_text[:50]}...")
|
| 572 |
|
|
|
|
| 624 |
real_confidence = combined_confidence
|
| 625 |
fake_confidence = 1 - combined_confidence
|
| 626 |
|
| 627 |
+
# Build the detailed report with better formatting
|
| 628 |
+
prediction_emoji = "✅" if distilbert_prediction == "REAL" else "❌" if distilbert_prediction == "FAKE" else "❓"
|
| 629 |
+
confidence_level = "Cao" if combined_confidence > 0.7 else "Trung bình" if combined_confidence > 0.4 else "Thấp"
|
| 630 |
+
confidence_emoji = "🟢" if combined_confidence > 0.7 else "🟡" if combined_confidence > 0.4 else "🔴"
|
| 631 |
+
|
| 632 |
+
# Convert technical metrics to user-friendly Vietnamese
|
| 633 |
+
source_quality = "Tốt" if source_credibility > 0.7 else "Trung bình" if source_credibility > 0.4 else "Kém"
|
| 634 |
+
source_count_text = f"{len(search_results)} nguồn tin" if len(search_results) > 0 else "Không tìm thấy nguồn"
|
| 635 |
+
|
| 636 |
+
# Simplify credibility text
|
| 637 |
+
if "High credibility" in credibility_text:
|
| 638 |
+
credibility_summary = f"✅ Nguồn tin đáng tin cậy"
|
| 639 |
+
elif "Medium credibility" in credibility_text:
|
| 640 |
+
credibility_summary = f"⚠️ Nguồn tin trung bình"
|
| 641 |
+
else:
|
| 642 |
+
credibility_summary = f"❌ Nguồn tin kém tin cậy"
|
| 643 |
+
|
| 644 |
+
# Simplify support text
|
| 645 |
+
if "strongly support" in support_text.lower():
|
| 646 |
+
support_summary = "✅ Các nguồn ủng hộ tin tức này"
|
| 647 |
+
elif "contradict" in support_text.lower():
|
| 648 |
+
support_summary = "❌ Các nguồn phản bác tin tức này"
|
| 649 |
+
else:
|
| 650 |
+
support_summary = "⚠️ Các nguồn có ý kiến trái chiều"
|
| 651 |
+
|
| 652 |
detailed_analysis = f"""
|
| 653 |
+
<div style="font-family: 'Segoe UI', Arial, sans-serif; line-height: 1.6; color: #333;">
|
| 654 |
+
|
| 655 |
+
## 🔍 **KẾT QUẢ PHÂN TÍCH TIN TỨC**
|
| 656 |
+
|
| 657 |
+
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 20px; border-radius: 10px; margin: 20px 0; text-align: center;">
|
| 658 |
+
<h2 style="margin: 0; font-size: 24px;">{prediction_emoji} {'TIN THẬT' if distilbert_prediction == 'REAL' else 'TIN GIẢ' if distilbert_prediction == 'FAKE' else 'KHÔNG XÁC ĐỊNH'}</h2>
|
| 659 |
+
<p style="margin: 10px 0 0 0; font-size: 18px; opacity: 0.9;">{confidence_emoji} Độ tin cậy: {confidence_level} ({combined_confidence:.0%})</p>
|
| 660 |
+
</div>
|
| 661 |
+
|
| 662 |
+
### 🤖 **Phân tích bằng AI**
|
| 663 |
+
<div style="background: #f8f9fa; padding: 15px; border-radius: 8px; border-left: 4px solid #007bff; margin: 10px 0;">
|
| 664 |
+
<p><strong>Kết quả:</strong> {prediction_emoji} {'Tin tức này có vẻ THẬT' if distilbert_prediction == 'REAL' else 'Tin tức này có vẻ GIẢ' if distilbert_prediction == 'FAKE' else 'Không thể xác định'}</p>
|
| 665 |
+
<p><strong>Độ chắc chắn:</strong> {f"{distilbert_confidence:.0%}" if distilbert_confidence else 'Không có'} - {'Rất cao' if distilbert_confidence and distilbert_confidence > 0.8 else 'Cao' if distilbert_confidence and distilbert_confidence > 0.6 else 'Trung bình' if distilbert_confidence and distilbert_confidence > 0.4 else 'Thấp'}</p>
|
| 666 |
+
</div>
|
| 667 |
+
|
| 668 |
+
### 🌐 **Kiểm tra nguồn tin**
|
| 669 |
+
<div style="background: #f8f9fa; padding: 15px; border-radius: 8px; border-left: 4px solid #28a745; margin: 10px 0;">
|
| 670 |
+
<p><strong>Tìm thấy:</strong> {source_count_text}</p>
|
| 671 |
+
<p><strong>Chất lượng nguồn:</strong> {source_quality} ({source_credibility:.0%})</p>
|
| 672 |
+
<p><strong>Đánh giá:</strong> {credibility_summary}</p>
|
| 673 |
+
<p><strong>Hỗ trợ:</strong> {support_summary}</p>
|
| 674 |
+
</div>
|
| 675 |
+
|
| 676 |
+
### 🧠 **Phân tích thông minh**
|
| 677 |
+
<div style="background: #f8f9fa; padding: 15px; border-radius: 8px; border-left: 4px solid #ffc107; margin: 10px 0;">
|
| 678 |
{gemini_analysis}
|
| 679 |
+
</div>
|
| 680 |
|
| 681 |
+
### 📊 **KẾT LUẬN CUỐI CÙNG**
|
| 682 |
+
<div style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); color: white; padding: 15px; border-radius: 8px; margin: 20px 0;">
|
| 683 |
+
<p style="margin: 0; font-size: 16px;"><strong>Tin tức này có khả năng {'THẬT' if real_confidence > fake_confidence else 'GIẢ'} với độ tin cậy {max(real_confidence, fake_confidence):.0%}</strong></p>
|
| 684 |
+
<p style="margin: 5px 0 0 0; font-size: 14px; opacity: 0.9;">Dựa trên phân tích AI, kiểm tra nguồn tin và đánh giá thông minh</p>
|
| 685 |
+
</div>
|
| 686 |
+
|
| 687 |
+
</div>
|
| 688 |
"""
|
| 689 |
|
| 690 |
return gr.update(value=detailed_analysis, visible=True), f"Độ chắc chắn là tin thật: {real_confidence:.1%}", f"Độ chắc chắn là tin giả: {fake_confidence:.1%}", gr.update(visible=True)
|
| 691 |
|
| 692 |
except Exception as e:
|
| 693 |
+
error_message = f"""
|
| 694 |
+
<div style="font-family: 'Segoe UI', Arial, sans-serif; line-height: 1.6; color: #333;">
|
| 695 |
+
|
| 696 |
+
## ❌ **LỖI PHÂN TÍCH**
|
| 697 |
+
|
| 698 |
+
<div style="background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%); color: white; padding: 20px; border-radius: 10px; margin: 20px 0; text-align: center;">
|
| 699 |
+
<h2 style="margin: 0; font-size: 24px;">⚠️ Có lỗi xảy ra</h2>
|
| 700 |
+
<p style="margin: 10px 0 0 0; font-size: 16px; opacity: 0.9;">Vui lòng thử lại sau</p>
|
| 701 |
+
</div>
|
| 702 |
+
|
| 703 |
+
<div style="background: #f8f9fa; padding: 15px; border-radius: 8px; border-left: 4px solid #dc3545; margin: 10px 0;">
|
| 704 |
+
<p><strong>Chi tiết lỗi:</strong> {str(e)}</p>
|
| 705 |
+
<p><strong>Gợi ý:</strong> Kiểm tra kết nối internet và thử lại</p>
|
| 706 |
+
</div>
|
| 707 |
+
|
| 708 |
+
</div>
|
| 709 |
+
"""
|
| 710 |
+
print(f"Analysis error: {e}")
|
| 711 |
return gr.update(value=error_message, visible=True), "Độ chắc chắn là tin thật: 0%", "Độ chắc chắn là tin giả: 0%", gr.update(visible=True)
|
| 712 |
|
| 713 |
# --- GRADIO INTERFACE ---
|