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!", }