MichaelChou0806 commited on
Commit
8137731
·
verified ·
1 Parent(s): 1101734

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -17
app.py CHANGED
@@ -8,24 +8,23 @@ print("===== 🚀 啟動中 =====")
8
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
9
  APP_PASSWORD = os.getenv("APP_PASSWORD")
10
 
11
- print(f"OPENAI_API_KEY: {OPENAI_API_KEY[:8]}********") # 前8碼檢查
12
  print(f"APP_PASSWORD: {APP_PASSWORD}")
13
 
14
  client = OpenAI(api_key=OPENAI_API_KEY)
15
 
16
- # === 建立 FastAPI ===
17
- app = FastAPI(title="Voice Transcribe API")
18
 
19
- @app.get("/ping")
20
  async def ping():
21
  return {"status": "ok", "APP_PASSWORD": APP_PASSWORD is not None}
22
 
23
- @app.post("/api/transcribe")
24
  async def transcribe_api(file: UploadFile, token: str = Form(...)):
25
  print(f"📥 收到 API 請求:{file.filename}, token={token}")
26
-
27
  if not APP_PASSWORD:
28
- raise HTTPException(status_code=500, detail="APP_PASSWORD not set on server.")
29
  if token != APP_PASSWORD:
30
  raise HTTPException(status_code=403, detail="Forbidden: invalid token")
31
 
@@ -33,12 +32,12 @@ async def transcribe_api(file: UploadFile, token: str = Form(...)):
33
  with open(temp_path, "wb") as f:
34
  f.write(await file.read())
35
 
 
36
  with open(temp_path, "rb") as audio_file:
37
  transcript = client.audio.transcriptions.create(model="whisper-1", file=audio_file)
38
-
39
  text = transcript.text.strip()
40
- print(f"🗣️ 轉錄文字:{text[:60]}...")
41
 
 
42
  summary_prompt = f"請幫我用中文摘要以下內容:\n\n{text}"
43
  summary = client.chat.completions.create(
44
  model="gpt-4o-mini",
@@ -47,7 +46,7 @@ async def transcribe_api(file: UploadFile, token: str = Form(...)):
47
 
48
  return JSONResponse({"text": text, "summary": summary})
49
 
50
- # === 建立 Gradio UI ===
51
  def gradio_ui(audio):
52
  if audio is None:
53
  return "請上傳音訊檔案", ""
@@ -66,12 +65,11 @@ demo = gr.Interface(
66
  fn=gradio_ui,
67
  inputs=gr.Audio(type="filepath", label="上傳音訊"),
68
  outputs=[gr.Textbox(label="轉錄文字"), gr.Textbox(label="AI 摘要")],
69
- title="LINE 語音轉錄與摘要 (整合 API )"
 
70
  )
71
 
72
- # === Gradio 掛上 FastAPI ===
73
- app = gr.mount_gradio_app(app, demo, path="/")
74
-
75
- print("✅ FastAPI 路由:")
76
- for r in app.routes:
77
- print("👉", r.path)
 
8
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
9
  APP_PASSWORD = os.getenv("APP_PASSWORD")
10
 
11
+ print(f"OPENAI_API_KEY: {OPENAI_API_KEY[:10]}********")
12
  print(f"APP_PASSWORD: {APP_PASSWORD}")
13
 
14
  client = OpenAI(api_key=OPENAI_API_KEY)
15
 
16
+ # === FastAPI 主應用 ===
17
+ api = FastAPI(title="Voice Transcribe API")
18
 
19
+ @api.get("/ping")
20
  async def ping():
21
  return {"status": "ok", "APP_PASSWORD": APP_PASSWORD is not None}
22
 
23
+ @api.post("/api/transcribe")
24
  async def transcribe_api(file: UploadFile, token: str = Form(...)):
25
  print(f"📥 收到 API 請求:{file.filename}, token={token}")
 
26
  if not APP_PASSWORD:
27
+ raise HTTPException(status_code=500, detail="APP_PASSWORD not set")
28
  if token != APP_PASSWORD:
29
  raise HTTPException(status_code=403, detail="Forbidden: invalid token")
30
 
 
32
  with open(temp_path, "wb") as f:
33
  f.write(await file.read())
34
 
35
+ # 語音轉文字
36
  with open(temp_path, "rb") as audio_file:
37
  transcript = client.audio.transcriptions.create(model="whisper-1", file=audio_file)
 
38
  text = transcript.text.strip()
 
39
 
40
+ # 生成摘要
41
  summary_prompt = f"請幫我用中文摘要以下內容:\n\n{text}"
42
  summary = client.chat.completions.create(
43
  model="gpt-4o-mini",
 
46
 
47
  return JSONResponse({"text": text, "summary": summary})
48
 
49
+ # === Gradio UI ===
50
  def gradio_ui(audio):
51
  if audio is None:
52
  return "請上傳音訊檔案", ""
 
65
  fn=gradio_ui,
66
  inputs=gr.Audio(type="filepath", label="上傳音訊"),
67
  outputs=[gr.Textbox(label="轉錄文字"), gr.Textbox(label="AI 摘要")],
68
+ title="LINE 語音轉錄與摘要 (API + UI)",
69
+ description="可透過網頁或捷徑呼叫 /api/transcribe"
70
  )
71
 
72
+ # 關鍵:讓 Hugging Face 找到 FastAPI 應用
73
+ app = gr.mount_gradio_app(api, demo, path="/")
74
+ # 暴露 app 物件給 Spaces runtime
75
+ application = app