Upload main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding: utf-8
|
| 2 |
+
import os
|
| 3 |
+
import io
|
| 4 |
+
import torch
|
| 5 |
+
import tempfile
|
| 6 |
+
from fastapi import FastAPI, HTTPException, Form, UploadFile, File
|
| 7 |
+
from fastapi.responses import StreamingResponse
|
| 8 |
+
|
| 9 |
+
# OpenVoice V2 ๊ด๋ จ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ํฌํธ
|
| 10 |
+
from openvoice import se_extractor
|
| 11 |
+
from openvoice.api import ToneColorConverter
|
| 12 |
+
|
| 13 |
+
# MeloTTS ๊ด๋ จ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ํฌํธ
|
| 14 |
+
from melo.api import TTS
|
| 15 |
+
|
| 16 |
+
# -------------------------------------------------------------------
|
| 17 |
+
# 1. FastAPI ์ฑ ์ด๊ธฐํ ๋ฐ ๋ชจ๋ธ ๋ก๋
|
| 18 |
+
# -------------------------------------------------------------------
|
| 19 |
+
app = FastAPI()
|
| 20 |
+
|
| 21 |
+
print("๐ Loading models...")
|
| 22 |
+
try:
|
| 23 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 24 |
+
|
| 25 |
+
# OpenVoice ๋ชจ๋ธ ๋ก๋
|
| 26 |
+
# Tone Color Extractor: ์์ ํน์ง์ ์ถ์ถํ๋ ๋ชจ๋ธ
|
| 27 |
+
# Tone Color Converter: ์์์ ๋ณํํ๋ ๋ชจ๋ธ
|
| 28 |
+
print("Loading OpenVoice V2 models...")
|
| 29 |
+
tone_color_converter = ToneColorConverter('checkpoints/converter', device=device)
|
| 30 |
+
print("โ
OpenVoice V2 loaded.")
|
| 31 |
+
|
| 32 |
+
# Melotts ๋ชจ๋ธ ๋ก๋ (ํ๊ตญ์ด ์ง์)
|
| 33 |
+
print("Loading Melotts model...")
|
| 34 |
+
melotts_model = TTS(language='KR', device=device)
|
| 35 |
+
speaker_ids = melotts_model.hps.data.spk2id
|
| 36 |
+
print("โ
Melotts loaded.")
|
| 37 |
+
|
| 38 |
+
except Exception as ex:
|
| 39 |
+
print(f"โ Failed to load models. Error: {ex}")
|
| 40 |
+
tone_color_converter = None
|
| 41 |
+
melotts_model = None
|
| 42 |
+
|
| 43 |
+
# -------------------------------------------------------------------
|
| 44 |
+
# 2. API ์๋ํฌ์ธํธ ์์ฑ
|
| 45 |
+
# -------------------------------------------------------------------
|
| 46 |
+
@app.post("/generate-cloned-speech/")
|
| 47 |
+
async def generate_cloned_speech(
|
| 48 |
+
text: str = Form(...),
|
| 49 |
+
reference_audio: UploadFile = File(...)
|
| 50 |
+
):
|
| 51 |
+
if not tone_color_converter or not melotts_model:
|
| 52 |
+
raise HTTPException(status_code=500, detail="Models are not loaded.")
|
| 53 |
+
|
| 54 |
+
# ์์ ํ์ผ ๊ฒฝ๋ก๋ฅผ ๊ด๋ฆฌํ๊ธฐ ์ํ ๋ณ์
|
| 55 |
+
reference_path = None
|
| 56 |
+
source_path = None
|
| 57 |
+
save_path = None
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
# 1. ์ฐธ์กฐ ์ค๋์ค(๋ชฉ์๋ฆฌ ์ฃผ์ธ)๋ฅผ ์์ ํ์ผ๋ก ์ ์ฅ
|
| 61 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_ref_file:
|
| 62 |
+
content = await reference_audio.read()
|
| 63 |
+
temp_ref_file.write(content)
|
| 64 |
+
reference_path = temp_ref_file.name
|
| 65 |
+
|
| 66 |
+
# 2. ์ฐธ์กฐ ์ค๋์ค์์ ์์ ํน์ง(Tone Color) ์ถ์ถ
|
| 67 |
+
target_se, audio_name = se_extractor.get_se(reference_path, tone_color_converter, target_dir='_outputs/form_clone', vad=True)
|
| 68 |
+
|
| 69 |
+
# 3. Melotts๋ฅผ ์ฌ์ฉํด ํ
์คํธ๋ก ๊ธฐ๋ณธ(Source) ์์ฑ ์์ฑ
|
| 70 |
+
# ์๋ ์กฐ์ ๊ฐ๋ฅ (speed)
|
| 71 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_src_file:
|
| 72 |
+
source_path = temp_src_file.name
|
| 73 |
+
|
| 74 |
+
melotts_model.tts_to_file(text, speaker_ids['KR'], source_path, speed=1.0)
|
| 75 |
+
|
| 76 |
+
# 4. OpenVoice๋ฅผ ์ฌ์ฉํด ๊ธฐ๋ณธ ์์ฑ์ ์ถ์ถํ ์์์ ์
ํ (๋ณํ)
|
| 77 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_save_file:
|
| 78 |
+
save_path = temp_save_file.name
|
| 79 |
+
|
| 80 |
+
# ํต์ฌ ๋ณํ ๊ณผ์
|
| 81 |
+
tone_color_converter.convert(
|
| 82 |
+
audio_src_path=source_path,
|
| 83 |
+
src_se=None, # ์์ค ์์ฑ์ ํน์ง์ ์ฌ์ฉ ์ ํจ
|
| 84 |
+
tgt_se=target_se, # ๋ชฉํ(์ฐธ์กฐ) ์์ฑ์ ํน์ง์ ์ฌ์ฉ
|
| 85 |
+
output_path=save_path,
|
| 86 |
+
message="@MyShell"
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
# 5. ์์ฑ๋ ํ์ผ์ ์ฝ์ด ์คํธ๋ฆฌ๋ฐ์ผ๋ก ๋ฐํ
|
| 90 |
+
with open(save_path, 'rb') as f:
|
| 91 |
+
audio_data = f.read()
|
| 92 |
+
|
| 93 |
+
return StreamingResponse(
|
| 94 |
+
io.BytesIO(audio_data),
|
| 95 |
+
media_type="audio/wav",
|
| 96 |
+
headers={"Content-Disposition": "inline; filename=cloned_speech.wav"}
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
except Exception as e:
|
| 100 |
+
error_msg = f"Error during speech generation: {str(e)}"
|
| 101 |
+
print(f"โ {error_msg}")
|
| 102 |
+
raise HTTPException(status_code=500, detail=error_msg)
|
| 103 |
+
|
| 104 |
+
finally:
|
| 105 |
+
# 6. ์์
์ด ๋๋๋ฉด ๋ชจ๋ ์์ ํ์ผ ์ญ์
|
| 106 |
+
for path in [reference_path, source_path, save_path]:
|
| 107 |
+
if path and os.path.exists(path):
|
| 108 |
+
os.remove(path)
|