Opera8 commited on
Commit
8660205
·
verified ·
1 Parent(s): e8e273e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +19 -5
main.py CHANGED
@@ -298,8 +298,18 @@ async def helper_download_file(client, msg_obj):
298
 
299
 
300
  # --- تنظیمات کلیدها ---
301
- GEMINI_KEYS_STR = os.environ.get("GEMINI_API_KEYS", "")
302
- GEMINI_KEYS = [k.strip() for k in GEMINI_KEYS_STR.split(",") if k.strip()]
 
 
 
 
 
 
 
 
 
 
303
 
304
  HF_TOKENS_STR = os.environ.get("HF_TOKENS", "")
305
  HF_TOKENS = [k.strip() for k in HF_TOKENS_STR.split(",") if k.strip()]
@@ -440,6 +450,7 @@ async def process_gemini(client, chat_id, prompt, file_bytes=None, file_name=Non
440
 
441
  keys_to_try = GEMINI_KEYS.copy()
442
  random.shuffle(keys_to_try)
 
443
  final_answer = None
444
 
445
  async with aiohttp.ClientSession() as session:
@@ -465,7 +476,7 @@ async def process_gemini(client, chat_id, prompt, file_bytes=None, file_name=Non
465
 
466
  if not final_answer:
467
  if history and history[-1]["role"] == "user": history.pop()
468
- await send_with_keyboard(client, chat_id, "❌ متأسفانه پاسخی دریافت نشد. اید سایز فایل بیش از حد مجاز بوده است)", False)
469
  return
470
 
471
  # کسر اعتبار در دیتاست (اگر نامحدود نیست)
@@ -513,6 +524,7 @@ async def process_image(client, chat_id, prompt):
513
  if GEMINI_KEYS:
514
  keys_to_try_gemini = GEMINI_KEYS.copy()
515
  random.shuffle(keys_to_try_gemini)
 
516
  gemini_sys_prompt = f"You are an expert AI image generation prompt engineer. Translate the following user input to English, and enhance it with high-quality, highly detailed, 4k resolution, cinematic lighting, and visually striking descriptive keywords. Return ONLY the final English prompt string.\nUser input: {prompt}"
517
  async with aiohttp.ClientSession() as session:
518
  for key in keys_to_try_gemini:
@@ -832,6 +844,7 @@ async def process_stt(client, chat_id, audio_bytes, file_name):
832
 
833
  keys_to_try = GEMINI_KEYS.copy()
834
  random.shuffle(keys_to_try)
 
835
  transcribed_text = None
836
  prompt = "لطفاً این فایل صوتی/تصویری را با دقت کامل گوش بده و صحبت‌های داخل آن را کلمه به کلمه به متن تبدیل کن. هیچ توضیح اضافه‌ای نده."
837
 
@@ -861,7 +874,7 @@ async def process_stt(client, chat_id, audio_bytes, file_name):
861
  save_db(user_credits_db)
862
  await send_with_keyboard(client, chat_id, f"📝 **متن استخراج شده:**\n\n{transcribed_text}", True)
863
  else:
864
- await send_with_keyboard(client, chat_id, f"❌ تبدیل فایل به متن ناموفق بود.", True)
865
 
866
 
867
  # --- ۵. پردازش تحلیل فایل مستقل ---
@@ -880,6 +893,7 @@ async def process_file_analysis(client, chat_id, file_bytes, file_name, prompt):
880
 
881
  keys_to_try = GEMINI_KEYS.copy()
882
  random.shuffle(keys_to_try)
 
883
  final_answer = None
884
 
885
  async with aiohttp.ClientSession() as session:
@@ -908,7 +922,7 @@ async def process_file_analysis(client, chat_id, file_bytes, file_name, prompt):
908
  save_db(user_credits_db)
909
  await send_with_keyboard(client, chat_id, f"💡 **نتیجه تحلیل:**\n\n{final_answer}", True)
910
  else:
911
- await send_with_keyboard(client, chat_id, "❌ پاسخی دریافت نشد.", True)
912
 
913
 
914
  # --- تنظیمات ربات روبیکا ---
 
298
 
299
 
300
  # --- تنظیمات کلیدها ---
301
+ GEMINI_KEYS_STR1 = os.environ.get("GEMINI_API_KEYS1", "")
302
+ GEMINI_KEYS_STR2 = os.environ.get("GEMINI_API_KEYS2", "")
303
+
304
+ _raw_keys = []
305
+ if GEMINI_KEYS_STR1:
306
+ _raw_keys.extend(GEMINI_KEYS_STR1.split(","))
307
+ if GEMINI_KEYS_STR2:
308
+ _raw_keys.extend(GEMINI_KEYS_STR2.split(","))
309
+
310
+ # حذف کلیدهای تکراری و خالی
311
+ GEMINI_KEYS = list(set([k.strip() for k in _raw_keys if k.strip()]))
312
+ print(f"✅ تعداد {len(GEMINI_KEYS)} کلید جیمینای با موفقیت شناسایی شد.")
313
 
314
  HF_TOKENS_STR = os.environ.get("HF_TOKENS", "")
315
  HF_TOKENS = [k.strip() for k in HF_TOKENS_STR.split(",") if k.strip()]
 
450
 
451
  keys_to_try = GEMINI_KEYS.copy()
452
  random.shuffle(keys_to_try)
453
+ keys_to_try = keys_to_try[:20] # انتخاب حداکثر ۲۰ کلید تصادفی برای تست در هر پاسخ
454
  final_answer = None
455
 
456
  async with aiohttp.ClientSession() as session:
 
476
 
477
  if not final_answer:
478
  if history and history[-1]["role"] == "user": history.pop()
479
+ await send_with_keyboard(client, chat_id, "❌ سرور شلوغ است فعلا بعدا امتحان کنید.", False)
480
  return
481
 
482
  # کسر اعتبار در دیتاست (اگر نامحدود نیست)
 
524
  if GEMINI_KEYS:
525
  keys_to_try_gemini = GEMINI_KEYS.copy()
526
  random.shuffle(keys_to_try_gemini)
527
+ keys_to_try_gemini = keys_to_try_gemini[:20] # انتخاب حداکثر ۲۰ کلید برای ترجمه پرامپت
528
  gemini_sys_prompt = f"You are an expert AI image generation prompt engineer. Translate the following user input to English, and enhance it with high-quality, highly detailed, 4k resolution, cinematic lighting, and visually striking descriptive keywords. Return ONLY the final English prompt string.\nUser input: {prompt}"
529
  async with aiohttp.ClientSession() as session:
530
  for key in keys_to_try_gemini:
 
844
 
845
  keys_to_try = GEMINI_KEYS.copy()
846
  random.shuffle(keys_to_try)
847
+ keys_to_try = keys_to_try[:20] # انتخاب حداکثر ۲۰ کلید تصادفی
848
  transcribed_text = None
849
  prompt = "لطفاً این فایل صوتی/تصویری را با دقت کامل گوش بده و صحبت‌های داخل آن را کلمه به کلمه به متن تبدیل کن. هیچ توضیح اضافه‌ای نده."
850
 
 
874
  save_db(user_credits_db)
875
  await send_with_keyboard(client, chat_id, f"📝 **متن استخراج شده:**\n\n{transcribed_text}", True)
876
  else:
877
+ await send_with_keyboard(client, chat_id, "❌ سرور شلوغ است فعلا بعدا امتحان کنید.", True)
878
 
879
 
880
  # --- ۵. پردازش تحلیل فایل مستقل ---
 
893
 
894
  keys_to_try = GEMINI_KEYS.copy()
895
  random.shuffle(keys_to_try)
896
+ keys_to_try = keys_to_try[:20] # انتخاب حداکثر ۲۰ کلید تصادفی
897
  final_answer = None
898
 
899
  async with aiohttp.ClientSession() as session:
 
922
  save_db(user_credits_db)
923
  await send_with_keyboard(client, chat_id, f"💡 **نتیجه تحلیل:**\n\n{final_answer}", True)
924
  else:
925
+ await send_with_keyboard(client, chat_id, "❌ سرور شلوغ است فعلا بعدا امتحان کنید.", True)
926
 
927
 
928
  # --- تنظیمات ربات روبیکا ---