MichaelChou0806 commited on
Commit
8702506
·
verified ·
1 Parent(s): a7f0ba7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -19
app.py CHANGED
@@ -3,38 +3,31 @@ import gradio as gr
3
  from fastapi import FastAPI, UploadFile, Form, HTTPException
4
  from openai import OpenAI
5
 
6
- # 初始化 FastAPI + Gradio
7
- app = FastAPI()
8
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
9
-
10
- # 載入安全金鑰(請在 Hugging Face → Settings → Secrets 裡設 APP_PASSWORD)
11
  APP_PASSWORD = os.getenv("APP_PASSWORD", None)
12
 
13
- # === API endpoint ===
14
- @app.post("/api/transcribe")
 
 
15
  async def transcribe_api(file: UploadFile, token: str = Form(...)):
16
- # 驗證 token
17
  if not APP_PASSWORD:
18
  raise HTTPException(status_code=500, detail="Server misconfiguration: APP_PASSWORD not set.")
19
  if token != APP_PASSWORD:
20
  raise HTTPException(status_code=403, detail="Forbidden: invalid token.")
21
 
22
- # 儲存臨時音訊檔
23
  contents = await file.read()
24
  temp_path = f"/tmp/{file.filename}"
25
  with open(temp_path, "wb") as f:
26
  f.write(contents)
27
 
28
- # 語音轉文字
29
  with open(temp_path, "rb") as audio_file:
30
  transcript = client.audio.transcriptions.create(
31
  model="whisper-1",
32
  file=audio_file
33
  )
34
-
35
  text = transcript.text.strip()
36
 
37
- # 簡短摘要
38
  summary_prompt = f"請幫我用中文摘要以下內容:\n\n{text}"
39
  summary = client.chat.completions.create(
40
  model="gpt-4o-mini",
@@ -44,7 +37,7 @@ async def transcribe_api(file: UploadFile, token: str = Form(...)):
44
  return {"text": text, "summary": summary}
45
 
46
 
47
- # === Gradio 前端 ===
48
  def transcribe_ui(audio):
49
  if audio is None:
50
  return "請上傳音訊檔案", ""
@@ -67,16 +60,13 @@ def transcribe_ui(audio):
67
  demo = gr.Interface(
68
  fn=transcribe_ui,
69
  inputs=gr.Audio(type="filepath", label="上傳音訊"),
70
- outputs=[
71
- gr.Textbox(label="轉錄結果"),
72
- gr.Textbox(label="AI 摘要")
73
- ],
74
  title="LINE 語音轉錄與摘要 (安全版)",
75
  description="上傳 LINE 語音或其他音訊,進行自動轉錄與摘要"
76
  )
77
 
78
- # 掛載 Gradio 到 FastAPI
79
- app = gr.mount_gradio_app(app, demo, path="/")
80
 
81
  if __name__ == "__main__":
82
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
3
  from fastapi import FastAPI, UploadFile, Form, HTTPException
4
  from openai import OpenAI
5
 
 
 
6
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
 
 
7
  APP_PASSWORD = os.getenv("APP_PASSWORD", None)
8
 
9
+ # 初始化 FastAPI
10
+ api = FastAPI()
11
+
12
+ @api.post("/api/transcribe")
13
  async def transcribe_api(file: UploadFile, token: str = Form(...)):
 
14
  if not APP_PASSWORD:
15
  raise HTTPException(status_code=500, detail="Server misconfiguration: APP_PASSWORD not set.")
16
  if token != APP_PASSWORD:
17
  raise HTTPException(status_code=403, detail="Forbidden: invalid token.")
18
 
 
19
  contents = await file.read()
20
  temp_path = f"/tmp/{file.filename}"
21
  with open(temp_path, "wb") as f:
22
  f.write(contents)
23
 
 
24
  with open(temp_path, "rb") as audio_file:
25
  transcript = client.audio.transcriptions.create(
26
  model="whisper-1",
27
  file=audio_file
28
  )
 
29
  text = transcript.text.strip()
30
 
 
31
  summary_prompt = f"請幫我用中文摘要以下內容:\n\n{text}"
32
  summary = client.chat.completions.create(
33
  model="gpt-4o-mini",
 
37
  return {"text": text, "summary": summary}
38
 
39
 
40
+ # === Gradio 部分 ===
41
  def transcribe_ui(audio):
42
  if audio is None:
43
  return "請上傳音訊檔案", ""
 
60
  demo = gr.Interface(
61
  fn=transcribe_ui,
62
  inputs=gr.Audio(type="filepath", label="上傳音訊"),
63
+ outputs=[gr.Textbox(label="轉錄結果"), gr.Textbox(label="AI 摘要")],
 
 
 
64
  title="LINE 語音轉錄與摘要 (安全版)",
65
  description="上傳 LINE 語音或其他音訊,進行自動轉錄與摘要"
66
  )
67
 
68
+ # 用不同變數名防止覆蓋
69
+ app = gr.mount_gradio_app(api, demo, path="/")
70
 
71
  if __name__ == "__main__":
72
+ demo.launch(server_name="0.0.0.0", server_port=7860, share=True)