File size: 1,401 Bytes
76706ee
 
 
 
 
 
 
767715a
76706ee
 
 
 
767715a
 
76706ee
 
 
 
767715a
 
76706ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)