Spaces:
Running
Running
File size: 5,754 Bytes
7621729 fbabb43 d4ada86 fbabb43 0073e6e fbabb43 0073e6e 4f3aa82 dcea9bb 8859b60 7621729 dcea9bb 010c9cb 7621729 0073e6e 4f3aa82 0073e6e 4f3aa82 8859b60 4f3aa82 0073e6e 4f3aa82 0073e6e 8859b60 4f3aa82 0073e6e 8859b60 4f3aa82 7621729 4f3aa82 8859b60 4f3aa82 7621729 0073e6e 8859b60 4f3aa82 7621729 010c9cb 0073e6e 8859b60 010c9cb 7621729 dcea9bb 010c9cb 7621729 4f3aa82 010c9cb 8859b60 010c9cb 7621729 0073e6e 010c9cb 0073e6e 010c9cb 4f3aa82 0073e6e 010c9cb 0073e6e 010c9cb dcea9bb 7621729 4f3aa82 0073e6e 010c9cb 0073e6e 010c9cb 8859b60 010c9cb 0073e6e 8859b60 010c9cb 0073e6e 010c9cb 0073e6e 010c9cb 0073e6e 010c9cb 0073e6e 010c9cb dcea9bb 7621729 0073e6e fbabb43 8859b60 0073e6e 7621729 d4ada86 8859b60 fbabb43 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
import os
import logging
import threading
import numpy as np
import torch
import librosa
import soundfile as sf
from pydub import AudioSegment
from telegram import Update
from telegram.ext import ApplicationBuilder, MessageHandler, filters, CommandHandler
from transformers import pipeline, AutoTokenizer, VitsModel
from huggingface_hub import login
import asyncio
from collections import defaultdict
# ===== تهيئة التوكن =====
login(token=os.getenv("HF_TOKEN"))
# ===== إعدادات النظام =====
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
logger = logging.getLogger(__name__)
# ===== تحميل النماذج مع صوت أنثوي محسّن =====
try:
# 1. نموذج التعرف على الكلام
asr_pipeline = pipeline(
"automatic-speech-recognition",
model="jonatasgrosman/wav2vec2-large-xlsr-53-arabic",
token=os.getenv("HF_TOKEN")
)
# 2. نموذج توليف الصوت الأنثوي (Mishkal TTS)
tts_tokenizer = AutoTokenizer.from_pretrained(
"miscellaneous-stuff/mishkal-tts",
token=os.getenv("HF_TOKEN")
)
tts_model = VitsModel.from_pretrained(
"miscellaneous-stuff/mishkal-tts",
token=os.getenv("HF_TOKEN")
)
except Exception as e:
logger.error(f"فشل تحميل النماذج: {str(e)}")
raise
# ===== ذاكرة المحادثة =====
conversation_history = defaultdict(list)
# ===== دوال معالجة الصوت =====
def enhance_audio(input_path: str, output_path: str) -> bool:
try:
audio = AudioSegment.from_wav(input_path)
audio = audio.low_pass_filter(3000) # تقليل الضوضاء
audio = audio.high_pass_filter(100) # إزالة الترددات المنخفضة
audio = audio.normalize() # توحيد مستوى الصوت
audio = audio.fade_in(150).fade_out(150)
audio.export(output_path, format="wav")
return True
except Exception as e:
logger.error(f"خطأ في تحسين الصوت: {str(e)}")
return False
async def speech_to_text(audio_path: str) -> str:
try:
audio, sr = librosa.load(audio_path, sr=16000)
sf.write("temp.wav", audio, sr)
result = asr_pipeline("temp.wav")
return result["text"]
except Exception as e:
logger.error(f"فشل التعرف على الصوت: {str(e)}")
return ""
async def generate_response(text: str, user_id: str) -> str:
"""توليد رد قصير مترابط"""
try:
# تحديث ذاكرة المحادثة
conversation_history[user_id].append(text)
context = "\n".join(conversation_history[user_id][-3:]) # أخر 3 رسائل
chatbot = pipeline(
"text-generation",
model="aubmindlab/aragpt2-base",
token=os.getenv("HF_TOKEN"),
max_length=50, # تقليل طول الرد
temperature=0.7, # زيادة التركيز
)
response = chatbot(
context,
num_return_sequences=1,
pad_token_id=50256
)
return response[0]['generated_text']
except Exception as e:
logger.error(f"فشل توليد الرد: {str(e)}")
return "حدث خطأ في توليد الرد."
async def text_to_speech(text: str) -> None:
"""تحويل النص إلى صوت أنثوي"""
try:
inputs = tts_tokenizer(text, return_tensors="pt")
with torch.no_grad():
output = tts_model(**inputs, speaker_id=2) # اختيار الصوت الأنثوي
waveform = output.waveform[0].numpy()
sf.write("bot_response.wav", waveform, tts_model.config.sampling_rate)
except Exception as e:
logger.error(f"فشل تحويل النص إلى صوت: {str(e)}")
# ===== دوال التفاعل مع المستخدم =====
async def start(update: Update, context):
"""رسالة الترحيب"""
await update.message.reply_text("مرحبًا! أنا بوت الدردشة الصوتية الأنثوي. أرسل لي رسالة صوتية وسأرد عليك بصوت أنثوي عالي الجودة 🎤")
async def process_voice(update: Update, context):
try:
user_id = update.message.from_user.id
voice_file = await update.message.voice.get_file()
await voice_file.download_to_drive("user_voice.ogg")
# معالجة الصوت
user_text = await speech_to_text("user_voice.ogg")
bot_response = await generate_response(user_text, str(user_id))
await text_to_speech(bot_response)
# إرسال الرد
if enhance_audio("bot_response.wav", "bot_response_enhanced.wav"):
await update.message.reply_voice("bot_response_enhanced.wav")
else:
await update.message.reply_voice("bot_response.wav")
except Exception as e:
logger.error(f"خطأ غير متوقع: {str(e)}")
await update.message.reply_text("⚠️ عذرًا، حدث خطأ في المعالجة.")
# ===== التشغيل الرئيسي =====
def run_bot():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
application = ApplicationBuilder().token(os.getenv("TELEGRAM_TOKEN")).build()
application.add_handler(CommandHandler("start", start))
application.add_handler(MessageHandler(filters.VOICE, process_voice))
application.run_polling(
close_loop=False,
stop_signals=[]
)
if __name__ == "__main__":
bot_thread = threading.Thread(target=run_bot, daemon=True)
bot_thread.start()
bot_thread.join() |