multija commited on
Commit
d4b913c
·
verified ·
1 Parent(s): 5b7d3ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -9
app.py CHANGED
@@ -1,17 +1,50 @@
 
 
1
  from fastapi import FastAPI, Body
2
- from fastapi.responses import FileResponse
3
- from TTS.api import TTS
4
 
5
- MODEL_FILE = "model/model_last.safetensors"
 
 
 
 
 
 
 
 
 
 
 
 
6
  CONFIG_FILE = "config/config.yaml"
 
7
 
8
- app = FastAPI()
 
 
 
 
 
 
9
 
10
- # Carrega o modelo na inicialização
11
- tts_model = TTS(model_path=MODEL_FILE, config_path=CONFIG_FILE)
 
 
12
 
13
  @app.post("/tts")
14
  def tts(text: str = Body(..., embed=True)):
15
- out_path = "output.wav"
16
- tts_model.tts_to_file(text=text, file_path=out_path)
17
- return FileResponse(out_path, media_type="audio/wav", filename="output.wav")
 
 
 
 
 
 
 
 
 
 
 
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}