Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,45 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
app = FastAPI()
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
@app.get("/")
|
| 6 |
def greet_json():
|
| 7 |
-
return {"Hello": "World!"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from fastapi import FastAPI, UploadFile, Form
|
| 3 |
+
from fastapi.responses import FileResponse
|
| 4 |
+
import torch
|
| 5 |
+
from TTS.api import TTS
|
| 6 |
+
import uvicorn
|
| 7 |
+
import shutil
|
| 8 |
|
| 9 |
+
# Coqui की Terms & Conditions को auto-accept करना
|
| 10 |
+
os.environ["COQUI_TOS_AGREED"] = "1"
|
| 11 |
+
|
| 12 |
+
# FastAPI app बनाना
|
| 13 |
app = FastAPI()
|
| 14 |
|
| 15 |
+
# Model लोड करना
|
| 16 |
+
tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2").to("cuda" if torch.cuda.is_available() else "cpu")
|
| 17 |
+
|
| 18 |
+
# Root endpoint
|
| 19 |
@app.get("/")
|
| 20 |
def greet_json():
|
| 21 |
+
return {"Hello": "World!"}
|
| 22 |
+
|
| 23 |
+
# Text-to-speech API endpoint
|
| 24 |
+
@app.post("/speak")
|
| 25 |
+
async def speak(
|
| 26 |
+
text: str = Form(...),
|
| 27 |
+
language: str = Form(...),
|
| 28 |
+
speaker_wav: UploadFile = None
|
| 29 |
+
):
|
| 30 |
+
# speaker.wav को सेव करना
|
| 31 |
+
if speaker_wav:
|
| 32 |
+
temp_audio_path = f"temp_{speaker_wav.filename}"
|
| 33 |
+
with open(temp_audio_path, "wb") as buffer:
|
| 34 |
+
shutil.copyfileobj(speaker_wav.file, buffer)
|
| 35 |
+
else:
|
| 36 |
+
return {"error": "Speaker audio file is required."}
|
| 37 |
+
|
| 38 |
+
output_path = "output.wav"
|
| 39 |
+
tts.tts_to_file(text=text, speaker_wav=temp_audio_path, language=language, file_path=output_path)
|
| 40 |
+
|
| 41 |
+
return FileResponse(output_path, media_type="audio/wav", filename="output.wav")
|
| 42 |
+
|
| 43 |
+
# Optional: Localhost पर रन करने के लिए
|
| 44 |
+
# if __name__ == "__main__":
|
| 45 |
+
# uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True)
|