Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,34 @@
|
|
| 1 |
-
import os
|
| 2 |
import builtins
|
| 3 |
-
import
|
| 4 |
-
import io
|
| 5 |
-
import time # 🔴 新增
|
| 6 |
-
import uvicorn
|
| 7 |
-
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
| 8 |
-
from fastapi.responses import StreamingResponse
|
| 9 |
-
import genie_tts
|
| 10 |
-
from huggingface_hub import snapshot_download
|
| 11 |
|
| 12 |
-
#
|
| 13 |
builtins.input = lambda prompt="": "y"
|
| 14 |
os.environ["GENIE_DATA_DIR"] = "/app/GenieData"
|
| 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 |
DEFAULT_REF_PATH = "/app/ref.wav"
|
| 22 |
DEFAULT_REF_TEXT = "琴是个称职的好团长。看到她认真工作的样子,就连我也忍不住想要多帮她一把。"
|
| 23 |
|
|
|
|
| 24 |
genie_tts.load_character("Default", "/app", "zh")
|
| 25 |
|
| 26 |
@app.post("/upload_and_tts")
|
|
@@ -31,40 +39,43 @@ async def upload_and_tts(
|
|
| 31 |
file: UploadFile = File(...)
|
| 32 |
):
|
| 33 |
try:
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
save_path = f"/app/ref_{
|
| 37 |
|
| 38 |
with open(save_path, "wb") as buffer:
|
| 39 |
shutil.copyfileobj(file.file, buffer)
|
| 40 |
|
| 41 |
-
print(f"🔥 [
|
| 42 |
-
|
| 43 |
-
# 马上设置新参考
|
| 44 |
genie_tts.set_reference_audio("Default", save_path, prompt_text, language)
|
| 45 |
|
| 46 |
-
out_path = f"/app/out_{
|
| 47 |
genie_tts.tts("Default", text, save_path=out_path, play=False)
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
# 我们返回后再删
|
| 51 |
def iterfile():
|
| 52 |
with open(out_path, "rb") as f:
|
| 53 |
yield from f
|
| 54 |
-
#
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
return StreamingResponse(iterfile(), media_type="audio/wav")
|
| 57 |
except Exception as e:
|
| 58 |
-
print(f"❌ Error: {e}")
|
| 59 |
raise HTTPException(status_code=500, detail=str(e))
|
| 60 |
|
| 61 |
@app.post("/tts")
|
| 62 |
async def safe_tts(data: dict):
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
| 1 |
import builtins
|
| 2 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# 🔴 核心:在所有 import 之前,必须先劫持 input,否则导入 genie_tts 时会因为 input 阻塞导致崩溃
|
| 5 |
builtins.input = lambda prompt="": "y"
|
| 6 |
os.environ["GENIE_DATA_DIR"] = "/app/GenieData"
|
| 7 |
|
| 8 |
+
# 🔴 核心:在导入之前,必须先确保数据已下载
|
| 9 |
+
from huggingface_hub import snapshot_download
|
| 10 |
if not os.path.exists("/app/GenieData/G2P"):
|
| 11 |
+
print("📦 Downloading GenieData Assets...")
|
| 12 |
snapshot_download(repo_id="High-Logic/Genie", allow_patterns=["GenieData/*"], local_dir="/app", local_dir_use_symlinks=False)
|
| 13 |
|
| 14 |
+
# 现在可以安全地 import 了
|
| 15 |
+
import shutil
|
| 16 |
+
import io
|
| 17 |
+
import time
|
| 18 |
+
import uvicorn
|
| 19 |
+
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
| 20 |
+
from fastapi.responses import StreamingResponse
|
| 21 |
+
import genie_tts
|
| 22 |
+
|
| 23 |
+
# ---------------------------------------------------------
|
| 24 |
+
# 服务器设置
|
| 25 |
+
# ---------------------------------------------------------
|
| 26 |
app = FastAPI()
|
| 27 |
|
| 28 |
DEFAULT_REF_PATH = "/app/ref.wav"
|
| 29 |
DEFAULT_REF_TEXT = "琴是个称职的好团长。看到她认真工作的样子,就连我也忍不住想要多帮她一把。"
|
| 30 |
|
| 31 |
+
print("⚡ Starting Genie Engine...")
|
| 32 |
genie_tts.load_character("Default", "/app", "zh")
|
| 33 |
|
| 34 |
@app.post("/upload_and_tts")
|
|
|
|
| 39 |
file: UploadFile = File(...)
|
| 40 |
):
|
| 41 |
try:
|
| 42 |
+
# 使用时间戳文件名强行刷新缓存
|
| 43 |
+
ts = int(time.time() * 1000)
|
| 44 |
+
save_path = f"/app/ref_{ts}.wav"
|
| 45 |
|
| 46 |
with open(save_path, "wb") as buffer:
|
| 47 |
shutil.copyfileobj(file.file, buffer)
|
| 48 |
|
| 49 |
+
print(f"🔥 [Custom] Loaded new unique audio: {save_path}")
|
|
|
|
|
|
|
| 50 |
genie_tts.set_reference_audio("Default", save_path, prompt_text, language)
|
| 51 |
|
| 52 |
+
out_path = f"/app/out_{ts}.wav"
|
| 53 |
genie_tts.tts("Default", text, save_path=out_path, play=False)
|
| 54 |
|
| 55 |
+
# 定义生成器,在发送完后尝试清理临时文件(可选)
|
|
|
|
| 56 |
def iterfile():
|
| 57 |
with open(out_path, "rb") as f:
|
| 58 |
yield from f
|
| 59 |
+
# 这里的清理可以防止占用过多存储空间
|
| 60 |
+
try: os.remove(save_path); os.remove(out_path)
|
| 61 |
+
except: pass
|
| 62 |
+
|
| 63 |
return StreamingResponse(iterfile(), media_type="audio/wav")
|
| 64 |
except Exception as e:
|
| 65 |
+
print(f"❌ Error in upload/tts: {e}")
|
| 66 |
raise HTTPException(status_code=500, detail=str(e))
|
| 67 |
|
| 68 |
@app.post("/tts")
|
| 69 |
async def safe_tts(data: dict):
|
| 70 |
+
print(f"🛡️ [Reset] Back to standard Qin voice.")
|
| 71 |
+
try:
|
| 72 |
+
genie_tts.set_reference_audio("Default", DEFAULT_REF_PATH, DEFAULT_REF_TEXT, "zh")
|
| 73 |
+
out_path = "/app/out_std.wav"
|
| 74 |
+
genie_tts.tts("Default", data.get("text", ""), save_path=out_path, play=False)
|
| 75 |
+
return StreamingResponse(open(out_path, "rb"), media_type="audio/wav")
|
| 76 |
+
except Exception as e:
|
| 77 |
+
print(f"❌ Error in std tts: {e}")
|
| 78 |
+
raise HTTPException(status_code=404, detail=str(e))
|
| 79 |
|
| 80 |
if __name__ == "__main__":
|
| 81 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|