Spaces:
Runtime error
Runtime error
Calvin commited on
Commit ·
9468636
1
Parent(s): 7df21d5
change model
Browse files
app.py
CHANGED
|
@@ -1,12 +1,19 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
-
from
|
|
|
|
| 3 |
import aiofiles
|
| 4 |
import uvicorn
|
| 5 |
import os
|
| 6 |
import time
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def generate_timestamps(script: str, wpm: int = 150):
|
| 11 |
sentences = [s.strip() for s in script.replace("\n", " ").split(".") if s.strip()]
|
| 12 |
timestamps = []
|
|
@@ -34,16 +41,18 @@ async def root():
|
|
| 34 |
@app.post("/tts")
|
| 35 |
async def text_to_speech(payload: dict):
|
| 36 |
text = payload.get("text", "")
|
| 37 |
-
lang = payload.get("lang", "id")
|
| 38 |
-
slow = payload.get("slow", False)
|
| 39 |
|
| 40 |
if not text:
|
| 41 |
return {"error": "Text is required"}
|
| 42 |
|
| 43 |
-
# Save audio file
|
| 44 |
-
file_path = "output.
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# Read audio for size
|
| 49 |
async with aiofiles.open(file_path, mode="rb") as f:
|
|
@@ -62,7 +71,7 @@ async def download_file(filename: str):
|
|
| 62 |
file_path = os.path.join(os.getcwd(), filename)
|
| 63 |
if os.path.exists(file_path):
|
| 64 |
from fastapi.responses import FileResponse
|
| 65 |
-
return FileResponse(file_path, media_type="audio/
|
| 66 |
return {"error": "File not found"}
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from transformers import VitsModel, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
import aiofiles
|
| 5 |
import uvicorn
|
| 6 |
import os
|
| 7 |
import time
|
| 8 |
+
import soundfile as sf
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
+
# Load model once at startup
|
| 13 |
+
model_name = "facebook/mms-tts-ind"
|
| 14 |
+
model = VitsModel.from_pretrained(model_name)
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 16 |
+
|
| 17 |
def generate_timestamps(script: str, wpm: int = 150):
|
| 18 |
sentences = [s.strip() for s in script.replace("\n", " ").split(".") if s.strip()]
|
| 19 |
timestamps = []
|
|
|
|
| 41 |
@app.post("/tts")
|
| 42 |
async def text_to_speech(payload: dict):
|
| 43 |
text = payload.get("text", "")
|
| 44 |
+
lang = payload.get("lang", "id") # Ignored here, since model is Indonesian only
|
| 45 |
+
slow = payload.get("slow", False) # Also ignored
|
| 46 |
|
| 47 |
if not text:
|
| 48 |
return {"error": "Text is required"}
|
| 49 |
|
| 50 |
+
# Save audio file as WAV
|
| 51 |
+
file_path = "output.wav"
|
| 52 |
+
inputs = tokenizer(text, return_tensors="pt")
|
| 53 |
+
with torch.no_grad():
|
| 54 |
+
output = model(**inputs).waveform
|
| 55 |
+
sf.write(file_path, output.squeeze().cpu().numpy(), model.config.sampling_rate)
|
| 56 |
|
| 57 |
# Read audio for size
|
| 58 |
async with aiofiles.open(file_path, mode="rb") as f:
|
|
|
|
| 71 |
file_path = os.path.join(os.getcwd(), filename)
|
| 72 |
if os.path.exists(file_path):
|
| 73 |
from fastapi.responses import FileResponse
|
| 74 |
+
return FileResponse(file_path, media_type="audio/wav", filename=filename)
|
| 75 |
return {"error": "File not found"}
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|