2506minecraft commited on
Commit
fbc664b
·
verified ·
1 Parent(s): be9615b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -14
app.py CHANGED
@@ -23,7 +23,7 @@ logging.basicConfig(
23
  )
24
  logger = logging.getLogger(__name__)
25
 
26
- # ===== تحميل النماذج مع صوت أنثوي محسّن =====
27
  try:
28
  # 1. نموذج التعرف على الكلام
29
  asr_pipeline = pipeline(
@@ -32,13 +32,13 @@ try:
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,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
@@ -74,7 +74,6 @@ async def speech_to_text(audio_path: str) -> str:
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)
@@ -84,8 +83,8 @@ async def generate_response(text: str, user_id: str) -> str:
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,
@@ -98,11 +97,10 @@ async def generate_response(text: str, user_id: str) -> str:
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:
@@ -110,8 +108,7 @@ async def text_to_speech(text: str) -> None:
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:
 
23
  )
24
  logger = logging.getLogger(__name__)
25
 
26
+ # ===== تحميل النماذج =====
27
  try:
28
  # 1. نموذج التعرف على الكلام
29
  asr_pipeline = pipeline(
 
32
  token=os.getenv("HF_TOKEN")
33
  )
34
 
35
+ # 2. نموذج توليف الصوت الأنثوي (فيسبوك)
36
  tts_tokenizer = AutoTokenizer.from_pretrained(
37
+ "facebook/mms-tts-arb", # النموذج البديل
38
  token=os.getenv("HF_TOKEN")
39
  )
40
  tts_model = VitsModel.from_pretrained(
41
+ "facebook/mms-tts-arb",
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
 
74
  return ""
75
 
76
  async def generate_response(text: str, user_id: str) -> str:
 
77
  try:
78
  # تحديث ذاكرة المحادثة
79
  conversation_history[user_id].append(text)
 
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,
 
97
  return "حدث خطأ في توليد الرد."
98
 
99
  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:
 
108
 
109
  # ===== دوال التفاعل مع المستخدم =====
110
  async def start(update: Update, context):
111
+ await update.message.reply_text("مرحبًا! أنا بوت الدردشة الصوتية الأنثوي 🎤\nأرسل لي رسالة صوتية وسأرد عليك بصوت أنثوي واضح.")
 
112
 
113
  async def process_voice(update: Update, context):
114
  try: