Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,14 @@ import shutil
|
|
| 4 |
from pydub import AudioSegment
|
| 5 |
from openai import OpenAI
|
| 6 |
import gradio as gr
|
| 7 |
-
from fastapi import
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# ======================================================
|
| 10 |
# 🔐 設定區
|
| 11 |
# ======================================================
|
| 12 |
-
PASSWORD = os.getenv("APP_PASSWORD", "
|
| 13 |
MAX_SIZE = 25 * 1024 * 1024
|
| 14 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 15 |
|
|
@@ -58,23 +60,20 @@ def transcribe_core(path, model="whisper-1"):
|
|
| 58 |
raw_parts.append(res)
|
| 59 |
full_raw = "\n".join(raw_parts)
|
| 60 |
|
| 61 |
-
# 簡轉繁
|
| 62 |
conv_prompt = (
|
| 63 |
"請將以下內容完整轉換為「繁體中文(台灣用語)」:\n"
|
| 64 |
-
"規則:1) 僅做簡→繁字形轉換;2) 不要意譯或改寫;3) 不要添加任何前後綴。\n"
|
| 65 |
-
"-----\n" + full_raw
|
| 66 |
)
|
| 67 |
trad_resp = client.chat.completions.create(
|
| 68 |
model="gpt-4o-mini",
|
| 69 |
messages=[
|
| 70 |
-
{"role": "system", "content": "
|
| 71 |
{"role": "user", "content": conv_prompt}
|
| 72 |
],
|
| 73 |
temperature=0.0,
|
| 74 |
)
|
| 75 |
full_trad = trad_resp.choices[0].message.content.strip()
|
| 76 |
|
| 77 |
-
# 摘要
|
| 78 |
sum_prompt = (
|
| 79 |
"請用台灣繁體中文撰寫摘要。若內容資訊多,可條列出重點;若內容簡短,請用一句話概述即可。\n\n"
|
| 80 |
+ full_trad
|
|
@@ -95,7 +94,7 @@ def transcribe_core(path, model="whisper-1"):
|
|
| 95 |
# ======================================================
|
| 96 |
def transcribe_with_password(password, file):
|
| 97 |
if password.strip() != PASSWORD:
|
| 98 |
-
|
| 99 |
if not file:
|
| 100 |
return "⚠️ 未選擇檔案", "", ""
|
| 101 |
text, summary = transcribe_core(file.name)
|
|
@@ -112,9 +111,30 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 112 |
run.click(transcribe_with_password, [pw, f], [s, t, su])
|
| 113 |
|
| 114 |
# ======================================================
|
| 115 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
# ======================================================
|
| 117 |
if __name__ == "__main__":
|
|
|
|
| 118 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 119 |
else:
|
|
|
|
| 120 |
demo.launch()
|
|
|
|
| 4 |
from pydub import AudioSegment
|
| 5 |
from openai import OpenAI
|
| 6 |
import gradio as gr
|
| 7 |
+
from fastapi import FastAPI, UploadFile, File, Form
|
| 8 |
+
import threading
|
| 9 |
+
import uvicorn
|
| 10 |
|
| 11 |
# ======================================================
|
| 12 |
# 🔐 設定區
|
| 13 |
# ======================================================
|
| 14 |
+
PASSWORD = os.getenv("APP_PASSWORD", "chou")
|
| 15 |
MAX_SIZE = 25 * 1024 * 1024
|
| 16 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 17 |
|
|
|
|
| 60 |
raw_parts.append(res)
|
| 61 |
full_raw = "\n".join(raw_parts)
|
| 62 |
|
|
|
|
| 63 |
conv_prompt = (
|
| 64 |
"請將以下內容完整轉換為「繁體中文(台灣用語)」:\n"
|
| 65 |
+
"規則:1) 僅做簡→繁字形轉換;2) 不要意譯或改寫;3) 不要添加任何前後綴。\n-----\n" + full_raw
|
|
|
|
| 66 |
)
|
| 67 |
trad_resp = client.chat.completions.create(
|
| 68 |
model="gpt-4o-mini",
|
| 69 |
messages=[
|
| 70 |
+
{"role": "system", "content": "你是嚴格的繁體中文轉換器。"},
|
| 71 |
{"role": "user", "content": conv_prompt}
|
| 72 |
],
|
| 73 |
temperature=0.0,
|
| 74 |
)
|
| 75 |
full_trad = trad_resp.choices[0].message.content.strip()
|
| 76 |
|
|
|
|
| 77 |
sum_prompt = (
|
| 78 |
"請用台灣繁體中文撰寫摘要。若內容資訊多,可條列出重點;若內容簡短,請用一句話概述即可。\n\n"
|
| 79 |
+ full_trad
|
|
|
|
| 94 |
# ======================================================
|
| 95 |
def transcribe_with_password(password, file):
|
| 96 |
if password.strip() != PASSWORD:
|
| 97 |
+
return "❌ 密碼錯誤", "", ""
|
| 98 |
if not file:
|
| 99 |
return "⚠️ 未選擇檔案", "", ""
|
| 100 |
text, summary = transcribe_core(file.name)
|
|
|
|
| 111 |
run.click(transcribe_with_password, [pw, f], [s, t, su])
|
| 112 |
|
| 113 |
# ======================================================
|
| 114 |
+
# 🌐 FastAPI for iPhone 捷徑
|
| 115 |
+
# ======================================================
|
| 116 |
+
api_app = FastAPI(title="LINE Transcription API")
|
| 117 |
+
|
| 118 |
+
@api_app.post("/api/transcribe")
|
| 119 |
+
async def api_transcribe(file: UploadFile = File(...), token: str = Form(...)):
|
| 120 |
+
if token != PASSWORD:
|
| 121 |
+
return {"error": "Invalid token"}
|
| 122 |
+
temp = file.filename
|
| 123 |
+
with open(temp, "wb") as f:
|
| 124 |
+
f.write(await file.read())
|
| 125 |
+
text, summary = transcribe_core(temp)
|
| 126 |
+
os.remove(temp)
|
| 127 |
+
return {"text": text, "summary": summary}
|
| 128 |
+
|
| 129 |
+
def run_api():
|
| 130 |
+
uvicorn.run(api_app, host="0.0.0.0", port=7861)
|
| 131 |
+
|
| 132 |
+
# ======================================================
|
| 133 |
+
# 🚀 啟動(同時啟 Gradio + FastAPI)
|
| 134 |
# ======================================================
|
| 135 |
if __name__ == "__main__":
|
| 136 |
+
threading.Thread(target=run_api, daemon=True).start()
|
| 137 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 138 |
else:
|
| 139 |
+
threading.Thread(target=run_api, daemon=True).start()
|
| 140 |
demo.launch()
|