Spaces:
Runtime error
Runtime error
Calvin commited on
Commit ·
679d5e9
1
Parent(s): b25b259
change importexit
Browse files
app.py
CHANGED
|
@@ -1,19 +1,17 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from fastapi.responses import FileResponse
|
| 3 |
-
from transformers import AutoProcessor, VitsModel
|
| 4 |
-
import torch
|
| 5 |
-
import soundfile as sf
|
| 6 |
import aiofiles
|
| 7 |
-
import uvicorn
|
| 8 |
import os
|
|
|
|
|
|
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
model = VitsModel.from_pretrained(MODEL_NAME).to(device)
|
| 17 |
|
| 18 |
@app.post("/tts")
|
| 19 |
async def tts_api(payload: dict):
|
|
@@ -21,21 +19,16 @@ async def tts_api(payload: dict):
|
|
| 21 |
if not text:
|
| 22 |
return {"error": "Text is required"}
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
outputs = model(**inputs)
|
| 27 |
-
audio = outputs.waveform.squeeze().cpu().numpy()
|
| 28 |
-
|
| 29 |
-
file_path = "output.wav"
|
| 30 |
-
sf.write(file_path, audio, samplerate=model.config.sampling_rate)
|
| 31 |
|
| 32 |
-
async with aiofiles.open(
|
| 33 |
audio_data = await f.read()
|
| 34 |
|
| 35 |
return {
|
| 36 |
"text": text,
|
| 37 |
-
"file_url": f"/download/{os.path.basename(
|
| 38 |
-
"size": len(audio_data)
|
| 39 |
}
|
| 40 |
|
| 41 |
@app.get("/download/{filename}")
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from fastapi.responses import FileResponse
|
|
|
|
|
|
|
|
|
|
| 3 |
import aiofiles
|
|
|
|
| 4 |
import os
|
| 5 |
+
from TTS.api import TTS # install via: pip install TTS
|
| 6 |
+
import uvicorn
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
# Load the Tacotron2 + HiFi-GAN Indonesian model (from Coqui TTS hub)
|
| 11 |
+
# Model ID on HF: "tts_models/id/id_tts_tacotron2"
|
| 12 |
+
tts = TTS(model_name="tts_models/id/id_tts_tacotron2")
|
| 13 |
|
| 14 |
+
OUTPUT_FILE = "output.wav"
|
|
|
|
| 15 |
|
| 16 |
@app.post("/tts")
|
| 17 |
async def tts_api(payload: dict):
|
|
|
|
| 19 |
if not text:
|
| 20 |
return {"error": "Text is required"}
|
| 21 |
|
| 22 |
+
# Generate speech and save to OUTPUT_FILE
|
| 23 |
+
tts.tts_to_file(text=text, file_path=OUTPUT_FILE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
async with aiofiles.open(OUTPUT_FILE, "rb") as f:
|
| 26 |
audio_data = await f.read()
|
| 27 |
|
| 28 |
return {
|
| 29 |
"text": text,
|
| 30 |
+
"file_url": f"/download/{os.path.basename(OUTPUT_FILE)}",
|
| 31 |
+
"size": len(audio_data),
|
| 32 |
}
|
| 33 |
|
| 34 |
@app.get("/download/{filename}")
|