|
from fastapi import APIRouter, HTTPException,UploadFile |
|
from fastapi.responses import StreamingResponse, FileResponse |
|
from app.rvc.rvc_infer import rvc_convert |
|
import os |
|
import base64 |
|
router = APIRouter( |
|
prefix="/clone", |
|
tags=["cloning"], |
|
responses={404: {"description": "Not found"}}, |
|
) |
|
|
|
@router.post("/") |
|
async def generate_voice(audio_file: UploadFile,speaker_name: str ): |
|
base_path = "/Users/saboor/Documents/TTS-RVC-API-1/models/" |
|
speaker_dir = os.path.join(base_path, speaker_name) |
|
|
|
if os.path.exists(speaker_dir): |
|
|
|
files_in_dir = os.listdir(speaker_dir) |
|
pth_files = [file for file in files_in_dir if file.endswith(".pth")] |
|
if len(pth_files) == 1: |
|
pth_file_path = os.path.join(speaker_dir, pth_files[0]) |
|
|
|
with open(f"temp_{audio_file.filename}", "wb") as f: |
|
f.write(audio_file.file.read()) |
|
|
|
wav_opt = rvc_convert( |
|
model_path=pth_file_path, |
|
f0_up_key=-4, |
|
input_path=f"temp_{audio_file.filename}", |
|
) |
|
with open(wav_opt, "rb") as wav_file: |
|
wav_bytes = wav_file.read() |
|
base64_bytes = base64.b64encode(wav_bytes) |
|
base64_string = base64_bytes.decode("utf-8") |
|
|
|
return {"base64_wav": base64_string} |