Spaces:
Runtime error
Runtime error
Calvin commited on
Commit ·
ad68369
1
Parent(s): d83cacb
change model
Browse files- app.py +15 -18
- requirements.txt +1 -2
app.py
CHANGED
|
@@ -1,42 +1,40 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
-
from
|
| 3 |
-
from peft import PeftModel
|
| 4 |
-
from transformers import CsmForConditionalGeneration
|
| 5 |
import torch
|
| 6 |
import soundfile as sf
|
| 7 |
import aiofiles
|
| 8 |
import uvicorn
|
| 9 |
import os
|
|
|
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 17 |
|
| 18 |
-
# Load
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
# Load processor
|
| 25 |
-
processor = AutoProcessor.from_pretrained(BASE_MODEL)
|
| 26 |
|
| 27 |
@app.post("/tts")
|
| 28 |
async def tts_api(payload: dict):
|
| 29 |
-
text = payload.get("text", "")
|
| 30 |
if not text:
|
| 31 |
return {"error": "Text is required"}
|
| 32 |
|
| 33 |
-
inputs = processor(
|
|
|
|
| 34 |
with torch.no_grad():
|
| 35 |
-
|
| 36 |
|
| 37 |
-
audio = outputs[0].detach().cpu().numpy()
|
| 38 |
file_path = "output.wav"
|
| 39 |
-
sf.write(file_path,
|
| 40 |
|
| 41 |
async with aiofiles.open(file_path, "rb") as f:
|
| 42 |
audio_data = await f.read()
|
|
@@ -51,7 +49,6 @@ async def tts_api(payload: dict):
|
|
| 51 |
async def download_file(filename: str):
|
| 52 |
fp = os.path.join(os.getcwd(), filename)
|
| 53 |
if os.path.exists(fp):
|
| 54 |
-
from fastapi.responses import FileResponse
|
| 55 |
return FileResponse(fp, media_type="audio/wav", filename=filename)
|
| 56 |
return {"error": "File not found"}
|
| 57 |
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from fastapi.responses import FileResponse
|
|
|
|
|
|
|
| 3 |
import torch
|
| 4 |
import soundfile as sf
|
| 5 |
import aiofiles
|
| 6 |
import uvicorn
|
| 7 |
import os
|
| 8 |
+
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
+
MODEL_TTS = "microsoft/speecht5_tts"
|
| 13 |
+
MODEL_VOCODER = "microsoft/speecht5_hifigan"
|
| 14 |
|
| 15 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 16 |
|
| 17 |
+
# Load processor, model, and vocoder
|
| 18 |
+
processor = SpeechT5Processor.from_pretrained(MODEL_TTS)
|
| 19 |
+
model = SpeechT5ForTextToSpeech.from_pretrained(MODEL_TTS).to(device)
|
| 20 |
+
vocoder = SpeechT5HifiGan.from_pretrained(MODEL_VOCODER).to(device)
|
| 21 |
|
| 22 |
+
# Generate random speaker embedding
|
| 23 |
+
speaker_embeddings = torch.randn(1, 512).to(device)
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
@app.post("/tts")
|
| 26 |
async def tts_api(payload: dict):
|
| 27 |
+
text = payload.get("text", "").strip()
|
| 28 |
if not text:
|
| 29 |
return {"error": "Text is required"}
|
| 30 |
|
| 31 |
+
inputs = processor(text=text, return_tensors="pt").to(device)
|
| 32 |
+
|
| 33 |
with torch.no_grad():
|
| 34 |
+
speech = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
|
| 35 |
|
|
|
|
| 36 |
file_path = "output.wav"
|
| 37 |
+
sf.write(file_path, speech.cpu().numpy(), samplerate=16000)
|
| 38 |
|
| 39 |
async with aiofiles.open(file_path, "rb") as f:
|
| 40 |
audio_data = await f.read()
|
|
|
|
| 49 |
async def download_file(filename: str):
|
| 50 |
fp = os.path.join(os.getcwd(), filename)
|
| 51 |
if os.path.exists(fp):
|
|
|
|
| 52 |
return FileResponse(fp, media_type="audio/wav", filename=filename)
|
| 53 |
return {"error": "File not found"}
|
| 54 |
|
requirements.txt
CHANGED
|
@@ -5,5 +5,4 @@ pydantic
|
|
| 5 |
transformers>=4.41.0
|
| 6 |
torch
|
| 7 |
soundfile
|
| 8 |
-
sentencepiece
|
| 9 |
-
peft
|
|
|
|
| 5 |
transformers>=4.41.0
|
| 6 |
torch
|
| 7 |
soundfile
|
| 8 |
+
sentencepiece
|
|
|