simler commited on
Commit
d73f8f9
·
verified ·
1 Parent(s): 9ae828b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -32
app.py CHANGED
@@ -1,31 +1,52 @@
1
- import os
2
  import builtins
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- # --- 初期化屏蔽 ---
12
- builtins.input = lambda prompt="": "y"
13
- os.environ["GENIE_DATA_DIR"] = "/app/GenieData"
14
 
15
- # 下载权重
16
- if not os.path.exists("/app/GenieData/G2P"):
17
- snapshot_download(repo_id="High-Logic/Genie", allow_patterns=["GenieData/*"], local_dir="/app", local_dir_use_symlinks=False)
18
 
 
 
 
19
  app = FastAPI()
20
 
21
- # 默认参考设定 (云端原始琴团长)
22
  DEFAULT_REF_PATH = "/app/ref.wav"
23
  DEFAULT_REF_TEXT = "琴是个称职的好团长。看到她认真工作的样子,就连我也忍不住想要多帮她一把。"
24
 
25
- print("⚡ Starting Engine...")
26
  genie_tts.load_character("Default", "/app", "zh")
27
 
28
- # 🟢 端点 1上传立刻合 (且干扰默认值)
29
  @app.post("/upload_and_tts")
30
  async def upload_and_tts(
31
  prompt_text: str = Form(...),
@@ -34,37 +55,35 @@ async def upload_and_tts(
34
  file: UploadFile = File(...)
35
  ):
36
  try:
37
- # 1. 保存临时上传的音频
38
- temp_ref = f"/app/temp_upload_ref.wav"
39
- with open(temp_ref, "wb") as buffer:
40
  shutil.copyfileobj(file.file, buffer)
41
 
42
- # 2. 设置本次参考
43
- genie_tts.set_reference_audio("Default", temp_ref, prompt_text, language)
 
44
 
45
- # 3. 推理合成
46
- temp_out = "/app/temp_out.wav"
47
- genie_tts.tts("Default", text, save_path=temp_out, play=False)
48
-
49
- return StreamingResponse(open(temp_out, "rb"), media_type="audio/wav")
50
  except Exception as e:
51
  raise HTTPException(status_code=500, detail=str(e))
52
 
53
- # 🟢 端点 2普通 TTS (强制回始琴团长)
54
  @app.post("/tts")
55
  async def safe_tts(data: dict):
56
- text = data.get("text", "")
57
- print(f"🔄 Safety Check: Forcing reset to original Qin voice...")
58
 
59
  try:
60
- # 1. 强制重置回云端内置的 ref.wav
61
  genie_tts.set_reference_audio("Default", DEFAULT_REF_PATH, DEFAULT_REF_TEXT, "zh")
62
 
63
- # 2. 推理合成
64
- temp_out = "/app/temp_std_out.wav"
65
- genie_tts.tts("Default", text, save_path=temp_out, play=False)
66
-
67
- return StreamingResponse(open(temp_out, "rb"), media_type="audio/wav")
68
  except Exception as e:
69
  raise HTTPException(status_code=404, detail=str(e))
70
 
 
 
1
  import builtins
2
+ import os
3
+
4
+ # ==========================================================
5
+ # 🔴 第一步:最高优先级!必须在所有库导入之前执行
6
+ # ==========================================================
7
+ # 1. 强行让所有库的 input 调用失效并默认返回 "y"
8
+ builtins.input = lambda prompt="": "y"
9
+
10
+ # 2. 强制设置数据目录环境变量
11
+ os.environ["GENIE_DATA_DIR"] = "/app/GenieData"
12
+
13
+ # 3. 阻塞式执行下载,确保 genie_tts 导入时资源已就位
14
+ from huggingface_hub import snapshot_download
15
+ if not os.path.exists("/app/GenieData/G2P"):
16
+ print("📦 [Wait] Downloading core assets before library initialization...")
17
+ snapshot_download(
18
+ repo_id="High-Logic/Genie",
19
+ allow_patterns=["GenieData/*"],
20
+ local_dir="/app",
21
+ local_dir_use_symlinks=False
22
+ )
23
+ print("✅ [Ready] Assets downloaded.")
24
+
25
+ # ==========================================================
26
+ # 🔴 第二步:资源就位后,再进行第三方库导入
27
+ # ==========================================================
28
  import shutil
29
  import io
30
  import uvicorn
31
  from fastapi import FastAPI, UploadFile, File, Form, HTTPException
32
  from fastapi.responses import StreamingResponse
 
 
 
 
 
 
33
 
34
+ # 此时导入 genie_tts 是安全的,因为它会发现文件已存在
35
+ import genie_tts
 
36
 
37
+ # ==========================================================
38
+ # 🔴 第三步:服务器逻辑 (方案 B)
39
+ # ==========================================================
40
  app = FastAPI()
41
 
42
+ # 琴团长原始配置
43
  DEFAULT_REF_PATH = "/app/ref.wav"
44
  DEFAULT_REF_TEXT = "琴是个称职的好团长。看到她认真工作的样子,就连我也忍不住想要多帮她一把。"
45
 
46
+ print("⚡ Starting Genie Engine...")
47
  genie_tts.load_character("Default", "/app", "zh")
48
 
49
+ # 🟢 逻辑收到新音频则使用覆盖,完清理(由下一次请求决定是否重置)
50
  @app.post("/upload_and_tts")
51
  async def upload_and_tts(
52
  prompt_text: str = Form(...),
 
55
  file: UploadFile = File(...)
56
  ):
57
  try:
58
+ # 1. 保存临时音频
59
+ save_path = f"/app/uploaded_temp.wav"
60
+ with open(save_path, "wb") as buffer:
61
  shutil.copyfileobj(file.file, buffer)
62
 
63
+ # 2. 本次任务:使用新音频
64
+ print(f"🔥 [Custom] Using new audio for this request.")
65
+ genie_tts.set_reference_audio("Default", save_path, prompt_text, language)
66
 
67
+ # 3. 推理
68
+ out_path = "/app/out_custom.wav"
69
+ genie_tts.tts("Default", text, save_path=out_path, play=False)
70
+ return StreamingResponse(open(out_path, "rb"), media_type="audio/wav")
 
71
  except Exception as e:
72
  raise HTTPException(status_code=500, detail=str(e))
73
 
74
+ # 🟢 逻辑标准 TTS 请求,强制重置云端
75
  @app.post("/tts")
76
  async def safe_tts(data: dict):
77
+ target_text = data.get("text", "")
78
+ print(f"🛡️ [Reset] Forcing back to original Qin voice.")
79
 
80
  try:
81
+ # 重置回默认
82
  genie_tts.set_reference_audio("Default", DEFAULT_REF_PATH, DEFAULT_REF_TEXT, "zh")
83
 
84
+ out_path = "/app/out_standard.wav"
85
+ genie_tts.tts("Default", target_text, save_path=out_path, play=False)
86
+ return StreamingResponse(open(out_path, "rb"), media_type="audio/wav")
 
 
87
  except Exception as e:
88
  raise HTTPException(status_code=404, detail=str(e))
89