2506minecraft commited on
Commit
0073e6e
·
verified ·
1 Parent(s): fbabb43

Update app.py

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