hynt commited on
Commit
5f2ef10
·
verified ·
1 Parent(s): 493e475

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -22
app.py CHANGED
@@ -15,10 +15,10 @@ from f5_tts.infer.utils_infer import (
15
  save_spectrogram,
16
  )
17
 
18
- # Lấy token từ secrets
19
  hf_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
20
 
21
- # Login vào Hugging Face
22
  if hf_token:
23
  login(token=hf_token)
24
 
@@ -31,6 +31,8 @@ def post_process(text):
31
  text = text.replace(" , , ", " , ")
32
  text = " " + text + " "
33
  text = text.replace(" ,, ", " , ")
 
 
34
  return " ".join(text.split())
35
 
36
  # Load models
@@ -38,7 +40,7 @@ vocoder = load_vocoder()
38
  model = load_model(
39
  DiT,
40
  dict(dim=1024, depth=22, heads=16, ff_mult=2, text_dim=512, conv_layers=4),
41
- ckpt_path=str(cached_path("hf://hynt/F5-TTS-Vietnamese-100h/model_390000.pt")),
42
  vocab_file=str(cached_path("hf://hynt/F5-TTS-Vietnamese-100h/vocab.txt")),
43
  )
44
 
@@ -46,11 +48,11 @@ model = load_model(
46
  def infer_tts(ref_audio_orig: str, gen_text: str, speed: float = 1.0, request: gr.Request = None):
47
 
48
  if not ref_audio_orig:
49
- raise gr.Error("Vui lòng tải lên tệp âm thanh mẫu.")
50
  if not gen_text.strip():
51
- raise gr.Error("Vui lòng nhập nội dung cần sinh giọng.")
52
  if len(gen_text.split()) > 1000:
53
- raise gr.Error("Vui lòng nhập nội dung cần sinh giọng nhỏ hơn 100 từ.")
54
 
55
  try:
56
  ref_audio, ref_text = preprocess_ref_audio_text(ref_audio_orig, "")
@@ -63,38 +65,39 @@ def infer_tts(ref_audio_orig: str, gen_text: str, speed: float = 1.0, request: g
63
 
64
  return (final_sample_rate, final_wave), spectrogram_path
65
  except Exception as e:
66
- raise gr.Error(f"Lỗi khi sinh giọng: {e}")
67
 
68
  # Gradio UI
69
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
70
  gr.Markdown("""
71
- # 🎤 F5-TTS: Tổng hợp giọng nói Tiếng Việt.
72
- # hình được huấn luyện 390.000 steps với bộ dữ liệu khoảng 150h trên 1 GPU RTX 3090.
73
- Nhập văn bản tải lên một mẫu giọng để tạo âm thanh tự nhiên.
74
  """)
75
 
76
  with gr.Row():
77
- ref_audio = gr.Audio(label="🔊 Mẫu giọng", type="filepath")
78
- gen_text = gr.Textbox(label="📝 Văn bản", placeholder="Nhập nội dung cần sinh giọng...", lines=3)
79
 
80
- speed = gr.Slider(0.3, 2.0, value=1.0, step=0.1, label="⚡ Tốc độ")
81
- btn_synthesize = gr.Button("🔥 Sinh giọng")
82
 
83
  with gr.Row():
84
- output_audio = gr.Audio(label="🎧 Âm thanh tạo ra", type="numpy")
85
  output_spectrogram = gr.Image(label="📊 Spectrogram")
86
 
87
  model_limitations = gr.Textbox(
88
- value="""1. hình thể hoạt động không tốt với các ký tự số, ngày tháng, tự đặc biệt, ... => Cần bổ sung thêm một module text normalization (chuẩn hoá text).
89
- 2. Nhịp điệu của một số audio thể chưa được mạch lạc, giật cục => Gợi ý hãy chọn các audio mẫu đọc ràng, không ngắt quãng quá nhiều, sẽ cải thiện được kết quả tổng hợp.
90
- 3. Audio reference text sử dụng model whisper-large-v3-turbo nên sẽ một vài trường hợp không nhận diện chính xác Tiếng Việt, dẫn đến kết quả tổng hợp giọng nói rất tệ.
91
- 4. Checkpoint của hình hiện tại dừng lại ở khoảng step thứ 390.000, được huấn luyện với 150 giờ dữ liệu public => Việc voice cloning cho các giọng ngoại lai thể không được chính xác tuyệt đối.""",
92
- label="❗ Hạn chế của hình",
93
- lines=4,
 
94
  interactive=False
95
  )
96
 
97
  btn_synthesize.click(infer_tts, inputs=[ref_audio, gen_text, speed], outputs=[output_audio, output_spectrogram])
98
 
99
- # Chạy Gradio với share=True để link gradio.live
100
  demo.queue().launch()
 
15
  save_spectrogram,
16
  )
17
 
18
+ # Retrieve token from secrets
19
  hf_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
20
 
21
+ # Log in to Hugging Face
22
  if hf_token:
23
  login(token=hf_token)
24
 
 
31
  text = text.replace(" , , ", " , ")
32
  text = " " + text + " "
33
  text = text.replace(" ,, ", " , ")
34
+ text = " " + text + " "
35
+ text = text.replace('"', "")
36
  return " ".join(text.split())
37
 
38
  # Load models
 
40
  model = load_model(
41
  DiT,
42
  dict(dim=1024, depth=22, heads=16, ff_mult=2, text_dim=512, conv_layers=4),
43
+ ckpt_path=str(cached_path("hf://hynt/F5-TTS-Vietnamese-100h/model_470000.pt")),
44
  vocab_file=str(cached_path("hf://hynt/F5-TTS-Vietnamese-100h/vocab.txt")),
45
  )
46
 
 
48
  def infer_tts(ref_audio_orig: str, gen_text: str, speed: float = 1.0, request: gr.Request = None):
49
 
50
  if not ref_audio_orig:
51
+ raise gr.Error("Please upload a sample audio file.")
52
  if not gen_text.strip():
53
+ raise gr.Error("Please enter the text content to generate voice.")
54
  if len(gen_text.split()) > 1000:
55
+ raise gr.Error("Please enter text content with less than 100 words.")
56
 
57
  try:
58
  ref_audio, ref_text = preprocess_ref_audio_text(ref_audio_orig, "")
 
65
 
66
  return (final_sample_rate, final_wave), spectrogram_path
67
  except Exception as e:
68
+ raise gr.Error(f"Error generating voice: {e}")
69
 
70
  # Gradio UI
71
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
72
  gr.Markdown("""
73
+ # 🎤 F5-TTS: Vietnamese Text-to-Speech Synthesis.
74
+ # The model was trained for 470,000 steps with approximately 150 hours of data on an RTX 3090 GPU.
75
+ Enter text and upload a sample voice to generate natural speech.
76
  """)
77
 
78
  with gr.Row():
79
+ ref_audio = gr.Audio(label="🔊 Sample Voice", type="filepath")
80
+ gen_text = gr.Textbox(label="📝 Text", placeholder="Enter the text to generate voice...", lines=3)
81
 
82
+ speed = gr.Slider(0.3, 2.0, value=1.0, step=0.1, label="⚡ Speed")
83
+ btn_synthesize = gr.Button("🔥 Generate Voice")
84
 
85
  with gr.Row():
86
+ output_audio = gr.Audio(label="🎧 Generated Audio", type="numpy")
87
  output_spectrogram = gr.Image(label="📊 Spectrogram")
88
 
89
  model_limitations = gr.Textbox(
90
+ value="""1. The model may not perform well with numerical characters, dates, special characters, etc. => A text normalization module is needed.
91
+ 2. The rhythm of some generated audios may be inconsistent or choppy => It is recommended to select clearly pronounced sample audios with minimal pauses for better synthesis quality.
92
+ 3. The reference audio text uses the whisper-large-v3-turbo model, which may not always accurately recognize Vietnamese, resulting in poor voice synthesis quality.
93
+ 4. The current model checkpoint is at around step 470,000, trained with 150 hours of public data => Voice cloning for non-native voices may not be perfectly accurate.
94
+ 5. Inference with overly long paragraphs may produce poor results.""",
95
+ label="❗ Model Limitations",
96
+ lines=5,
97
  interactive=False
98
  )
99
 
100
  btn_synthesize.click(infer_tts, inputs=[ref_audio, gen_text, speed], outputs=[output_audio, output_spectrogram])
101
 
102
+ # Run Gradio with share=True to get a gradio.live link
103
  demo.queue().launch()