File size: 2,134 Bytes
f46dce0
1ba3d0c
30b69e6
882c6ec
 
 
e578353
882c6ec
 
 
 
 
 
673b955
 
882c6ec
673b955
882c6ec
 
 
 
e578353
f87e51e
e578353
882c6ec
 
 
 
 
 
 
 
 
 
e578353
882c6ec
e578353
882c6ec
e578353
 
882c6ec
f87e51e
882c6ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os

os.environ["HF_HOME"] = "/tmp/hf"
os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf"
os.environ["HF_DATASETS_CACHE"] = "/tmp/hf"
os.makedirs("/tmp/hf", exist_ok=True)

from fastapi import FastAPI, Query
from fastapi.responses import StreamingResponse
from transformers import VitsModel, AutoTokenizer
import torch, scipy.io.wavfile as wavfile
import io
import edge_tts


app = FastAPI(title="Bambara TTS API")

# Load model once at startup
model = VitsModel.from_pretrained("facebook/mms-tts-bam")
tokenizer = AutoTokenizer.from_pretrained("facebook/mms-tts-bam")
sampling_rate = model.config.sampling_rate


@app.get("/tts/")
async def tts(text: str = Query(..., description="Bambara text to synthesize")):
    inputs = tokenizer(text, return_tensors="pt")
    inputs = {k: v.to("cpu") for k, v in inputs.items()}

    with torch.no_grad():
        output = model(**inputs).waveform

    waveform = output[0]

    # Stream audio instead of saving to disk
    buffer = io.BytesIO()
    wavfile.write(buffer, rate=sampling_rate, data=waveform.numpy())
    buffer.seek(0)

    return StreamingResponse(buffer, media_type="audio/wav")


@app.get("/noneBmTts/")
async def noneBmTts(
    text: str = Query(..., description="Text to synthesize"),
    voice: str = Query(
        "fr-FR-DeniseNeural", description="Voice ID (e.g., en-US-GuyNeural)"
    ),
):
    try:
        # Create the Communicate object with the requested text and voice
        communicate = edge_tts.Communicate(text, voice)

        buffer = io.BytesIO()

        # Stream the audio chunks into the buffer
        async for chunk in communicate.stream():
            if chunk["type"] == "audio":
                buffer.write(chunk["data"])

        # Check if we actually got data
        if buffer.tell() == 0:
            raise HTTPException(
                status_code=400, detail="Synthesis failed to produce audio."
            )

        buffer.seek(0)
        return StreamingResponse(buffer, media_type="audio/mpeg")

    except Exception as e:
        # Catch errors like invalid voice names
        raise HTTPException(status_code=400, detail=str(e))