multija commited on
Commit
983bcf7
·
verified ·
1 Parent(s): 7e590d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -29
app.py CHANGED
@@ -1,50 +1,45 @@
1
- import os
 
2
  import requests
3
- from fastapi import FastAPI, Body
4
- from f5_tts.inference import inference
5
- import soundfile as sf
6
-
7
- app = FastAPI()
8
 
9
- # URLs dos arquivos
10
  MODEL_URL = "https://huggingface.co/firstpixel/F5-TTS-pt-br/resolve/main/pt-br/model_last.safetensors"
11
  CONFIG_URL = "https://raw.githubusercontent.com/SWivid/F5-TTS/refs/heads/main/src/f5_tts/configs/F5TTS_Base.yaml"
12
- VOCAB_URL = "https://huggingface.co/SWivid/F5-TTS/raw/main/F5TTS_Base/vocab.txt"
 
 
 
 
13
 
14
- # Pastas locais
15
  os.makedirs("model", exist_ok=True)
16
  os.makedirs("config", exist_ok=True)
17
  os.makedirs("vocoder", exist_ok=True)
18
 
19
- MODEL_FILE = "model/model_last.safetensors"
20
- CONFIG_FILE = "config/config.yaml"
21
- VOCAB_FILE = "vocoder/vocos.txt"
22
-
23
  def download_file(url, path):
24
  if not os.path.exists(path):
25
  r = requests.get(url)
26
  r.raise_for_status()
27
  with open(path, "wb") as f:
28
  f.write(r.content)
29
- return path
30
 
31
- # Baixar arquivos necessários
32
  download_file(MODEL_URL, MODEL_FILE)
33
  download_file(CONFIG_URL, CONFIG_FILE)
34
  download_file(VOCAB_URL, VOCAB_FILE)
35
 
36
- @app.post("/tts")
37
- def tts(text: str = Body(..., embed=True)):
38
- """
39
- Recebe um texto e gera output.wav
40
- """
41
- # Chamar inferência do F5-TTS
42
- wav = inference(text=text,
43
- model_file=MODEL_FILE,
44
- config_file=CONFIG_FILE,
45
- vocab_file=VOCAB_FILE)
46
-
47
- # Salvar output
48
  output_path = "output.wav"
49
- sf.write(output_path, wav, 24000)
50
- return {"message": "Arquivo gerado com sucesso!", "file": output_path}
 
1
+ from fastapi import FastAPI
2
+ from f5_tts.api import F5TTS
3
  import requests
4
+ import os
 
 
 
 
5
 
 
6
  MODEL_URL = "https://huggingface.co/firstpixel/F5-TTS-pt-br/resolve/main/pt-br/model_last.safetensors"
7
  CONFIG_URL = "https://raw.githubusercontent.com/SWivid/F5-TTS/refs/heads/main/src/f5_tts/configs/F5TTS_Base.yaml"
8
+ VOCAB_URL = "https://huggingface.co/SWivid/F5-TTS/raw/main/F5TTS_Base/vocab.txt"
9
+
10
+ MODEL_FILE = "model/model_last.safetensors"
11
+ CONFIG_FILE = "config/config.yaml"
12
+ VOCAB_FILE = "vocoder/vocos.txt"
13
 
 
14
  os.makedirs("model", exist_ok=True)
15
  os.makedirs("config", exist_ok=True)
16
  os.makedirs("vocoder", exist_ok=True)
17
 
18
+ # Função simples para baixar arquivos
 
 
 
19
  def download_file(url, path):
20
  if not os.path.exists(path):
21
  r = requests.get(url)
22
  r.raise_for_status()
23
  with open(path, "wb") as f:
24
  f.write(r.content)
 
25
 
26
+ # Baixa arquivos se não existirem
27
  download_file(MODEL_URL, MODEL_FILE)
28
  download_file(CONFIG_URL, CONFIG_FILE)
29
  download_file(VOCAB_URL, VOCAB_FILE)
30
 
31
+ # Inicializa F5-TTS
32
+ tts_model = F5TTS(model_path=MODEL_FILE, config_path=CONFIG_FILE)
33
+
34
+ # Cria app FastAPI
35
+ app = FastAPI()
36
+
37
+ @app.get("/")
38
+ def root():
39
+ return {"message": "F5-TTS API está rodando!"}
40
+
41
+ @app.get("/speak")
42
+ def speak(text: str):
43
  output_path = "output.wav"
44
+ tts_model.tts_to_file(text=text, file_path=output_path)
45
+ return {"audio_file": output_path}