Spaces:
Configuration error
Configuration error
Create bot.py
Browse files
bot.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 9 |
+
# إضافة مسار المشروع إلى مسارات بايثون للوصول للمكتبات المحلية
|
| 10 |
+
sys.path.append(os.getcwd())
|
| 11 |
+
|
| 12 |
+
from infer.modules.vc.pipeline import Pipeline
|
| 13 |
+
from configs.config import Config
|
| 14 |
+
|
| 15 |
+
# إعداد السجلات
|
| 16 |
+
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
|
| 17 |
+
logger = logging.getLogger(__name__)
|
| 18 |
+
|
| 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):
|
| 48 |
+
await update.message.reply_text("مرحباً! أنا بوت تحويل الصوت. أرسل لي رسالة صوتية أو ملفاً صوتياً.")
|
| 49 |
+
|
| 50 |
+
async def handle_audio(update, context):
|
| 51 |
+
if pipeline is None:
|
| 52 |
+
await update.message.reply_text("عذراً، البوت يواجه مشكلة تقنية (فشل تحميل النموذج).")
|
| 53 |
+
return
|
| 54 |
+
|
| 55 |
+
await update.message.reply_text("تم استلام صوتك، جاري المعالجة...")
|
| 56 |
+
|
| 57 |
+
try:
|
| 58 |
+
if update.message.voice:
|
| 59 |
+
file_id = update.message.voice.file_id
|
| 60 |
+
elif update.message.audio:
|
| 61 |
+
file_id = update.message.audio.file_id
|
| 62 |
+
else:
|
| 63 |
+
return
|
| 64 |
+
|
| 65 |
+
audio_file = await context.bot.get_file(file_id)
|
| 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
|
| 77 |
+
sf.write(output_path, output_audio[1], output_audio[0], format='WAV')
|
| 78 |
+
|
| 79 |
+
await update.message.reply_audio(audio=open(output_path, 'rb'), title="صوت محول")
|
| 80 |
+
|
| 81 |
+
except Exception as e:
|
| 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()
|