Spaces:
Running
on
A10G
Running
on
A10G
Upload 9 files
Browse files
main.py
CHANGED
|
@@ -1,17 +1,16 @@
|
|
| 1 |
"""
|
| 2 |
MagicPath AI Vocal Effects Server - DiffVox LLM ํตํฉ ๋ฒ์
|
| 3 |
=========================================================
|
| 4 |
-
Dry ๋ณด์ปฌ ํ์ผ์ ๋ฐ์์ ํ์ต๋ AI๊ฐ ์ดํํฐ ํ๋ผ๋ฏธํฐ๋ฅผ ์์ธกํ๊ณ ,
|
| 5 |
-
์ค์ ๋ก ์ดํํธ๋ฅผ ์ ์ฉํ ์ค๋์ค๋ฅผ ๋ฐํํ๋ ์๋ฒ
|
| 6 |
"""
|
| 7 |
|
| 8 |
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
| 9 |
from fastapi.middleware.cors import CORSMiddleware
|
| 10 |
from fastapi.responses import FileResponse, JSONResponse
|
|
|
|
| 11 |
import tempfile
|
| 12 |
import os
|
| 13 |
import uuid
|
| 14 |
-
|
| 15 |
|
| 16 |
# ๋ด๋ถ ๋ชจ๋
|
| 17 |
from models.ai_effector import AIEffector
|
|
@@ -21,12 +20,16 @@ from audio_processing.effect_chain import EffectChain
|
|
| 21 |
# ์ค์
|
| 22 |
# ============================================
|
| 23 |
|
| 24 |
-
# ํ์ต๋ ๋ชจ๋ธ ๊ฒฝ๋ก
|
| 25 |
-
MODEL_PATH = os.environ.get("DIFFVOX_MODEL_PATH", "heybaeheef/KU_SW_Academy")
|
| 26 |
BASE_MODEL_NAME = os.environ.get("BASE_MODEL_NAME", "Qwen/Qwen3-8B")
|
| 27 |
AUDIO_FEATURE_DIM = int(os.environ.get("AUDIO_FEATURE_DIM", "64"))
|
| 28 |
USE_HUGGINGFACE = os.environ.get("USE_HUGGINGFACE", "true").lower() == "true"
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
# ============================================
|
| 31 |
# FastAPI ์ฑ ์ด๊ธฐํ
|
| 32 |
# ============================================
|
|
@@ -40,7 +43,7 @@ app = FastAPI(
|
|
| 40 |
# CORS ์ค์
|
| 41 |
app.add_middleware(
|
| 42 |
CORSMiddleware,
|
| 43 |
-
allow_origins=["*"],
|
| 44 |
allow_credentials=True,
|
| 45 |
allow_methods=["*"],
|
| 46 |
allow_headers=["*"],
|
|
@@ -64,10 +67,6 @@ ai_effector = AIEffector(
|
|
| 64 |
)
|
| 65 |
effect_chain = EffectChain()
|
| 66 |
|
| 67 |
-
# ์์ ํ์ผ ์ ์ฅ ๊ฒฝ๋ก
|
| 68 |
-
TEMP_DIR = Path(tempfile.gettempdir()) / "magicpath"
|
| 69 |
-
TEMP_DIR.mkdir(exist_ok=True)
|
| 70 |
-
|
| 71 |
|
| 72 |
# ============================================
|
| 73 |
# API ์๋ํฌ์ธํธ
|
|
@@ -80,9 +79,11 @@ async def root():
|
|
| 80 |
"status": "running",
|
| 81 |
"message": "MagicPath AI Vocal Effects Server v2.0 (DiffVox LLM)",
|
| 82 |
"ai_model_loaded": ai_effector.is_loaded(),
|
|
|
|
| 83 |
"endpoints": {
|
| 84 |
"POST /process": "์ค๋์ค ํ์ผ ์ฒ๋ฆฌ ํ ๋ฐํ",
|
| 85 |
"POST /predict": "ํ๋ผ๋ฏธํฐ๋ง ์์ธก (JSON)",
|
|
|
|
| 86 |
"GET /health": "์๋ฒ ์ํ ํ์ธ"
|
| 87 |
}
|
| 88 |
}
|
|
@@ -105,28 +106,18 @@ async def predict_parameters(
|
|
| 105 |
audio: UploadFile = File(..., description="Dry ๋ณด์ปฌ ์ค๋์ค ํ์ผ"),
|
| 106 |
prompt: str = Form("", description="ํ
์คํธ ๋ช
๋ น (์: 'warm', 'bright')")
|
| 107 |
):
|
| 108 |
-
"""
|
| 109 |
-
AI ๋ชจ๋ธ๋ก ์ดํํฐ ํ๋ผ๋ฏธํฐ ์์ธก (์ค๋์ค ์ฒ๋ฆฌ ์์ด)
|
| 110 |
-
|
| 111 |
-
- audio: wav, mp3 ๋ฑ ์ค๋์ค ํ์ผ
|
| 112 |
-
- prompt: ์ํ๋ ์ฌ์ด๋ ์ค๋ช
|
| 113 |
-
|
| 114 |
-
Returns: ์์ธก๋ ์ดํํฐ ํ๋ผ๋ฏธํฐ JSON
|
| 115 |
-
"""
|
| 116 |
try:
|
| 117 |
-
# ์์ ํ์ผ๋ก ์ ์ฅ
|
| 118 |
input_path = TEMP_DIR / f"{uuid.uuid4()}_{audio.filename}"
|
| 119 |
with open(input_path, "wb") as f:
|
| 120 |
content = await audio.read()
|
| 121 |
f.write(content)
|
| 122 |
|
| 123 |
-
# AI ๋ชจ๋ธ๋ก ํ๋ผ๋ฏธํฐ ์์ธก
|
| 124 |
parameters = ai_effector.predict(
|
| 125 |
audio_path=str(input_path),
|
| 126 |
text_prompt=prompt
|
| 127 |
)
|
| 128 |
|
| 129 |
-
# ์์ ํ์ผ ์ญ์
|
| 130 |
os.remove(input_path)
|
| 131 |
|
| 132 |
return JSONResponse(content={
|
|
@@ -145,50 +136,32 @@ async def process_audio(
|
|
| 145 |
audio: UploadFile = File(..., description="Dry ๋ณด์ปฌ ์ค๋์ค ํ์ผ"),
|
| 146 |
prompt: str = Form("", description="ํ
์คํธ ๋ช
๋ น (์: 'warm', 'bright')")
|
| 147 |
):
|
| 148 |
-
"""
|
| 149 |
-
AI๊ฐ ์์ธกํ ํ๋ผ๋ฏธํฐ๋ก ์ค์ ์ค๋์ค ์ฒ๋ฆฌ
|
| 150 |
-
|
| 151 |
-
- audio: wav, mp3 ๋ฑ ์ค๋์ค ํ์ผ
|
| 152 |
-
- prompt: ์ํ๋ ์ฌ์ด๋ ์ค๋ช
|
| 153 |
-
|
| 154 |
-
Returns: ์ฒ๋ฆฌ๋ ์ค๋์ค ํ์ผ (wav)
|
| 155 |
-
"""
|
| 156 |
input_path = None
|
| 157 |
output_path = None
|
| 158 |
|
| 159 |
try:
|
| 160 |
-
# ์์ ํ์ผ ๊ฒฝ๋ก ์์ฑ
|
| 161 |
file_id = str(uuid.uuid4())
|
| 162 |
input_path = TEMP_DIR / f"{file_id}_input_{audio.filename}"
|
| 163 |
output_path = TEMP_DIR / f"{file_id}_output.wav"
|
| 164 |
|
| 165 |
-
# ์
๋ ฅ ํ์ผ ์ ์ฅ
|
| 166 |
with open(input_path, "wb") as f:
|
| 167 |
content = await audio.read()
|
| 168 |
f.write(content)
|
| 169 |
|
| 170 |
-
print(f"[Process] ์
๋ ฅ ํ์ผ: {input_path}")
|
| 171 |
-
print(f"[Process] ํ๋กฌํํธ: {prompt}")
|
| 172 |
-
|
| 173 |
-
# Step 1: AI ๋ชจ๋ธ๋ก ํ๋ผ๋ฏธํฐ ์์ธก
|
| 174 |
parameters = ai_effector.predict(
|
| 175 |
audio_path=str(input_path),
|
| 176 |
text_prompt=prompt
|
| 177 |
)
|
| 178 |
|
| 179 |
-
print(f"[Process] ์์ธก๋ ํ๋ผ๋ฏธํฐ: {len(parameters)}๊ฐ")
|
| 180 |
-
|
| 181 |
-
# Step 2: ์ดํํฐ ์ฒด์ธ๏ฟฝ๏ฟฝ๏ฟฝ๋ก ์ค๋์ค ์ฒ๋ฆฌ
|
| 182 |
effect_chain.process(
|
| 183 |
input_path=str(input_path),
|
| 184 |
output_path=str(output_path),
|
| 185 |
parameters=parameters
|
| 186 |
)
|
| 187 |
|
| 188 |
-
# ์
๋ ฅ ํ์ผ ์ญ์
|
| 189 |
os.remove(input_path)
|
| 190 |
|
| 191 |
-
# ์ฒ๋ฆฌ๋ ์ค๋์ค ๋ฐํ
|
| 192 |
return FileResponse(
|
| 193 |
path=str(output_path),
|
| 194 |
media_type="audio/wav",
|
|
@@ -197,15 +170,10 @@ async def process_audio(
|
|
| 197 |
)
|
| 198 |
|
| 199 |
except Exception as e:
|
| 200 |
-
|
| 201 |
-
if input_path and input_path.exists():
|
| 202 |
os.remove(input_path)
|
| 203 |
-
if output_path and output_path.exists():
|
| 204 |
os.remove(output_path)
|
| 205 |
-
|
| 206 |
-
print(f"[Process] โ ์๋ฌ: {e}")
|
| 207 |
-
import traceback
|
| 208 |
-
traceback.print_exc()
|
| 209 |
raise HTTPException(status_code=500, detail=str(e))
|
| 210 |
|
| 211 |
|
|
@@ -214,11 +182,7 @@ async def process_audio_with_params(
|
|
| 214 |
audio: UploadFile = File(..., description="Dry ๋ณด์ปฌ ์ค๋์ค ํ์ผ"),
|
| 215 |
prompt: str = Form("", description="ํ
์คํธ ๋ช
๋ น")
|
| 216 |
):
|
| 217 |
-
"""
|
| 218 |
-
์ค๋์ค ์ฒ๋ฆฌ + ์ฌ์ฉ๋ ํ๋ผ๋ฏธํฐ๋ ํจ๊ป ๋ฐํ
|
| 219 |
-
|
| 220 |
-
Returns: JSON (์ฒ๋ฆฌ๋ ์ค๋์ค URL + ํ๋ผ๋ฏธํฐ)
|
| 221 |
-
"""
|
| 222 |
input_path = None
|
| 223 |
output_path = None
|
| 224 |
|
|
@@ -231,13 +195,11 @@ async def process_audio_with_params(
|
|
| 231 |
content = await audio.read()
|
| 232 |
f.write(content)
|
| 233 |
|
| 234 |
-
# AI ํ๋ผ๋ฏธํฐ ์์ธก
|
| 235 |
parameters = ai_effector.predict(
|
| 236 |
audio_path=str(input_path),
|
| 237 |
text_prompt=prompt
|
| 238 |
)
|
| 239 |
|
| 240 |
-
# ์ค๋์ค ์ฒ๋ฆฌ
|
| 241 |
effect_chain.process(
|
| 242 |
input_path=str(input_path),
|
| 243 |
output_path=str(output_path),
|
|
@@ -246,8 +208,6 @@ async def process_audio_with_params(
|
|
| 246 |
|
| 247 |
os.remove(input_path)
|
| 248 |
|
| 249 |
-
# Base64 ์ธ์ฝ๋ฉ์ผ๋ก ์ค๋์ค ๋ฐํ (๋๋ URL)
|
| 250 |
-
import base64
|
| 251 |
with open(output_path, "rb") as f:
|
| 252 |
audio_base64 = base64.b64encode(f.read()).decode('utf-8')
|
| 253 |
|
|
@@ -263,13 +223,13 @@ async def process_audio_with_params(
|
|
| 263 |
})
|
| 264 |
|
| 265 |
except Exception as e:
|
| 266 |
-
if input_path and input_path.exists():
|
| 267 |
os.remove(input_path)
|
| 268 |
-
if output_path and output_path.exists():
|
| 269 |
os.remove(output_path)
|
| 270 |
raise HTTPException(status_code=500, detail=str(e))
|
| 271 |
|
| 272 |
|
| 273 |
if __name__ == "__main__":
|
| 274 |
import uvicorn
|
| 275 |
-
uvicorn.run(app, host="0.0.0.0", port=
|
|
|
|
| 1 |
"""
|
| 2 |
MagicPath AI Vocal Effects Server - DiffVox LLM ํตํฉ ๋ฒ์
|
| 3 |
=========================================================
|
|
|
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
from fastapi.responses import FileResponse, JSONResponse
|
| 9 |
+
from pathlib import Path
|
| 10 |
import tempfile
|
| 11 |
import os
|
| 12 |
import uuid
|
| 13 |
+
import base64
|
| 14 |
|
| 15 |
# ๋ด๋ถ ๋ชจ๋
|
| 16 |
from models.ai_effector import AIEffector
|
|
|
|
| 20 |
# ์ค์
|
| 21 |
# ============================================
|
| 22 |
|
| 23 |
+
# ํ์ต๋ ๋ชจ๋ธ ๊ฒฝ๋ก - checkpoints ํด๋ ํฌํจ!
|
| 24 |
+
MODEL_PATH = os.environ.get("DIFFVOX_MODEL_PATH", "heybaeheef/KU_SW_Academy/checkpoints")
|
| 25 |
BASE_MODEL_NAME = os.environ.get("BASE_MODEL_NAME", "Qwen/Qwen3-8B")
|
| 26 |
AUDIO_FEATURE_DIM = int(os.environ.get("AUDIO_FEATURE_DIM", "64"))
|
| 27 |
USE_HUGGINGFACE = os.environ.get("USE_HUGGINGFACE", "true").lower() == "true"
|
| 28 |
|
| 29 |
+
# ์์ ํ์ผ ์ ์ฅ ๊ฒฝ๋ก - ๋จผ์ ์ ์
|
| 30 |
+
TEMP_DIR = Path(tempfile.gettempdir()) / "magicpath"
|
| 31 |
+
TEMP_DIR.mkdir(exist_ok=True)
|
| 32 |
+
|
| 33 |
# ============================================
|
| 34 |
# FastAPI ์ฑ ์ด๊ธฐํ
|
| 35 |
# ============================================
|
|
|
|
| 43 |
# CORS ์ค์
|
| 44 |
app.add_middleware(
|
| 45 |
CORSMiddleware,
|
| 46 |
+
allow_origins=["*"],
|
| 47 |
allow_credentials=True,
|
| 48 |
allow_methods=["*"],
|
| 49 |
allow_headers=["*"],
|
|
|
|
| 67 |
)
|
| 68 |
effect_chain = EffectChain()
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
# ============================================
|
| 72 |
# API ์๋ํฌ์ธํธ
|
|
|
|
| 79 |
"status": "running",
|
| 80 |
"message": "MagicPath AI Vocal Effects Server v2.0 (DiffVox LLM)",
|
| 81 |
"ai_model_loaded": ai_effector.is_loaded(),
|
| 82 |
+
"model_path": MODEL_PATH,
|
| 83 |
"endpoints": {
|
| 84 |
"POST /process": "์ค๋์ค ํ์ผ ์ฒ๋ฆฌ ํ ๋ฐํ",
|
| 85 |
"POST /predict": "ํ๋ผ๋ฏธํฐ๋ง ์์ธก (JSON)",
|
| 86 |
+
"POST /process_with_params": "์ค๋์ค ์ฒ๋ฆฌ + ํ๋ผ๋ฏธํฐ ๋ฐํ",
|
| 87 |
"GET /health": "์๋ฒ ์ํ ํ์ธ"
|
| 88 |
}
|
| 89 |
}
|
|
|
|
| 106 |
audio: UploadFile = File(..., description="Dry ๋ณด์ปฌ ์ค๋์ค ํ์ผ"),
|
| 107 |
prompt: str = Form("", description="ํ
์คํธ ๋ช
๋ น (์: 'warm', 'bright')")
|
| 108 |
):
|
| 109 |
+
"""AI ๋ชจ๋ธ๋ก ์ดํํฐ ํ๋ผ๋ฏธํฐ ์์ธก"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
try:
|
|
|
|
| 111 |
input_path = TEMP_DIR / f"{uuid.uuid4()}_{audio.filename}"
|
| 112 |
with open(input_path, "wb") as f:
|
| 113 |
content = await audio.read()
|
| 114 |
f.write(content)
|
| 115 |
|
|
|
|
| 116 |
parameters = ai_effector.predict(
|
| 117 |
audio_path=str(input_path),
|
| 118 |
text_prompt=prompt
|
| 119 |
)
|
| 120 |
|
|
|
|
| 121 |
os.remove(input_path)
|
| 122 |
|
| 123 |
return JSONResponse(content={
|
|
|
|
| 136 |
audio: UploadFile = File(..., description="Dry ๋ณด์ปฌ ์ค๋์ค ํ์ผ"),
|
| 137 |
prompt: str = Form("", description="ํ
์คํธ ๋ช
๋ น (์: 'warm', 'bright')")
|
| 138 |
):
|
| 139 |
+
"""AI๊ฐ ์์ธกํ ํ๋ผ๋ฏธํฐ๋ก ์ค์ ์ค๋์ค ์ฒ๋ฆฌ"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
input_path = None
|
| 141 |
output_path = None
|
| 142 |
|
| 143 |
try:
|
|
|
|
| 144 |
file_id = str(uuid.uuid4())
|
| 145 |
input_path = TEMP_DIR / f"{file_id}_input_{audio.filename}"
|
| 146 |
output_path = TEMP_DIR / f"{file_id}_output.wav"
|
| 147 |
|
|
|
|
| 148 |
with open(input_path, "wb") as f:
|
| 149 |
content = await audio.read()
|
| 150 |
f.write(content)
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
parameters = ai_effector.predict(
|
| 153 |
audio_path=str(input_path),
|
| 154 |
text_prompt=prompt
|
| 155 |
)
|
| 156 |
|
|
|
|
|
|
|
|
|
|
| 157 |
effect_chain.process(
|
| 158 |
input_path=str(input_path),
|
| 159 |
output_path=str(output_path),
|
| 160 |
parameters=parameters
|
| 161 |
)
|
| 162 |
|
|
|
|
| 163 |
os.remove(input_path)
|
| 164 |
|
|
|
|
| 165 |
return FileResponse(
|
| 166 |
path=str(output_path),
|
| 167 |
media_type="audio/wav",
|
|
|
|
| 170 |
)
|
| 171 |
|
| 172 |
except Exception as e:
|
| 173 |
+
if input_path and Path(input_path).exists():
|
|
|
|
| 174 |
os.remove(input_path)
|
| 175 |
+
if output_path and Path(output_path).exists():
|
| 176 |
os.remove(output_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
raise HTTPException(status_code=500, detail=str(e))
|
| 178 |
|
| 179 |
|
|
|
|
| 182 |
audio: UploadFile = File(..., description="Dry ๋ณด์ปฌ ์ค๋์ค ํ์ผ"),
|
| 183 |
prompt: str = Form("", description="ํ
์คํธ ๋ช
๋ น")
|
| 184 |
):
|
| 185 |
+
"""์ค๋์ค ์ฒ๋ฆฌ + ์ฌ์ฉ๋ ํ๋ผ๋ฏธํฐ๋ ํจ๊ป ๋ฐํ"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
input_path = None
|
| 187 |
output_path = None
|
| 188 |
|
|
|
|
| 195 |
content = await audio.read()
|
| 196 |
f.write(content)
|
| 197 |
|
|
|
|
| 198 |
parameters = ai_effector.predict(
|
| 199 |
audio_path=str(input_path),
|
| 200 |
text_prompt=prompt
|
| 201 |
)
|
| 202 |
|
|
|
|
| 203 |
effect_chain.process(
|
| 204 |
input_path=str(input_path),
|
| 205 |
output_path=str(output_path),
|
|
|
|
| 208 |
|
| 209 |
os.remove(input_path)
|
| 210 |
|
|
|
|
|
|
|
| 211 |
with open(output_path, "rb") as f:
|
| 212 |
audio_base64 = base64.b64encode(f.read()).decode('utf-8')
|
| 213 |
|
|
|
|
| 223 |
})
|
| 224 |
|
| 225 |
except Exception as e:
|
| 226 |
+
if input_path and Path(input_path).exists():
|
| 227 |
os.remove(input_path)
|
| 228 |
+
if output_path and Path(output_path).exists():
|
| 229 |
os.remove(output_path)
|
| 230 |
raise HTTPException(status_code=500, detail=str(e))
|
| 231 |
|
| 232 |
|
| 233 |
if __name__ == "__main__":
|
| 234 |
import uvicorn
|
| 235 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|