Spaces:
Running
Running
Upload 3 files
Browse files- Dockerfile +21 -0
- app.py +29 -0
- requirements.txt +6 -0
Dockerfile
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use lightweight Python image
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
+
|
| 4 |
+
# Install system deps
|
| 5 |
+
RUN apt-get update && apt-get install -y git ffmpeg libsndfile1 && rm -rf /var/lib/apt/lists/*
|
| 6 |
+
|
| 7 |
+
# Set workdir
|
| 8 |
+
WORKDIR /code
|
| 9 |
+
|
| 10 |
+
# Install requirements
|
| 11 |
+
COPY requirements.txt .
|
| 12 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 13 |
+
|
| 14 |
+
# Copy app
|
| 15 |
+
COPY app.py .
|
| 16 |
+
|
| 17 |
+
# Expose port for HF Space
|
| 18 |
+
EXPOSE 7860
|
| 19 |
+
|
| 20 |
+
# Run with uvicorn
|
| 21 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Query
|
| 2 |
+
from fastapi.responses import FileResponse
|
| 3 |
+
from transformers import VitsModel, AutoTokenizer
|
| 4 |
+
import torch, scipy.io.wavfile as wavfile
|
| 5 |
+
import uuid, os
|
| 6 |
+
|
| 7 |
+
app = FastAPI(title="Bambara TTS API")
|
| 8 |
+
|
| 9 |
+
# Load model once at startup
|
| 10 |
+
model = VitsModel.from_pretrained("facebook/mms-tts-bam")
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained("facebook/mms-tts-bam")
|
| 12 |
+
sampling_rate = model.config.sampling_rate
|
| 13 |
+
|
| 14 |
+
@app.get("/tts/")
|
| 15 |
+
async def tts(text: str = Query(..., description="Bambara text to synthesize")):
|
| 16 |
+
# Tokenize input
|
| 17 |
+
inputs = tokenizer(text, return_tensors="pt")
|
| 18 |
+
|
| 19 |
+
# Generate waveform
|
| 20 |
+
with torch.no_grad():
|
| 21 |
+
output = model(**inputs).waveform
|
| 22 |
+
|
| 23 |
+
waveform = output[0]
|
| 24 |
+
|
| 25 |
+
# Save to temp file
|
| 26 |
+
filename = f"tts_{uuid.uuid4().hex}.wav"
|
| 27 |
+
wavfile.write(filename, rate=sampling_rate, data=waveform.numpy())
|
| 28 |
+
|
| 29 |
+
return FileResponse(filename, media_type="audio/wav", filename=filename)
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
transformers==4.44.2
|
| 4 |
+
accelerate
|
| 5 |
+
torch
|
| 6 |
+
scipy
|