Hashim-dotcom commited on
Commit
b438356
·
verified ·
1 Parent(s): df9a08e

Update bot.py

Browse files
Files changed (1) hide show
  1. bot.py +25 -5
bot.py CHANGED
@@ -1,23 +1,30 @@
1
  import os
2
  import sys
3
  import telegram
4
- from telegram.ext import Application, CommandHandler, MessageHandler, filters
 
5
  import torch
6
  import logging
7
  from huggingface_hub import hf_hub_download
8
  import asyncio
 
9
 
 
10
  sys.path.append(os.getcwd())
 
11
  from infer.modules.vc.pipeline import Pipeline
12
  from configs.config import Config
13
 
 
14
  logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
15
  logger = logging.getLogger(__name__)
16
 
 
17
  config = Config()
18
  config.device = "cpu"
19
  config.is_half = False
20
 
 
21
  HUB_REPO_ID = "Hashim-dotcom/Hashim.ai.rvc"
22
  MODEL_FILE = "model.pth"
23
  WEIGHTS_DIR = "assets/weights"
@@ -25,19 +32,21 @@ pipeline = None
25
  try:
26
  logger.info("تجهيز مجلد النماذج...")
27
  os.makedirs(WEIGHTS_DIR, exist_ok=True)
 
28
  logger.info(f"جاري تحميل نموذجك الخاص '{MODEL_FILE}'...")
29
  model_path_on_disk = hf_hub_download(repo_id=HUB_REPO_ID, filename=MODEL_FILE, cache_dir="/tmp/.cache/huggingface")
 
30
  final_model_path = os.path.join(WEIGHTS_DIR, MODEL_FILE)
31
  os.rename(model_path_on_disk, final_model_path)
32
  logger.info("تم تحميل النموذج بنجاح!")
33
 
34
- # === هذا هو السطر الذي تم إصلاحه ===
35
  pipeline = Pipeline(config)
36
- # ==================================
37
 
38
  except Exception as e:
39
  logger.error(f"حدث خطأ فادح أثناء تحميل النموذج: {e}")
40
 
 
41
  async def start(update, context):
42
  await update.message.reply_text("مرحباً! أنا بوت تحويل الصوت. أرسل لي رسالة صوتية أو ملفاً صوتياً.")
43
 
@@ -58,7 +67,6 @@ async def handle_audio(update, context):
58
 
59
  output_audio = pipeline.infer(model_name=MODEL_FILE, input_audio_path=input_path, f0_up_key=0)
60
  output_path = f"{file_id}_output.wav"
61
- import soundfile as sf
62
  sf.write(output_path, output_audio[1], output_audio[0], format='WAV')
63
 
64
  await update.message.reply_audio(audio=open(output_path, 'rb'), title="صوت محول")
@@ -74,7 +82,18 @@ async def main():
74
  if not TOKEN:
75
  logger.critical("خطأ: لم يتم العثور على TELEGRAM_TOKEN.")
76
  return
77
- application = Application.builder().token(TOKEN).build()
 
 
 
 
 
 
 
 
 
 
 
78
  application.add_handler(CommandHandler("start", start))
79
  application.add_handler(MessageHandler(filters.VOICE | filters.AUDIO, handle_audio))
80
  logger.info("البوت قيد التشغيل...")
@@ -82,5 +101,6 @@ async def main():
82
  await application.updater.start_polling()
83
  await application.start()
84
  while True: await asyncio.sleep(3600)
 
85
  if __name__ == "__main__":
86
  asyncio.run(main())
 
1
  import os
2
  import sys
3
  import telegram
4
+ from telegram.ext import Application, CommandHandler, MessageHandler, filters, AIORateLimiter
5
+ from telegram.request import HTTPXRequest
6
  import torch
7
  import logging
8
  from huggingface_hub import hf_hub_download
9
  import asyncio
10
+ import soundfile as sf
11
 
12
+ # إضافة مسار المشروع إلى مسارات بايثون للوصول للمكتبات المحلية
13
  sys.path.append(os.getcwd())
14
+
15
  from infer.modules.vc.pipeline import Pipeline
16
  from configs.config import Config
17
 
18
+ # إعداد السجلات
19
  logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
20
  logger = logging.getLogger(__name__)
21
 
22
+ # --- إعدادات أساسية ---
23
  config = Config()
24
  config.device = "cpu"
25
  config.is_half = False
26
 
27
+ # --- تحميل النموذج ---
28
  HUB_REPO_ID = "Hashim-dotcom/Hashim.ai.rvc"
29
  MODEL_FILE = "model.pth"
30
  WEIGHTS_DIR = "assets/weights"
 
32
  try:
33
  logger.info("تجهيز مجلد النماذج...")
34
  os.makedirs(WEIGHTS_DIR, exist_ok=True)
35
+
36
  logger.info(f"جاري تحميل نموذجك الخاص '{MODEL_FILE}'...")
37
  model_path_on_disk = hf_hub_download(repo_id=HUB_REPO_ID, filename=MODEL_FILE, cache_dir="/tmp/.cache/huggingface")
38
+
39
  final_model_path = os.path.join(WEIGHTS_DIR, MODEL_FILE)
40
  os.rename(model_path_on_disk, final_model_path)
41
  logger.info("تم تحميل النموذج بنجاح!")
42
 
43
+ # هذا هو السطر الذي تم إصلاحه
44
  pipeline = Pipeline(config)
 
45
 
46
  except Exception as e:
47
  logger.error(f"حدث خطأ فادح أثناء تحميل النموذج: {e}")
48
 
49
+ # --- دوال البوت ---
50
  async def start(update, context):
51
  await update.message.reply_text("مرحباً! أنا بوت تحويل الصوت. أرسل لي رسالة صوتية أو ملفاً صوتياً.")
52
 
 
67
 
68
  output_audio = pipeline.infer(model_name=MODEL_FILE, input_audio_path=input_path, f0_up_key=0)
69
  output_path = f"{file_id}_output.wav"
 
70
  sf.write(output_path, output_audio[1], output_audio[0], format='WAV')
71
 
72
  await update.message.reply_audio(audio=open(output_path, 'rb'), title="صوت محول")
 
82
  if not TOKEN:
83
  logger.critical("خطأ: لم يتم العثور على TELEGRAM_TOKEN.")
84
  return
85
+
86
+ # === هذا هو الجزء الذي تم إصلاحه ليتوافق مع Dockerfile ===
87
+ request = HTTPXRequest(http_version="1.1")
88
+ application = (
89
+ Application.builder()
90
+ .token(TOKEN)
91
+ .request(request)
92
+ .rate_limiter(AIORateLimiter())
93
+ .build()
94
+ )
95
+ # =======================================================
96
+
97
  application.add_handler(CommandHandler("start", start))
98
  application.add_handler(MessageHandler(filters.VOICE | filters.AUDIO, handle_audio))
99
  logger.info("البوت قيد التشغيل...")
 
101
  await application.updater.start_polling()
102
  await application.start()
103
  while True: await asyncio.sleep(3600)
104
+
105
  if __name__ == "__main__":
106
  asyncio.run(main())