Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -301,17 +301,23 @@ def google_search(news_text):
|
|
| 301 |
def analyze_sources(search_results):
|
| 302 |
"""Check how trustworthy the news sources are"""
|
| 303 |
if not search_results:
|
| 304 |
-
return 0.50, 0.20, "No sources found"
|
| 305 |
|
| 306 |
|
| 307 |
credible_count = 0
|
| 308 |
total_sources = len(search_results)
|
|
|
|
|
|
|
| 309 |
|
| 310 |
for result in search_results:
|
| 311 |
domain = result['link'].split('/')[2] if '//' in result['link'] else ''
|
|
|
|
|
|
|
|
|
|
| 312 |
for source, credibility in CREDIBLE_SOURCES.items():
|
| 313 |
if source in domain:
|
| 314 |
credible_count += 1
|
|
|
|
| 315 |
break
|
| 316 |
|
| 317 |
source_credibility = credible_count / total_sources if total_sources > 0 else 0.50
|
|
@@ -327,7 +333,7 @@ def analyze_sources(search_results):
|
|
| 327 |
else:
|
| 328 |
credibility_text = f"Low credibility: {credible_count}/{total_sources} sources from reputable outlets"
|
| 329 |
|
| 330 |
-
return source_credibility, popularity_score, credibility_text
|
| 331 |
|
| 332 |
def analyze_source_support(news_text, search_results):
|
| 333 |
"""Check if the search results agree or disagree with the news"""
|
|
@@ -589,11 +595,12 @@ def analyze_news(news_text):
|
|
| 589 |
# Step 3: Check the sources we found
|
| 590 |
print("3. Analyzing sources and popularity...")
|
| 591 |
try:
|
| 592 |
-
source_credibility, popularity_score, credibility_text = analyze_sources(search_results)
|
| 593 |
source_support, support_text = analyze_source_support(news_text, search_results)
|
| 594 |
except Exception as e:
|
| 595 |
print(f"Source analysis error: {e}")
|
| 596 |
source_credibility, popularity_score, credibility_text = 0.5, 0.2, "Lỗi phân tích nguồn"
|
|
|
|
| 597 |
source_support, support_text = 0.5, "Lỗi phân tích hỗ trợ nguồn"
|
| 598 |
|
| 599 |
# Step 4: Get Gemini AI analysis
|
|
@@ -633,6 +640,18 @@ def analyze_news(news_text):
|
|
| 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"
|
|
@@ -671,6 +690,8 @@ def analyze_news(news_text):
|
|
| 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**
|
|
|
|
| 301 |
def analyze_sources(search_results):
|
| 302 |
"""Check how trustworthy the news sources are"""
|
| 303 |
if not search_results:
|
| 304 |
+
return 0.50, 0.20, "No sources found", []
|
| 305 |
|
| 306 |
|
| 307 |
credible_count = 0
|
| 308 |
total_sources = len(search_results)
|
| 309 |
+
found_sources = []
|
| 310 |
+
credible_sources_found = []
|
| 311 |
|
| 312 |
for result in search_results:
|
| 313 |
domain = result['link'].split('/')[2] if '//' in result['link'] else ''
|
| 314 |
+
found_sources.append(domain)
|
| 315 |
+
|
| 316 |
+
# Check if this domain matches any credible source
|
| 317 |
for source, credibility in CREDIBLE_SOURCES.items():
|
| 318 |
if source in domain:
|
| 319 |
credible_count += 1
|
| 320 |
+
credible_sources_found.append(f"{source} ({credibility:.0%})")
|
| 321 |
break
|
| 322 |
|
| 323 |
source_credibility = credible_count / total_sources if total_sources > 0 else 0.50
|
|
|
|
| 333 |
else:
|
| 334 |
credibility_text = f"Low credibility: {credible_count}/{total_sources} sources from reputable outlets"
|
| 335 |
|
| 336 |
+
return source_credibility, popularity_score, credibility_text, found_sources, credible_sources_found
|
| 337 |
|
| 338 |
def analyze_source_support(news_text, search_results):
|
| 339 |
"""Check if the search results agree or disagree with the news"""
|
|
|
|
| 595 |
# Step 3: Check the sources we found
|
| 596 |
print("3. Analyzing sources and popularity...")
|
| 597 |
try:
|
| 598 |
+
source_credibility, popularity_score, credibility_text, found_sources, credible_sources_found = analyze_sources(search_results)
|
| 599 |
source_support, support_text = analyze_source_support(news_text, search_results)
|
| 600 |
except Exception as e:
|
| 601 |
print(f"Source analysis error: {e}")
|
| 602 |
source_credibility, popularity_score, credibility_text = 0.5, 0.2, "Lỗi phân tích nguồn"
|
| 603 |
+
found_sources, credible_sources_found = [], []
|
| 604 |
source_support, support_text = 0.5, "Lỗi phân tích hỗ trợ nguồn"
|
| 605 |
|
| 606 |
# Step 4: Get Gemini AI analysis
|
|
|
|
| 640 |
source_quality = "Tốt" if source_credibility > 0.7 else "Trung bình" if source_credibility > 0.4 else "Kém"
|
| 641 |
source_count_text = f"{len(search_results)} nguồn tin" if len(search_results) > 0 else "Không tìm thấy nguồn"
|
| 642 |
|
| 643 |
+
# Create source list display
|
| 644 |
+
sources_display = ""
|
| 645 |
+
if found_sources:
|
| 646 |
+
sources_display = "<br>".join([f"• {source}" for source in found_sources[:5]]) # Show max 5 sources
|
| 647 |
+
if len(found_sources) > 5:
|
| 648 |
+
sources_display += f"<br>• ... và {len(found_sources) - 5} nguồn khác"
|
| 649 |
+
|
| 650 |
+
# Show credible sources found
|
| 651 |
+
credible_display = ""
|
| 652 |
+
if credible_sources_found:
|
| 653 |
+
credible_display = f"<br><strong>Nguồn uy tín:</strong><br>" + "<br>".join([f"✅ {source}" for source in credible_sources_found])
|
| 654 |
+
|
| 655 |
# Simplify credibility text
|
| 656 |
if "High credibility" in credibility_text:
|
| 657 |
credibility_summary = f"✅ Nguồn tin đáng tin cậy"
|
|
|
|
| 690 |
<p><strong>Chất lượng nguồn:</strong> {source_quality} ({source_credibility:.0%})</p>
|
| 691 |
<p><strong>Đánh giá:</strong> {credibility_summary}</p>
|
| 692 |
<p><strong>Hỗ trợ:</strong> {support_summary}</p>
|
| 693 |
+
{sources_display and f'<p><strong>Nguồn tìm thấy:</strong><br>{sources_display}</p>' or ''}
|
| 694 |
+
{credible_display}
|
| 695 |
</div>
|
| 696 |
|
| 697 |
### 🧠 **Phân tích thông minh**
|