NLong commited on
Commit
4ec4559
·
verified ·
1 Parent(s): 686b755

Upload 3 files

Browse files
Files changed (1) hide show
  1. app.py +62 -4
app.py CHANGED
@@ -738,10 +738,32 @@ def google_search(news_text):
738
  error_str = str(e).lower()
739
  if any(keyword in error_str for keyword in ["403", "blocked", "quota", "limit", "exceeded"]):
740
  print("Google Search API blocked/quota exceeded, using fallback...")
 
 
 
 
 
 
 
 
741
  elif "invalid" in error_str or "unauthorized" in error_str:
742
  print("API key issue, using fallback...")
743
-
744
- return google_search_fallback(news_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
745
 
746
  def analyze_sources(search_results):
747
  """Check how trustworthy the news sources are"""
@@ -1253,11 +1275,28 @@ def analyze_news(news_text):
1253
 
1254
  # Step 1: Search Google for related information
1255
  print("1. Running Google Search...")
 
1256
  try:
1257
- search_results = google_search(news_text)
 
 
 
 
 
 
 
 
 
 
 
1258
  except Exception as e:
1259
  print(f"Google Search error: {e}")
1260
  search_results = []
 
 
 
 
 
1261
 
1262
  # Step 2: Run our trained model
1263
  print("2. Running DistilBERT analysis...")
@@ -1354,7 +1393,17 @@ def analyze_news(news_text):
1354
  credible_display = f"<br><strong>Nguồn uy tín:</strong><br>" + "<br>".join([f"✅ {source}" for source in credible_sources_found])
1355
 
1356
  # Simplify credibility text
1357
- if "High credibility" in credibility_text:
 
 
 
 
 
 
 
 
 
 
1358
  credibility_summary = f"✅ Nguồn tin đáng tin cậy"
1359
  elif "Medium credibility" in credibility_text:
1360
  credibility_summary = f"⚠️ Nguồn tin trung bình"
@@ -1395,6 +1444,15 @@ def analyze_news(news_text):
1395
  {credible_display}
1396
  </div>
1397
 
 
 
 
 
 
 
 
 
 
1398
  ### 🧠 **Phân tích thông minh**
1399
  <div style="background: #f8f9fa; padding: 20px; border-radius: 10px; border-left: 4px solid #ffc107; margin: 15px 0; font-family: 'Segoe UI', Arial, sans-serif; line-height: 1.6;">
1400
  <div style="white-space: pre-line; color: #333;">
 
738
  error_str = str(e).lower()
739
  if any(keyword in error_str for keyword in ["403", "blocked", "quota", "limit", "exceeded"]):
740
  print("Google Search API blocked/quota exceeded, using fallback...")
741
+ # Return error information along with fallback results
742
+ fallback_results = google_search_fallback(news_text)
743
+ return {
744
+ 'results': fallback_results,
745
+ 'error': 'QUOTA_EXCEEDED',
746
+ 'error_message': 'Google Search API quota exceeded. Using content analysis only.',
747
+ 'error_details': str(e)
748
+ }
749
  elif "invalid" in error_str or "unauthorized" in error_str:
750
  print("API key issue, using fallback...")
751
+ fallback_results = google_search_fallback(news_text)
752
+ return {
753
+ 'results': fallback_results,
754
+ 'error': 'API_KEY_INVALID',
755
+ 'error_message': 'Google Search API key invalid. Using content analysis only.',
756
+ 'error_details': str(e)
757
+ }
758
+ else:
759
+ print("Unknown Google Search error, using fallback...")
760
+ fallback_results = google_search_fallback(news_text)
761
+ return {
762
+ 'results': fallback_results,
763
+ 'error': 'UNKNOWN_ERROR',
764
+ 'error_message': 'Google Search failed. Using content analysis only.',
765
+ 'error_details': str(e)
766
+ }
767
 
768
  def analyze_sources(search_results):
769
  """Check how trustworthy the news sources are"""
 
1275
 
1276
  # Step 1: Search Google for related information
1277
  print("1. Running Google Search...")
1278
+ search_error = None
1279
  try:
1280
+ search_response = google_search(news_text)
1281
+ # Handle both old format (list) and new format (dict with error info)
1282
+ if isinstance(search_response, dict) and 'error' in search_response:
1283
+ search_results = search_response['results']
1284
+ search_error = {
1285
+ 'type': search_response['error'],
1286
+ 'message': search_response['error_message'],
1287
+ 'details': search_response['error_details']
1288
+ }
1289
+ print(f"Google Search error: {search_error['message']}")
1290
+ else:
1291
+ search_results = search_response
1292
  except Exception as e:
1293
  print(f"Google Search error: {e}")
1294
  search_results = []
1295
+ search_error = {
1296
+ 'type': 'EXCEPTION',
1297
+ 'message': 'Google Search failed with exception',
1298
+ 'details': str(e)
1299
+ }
1300
 
1301
  # Step 2: Run our trained model
1302
  print("2. Running DistilBERT analysis...")
 
1393
  credible_display = f"<br><strong>Nguồn uy tín:</strong><br>" + "<br>".join([f"✅ {source}" for source in credible_sources_found])
1394
 
1395
  # Simplify credibility text
1396
+ if search_error:
1397
+ if search_error['type'] == 'QUOTA_EXCEEDED':
1398
+ credibility_summary = f"⚠️ Google Search hết quota - chỉ dùng phân tích nội dung"
1399
+ source_count_text = "Không có (API hết quota)"
1400
+ elif search_error['type'] == 'API_KEY_INVALID':
1401
+ credibility_summary = f"❌ Google Search API key không hợp lệ"
1402
+ source_count_text = "Không có (API key lỗi)"
1403
+ else:
1404
+ credibility_summary = f"⚠️ Google Search lỗi - chỉ dùng phân tích nội dung"
1405
+ source_count_text = "Không có (lỗi API)"
1406
+ elif "High credibility" in credibility_text:
1407
  credibility_summary = f"✅ Nguồn tin đáng tin cậy"
1408
  elif "Medium credibility" in credibility_text:
1409
  credibility_summary = f"⚠️ Nguồn tin trung bình"
 
1444
  {credible_display}
1445
  </div>
1446
 
1447
+ {search_error and f'''
1448
+ ### ⚠️ **Cảnh báo Google Search**
1449
+ <div style="background: #fff3cd; padding: 15px; border-radius: 8px; border-left: 4px solid #ffc107; margin: 10px 0;">
1450
+ <p><strong>Lỗi:</strong> {search_error["message"]}</p>
1451
+ <p><strong>Chi tiết:</strong> {search_error["details"]}</p>
1452
+ <p><strong>Ảnh hưởng:</strong> Hệ thống đang sử dụng phân tích nội dung thay vì tìm kiếm Google. Kết quả có thể kém chính xác hơn.</p>
1453
+ </div>
1454
+ ''' or ''}
1455
+
1456
  ### 🧠 **Phân tích thông minh**
1457
  <div style="background: #f8f9fa; padding: 20px; border-radius: 10px; border-left: 4px solid #ffc107; margin: 15px 0; font-family: 'Segoe UI', Arial, sans-serif; line-height: 1.6;">
1458
  <div style="white-space: pre-line; color: #333;">