Spaces:
Runtime error
Runtime error
Calvin commited on
Commit ·
d83cacb
1
Parent(s): f707d78
change model
Browse files- app.py +20 -25
- requirements.txt +3 -2
app.py
CHANGED
|
@@ -1,32 +1,28 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
-
from transformers import
|
|
|
|
|
|
|
| 3 |
import torch
|
|
|
|
| 4 |
import aiofiles
|
| 5 |
import uvicorn
|
| 6 |
import os
|
| 7 |
-
import soundfile as sf
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 14 |
-
model = CsmForConditionalGeneration.from_pretrained(MODEL_NAME).to(device)
|
| 15 |
-
processor = AutoProcessor.from_pretrained(MODEL_NAME)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
dur = round(len(s.split()) * spw, 2)
|
| 23 |
-
timestamps.append({"sentence": s, "start": round(current,2), "end": round(current+dur,2)})
|
| 24 |
-
current += dur
|
| 25 |
-
return timestamps
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
return {"message": "API is running!"}
|
| 30 |
|
| 31 |
@app.post("/tts")
|
| 32 |
async def tts_api(payload: dict):
|
|
@@ -34,20 +30,19 @@ async def tts_api(payload: dict):
|
|
| 34 |
if not text:
|
| 35 |
return {"error": "Text is required"}
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
outputs = model.generate(**inputs, max_new_tokens=200, output_audio=True)
|
| 41 |
-
audio = outputs[0].detach().cpu().numpy()
|
| 42 |
|
|
|
|
|
|
|
| 43 |
sf.write(file_path, audio, 24000)
|
| 44 |
|
| 45 |
async with aiofiles.open(file_path, "rb") as f:
|
| 46 |
audio_data = await f.read()
|
| 47 |
|
| 48 |
return {
|
| 49 |
-
"
|
| 50 |
-
"timestamps": generate_timestamps(text),
|
| 51 |
"file_url": f"/download/{os.path.basename(file_path)}",
|
| 52 |
"size": len(audio_data)
|
| 53 |
}
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from transformers import AutoProcessor
|
| 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 |
+
BASE_MODEL = "deep-io/csm-1b"
|
| 14 |
+
FINETUNE_MODEL = "Ellbendls/csm-1b-indonesian-fine-tuned"
|
| 15 |
+
|
| 16 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Load base model
|
| 19 |
+
model = CsmForConditionalGeneration.from_pretrained(BASE_MODEL, torch_dtype=torch.float16).to(device)
|
| 20 |
+
|
| 21 |
+
# Apply LoRA adapter
|
| 22 |
+
model = PeftModel.from_pretrained(model, FINETUNE_MODEL)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Load processor
|
| 25 |
+
processor = AutoProcessor.from_pretrained(BASE_MODEL)
|
|
|
|
| 26 |
|
| 27 |
@app.post("/tts")
|
| 28 |
async def tts_api(payload: dict):
|
|
|
|
| 30 |
if not text:
|
| 31 |
return {"error": "Text is required"}
|
| 32 |
|
| 33 |
+
inputs = processor(f"[0]{text}", return_tensors="pt").to(device)
|
| 34 |
+
with torch.no_grad():
|
| 35 |
+
outputs = model.generate(**inputs, max_new_tokens=200, output_audio=True)
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
audio = outputs[0].detach().cpu().numpy()
|
| 38 |
+
file_path = "output.wav"
|
| 39 |
sf.write(file_path, audio, 24000)
|
| 40 |
|
| 41 |
async with aiofiles.open(file_path, "rb") as f:
|
| 42 |
audio_data = await f.read()
|
| 43 |
|
| 44 |
return {
|
| 45 |
+
"text": text,
|
|
|
|
| 46 |
"file_url": f"/download/{os.path.basename(file_path)}",
|
| 47 |
"size": len(audio_data)
|
| 48 |
}
|
requirements.txt
CHANGED
|
@@ -2,7 +2,8 @@ fastapi
|
|
| 2 |
uvicorn[standard]
|
| 3 |
aiofiles
|
| 4 |
pydantic
|
| 5 |
-
transformers
|
| 6 |
torch
|
| 7 |
soundfile
|
| 8 |
-
sentencepiece
|
|
|
|
|
|
| 2 |
uvicorn[standard]
|
| 3 |
aiofiles
|
| 4 |
pydantic
|
| 5 |
+
transformers>=4.41.0
|
| 6 |
torch
|
| 7 |
soundfile
|
| 8 |
+
sentencepiece
|
| 9 |
+
peft
|