File size: 1,422 Bytes
464a486
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c1a05f
 
464a486
 
 
 
8c1a05f
 
464a486
 
8c1a05f
464a486
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException
from fastapi.responses import StreamingResponse
from fastapi.middleware.cors import CORSMiddleware
import io
from pydantic import BaseModel

app = FastAPI()

# Allow specific domains or all (*) for testing
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Replace with your domain
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

from melo.api import TTS
# Speed is adjustable
speed = 1.0
device = 'cpu' # or cuda:0

# Pydantic model for request body
class TextInput(BaseModel):
    text: str
    lang: str
    speaker: str

@app.post("/generate-tts")
async def generate_tts(input: TextInput):
    try:
        model = TTS(language=input.lang, device=device)
        speaker_ids = model.hps.data.spk2id
        # Generate audio to a temporary buffer
        buffer = io.BytesIO()
        model.tts_to_file(input.text, speaker_ids[input.speaker], buffer, speed=speed, format='wav')
        buffer.seek(0)
        # Return audio as streaming response
        return StreamingResponse(
            buffer,
            media_type="audio/wav",
            headers={"Content-Disposition": 'attachment; filename="output.wav"'}
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error generating audio: {str(e)}")

@app.get("/")
def greet_json():
    return {
        "Hello": "World!",
        }