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

Update bot.py

Browse files
Files changed (1) hide show
  1. bot.py +43 -17
bot.py CHANGED
@@ -5,6 +5,8 @@ from telegram.ext import Application, CommandHandler, MessageHandler, filters
5
  import torch
6
  import logging
7
  from huggingface_hub import hf_hub_download
 
 
8
 
9
  # إضافة مسار المشروع إلى مسارات بايثون للوصول للمكتبات المحلية
10
  sys.path.append(os.getcwd())
@@ -19,29 +21,32 @@ logger = logging.getLogger(__name__)
19
  # --- إعدادات أساسية ---
20
  config = Config()
21
  config.device = "cpu"
22
- config.is_half = False # الدقة الكاملة أفضل للمعالج العادي
23
 
24
  # --- تحميل النموذج الخاص بك عند بدء التشغيل ---
25
  HUB_REPO_ID = "Hashim-dotcom/Hashim.ai.rvc"
26
  MODEL_FILE = "model.pth"
27
  WEIGHTS_DIR = "assets/weights"
28
 
29
- logger.info("تجهيز مجلد النماذج...")
30
- os.makedirs(WEIGHTS_DIR, exist_ok=True)
31
-
32
- logger.info(f"جاري تحميل نموذجك الخاص '{MODEL_FILE}' من Hugging Face Hub...")
33
  try:
34
- model_path_on_disk = hf_hub_download(repo_id=HUB_REPO_ID, filename=MODEL_FILE)
 
 
 
 
 
35
  # نسخ النموذج إلى المجلد الذي يتوقعه RVC
36
- os.rename(model_path_on_disk, os.path.join(WEIGHTS_DIR, MODEL_FILE))
 
37
  logger.info("تم تحميل النموذج بنجاح!")
38
 
39
- # الآن نقوم بإنشاء وتشغيل المحرك بالنموذج الخاص بك
40
  pipeline = Pipeline(config)
 
41
 
42
  except Exception as e:
43
  logger.error(f"حدث خطأ فادح أثناء تحميل النموذج: {e}")
44
- pipeline = None
45
 
46
  # --- دوال البوت ---
47
  async def start(update, context):
@@ -54,6 +59,7 @@ async def handle_audio(update, context):
54
 
55
  await update.message.reply_text("تم استلام صوتك، جاري المعالجة...")
56
 
 
57
  try:
58
  if update.message.voice:
59
  file_id = update.message.voice.file_id
@@ -66,11 +72,10 @@ async def handle_audio(update, context):
66
  input_path = f"{file_id}_input"
67
  await audio_file.download_to_drive(input_path)
68
 
69
- # --- عملية التحويل ---
70
  output_audio = pipeline.infer(
71
  model_name=MODEL_FILE,
72
  input_audio_path=input_path,
73
- f0_up_key=0 # تغيير حدة الصوت (0 = بدون تغيير)
74
  )
75
  output_path = f"{file_id}_output.wav"
76
  import soundfile as sf
@@ -82,24 +87,45 @@ async def handle_audio(update, context):
82
  logger.error(f"حدث خطأ أثناء المعالجة: {e}")
83
  await update.message.reply_text(f"عذراً، حدث خطأ: {str(e)}")
84
  finally:
85
- # تنظيف الملفات
86
  if 'input_path' in locals() and os.path.exists(input_path):
87
  os.remove(input_path)
88
  if 'output_path' in locals() and os.path.exists(output_path):
89
  os.remove(output_path)
90
 
91
- def main():
 
92
  TOKEN = os.getenv("TELEGRAM_TOKEN")
93
  if not TOKEN:
94
- logger.critical("خطأ: لم يتم العثور على TELEGRAM_TOKEN في الأسرار.")
95
  return
96
 
97
- application = Application.builder().token(TOKEN).build()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  application.add_handler(CommandHandler("start", start))
99
  application.add_handler(MessageHandler(filters.VOICE | filters.AUDIO, handle_audio))
100
 
101
  logger.info("البوت قيد التشغيل...")
102
- application.run_polling()
 
 
 
 
 
 
 
103
 
104
  if __name__ == "__main__":
105
- main()
 
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())
 
21
  # --- إعدادات أساسية ---
22
  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"
30
 
31
+ pipeline = None
 
 
 
32
  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("تم تحميل النموذج بنجاح!")
43
 
44
+ # === هذا هو السطر الذي تم إصلاحه ===
45
  pipeline = Pipeline(config)
46
+ # ==================================
47
 
48
  except Exception as e:
49
  logger.error(f"حدث خطأ فادح أثناء تحميل النموذج: {e}")
 
50
 
51
  # --- دوال البوت ---
52
  async def start(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
 
72
  input_path = f"{file_id}_input"
73
  await audio_file.download_to_drive(input_path)
74
 
 
75
  output_audio = pipeline.infer(
76
  model_name=MODEL_FILE,
77
  input_audio_path=input_path,
78
+ f0_up_key=0
79
  )
80
  output_path = f"{file_id}_output.wav"
81
  import soundfile as sf
 
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")
98
  if not TOKEN:
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()
110
+ .token(TOKEN)
111
+ .request(request)
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())