artificialguybr commited on
Commit
759578e
·
verified ·
1 Parent(s): fd915fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -37
app.py CHANGED
@@ -1,15 +1,23 @@
1
  import os
2
  import sys
 
3
  import traceback
4
- from pathlib import Path
5
-
6
  import gradio as gr
7
  import numpy as np
8
  import spaces
9
  import torch
10
  from huggingface_hub import snapshot_download
11
 
12
- sys.path.append(str(Path(__file__).parent))
 
 
 
 
 
 
 
 
 
13
 
14
  from fish_speech.models.text2semantic.inference import (
15
  init_model,
@@ -19,14 +27,13 @@ from fish_speech.models.text2semantic.inference import (
19
  encode_audio
20
  )
21
 
22
-
23
  device = "cuda" if torch.cuda.is_available() else "cpu"
24
  precision = torch.bfloat16
25
 
26
- print("Downloading Fish Audio S2 Pro weights...")
27
  checkpoint_dir = snapshot_download(repo_id="fishaudio/s2-pro")
28
 
29
- print("Loading LLAMA model...")
30
  llama_model, decode_one_token = init_model(
31
  checkpoint_path=checkpoint_dir,
32
  device=device,
@@ -41,13 +48,11 @@ with torch.device(device):
41
  dtype=next(llama_model.parameters()).dtype,
42
  )
43
 
44
- print("Loading Codec model...")
45
  codec_checkpoint = os.path.join(checkpoint_dir, "codec.pth")
46
  codec_model = load_codec_model(codec_checkpoint, device=device, precision=precision)
47
 
48
- print("All models loaded successfully!")
49
-
50
-
51
 
52
  @spaces.GPU(duration=120)
53
  def tts_inference(
@@ -60,10 +65,6 @@ def tts_inference(
60
  repetition_penalty,
61
  temperature
62
  ):
63
- """
64
- Main TTS Generation function decorated with @spaces.GPU
65
- to request GPU allocation only during execution.
66
- """
67
  try:
68
  prompt_tokens_list = None
69
 
@@ -96,7 +97,7 @@ def tts_inference(
96
  break
97
 
98
  if not codes:
99
- raise gr.Error("No audio generated. Please check your text.")
100
 
101
  merged_codes = torch.cat(codes, dim=1)
102
  audio_waveform = decode_to_audio(merged_codes.to(device), codec_model)
@@ -106,7 +107,7 @@ def tts_inference(
106
 
107
  except Exception as e:
108
  traceback.print_exc()
109
- raise gr.Error(f"Inference Error: {str(e)}")
110
 
111
 
112
  custom_theme = gr.themes.Soft(
@@ -129,8 +130,8 @@ with gr.Blocks(theme=custom_theme, title="Fish Audio S2 Pro") as app:
129
  🐟 Fish Audio S2 Pro
130
  </h1>
131
  <p style="font-size: 1.1rem; color: #4B5563;">
132
- State-of-the-Art Dual-Autoregressive Text-to-Speech.
133
- Supports 80+ languages, emotional inline control (e.g., <code>[laugh]</code>, <code>[whisper]</code>), and zero-shot voice cloning.
134
  </p>
135
  </div>
136
  """
@@ -138,47 +139,47 @@ with gr.Blocks(theme=custom_theme, title="Fish Audio S2 Pro") as app:
138
 
139
  with gr.Row():
140
  with gr.Column(scale=5):
141
- gr.Markdown("### ✍️ Text Input")
142
  text_input = gr.Textbox(
143
  show_label=False,
144
- placeholder="Enter the text you want to synthesize here.\nTry adding tags like [laugh], [whisper], or [angry]!",
145
  lines=7
146
  )
147
 
148
- with gr.Accordion("🎙️ Voice Cloning (Optional Reference)", open=False):
149
- gr.Markdown("Upload a 5-10 second clear audio clip and type its exact transcription to clone the voice.")
150
- ref_audio = gr.Audio(label="Reference Audio", type="filepath")
151
- ref_text = gr.Textbox(label="Reference Text", placeholder="Transcription of the reference audio...")
152
 
153
- with gr.Accordion("⚙️ Advanced Settings", open=False):
154
  with gr.Row():
155
- max_new_tokens = gr.Slider(0, 2048, 1024, step=8, label="Max New Tokens (0 = unlimited)")
156
- chunk_length = gr.Slider(100, 400, 200, step=8, label="Chunk Length")
157
  with gr.Row():
158
  top_p = gr.Slider(0.1, 1.0, 0.7, step=0.01, label="Top-P")
159
- repetition_penalty = gr.Slider(0.9, 2.0, 1.2, step=0.01, label="Repetition Penalty")
160
- temperature = gr.Slider(0.1, 1.0, 0.7, step=0.01, label="Temperature")
161
 
162
- generate_btn = gr.Button("🚀 Generate Speech", variant="primary", size="lg")
163
 
164
  with gr.Column(scale=4):
165
- gr.Markdown("### 🎧 Output")
166
- audio_output = gr.Audio(label="Generated Audio", type="numpy", interactive=False, autoplay=True)
167
 
168
  gr.Markdown(
169
  """
170
  <div style="background-color: #EFF6FF; padding: 15px; border-radius: 8px; margin-top: 20px;">
171
- <h4 style="margin-top: 0; color: #1D4ED8;">💡 Pro Tips</h4>
172
  <ul style="margin-bottom: 0; color: #1E3A8A; font-size: 0.95rem;">
173
- <li>You don't need phonemes, the model understands raw text seamlessly.</li>
174
- <li>Try wrapping specific words in brackets for inline emotional control.</li>
175
- <li>For cloning, the closer the transcript matches the audio, the better the result.</li>
176
  </ul>
177
  </div>
178
  """
179
  )
180
 
181
- gr.Markdown("### 🌟 Examples")
182
  gr.Examples(
183
  examples=[
184
  ["Hello world! This is a test of the Fish Audio S2 Pro model.", None, "", 1024, 200, 0.7, 1.2, 0.7],
 
1
  import os
2
  import sys
3
+ import subprocess
4
  import traceback
 
 
5
  import gradio as gr
6
  import numpy as np
7
  import spaces
8
  import torch
9
  from huggingface_hub import snapshot_download
10
 
11
+ REPO_URL = "https://github.com/fishaudio/fish-speech.git"
12
+ REPO_DIR = "fish-speech"
13
+
14
+ if not os.path.exists(REPO_DIR):
15
+ print(f"Clonando o repositório de {REPO_URL}...")
16
+ subprocess.run(["git", "clone", REPO_URL, REPO_DIR], check=True)
17
+ print("Repositório clonado com sucesso!")
18
+
19
+ os.chdir(REPO_DIR)
20
+ sys.path.insert(0, os.getcwd())
21
 
22
  from fish_speech.models.text2semantic.inference import (
23
  init_model,
 
27
  encode_audio
28
  )
29
 
 
30
  device = "cuda" if torch.cuda.is_available() else "cpu"
31
  precision = torch.bfloat16
32
 
33
+ print("Baixando os pesos do Fish Audio S2 Pro...")
34
  checkpoint_dir = snapshot_download(repo_id="fishaudio/s2-pro")
35
 
36
+ print("Carregando o modelo LLAMA (isso pode levar alguns instantes)...")
37
  llama_model, decode_one_token = init_model(
38
  checkpoint_path=checkpoint_dir,
39
  device=device,
 
48
  dtype=next(llama_model.parameters()).dtype,
49
  )
50
 
51
+ print("Carregando o modelo Codec (VQGAN)...")
52
  codec_checkpoint = os.path.join(checkpoint_dir, "codec.pth")
53
  codec_model = load_codec_model(codec_checkpoint, device=device, precision=precision)
54
 
55
+ print("✅ Todos os modelos carregados com sucesso!")
 
 
56
 
57
  @spaces.GPU(duration=120)
58
  def tts_inference(
 
65
  repetition_penalty,
66
  temperature
67
  ):
 
 
 
 
68
  try:
69
  prompt_tokens_list = None
70
 
 
97
  break
98
 
99
  if not codes:
100
+ raise gr.Error("Nenhum áudio foi gerado. Verifique o seu texto de entrada.")
101
 
102
  merged_codes = torch.cat(codes, dim=1)
103
  audio_waveform = decode_to_audio(merged_codes.to(device), codec_model)
 
107
 
108
  except Exception as e:
109
  traceback.print_exc()
110
+ raise gr.Error(f"Erro na Inferência: {str(e)}")
111
 
112
 
113
  custom_theme = gr.themes.Soft(
 
130
  🐟 Fish Audio S2 Pro
131
  </h1>
132
  <p style="font-size: 1.1rem; color: #4B5563;">
133
+ State-of-the-Art Dual-Autoregressive Text-to-Speech.<br>
134
+ Suporta mais de 80 idiomas, controle emocional no texto (ex: <code>[laugh]</code>, <code>[whisper]</code>) e clonagem de voz Zero-Shot.
135
  </p>
136
  </div>
137
  """
 
139
 
140
  with gr.Row():
141
  with gr.Column(scale=5):
142
+ gr.Markdown("### ✍️ Texto de Entrada")
143
  text_input = gr.Textbox(
144
  show_label=False,
145
+ placeholder="Digite o texto que você deseja sintetizar aqui.\nTente adicionar tags como [laugh], [whisper], ou [angry]!",
146
  lines=7
147
  )
148
 
149
+ with gr.Accordion("🎙️ Clonagem de Voz (Referência Opcional)", open=False):
150
+ gr.Markdown("Faça upload de um áudio limpo de 5 a 10 segundos e digite exatamente o que é dito nele para clonar a voz.")
151
+ ref_audio = gr.Audio(label="Áudio de Referência", type="filepath")
152
+ ref_text = gr.Textbox(label="Texto do Áudio", placeholder="Transcrição exata do áudio de referência...")
153
 
154
+ with gr.Accordion("⚙️ Configurações Avançadas", open=False):
155
  with gr.Row():
156
+ max_new_tokens = gr.Slider(0, 2048, 1024, step=8, label="Max New Tokens (0 = sem limite)")
157
+ chunk_length = gr.Slider(100, 400, 200, step=8, label="Tamanho do Chunk")
158
  with gr.Row():
159
  top_p = gr.Slider(0.1, 1.0, 0.7, step=0.01, label="Top-P")
160
+ repetition_penalty = gr.Slider(0.9, 2.0, 1.2, step=0.01, label="Penalidade de Repetição")
161
+ temperature = gr.Slider(0.1, 1.0, 0.7, step=0.01, label="Temperatura")
162
 
163
+ generate_btn = gr.Button("🚀 Gerar Áudio", variant="primary", size="lg")
164
 
165
  with gr.Column(scale=4):
166
+ gr.Markdown("### 🎧 Resultado")
167
+ audio_output = gr.Audio(label="Áudio Gerado", type="numpy", interactive=False, autoplay=True)
168
 
169
  gr.Markdown(
170
  """
171
  <div style="background-color: #EFF6FF; padding: 15px; border-radius: 8px; margin-top: 20px;">
172
+ <h4 style="margin-top: 0; color: #1D4ED8;">💡 Dicas Profissionais</h4>
173
  <ul style="margin-bottom: 0; color: #1E3A8A; font-size: 0.95rem;">
174
+ <li>O modelo compreende texto natural perfeitamente, sem necessidade de fonemas manuais.</li>
175
+ <li>Envolva palavras com colchetes para ditar emoções. Ex: <i>[pitch up] Uau! [laugh]</i>.</li>
176
+ <li>Para clonagem, quanto mais exata a transcrição do áudio de base, melhor o resultado.</li>
177
  </ul>
178
  </div>
179
  """
180
  )
181
 
182
+ gr.Markdown("### 🌟 Exemplos")
183
  gr.Examples(
184
  examples=[
185
  ["Hello world! This is a test of the Fish Audio S2 Pro model.", None, "", 1024, 200, 0.7, 1.2, 0.7],