danicor commited on
Commit
1955104
·
verified ·
1 Parent(s): e2bf989

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -23
app.py CHANGED
@@ -523,18 +523,19 @@ class MultilingualTranslator:
523
  def notify_wordpress_completion_and_charge(request_id: str, character_count: int,
524
  translation_length: int, source_lang: str,
525
  target_lang: str):
526
- """ارسال مستقیم نتیجه ترجمه به وردپرس"""
527
  try:
528
- # ابتدا نتیجه ترجمه را پیدا کن
529
  if request_id not in translator.completed_translations:
530
  print(f"❌ Translation result not found for {request_id}")
531
  return False
532
 
533
  translation_data = translator.completed_translations[request_id]
534
 
535
- # استفاده از URL از تنظیمات محیطی
536
  wordpress_base_url = os.getenv("WORDPRESS_NOTIFICATION_URL", "https://echovizio.us.to")
537
 
 
538
  payload = {
539
  'request_id': request_id,
540
  'character_count': character_count,
@@ -543,44 +544,53 @@ def notify_wordpress_completion_and_charge(request_id: str, character_count: int
543
  'target_lang': target_lang,
544
  'completed_at': datetime.now().isoformat(),
545
  'status': 'completed',
546
- # ارسال مستقیم نتیجه ترجمه
547
  'translated_text': translation_data['result']['translated_text'],
548
  'processing_time': translation_data['result']['processing_time'],
549
- 'from_cache': translation_data['result'].get('from_cache', False)
 
550
  }
551
 
552
  headers = {
553
  'Content-Type': 'application/json',
554
- 'User-Agent': 'MLT-Server/1.0'
 
555
  }
556
 
557
  notification_url = wordpress_base_url.rstrip('/') + '/wp-json/mlt/v1/notification'
558
 
559
- print(f"🔔 Sending translation result to: {notification_url}")
 
560
 
561
- try:
562
- response = requests.post(
563
- notification_url,
564
- json=payload,
565
- headers=headers,
566
- timeout=30,
567
- verify=False
568
- )
569
-
570
- if response.status_code == 200:
 
571
  print(f"✅ WordPress notification successful for {request_id}")
572
  return True
573
  else:
574
- print(f"❌ WordPress notification failed: {response.status_code}")
575
- print(f"❌ Response: {response.text}")
576
  return False
577
-
578
- except Exception as e:
579
- print(f" Request error: {str(e)}")
580
  return False
581
 
 
 
 
 
 
 
582
  except Exception as e:
583
- print(f" Notification setup error: {str(e)}")
584
  return False
585
 
586
  def process_heavy_translation_background(text: str, source_lang: str, target_lang: str,
 
523
  def notify_wordpress_completion_and_charge(request_id: str, character_count: int,
524
  translation_length: int, source_lang: str,
525
  target_lang: str):
526
+ """Send direct translation result to WordPress with improved error handling"""
527
  try:
528
+ # Get translation result
529
  if request_id not in translator.completed_translations:
530
  print(f"❌ Translation result not found for {request_id}")
531
  return False
532
 
533
  translation_data = translator.completed_translations[request_id]
534
 
535
+ # Use environment variable for WordPress URL
536
  wordpress_base_url = os.getenv("WORDPRESS_NOTIFICATION_URL", "https://echovizio.us.to")
537
 
538
+ # Send complete translation data in the notification
539
  payload = {
540
  'request_id': request_id,
541
  'character_count': character_count,
 
544
  'target_lang': target_lang,
545
  'completed_at': datetime.now().isoformat(),
546
  'status': 'completed',
547
+ # Include the actual translation result
548
  'translated_text': translation_data['result']['translated_text'],
549
  'processing_time': translation_data['result']['processing_time'],
550
+ 'from_cache': translation_data['result'].get('from_cache', False),
551
+ 'chunks_count': translation_data['result'].get('chunks_count', 1)
552
  }
553
 
554
  headers = {
555
  'Content-Type': 'application/json',
556
+ 'User-Agent': 'MLT-Server/1.0',
557
+ 'Accept': 'application/json'
558
  }
559
 
560
  notification_url = wordpress_base_url.rstrip('/') + '/wp-json/mlt/v1/notification'
561
 
562
+ print(f"📡 Sending complete translation to: {notification_url}")
563
+ print(f"🎯 Payload size: {len(payload['translated_text'])} characters")
564
 
565
+ response = requests.post(
566
+ notification_url,
567
+ json=payload,
568
+ headers=headers,
569
+ timeout=45, # Increased timeout
570
+ verify=False
571
+ )
572
+
573
+ if response.status_code == 200:
574
+ response_data = response.json()
575
+ if response_data.get('success'):
576
  print(f"✅ WordPress notification successful for {request_id}")
577
  return True
578
  else:
579
+ print(f"❌ WordPress rejected notification: {response.text}")
 
580
  return False
581
+ else:
582
+ print(f"❌ WordPress notification failed with status {response.status_code}")
583
+ print(f"📄 Response: {response.text}")
584
  return False
585
 
586
+ except requests.exceptions.Timeout:
587
+ print(f"⏰ WordPress notification timeout for {request_id}")
588
+ return False
589
+ except requests.exceptions.ConnectionError:
590
+ print(f"🔌 WordPress connection error for {request_id}")
591
+ return False
592
  except Exception as e:
593
+ print(f"💥 Notification error for {request_id}: {str(e)}")
594
  return False
595
 
596
  def process_heavy_translation_background(text: str, source_lang: str, target_lang: str,