Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,50 @@
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, Body
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
CONFIG_FILE = "config/config.yaml"
|
|
|
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
@app.post("/tts")
|
| 14 |
def tts(text: str = Body(..., embed=True)):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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}
|