2506minecraft commited on
Commit
21eba45
·
verified ·
1 Parent(s): 0244b44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -10
app.py CHANGED
@@ -32,13 +32,13 @@ try:
32
  token=os.getenv("HF_TOKEN")
33
  )
34
 
35
- # 2. نموذج توليف الصوت الأنثوي (الاسم الصحيح)
36
  tts_tokenizer = AutoTokenizer.from_pretrained(
37
- "facebook/mms-tts-ara", # التصحيح النهائي
38
  token=os.getenv("HF_TOKEN")
39
  )
40
  tts_model = VitsModel.from_pretrained(
41
- "facebook/mms-tts-ara",
42
  token=os.getenv("HF_TOKEN")
43
  )
44
 
@@ -53,9 +53,9 @@ conversation_history = defaultdict(list)
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
@@ -77,14 +77,14 @@ async def generate_response(text: str, user_id: str) -> str:
77
  try:
78
  # تحديث ذاكرة المحادثة
79
  conversation_history[user_id].append(text)
80
- context = "\n".join(conversation_history[user_id][-3:])
81
 
82
  chatbot = pipeline(
83
  "text-generation",
84
  model="aubmindlab/aragpt2-base",
85
  token=os.getenv("HF_TOKEN"),
86
- max_length=50,
87
- temperature=0.7,
88
  )
89
  response = chatbot(
90
  context,
@@ -100,7 +100,7 @@ async def text_to_speech(text: str) -> None:
100
  try:
101
  inputs = tts_tokenizer(text, return_tensors="pt")
102
  with torch.no_grad():
103
- output = tts_model(**inputs, speaker_id=1) # الصوت الأنثوي
104
  waveform = output.waveform[0].numpy()
105
  sf.write("bot_response.wav", waveform, tts_model.config.sampling_rate)
106
  except Exception as e:
@@ -116,10 +116,12 @@ async def process_voice(update: Update, context):
116
  voice_file = await update.message.voice.get_file()
117
  await voice_file.download_to_drive("user_voice.ogg")
118
 
 
119
  user_text = await speech_to_text("user_voice.ogg")
120
  bot_response = await generate_response(user_text, str(user_id))
121
  await text_to_speech(bot_response)
122
 
 
123
  if enhance_audio("bot_response.wav", "bot_response_enhanced.wav"):
124
  await update.message.reply_voice("bot_response_enhanced.wav")
125
  else:
 
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
 
 
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
 
77
  try:
78
  # تحديث ذاكرة المحادثة
79
  conversation_history[user_id].append(text)
80
+ context = "\n".join(conversation_history[user_id][-3:]) # أخر 3 رسائل
81
 
82
  chatbot = pipeline(
83
  "text-generation",
84
  model="aubmindlab/aragpt2-base",
85
  token=os.getenv("HF_TOKEN"),
86
+ max_length=50, # تقليل طول الرد
87
+ temperature=0.7, # تقليل العشوائية
88
  )
89
  response = chatbot(
90
  context,
 
100
  try:
101
  inputs = tts_tokenizer(text, return_tensors="pt")
102
  with torch.no_grad():
103
+ output = tts_model(**inputs, speaker_id=2) # اختيار الصوت الأنثوي
104
  waveform = output.waveform[0].numpy()
105
  sf.write("bot_response.wav", waveform, tts_model.config.sampling_rate)
106
  except Exception as e:
 
116
  voice_file = await update.message.voice.get_file()
117
  await voice_file.download_to_drive("user_voice.ogg")
118
 
119
+ # معالجة الصوت
120
  user_text = await speech_to_text("user_voice.ogg")
121
  bot_response = await generate_response(user_text, str(user_id))
122
  await text_to_speech(bot_response)
123
 
124
+ # إرسال الرد
125
  if enhance_audio("bot_response.wav", "bot_response_enhanced.wav"):
126
  await update.message.reply_voice("bot_response_enhanced.wav")
127
  else: