OK 版本 (無 Toekn 認證)
Browse files
app.py
CHANGED
|
@@ -1,80 +1,111 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
from
|
| 5 |
from openai import OpenAI
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
transcript = client.audio.transcriptions.create(model="whisper-1", file=f)
|
| 57 |
-
text = transcript.text.strip()
|
| 58 |
-
summary_prompt = f"請用繁體中文摘要以下內容:\n\n{text}"
|
| 59 |
-
summary = client.chat.completions.create(
|
| 60 |
model="gpt-4o-mini",
|
| 61 |
-
messages=[{"role": "user", "content":
|
|
|
|
| 62 |
).choices[0].message.content.strip()
|
| 63 |
-
return
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import time
|
| 3 |
+
import shutil
|
| 4 |
+
from pydub import AudioSegment
|
| 5 |
from openai import OpenAI
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from fastapi import FastAPI, File, UploadFile
|
| 8 |
+
|
| 9 |
+
# ========================
|
| 10 |
+
# 🔐 基本設定
|
| 11 |
+
# ========================
|
| 12 |
+
PASSWORD = os.getenv("APP_PASSWORD", "defaultpass")
|
| 13 |
+
MAX_SIZE = 25 * 1024 * 1024
|
| 14 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 15 |
+
|
| 16 |
+
# FastAPI App for捷徑 API
|
| 17 |
+
app = FastAPI()
|
| 18 |
+
|
| 19 |
+
# ========================
|
| 20 |
+
# 🎧 音訊轉錄核心
|
| 21 |
+
# ========================
|
| 22 |
+
def split_audio_if_needed(path: str):
|
| 23 |
+
size = os.path.getsize(path)
|
| 24 |
+
if size <= MAX_SIZE:
|
| 25 |
+
return [path]
|
| 26 |
+
audio = AudioSegment.from_file(path)
|
| 27 |
+
n = int(size / MAX_SIZE) + 1
|
| 28 |
+
chunk_ms = len(audio) / n
|
| 29 |
+
parts = []
|
| 30 |
+
for i in range(n):
|
| 31 |
+
fn = f"chunk_{i+1}.wav"
|
| 32 |
+
audio[int(i * chunk_ms):int((i + 1) * chunk_ms)].export(fn, format="wav")
|
| 33 |
+
parts.append(fn)
|
| 34 |
+
return parts
|
| 35 |
+
|
| 36 |
+
def transcribe_core(path: str, model: str = "whisper-1"):
|
| 37 |
+
if path.lower().endswith(".mp4"):
|
| 38 |
+
fixed = path[:-4] + ".m4a"
|
| 39 |
+
try:
|
| 40 |
+
shutil.copy(path, fixed)
|
| 41 |
+
path = fixed
|
| 42 |
+
print("🔧 已自動修正 mp4 → m4a")
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"⚠️ mp4→m4a 轉檔失敗:{e}")
|
| 45 |
+
|
| 46 |
+
chunks = split_audio_if_needed(path)
|
| 47 |
+
txts = []
|
| 48 |
+
for f in chunks:
|
| 49 |
+
with open(f, "rb") as af:
|
| 50 |
+
t = client.audio.transcriptions.create(
|
| 51 |
+
model=model, file=af, response_format="text"
|
| 52 |
+
)
|
| 53 |
+
txts.append(t)
|
| 54 |
+
full = "\n".join(txts)
|
| 55 |
+
summ = client.chat.completions.create(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
model="gpt-4o-mini",
|
| 57 |
+
messages=[{"role": "user", "content": f"請用繁體中文摘要以下內容:\n{full}"}],
|
| 58 |
+
temperature=0.4,
|
| 59 |
).choices[0].message.content.strip()
|
| 60 |
+
return full, summ
|
| 61 |
|
| 62 |
+
# ========================
|
| 63 |
+
# 🌐 FastAPI 端點(捷徑用)
|
| 64 |
+
# ========================
|
| 65 |
+
@app.post("/api/transcribe")
|
| 66 |
+
async def api_transcribe(file: UploadFile = File(...)):
|
| 67 |
+
"""供 iPhone 捷徑上傳音訊並取得 JSON"""
|
| 68 |
+
temp = file.filename
|
| 69 |
+
with open(temp, "wb") as f:
|
| 70 |
+
f.write(await file.read())
|
| 71 |
+
text, summary = transcribe_core(temp)
|
| 72 |
+
os.remove(temp)
|
| 73 |
+
return {"text": text, "summary": summary}
|
| 74 |
+
|
| 75 |
+
@app.get("/health")
|
| 76 |
+
def health():
|
| 77 |
+
"""捷徑可先 ping 這個確認服務運作中"""
|
| 78 |
+
return {"status": "ok", "time": int(time.time())}
|
| 79 |
+
|
| 80 |
+
# ========================
|
| 81 |
+
# 💬 Gradio 介面
|
| 82 |
+
# ========================
|
| 83 |
+
def transcribe_with_pw(password, file):
|
| 84 |
+
if password.strip() != PASSWORD:
|
| 85 |
+
return "❌ 密碼錯誤", "", ""
|
| 86 |
+
if not file:
|
| 87 |
+
return "⚠️ 未選擇檔案", "", ""
|
| 88 |
+
text, summary = transcribe_core(file.name)
|
| 89 |
+
return "✅ 完成", text, summary
|
| 90 |
+
|
| 91 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 92 |
+
gr.Markdown("## 🎧 LINE 語音轉錄與摘要工具(支援 .m4a / .mp4)")
|
| 93 |
+
pw = gr.Textbox(label="輸入密碼", type="password")
|
| 94 |
+
f = gr.File(label="上傳音訊檔 (.m4a/.mp3/.wav/.mp4)")
|
| 95 |
+
run = gr.Button("開始轉錄 🚀")
|
| 96 |
+
s = gr.Textbox(label="狀態", interactive=False)
|
| 97 |
+
t = gr.Textbox(label="逐字稿", lines=10)
|
| 98 |
+
su = gr.Textbox(label="摘要", lines=8)
|
| 99 |
+
|
| 100 |
+
run.click(transcribe_with_pw, [pw, f], [s, t, su])
|
| 101 |
+
|
| 102 |
+
# ========================
|
| 103 |
+
# 🚀 啟動(單一埠)
|
| 104 |
+
# ========================
|
| 105 |
+
# 讓 Gradio 介面掛載到 FastAPI
|
| 106 |
+
gr.mount_gradio_app(app, demo, path="/")
|
| 107 |
+
|
| 108 |
+
# Hugging Face 自動綁定 port=7860,不用手動設定
|
| 109 |
+
if __name__ == "__main__":
|
| 110 |
+
import uvicorn
|
| 111 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|