Amir20255 commited on
Commit
f4a1d9e
·
verified ·
1 Parent(s): fcf61f9

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +22 -30
  2. requirements.txt +1 -5
app.py CHANGED
@@ -1,39 +1,31 @@
1
  import gradio as gr
2
- from TTS.api import TTS
3
- import os
 
4
 
5
- # تحميل نموذج FastSpeech 2 (أو نموذج أسرع)
6
- tts = TTS(model_name="fastspeech2-en-ljspeech", progress_bar=True)
7
-
8
- # دالة لتحويل النص إلى جمل صغيرة بحيث لا تتجاوز 50 كلمة
9
- def split_text_into_chunks(text, max_words=50):
10
- words = text.split()
11
- chunks = [words[i:i + max_words] for i in range(0, len(words), max_words)]
12
- return [" ".join(chunk) for chunk in chunks]
 
13
 
14
  # دالة لتحويل النص إلى صوت
15
  def text_to_speech(text):
16
- # تقسيم النص إلى جمل أو مقاطع
17
- chunks = split_text_into_chunks(text)
18
-
19
- # توليد الصوت لكل جزء
20
- audio_paths = []
21
- for i, chunk in enumerate(chunks):
22
- output_file = f"output_{i}.wav"
23
- tts.tts_to_file(text=chunk, file_path=output_file)
24
- audio_paths.append(output_file)
25
-
26
- # دمج المقاطع الصوتية معًا
27
- final_output = "final_output.wav"
28
- with open(final_output, 'wb') as f:
29
- for audio_path in audio_paths:
30
- with open(audio_path, 'rb') as audio_file:
31
- f.write(audio_file.read())
32
- os.remove(audio_path) # حذف الملفات المؤقتة بعد الدمج
33
-
34
- return final_output
35
 
36
- # واجهة المستخدم مع Gradio
37
  iface = gr.Interface(fn=text_to_speech, inputs="text", outputs="audio", live=True)
38
 
39
  # تشغيل التطبيق
 
1
  import gradio as gr
2
+ import torchaudio
3
+ from speechbrain.pretrained import FastSpeech2
4
+ from speechbrain.pretrained import HIFIGAN
5
 
6
+ # تحميل النموذج الصوتي
7
+ fastspeech2 = FastSpeech2.from_hparams(
8
+ source="speechbrain/tts-fastspeech2-ljspeech",
9
+ savedir="tmpdir_tts"
10
+ )
11
+ hifi_gan = HIFIGAN.from_hparams(
12
+ source="speechbrain/tts-hifigan-ljspeech",
13
+ savedir="tmpdir_vocoder"
14
+ )
15
 
16
  # دالة لتحويل النص إلى صوت
17
  def text_to_speech(text):
18
+ mel_output, durations, pitch, energy = fastspeech2.encode_text(
19
+ [text],
20
+ pace=1.0,
21
+ pitch_rate=1.0,
22
+ energy_rate=1.0
23
+ )
24
+ waveform = hifi_gan.decode_batch(mel_output)
25
+ torchaudio.save("output.wav", waveform.squeeze(1), 22050)
26
+ return "output.wav"
 
 
 
 
 
 
 
 
 
 
27
 
28
+ # واجهة المستخدم باستخدام Gradio
29
  iface = gr.Interface(fn=text_to_speech, inputs="text", outputs="audio", live=True)
30
 
31
  # تشغيل التطبيق
requirements.txt CHANGED
@@ -1,8 +1,4 @@
1
- TTS
2
- numpy<=1.26
3
- pydub
4
- numba<0.59
5
  speechbrain==0.5.12
6
  gradio==3.10.0
7
  torch==1.12.1
8
- librosa==0.9.2
 
 
 
 
 
1
  speechbrain==0.5.12
2
  gradio==3.10.0
3
  torch==1.12.1
4
+ torchaudio==0.12.1