Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ from fastapi import FastAPI, File, UploadFile, Form, HTTPException
|
|
| 8 |
from fastapi.responses import JSONResponse
|
| 9 |
|
| 10 |
# ======================================================
|
| 11 |
-
# 🔐 基本設定
|
| 12 |
# ======================================================
|
| 13 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
| 14 |
APP_PASSWORD = os.environ.get("APP_PASSWORD")
|
|
@@ -16,6 +16,7 @@ APP_PASSWORD = os.environ.get("APP_PASSWORD")
|
|
| 16 |
print("===== 🚀 啟動中 =====")
|
| 17 |
print(f"OPENAI_API_KEY: {'✅ 已載入' if OPENAI_API_KEY else '❌ 未載入'}")
|
| 18 |
print(f"APP_PASSWORD: {'✅ 已載入' if APP_PASSWORD else '❌ 未載入'}")
|
|
|
|
| 19 |
|
| 20 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 21 |
MAX_SIZE = 25 * 1024 * 1024
|
|
@@ -53,7 +54,6 @@ def transcribe_core(path: str, model: str = "whisper-1"):
|
|
| 53 |
).choices[0].message.content.strip()
|
| 54 |
return full, summ
|
| 55 |
|
| 56 |
-
|
| 57 |
# ======================================================
|
| 58 |
# 🌐 FastAPI 主要端點
|
| 59 |
# ======================================================
|
|
@@ -68,7 +68,7 @@ async def api_transcribe(
|
|
| 68 |
file: UploadFile = File(...),
|
| 69 |
token: str = Form(default=None)
|
| 70 |
):
|
| 71 |
-
"""
|
| 72 |
if APP_PASSWORD and token != APP_PASSWORD:
|
| 73 |
raise HTTPException(status_code=403, detail="Forbidden: invalid token")
|
| 74 |
|
|
@@ -80,9 +80,8 @@ async def api_transcribe(
|
|
| 80 |
os.remove(temp)
|
| 81 |
return JSONResponse({"text": text, "summary": summary})
|
| 82 |
|
| 83 |
-
|
| 84 |
# ======================================================
|
| 85 |
-
# 💬 Gradio 介面
|
| 86 |
# ======================================================
|
| 87 |
def transcribe_with_pw(password, file):
|
| 88 |
if APP_PASSWORD and password.strip() != APP_PASSWORD:
|
|
@@ -92,7 +91,6 @@ def transcribe_with_pw(password, file):
|
|
| 92 |
text, summary = transcribe_core(file.name)
|
| 93 |
return "✅ 完成", text, summary
|
| 94 |
|
| 95 |
-
|
| 96 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 97 |
gr.Markdown("## 🎧 LINE 語音轉錄與摘要工具(支援 .m4a / .mp4)")
|
| 98 |
pw = gr.Textbox(label="輸入密碼", type="password")
|
|
@@ -103,7 +101,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 103 |
su = gr.Textbox(label="摘要", lines=8)
|
| 104 |
run.click(transcribe_with_pw, [pw, f], [s, t, su])
|
| 105 |
|
| 106 |
-
|
| 107 |
# ======================================================
|
| 108 |
# 🚀 關鍵啟動(讓 HF 正確偵測)
|
| 109 |
# ======================================================
|
|
|
|
| 8 |
from fastapi.responses import JSONResponse
|
| 9 |
|
| 10 |
# ======================================================
|
| 11 |
+
# 🔐 基本設定(支援 Secrets)
|
| 12 |
# ======================================================
|
| 13 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
| 14 |
APP_PASSWORD = os.environ.get("APP_PASSWORD")
|
|
|
|
| 16 |
print("===== 🚀 啟動中 =====")
|
| 17 |
print(f"OPENAI_API_KEY: {'✅ 已載入' if OPENAI_API_KEY else '❌ 未載入'}")
|
| 18 |
print(f"APP_PASSWORD: {'✅ 已載入' if APP_PASSWORD else '❌ 未載入'}")
|
| 19 |
+
print(APP_PASSWORD)
|
| 20 |
|
| 21 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 22 |
MAX_SIZE = 25 * 1024 * 1024
|
|
|
|
| 54 |
).choices[0].message.content.strip()
|
| 55 |
return full, summ
|
| 56 |
|
|
|
|
| 57 |
# ======================================================
|
| 58 |
# 🌐 FastAPI 主要端點
|
| 59 |
# ======================================================
|
|
|
|
| 68 |
file: UploadFile = File(...),
|
| 69 |
token: str = Form(default=None)
|
| 70 |
):
|
| 71 |
+
"""捷徑上傳音訊"""
|
| 72 |
if APP_PASSWORD and token != APP_PASSWORD:
|
| 73 |
raise HTTPException(status_code=403, detail="Forbidden: invalid token")
|
| 74 |
|
|
|
|
| 80 |
os.remove(temp)
|
| 81 |
return JSONResponse({"text": text, "summary": summary})
|
| 82 |
|
|
|
|
| 83 |
# ======================================================
|
| 84 |
+
# 💬 Gradio 介面
|
| 85 |
# ======================================================
|
| 86 |
def transcribe_with_pw(password, file):
|
| 87 |
if APP_PASSWORD and password.strip() != APP_PASSWORD:
|
|
|
|
| 91 |
text, summary = transcribe_core(file.name)
|
| 92 |
return "✅ 完成", text, summary
|
| 93 |
|
|
|
|
| 94 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 95 |
gr.Markdown("## 🎧 LINE 語音轉錄與摘要工具(支援 .m4a / .mp4)")
|
| 96 |
pw = gr.Textbox(label="輸入密碼", type="password")
|
|
|
|
| 101 |
su = gr.Textbox(label="摘要", lines=8)
|
| 102 |
run.click(transcribe_with_pw, [pw, f], [s, t, su])
|
| 103 |
|
|
|
|
| 104 |
# ======================================================
|
| 105 |
# 🚀 關鍵啟動(讓 HF 正確偵測)
|
| 106 |
# ======================================================
|