simler commited on
Commit
b473cde
·
verified ·
1 Parent(s): 69346ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -25
app.py CHANGED
@@ -1,26 +1,38 @@
1
  import builtins
2
  import os
3
  import shutil
 
 
 
 
 
 
4
 
5
- # 1. 劫持 input 防止崩溃
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(repo_id="High-Logic/Genie", allow_patterns=["GenieData/*"], local_dir="/app", local_dir_use_symlinks=False)
 
 
 
 
 
16
 
17
- # 4. 导入库
18
- from fastapi import UploadFile, File, Form
19
- import genie_tts
20
- from genie_tts.Server import app as genie_app
 
 
21
 
22
- # --- 核心:上传并设置参考音频 ---
23
- @genie_app.post("/upload_and_set")
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
- return {"error": str(e)}, 500
45
 
46
- # --- 极简调试:只看当前角色状态 ---
47
- @genie_app.get("/status")
48
- async def status():
49
- return {"status": "ok", "engine": "Genie-TTS V2 Pro Plus"}
50
-
51
- if __name__ == "__main__":
52
- # 启动前先预载角色模型
 
53
  try:
54
- genie_tts.load_character("Default", "/app", "zh")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  except Exception as e:
56
- print(f"Initial load error: {e}")
 
57
 
58
- # 启动 FastAPI 服务 (HF Space 固定 7860)
59
- genie_tts.start_server(host="0.0.0.0", port=7860)
 
 
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)