Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,38 @@
|
|
| 1 |
import builtins
|
| 2 |
import os
|
| 3 |
import shutil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# 1.
|
| 6 |
builtins.input = lambda prompt="": "y"
|
| 7 |
|
| 8 |
-
# 2.
|
| 9 |
os.environ["GENIE_DATA_DIR"] = "/app/GenieData"
|
| 10 |
|
| 11 |
-
# 3.
|
| 12 |
-
from huggingface_hub import snapshot_download
|
| 13 |
if not os.path.exists("/app/GenieData/G2P"):
|
| 14 |
print("📦 Downloading GenieData Dependencies...")
|
| 15 |
-
snapshot_download(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
@
|
| 24 |
async def upload_and_set(
|
| 25 |
character_name: str = Form("Default"),
|
| 26 |
prompt_text: str = Form(...),
|
|
@@ -28,7 +40,6 @@ async def upload_and_set(
|
|
| 28 |
file: UploadFile = File(...)
|
| 29 |
):
|
| 30 |
char_name = character_name.capitalize()
|
| 31 |
-
# 存放在固定位置
|
| 32 |
save_path = f"/app/uploaded_ref.wav"
|
| 33 |
|
| 34 |
with open(save_path, "wb") as buffer:
|
|
@@ -36,24 +47,40 @@ async def upload_and_set(
|
|
| 36 |
|
| 37 |
print(f"🔄 Setting reference audio for {char_name}: {prompt_text}")
|
| 38 |
try:
|
| 39 |
-
# 🟢 调用官方顶级 API,这是最稳妥的
|
| 40 |
genie_tts.set_reference_audio(char_name, save_path, prompt_text, language)
|
| 41 |
return {"message": f"Successfully updated style for {char_name}"}
|
| 42 |
except Exception as e:
|
| 43 |
print(f"❌ Error: {e}")
|
| 44 |
-
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
@
|
| 48 |
-
async def
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
| 53 |
try:
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
except Exception as e:
|
| 56 |
-
print(f"
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
| 1 |
import builtins
|
| 2 |
import os
|
| 3 |
import shutil
|
| 4 |
+
import io
|
| 5 |
+
import uvicorn
|
| 6 |
+
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
| 7 |
+
from fastapi.responses import StreamingResponse
|
| 8 |
+
import genie_tts
|
| 9 |
+
from huggingface_hub import snapshot_download
|
| 10 |
|
| 11 |
+
# 1. 劫持交互式输入,防止部署卡死
|
| 12 |
builtins.input = lambda prompt="": "y"
|
| 13 |
|
| 14 |
+
# 2. 环境变量设置
|
| 15 |
os.environ["GENIE_DATA_DIR"] = "/app/GenieData"
|
| 16 |
|
| 17 |
+
# 3. 手动下载核心权重(G2P, HuBERT 等)
|
|
|
|
| 18 |
if not os.path.exists("/app/GenieData/G2P"):
|
| 19 |
print("📦 Downloading GenieData Dependencies...")
|
| 20 |
+
snapshot_download(
|
| 21 |
+
repo_id="High-Logic/Genie",
|
| 22 |
+
allow_patterns=["GenieData/*"],
|
| 23 |
+
local_dir="/app",
|
| 24 |
+
local_dir_use_symlinks=False
|
| 25 |
+
)
|
| 26 |
|
| 27 |
+
# 创建独立的 FastAPI 应用
|
| 28 |
+
app = FastAPI(title="Genie-TTS Custom Server")
|
| 29 |
+
|
| 30 |
+
# 启动时预载角色模型
|
| 31 |
+
print("⚡ Loading Default Character...")
|
| 32 |
+
genie_tts.load_character("Default", "/app", "zh")
|
| 33 |
|
| 34 |
+
# 自定义上传并设置端点
|
| 35 |
+
@app.post("/upload_and_set")
|
| 36 |
async def upload_and_set(
|
| 37 |
character_name: str = Form("Default"),
|
| 38 |
prompt_text: str = Form(...),
|
|
|
|
| 40 |
file: UploadFile = File(...)
|
| 41 |
):
|
| 42 |
char_name = character_name.capitalize()
|
|
|
|
| 43 |
save_path = f"/app/uploaded_ref.wav"
|
| 44 |
|
| 45 |
with open(save_path, "wb") as buffer:
|
|
|
|
| 47 |
|
| 48 |
print(f"🔄 Setting reference audio for {char_name}: {prompt_text}")
|
| 49 |
try:
|
|
|
|
| 50 |
genie_tts.set_reference_audio(char_name, save_path, prompt_text, language)
|
| 51 |
return {"message": f"Successfully updated style for {char_name}"}
|
| 52 |
except Exception as e:
|
| 53 |
print(f"❌ Error: {e}")
|
| 54 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 55 |
|
| 56 |
+
# 手动接管 TTS 合成逻辑
|
| 57 |
+
@app.post("/tts")
|
| 58 |
+
async def tts_endpoint(data: dict):
|
| 59 |
+
char_name = data.get("character_name", "Default").capitalize()
|
| 60 |
+
text = data.get("text", "")
|
| 61 |
+
|
| 62 |
+
print(f"🎙️ Generating TTS for {char_name}: {text[:20]}...")
|
| 63 |
+
|
| 64 |
try:
|
| 65 |
+
temp_out = "/app/temp_tts_out.wav"
|
| 66 |
+
# 直接调用推理核心
|
| 67 |
+
genie_tts.tts(
|
| 68 |
+
character_name=char_name,
|
| 69 |
+
text=text,
|
| 70 |
+
save_path=temp_out,
|
| 71 |
+
play=False
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
if os.path.exists(temp_out):
|
| 75 |
+
with open(temp_out, "rb") as f:
|
| 76 |
+
return StreamingResponse(io.BytesIO(f.read()), media_type="audio/wav")
|
| 77 |
+
else:
|
| 78 |
+
raise Exception("Generated file not found")
|
| 79 |
+
|
| 80 |
except Exception as e:
|
| 81 |
+
print(f"❌ TTS Error: {e}")
|
| 82 |
+
raise HTTPException(status_code=404, detail=str(e))
|
| 83 |
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
# 在 Space 要求的 7860 端口启动
|
| 86 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|