Opera8 commited on
Commit
6b12d07
·
verified ·
1 Parent(s): d68e93e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +20 -49
main.py CHANGED
@@ -1995,7 +1995,7 @@ async def process_podcast(client, chat_id, prompt):
1995
  os.remove(file_name_mp3)
1996
 
1997
  # ==============================================================================
1998
- # 🟢 پارت 18: استخراج متن از صدا (STT) و تحلیل تصویر (متصل به هوش مصنوعی Gemma و Aya)
1999
  # ==============================================================================
2000
  async def process_stt(client, chat_id, audio_bytes, file_name):
2001
  str_chat_id = str(chat_id).replace("`", "").replace("'", "").replace('"', "").strip()
@@ -2046,44 +2046,6 @@ async def process_stt(client, chat_id, audio_bytes, file_name):
2046
  else:
2047
  await send_with_keyboard(client, chat_id, "❌ سرور شلوغ است فعلا بعدا امتحان کنید.", True)
2048
 
2049
- async def process_image_analysis(client, chat_id, file_bytes, file_name, prompt):
2050
- str_chat_id = str(chat_id).replace("`", "").replace("'", "").replace('"', "").strip()
2051
- creds = get_user_credits(str_chat_id)
2052
- # از همان سهمیه قبلی فایل برای تحلیل تصویر استفاده میکنیم تا در دیتابیس ارور نگیرید
2053
- if creds["file"] <= 0:
2054
- return await send_with_keyboard(client, chat_id, "❌ اعتبار تحلیل تصویر شما تمام شده است. لطفاً از منوی اصلی وارد بخش «خرید اشتراک 💎» شوید.", False)
2055
-
2056
- proc_msg = await send_with_keyboard(client, chat_id, "👁️ در حال درک و تحلیل دقیق تصویر توسط هوش مصنوعی...", False)
2057
-
2058
- mime_type, _ = mimetypes.guess_type(file_name)
2059
-
2060
- if not mime_type:
2061
- if file_name.endswith(('.jpg', '.jpeg')): mime_type = "image/jpeg"
2062
- elif file_name.endswith('.png'): mime_type = "image/png"
2063
- else: mime_type = "image/jpeg"
2064
-
2065
- gemma_prompt = prompt if prompt else "این تصویر را با جزئیات کامل تحلیل کن"
2066
-
2067
- # استفاده از سیستم هوشمند (تلاش ۵ باره در اکشن + انتقال نامرئی به مدل Aya)
2068
- final_answer = await smart_vision_analysis(gemma_prompt, file_bytes, file_name, mime_type)
2069
-
2070
- try:
2071
- if proc_msg:
2072
- msg_id = getattr(proc_msg, 'message_id', None)
2073
- if isinstance(proc_msg, dict): msg_id = proc_msg.get('message_update', {}).get('message_id') or proc_msg.get('message_id')
2074
- if msg_id: await client.delete_messages(chat_id,[msg_id])
2075
- except Exception: pass
2076
-
2077
- if final_answer:
2078
- # 🧹 اعمال فیلتر پاکسازی فرمول‌های ریاضی و لاتک
2079
- final_answer = clean_math_formatting(final_answer)
2080
- sent = await send_with_keyboard(client, chat_id, f"💡 **نتیجه تحلیل تصویر:**\n\n{final_answer}", True)
2081
- if sent and not creds.get("is_premium"):
2082
- user_credits_db[str_chat_id]["file"] -= 1
2083
- save_db(user_credits_db)
2084
- else:
2085
- await send_with_keyboard(client, chat_id, "❌ سرور تحلیل تصویر شلوغ است یا خطایی رخ داد. لطفاً کمی بعد امتحان کنید.", True)
2086
-
2087
  # ==============================================================================
2088
  # 🟢 پارت 19: ساخت مقاله و خروجی فایل متنی (Word/PDF)
2089
  # ==============================================================================
@@ -2702,7 +2664,7 @@ else:
2702
  if is_user_spamming(str_chat_id): return
2703
 
2704
  if str_chat_id not in user_states:
2705
- user_states[str_chat_id] = {"mode": None, "text": "", "history":[], "file_bytes": None, "file_name": None, "last_time": time.time(), "video_type": None, "selected_model": None, "vid_width": 1024, "vid_height": 1024}
2706
  else:
2707
  user_states[str_chat_id]["last_time"] = time.time()
2708
 
@@ -3091,10 +3053,18 @@ else:
3091
 
3092
  if user_text_str in["/chat", "💬 چت", "چت با هوش مصنوعی 🤖"]:
3093
  user_states[str_chat_id]["mode"] = "chat"
3094
- user_states[str_chat_id]["history"] =[]
 
3095
  await send_with_keyboard(client, chat_id, "💬 شما وارد بخش **چت با هوش مصنوعی** شدید.\n\nهر سوالی دارید بفرستید تا جواب بدم:\n(برای خروج دکمه «برگشت♻️» را بزنید)", True)
3096
  return
3097
 
 
 
 
 
 
 
 
3098
  if user_text_str in["/image", "🎨 عکس", "ساخت تصاویر🎨"]:
3099
  user_states[str_chat_id]["mode"] = "image_waiting_for_text"
3100
  await send_with_keyboard(client, chat_id, "🎨 شما وارد بخش **ساخت عکس پیشرفته** شدید.\n\nمتن (ایده) خود را به صورت کامل ارسال کنید:\n(برای خروج دکمه «برگشت♻️» را بزنید)", True)
@@ -3174,12 +3144,6 @@ else:
3174
  await send_with_keyboard(client, chat_id, clone_text, True)
3175
  return
3176
 
3177
- if user_text_str in["/image_analysis", "تحلیل تصویر 🔍"]:
3178
- user_states[str_chat_id]["mode"] = "image_waiting_for_file"
3179
- user_states[str_chat_id]["file_bytes"] = None
3180
- await send_with_keyboard(client, chat_id, "🖼️ شما وارد بخش **تحلیل تصویر اختصاصی** شدید.\n\nلطفاً تصویر خود را ارسال کنید:\n(برای خروج دکمه «برگشت♻️» را بزنید)", True)
3181
- return
3182
-
3183
  if user_text_str in["/stt", "فایل صوتی به متن 📝"]:
3184
  user_states[str_chat_id]["mode"] = "stt_waiting_for_audio"
3185
  await send_with_keyboard(client, chat_id, "📝 شما وارد بخش **تبدیل صدا به متن** شدید.\n\nلطفاً فایل خود (ویس، آهنگ، ویدیو و...) را ارسال کنید:\n(برای خروج دکمه «برگشت♻️» را بزنید)", True)
@@ -3356,13 +3320,20 @@ else:
3356
 
3357
  elif current_mode == "chat":
3358
  if is_file:
3359
- await send_with_keyboard(client, chat_id, "📥 در حال دانلود فایل...", False)
 
3360
  try:
3361
  file_bytes = await helper_download_file(client, msg_obj)
3362
  user_states[str_chat_id]["file_bytes"] = file_bytes
3363
  user_states[str_chat_id]["file_name"] = file_name
3364
  user_states[str_chat_id]["mode"] = "chat_waiting_for_prompt"
3365
- await send_with_keyboard(client, chat_id, "✅ فایل با موفقیت دریافت شد.\n\nحالا لطفاً متنی بفرستید و بگویید **چه کاری با این فایل انجام دهم؟**\n(مثلاً: این تصویر را توصیف کن، یا متن این سند را خلاصه کن)", False)
 
 
 
 
 
 
3366
  except Exception as dl_err:
3367
  await send_with_keyboard(client, chat_id, f"❌ خطا در دریافت فایل!\n{str(dl_err)}", False)
3368
  elif user_text_str:
 
1995
  os.remove(file_name_mp3)
1996
 
1997
  # ==============================================================================
1998
+ # 🟢 پارت 18: استخراج متن از صدا (STT)
1999
  # ==============================================================================
2000
  async def process_stt(client, chat_id, audio_bytes, file_name):
2001
  str_chat_id = str(chat_id).replace("`", "").replace("'", "").replace('"', "").strip()
 
2046
  else:
2047
  await send_with_keyboard(client, chat_id, "❌ سرور شلوغ است فعلا بعدا امتحان کنید.", True)
2048
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2049
  # ==============================================================================
2050
  # 🟢 پارت 19: ساخت مقاله و خروجی فایل متنی (Word/PDF)
2051
  # ==============================================================================
 
2664
  if is_user_spamming(str_chat_id): return
2665
 
2666
  if str_chat_id not in user_states:
2667
+ user_states[str_chat_id] = {"mode": None, "text": "", "history":[], "file_bytes": None, "file_name": None, "last_time": time.time(), "video_type": None, "selected_model": None, "vid_width": 1024, "vid_height": 1024, "entry_point": None}
2668
  else:
2669
  user_states[str_chat_id]["last_time"] = time.time()
2670
 
 
3053
 
3054
  if user_text_str in["/chat", "💬 چت", "چت با هوش مصنوعی 🤖"]:
3055
  user_states[str_chat_id]["mode"] = "chat"
3056
+ user_states[str_chat_id]["history"] = []
3057
+ user_states[str_chat_id]["entry_point"] = "chat"
3058
  await send_with_keyboard(client, chat_id, "💬 شما وارد بخش **چت با هوش مصنوعی** شدید.\n\nهر سوالی دارید بفرستید تا جواب بدم:\n(برای خروج دکمه «برگشت♻️» را بزنید)", True)
3059
  return
3060
 
3061
+ if user_text_str in["/image_analysis", "تحلیل تصویر 🔍"]:
3062
+ user_states[str_chat_id]["mode"] = "chat"
3063
+ user_states[str_chat_id]["history"] = []
3064
+ user_states[str_chat_id]["entry_point"] = "image_analysis"
3065
+ await send_with_keyboard(client, chat_id, "🖼️ شما وارد بخش **تحلیل تصویر اختصاصی** شدید.\n\nلطفاً تصویر خود را ارسال کنید:\n(برای خروج دکمه «برگشت♻️» را بزنید)", True)
3066
+ return
3067
+
3068
  if user_text_str in["/image", "🎨 عکس", "ساخت تصاویر🎨"]:
3069
  user_states[str_chat_id]["mode"] = "image_waiting_for_text"
3070
  await send_with_keyboard(client, chat_id, "🎨 شما وارد بخش **ساخت عکس پیشرفته** شدید.\n\nمتن (ایده) خود را به صورت کامل ارسال کنید:\n(برای خروج دکمه «برگشت♻️» را بزنید)", True)
 
3144
  await send_with_keyboard(client, chat_id, clone_text, True)
3145
  return
3146
 
 
 
 
 
 
 
3147
  if user_text_str in["/stt", "فایل صوتی به متن 📝"]:
3148
  user_states[str_chat_id]["mode"] = "stt_waiting_for_audio"
3149
  await send_with_keyboard(client, chat_id, "📝 شما وارد بخش **تبدیل صدا به متن** شدید.\n\nلطفاً فایل خود (ویس، آهنگ، ویدیو و...) را ارسال کنید:\n(برای خروج دکمه «برگشت♻️» را بزنید)", True)
 
3320
 
3321
  elif current_mode == "chat":
3322
  if is_file:
3323
+ dl_msg = "📥 در حال دریافت تصویر..." if user_states[str_chat_id].get("entry_point") == "image_analysis" else "📥 در حال دانلود فایل..."
3324
+ await send_with_keyboard(client, chat_id, dl_msg, False)
3325
  try:
3326
  file_bytes = await helper_download_file(client, msg_obj)
3327
  user_states[str_chat_id]["file_bytes"] = file_bytes
3328
  user_states[str_chat_id]["file_name"] = file_name
3329
  user_states[str_chat_id]["mode"] = "chat_waiting_for_prompt"
3330
+
3331
+ if user_states[str_chat_id].get("entry_point") == "image_analysis":
3332
+ msg_text = "✅ تصویر با موفقیت دریافت شد.\n\nحالا لطفاً متنی بگویید **چگونه تحلیل شود؟**\n(می‌توانید سوال خاصی بپرسید یا فقط بخواهید تصویر را توصیف کند)"
3333
+ else:
3334
+ msg_text = "✅ فایل با موفقیت دریافت شد.\n\nحالا لطفاً متنی بفرستید و بگویید **چه کاری با این فایل انجام دهم؟**\n(مثلاً: این تصویر را توصیف کن، یا متن این سند را خلاصه کن)"
3335
+
3336
+ await send_with_keyboard(client, chat_id, msg_text, False)
3337
  except Exception as dl_err:
3338
  await send_with_keyboard(client, chat_id, f"❌ خطا در دریافت فایل!\n{str(dl_err)}", False)
3339
  elif user_text_str: