Hashim-dotcom commited on
Commit
a3c9bd6
·
verified ·
1 Parent(s): 58720e5

Update bot.py

Browse files
Files changed (1) hide show
  1. bot.py +10 -22
bot.py CHANGED
@@ -5,10 +5,9 @@ from telegram.ext import Application, CommandHandler, MessageHandler, filters
5
  import torch
6
  import logging
7
  from huggingface_hub import hf_hub_download
8
- from urllib3.util import Retry
9
  import asyncio
10
 
11
- # إضافة مسار المشروع إلى مسارات بايثون للوصول للمكتبات المحلية
12
  sys.path.append(os.getcwd())
13
 
14
  from infer.modules.vc.pipeline import Pipeline
@@ -23,7 +22,7 @@ config = Config()
23
  config.device = "cpu"
24
  config.is_half = False
25
 
26
- # --- تحميل النموذج الخاص بك عند بدء التشغيل ---
27
  HUB_REPO_ID = "Hashim-dotcom/Hashim.ai.rvc"
28
  MODEL_FILE = "model.pth"
29
  WEIGHTS_DIR = "assets/weights"
@@ -33,10 +32,9 @@ try:
33
  logger.info("تجهيز مجلد النماذج...")
34
  os.makedirs(WEIGHTS_DIR, exist_ok=True)
35
 
36
- logger.info(f"جاري تحميل نموذجك الخاص '{MODEL_FILE}' من Hugging Face Hub...")
37
  model_path_on_disk = hf_hub_download(repo_id=HUB_REPO_ID, filename=MODEL_FILE, cache_dir="/tmp/.cache/huggingface")
38
 
39
- # نسخ النموذج إلى المجلد الذي يتوقعه RVC
40
  final_model_path = os.path.join(WEIGHTS_DIR, MODEL_FILE)
41
  os.rename(model_path_on_disk, final_model_path)
42
  logger.info("تم تحميل النموذج بنجاح!")
@@ -59,14 +57,12 @@ async def handle_audio(update, context):
59
 
60
  await update.message.reply_text("تم استلام صوتك، جاري المعالجة...")
61
 
62
- # ... (بقية كود معالجة الصوت يبقى كما هو)
63
  try:
64
  if update.message.voice:
65
  file_id = update.message.voice.file_id
66
  elif update.message.audio:
67
  file_id = update.message.audio.file_id
68
- else:
69
- return
70
 
71
  audio_file = await context.bot.get_file(file_id)
72
  input_path = f"{file_id}_input"
@@ -87,11 +83,9 @@ async def handle_audio(update, context):
87
  logger.error(f"حدث خطأ أثناء المعالجة: {e}")
88
  await update.message.reply_text(f"عذراً، حدث خطأ: {str(e)}")
89
  finally:
90
- if 'input_path' in locals() and os.path.exists(input_path):
91
- os.remove(input_path)
92
- if 'output_path' in locals() and os.path.exists(output_path):
93
- os.remove(output_path)
94
-
95
 
96
  async def main():
97
  TOKEN = os.getenv("TELEGRAM_TOKEN")
@@ -99,11 +93,10 @@ async def main():
99
  logger.critical("خطأ: لم يتم العثور على TELEGRAM_TOKEN.")
100
  return
101
 
102
- # === هذا هو الإصلاح الثاني لمشكلة الشبكة ===
103
  from telegram.ext import AIORateLimiter
104
  from telegram.request import HTTPXRequest
105
 
106
- request = HTTPXRequest(http_version="1.1") # استخدام إصدار HTTP أقدم وأكثر توافقاً
107
 
108
  application = (
109
  Application.builder()
@@ -112,20 +105,15 @@ async def main():
112
  .rate_limiter(AIORateLimiter())
113
  .build()
114
  )
115
- # =========================================
116
 
117
  application.add_handler(CommandHandler("start", start))
118
  application.add_handler(MessageHandler(filters.VOICE | filters.AUDIO, handle_audio))
119
 
120
  logger.info("البوت قيد التشغيل...")
121
- # نشغل البوت بطريقة لا تمنع البرنامج من الإقلاع بشكل كامل
122
  await application.initialize()
123
- await application.start()
124
  await application.updater.start_polling()
125
- # حلقة لا نهائية لإبقاء البرنامج يعمل
126
- while True:
127
- await asyncio.sleep(3600)
128
-
129
 
130
  if __name__ == "__main__":
131
  asyncio.run(main())
 
5
  import torch
6
  import logging
7
  from huggingface_hub import hf_hub_download
 
8
  import asyncio
9
 
10
+ # إضافة مسار المشروع إلى مسارات بايثون
11
  sys.path.append(os.getcwd())
12
 
13
  from infer.modules.vc.pipeline import Pipeline
 
22
  config.device = "cpu"
23
  config.is_half = False
24
 
25
+ # --- تحميل النموذج ---
26
  HUB_REPO_ID = "Hashim-dotcom/Hashim.ai.rvc"
27
  MODEL_FILE = "model.pth"
28
  WEIGHTS_DIR = "assets/weights"
 
32
  logger.info("تجهيز مجلد النماذج...")
33
  os.makedirs(WEIGHTS_DIR, exist_ok=True)
34
 
35
+ logger.info(f"جاري تحميل نموذجك الخاص '{MODEL_FILE}'...")
36
  model_path_on_disk = hf_hub_download(repo_id=HUB_REPO_ID, filename=MODEL_FILE, cache_dir="/tmp/.cache/huggingface")
37
 
 
38
  final_model_path = os.path.join(WEIGHTS_DIR, MODEL_FILE)
39
  os.rename(model_path_on_disk, final_model_path)
40
  logger.info("تم تحميل النموذج بنجاح!")
 
57
 
58
  await update.message.reply_text("تم استلام صوتك، جاري المعالجة...")
59
 
 
60
  try:
61
  if update.message.voice:
62
  file_id = update.message.voice.file_id
63
  elif update.message.audio:
64
  file_id = update.message.audio.file_id
65
+ else: return
 
66
 
67
  audio_file = await context.bot.get_file(file_id)
68
  input_path = f"{file_id}_input"
 
83
  logger.error(f"حدث خطأ أثناء المعالجة: {e}")
84
  await update.message.reply_text(f"عذراً، حدث خطأ: {str(e)}")
85
  finally:
86
+ # تنظيف الملفات
87
+ if 'input_path' in locals() and os.path.exists(input_path): os.remove(input_path)
88
+ if 'output_path' in locals() and os.path.exists(output_path): os.remove(output_path)
 
 
89
 
90
  async def main():
91
  TOKEN = os.getenv("TELEGRAM_TOKEN")
 
93
  logger.critical("خطأ: لم يتم العثور على TELEGRAM_TOKEN.")
94
  return
95
 
 
96
  from telegram.ext import AIORateLimiter
97
  from telegram.request import HTTPXRequest
98
 
99
+ request = HTTPXRequest(http_version="1.1")
100
 
101
  application = (
102
  Application.builder()
 
105
  .rate_limiter(AIORateLimiter())
106
  .build()
107
  )
 
108
 
109
  application.add_handler(CommandHandler("start", start))
110
  application.add_handler(MessageHandler(filters.VOICE | filters.AUDIO, handle_audio))
111
 
112
  logger.info("البوت قيد التشغيل...")
 
113
  await application.initialize()
 
114
  await application.updater.start_polling()
115
+ await application.start()
116
+ while True: await asyncio.sleep(3600)
 
 
117
 
118
  if __name__ == "__main__":
119
  asyncio.run(main())