|
|
import os |
|
|
from zipfile import ZipFile |
|
|
|
|
|
import torch |
|
|
from fastapi import FastAPI |
|
|
from fastapi.responses import StreamingResponse |
|
|
|
|
|
from OpenVoice import se_extractor |
|
|
from OpenVoice.api import BaseSpeakerTTS, ToneColorConverter |
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
|
|
|
|
|
|
CHECKPOINT_ZIP_URL = "https://myshell-public-repo-hosting.s3.amazonaws.com/checkpoints_1226.zip" |
|
|
CKPT_DIR = "checkpoints" |
|
|
EN_CKPT_BASE = os.path.join(CKPT_DIR, "base_speakers", "EN") |
|
|
CONVERTER_CKPT = os.path.join(CKPT_DIR, "converter") |
|
|
|
|
|
os.makedirs("outputs", exist_ok=True) |
|
|
|
|
|
if not os.path.exists(CKPT_DIR): |
|
|
print("Downloading OpenVoice checkpoints ...") |
|
|
os.system(f"wget {CHECKPOINT_ZIP_URL} -O ckpt.zip") |
|
|
print("Extracting checkpoints ...") |
|
|
ZipFile("ckpt.zip").extractall() |
|
|
print("Checkpoints ready.") |
|
|
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
|
|
|
|
|
|
|
base_speaker_tts = BaseSpeakerTTS(f"{EN_CKPT_BASE}/config.json", device=device) |
|
|
base_speaker_tts.load_ckpt(f"{EN_CKPT_BASE}/checkpoint.pth") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/tts") |
|
|
async def tts_endpoint( |
|
|
text: str, |
|
|
speaker: str = "default", |
|
|
speed: float = 1.0, |
|
|
): |
|
|
""" |
|
|
يحوّل النص إلى صوت WAV باستخدام OpenVoice. |
|
|
""" |
|
|
out_path = "outputs/out.wav" |
|
|
|
|
|
|
|
|
language = "English" |
|
|
|
|
|
|
|
|
base_speaker_tts.tts( |
|
|
text=text, |
|
|
output_path=out_path, |
|
|
speaker=speaker, |
|
|
language=language, |
|
|
speed=speed, |
|
|
) |
|
|
|
|
|
audio_file = open(out_path, "rb") |
|
|
return StreamingResponse(audio_file, media_type="audio/wav") |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
import uvicorn |
|
|
|
|
|
uvicorn.run(app, host="0.0.0.0", port=7860) |
|
|
|