Hashim-dotcom commited on
Commit
25e4b77
·
verified ·
1 Parent(s): 324dab7

Delete bot.py

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