import os from fastapi import FastAPI, UploadFile, Form from fastapi.responses import FileResponse import torch from TTS.api import TTS import uvicorn import shutil # Coqui की Terms & Conditions को auto-accept करना os.environ["COQUI_TOS_AGREED"] = "1" # FastAPI app बनाना app = FastAPI() # Model लोड करना tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2").to("cuda" if torch.cuda.is_available() else "cpu") # Root endpoint @app.get("/") def greet_json(): return {"Hello": "World!"} # Text-to-speech API endpoint @app.post("/speak") async def speak( text: str = Form(...), language: str = Form(...), speaker_wav: UploadFile = None ): # speaker.wav को सेव करना if speaker_wav: temp_audio_path = f"temp_{speaker_wav.filename}" with open(temp_audio_path, "wb") as buffer: shutil.copyfileobj(speaker_wav.file, buffer) else: return {"error": "Speaker audio file is required."} output_path = "output.wav" tts.tts_to_file(text=text, speaker_wav=temp_audio_path, language=language, file_path=output_path) return FileResponse(output_path, media_type="audio/wav", filename="output.wav") # Optional: Localhost पर रन करने के लिए # if __name__ == "__main__": # uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True)