Spaces:
Sleeping
Sleeping
Version2
Browse files
app.py
CHANGED
|
@@ -40,17 +40,18 @@ import torch
|
|
| 40 |
from parler_tts import ParlerTTSForConditionalGeneration
|
| 41 |
from transformers import AutoTokenizer
|
| 42 |
import soundfile as sf
|
| 43 |
-
import
|
| 44 |
|
| 45 |
from fastapi import FastAPI
|
| 46 |
from pydantic import BaseModel
|
| 47 |
-
from fastapi.responses import
|
| 48 |
|
| 49 |
app = FastAPI()
|
| 50 |
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
-
# Load model ONCE when server starts
|
| 54 |
model = ParlerTTSForConditionalGeneration.from_pretrained(
|
| 55 |
"Surya132/tts-custom"
|
| 56 |
).to(device)
|
|
@@ -59,22 +60,33 @@ tokenizer = AutoTokenizer.from_pretrained(
|
|
| 59 |
"Surya132/tts-custom"
|
| 60 |
)
|
| 61 |
|
|
|
|
| 62 |
class TTSRequest(BaseModel):
|
| 63 |
prompt: str
|
| 64 |
description: str
|
| 65 |
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
@app.post("/generate")
|
| 68 |
def generate_audio(req: TTSRequest):
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
).to(device)
|
| 73 |
|
|
|
|
| 74 |
prompt_inputs = tokenizer(
|
| 75 |
-
req.prompt,
|
|
|
|
| 76 |
).to(device)
|
| 77 |
|
|
|
|
| 78 |
generation = model.generate(
|
| 79 |
input_ids=description_inputs.input_ids,
|
| 80 |
attention_mask=description_inputs.attention_mask,
|
|
@@ -84,13 +96,14 @@ def generate_audio(req: TTSRequest):
|
|
| 84 |
|
| 85 |
audio = generation.cpu().numpy().squeeze()
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
sf.write(
|
|
|
|
| 90 |
|
| 91 |
-
return
|
| 92 |
|
| 93 |
-
import uvicorn
|
| 94 |
|
| 95 |
if __name__ == "__main__":
|
|
|
|
| 96 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 40 |
from parler_tts import ParlerTTSForConditionalGeneration
|
| 41 |
from transformers import AutoTokenizer
|
| 42 |
import soundfile as sf
|
| 43 |
+
import io
|
| 44 |
|
| 45 |
from fastapi import FastAPI
|
| 46 |
from pydantic import BaseModel
|
| 47 |
+
from fastapi.responses import StreamingResponse
|
| 48 |
|
| 49 |
app = FastAPI()
|
| 50 |
|
| 51 |
+
# Select device
|
| 52 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 53 |
|
| 54 |
+
# Load model ONCE when the server starts
|
| 55 |
model = ParlerTTSForConditionalGeneration.from_pretrained(
|
| 56 |
"Surya132/tts-custom"
|
| 57 |
).to(device)
|
|
|
|
| 60 |
"Surya132/tts-custom"
|
| 61 |
)
|
| 62 |
|
| 63 |
+
# Request schema
|
| 64 |
class TTSRequest(BaseModel):
|
| 65 |
prompt: str
|
| 66 |
description: str
|
| 67 |
|
| 68 |
|
| 69 |
+
@app.get("/")
|
| 70 |
+
def root():
|
| 71 |
+
return {"message": "TTS API running. Use POST /generate"}
|
| 72 |
+
|
| 73 |
+
|
| 74 |
@app.post("/generate")
|
| 75 |
def generate_audio(req: TTSRequest):
|
| 76 |
|
| 77 |
+
# Tokenize description
|
| 78 |
+
description_inputs = tokenizer(
|
| 79 |
+
req.description,
|
| 80 |
+
return_tensors="pt"
|
| 81 |
).to(device)
|
| 82 |
|
| 83 |
+
# Tokenize prompt
|
| 84 |
prompt_inputs = tokenizer(
|
| 85 |
+
req.prompt,
|
| 86 |
+
return_tensors="pt"
|
| 87 |
).to(device)
|
| 88 |
|
| 89 |
+
# Generate audio
|
| 90 |
generation = model.generate(
|
| 91 |
input_ids=description_inputs.input_ids,
|
| 92 |
attention_mask=description_inputs.attention_mask,
|
|
|
|
| 96 |
|
| 97 |
audio = generation.cpu().numpy().squeeze()
|
| 98 |
|
| 99 |
+
# Write audio to memory buffer instead of file
|
| 100 |
+
buffer = io.BytesIO()
|
| 101 |
+
sf.write(buffer, audio, model.config.sampling_rate, format="WAV")
|
| 102 |
+
buffer.seek(0)
|
| 103 |
|
| 104 |
+
return StreamingResponse(buffer, media_type="audio/wav")
|
| 105 |
|
|
|
|
| 106 |
|
| 107 |
if __name__ == "__main__":
|
| 108 |
+
import uvicorn
|
| 109 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|