MichaelChou0806 commited on
Commit
89d8668
·
verified ·
1 Parent(s): c69672a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -4
app.py CHANGED
@@ -1,12 +1,37 @@
1
- import os, shutil
2
  from pydub import AudioSegment
3
  from openai import OpenAI
4
  import gradio as gr
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  PASSWORD = os.getenv("APP_PASSWORD", "chou")
7
  MAX_SIZE = 25 * 1024 * 1024
8
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
9
 
 
 
 
 
 
 
 
10
  def split_audio(path):
11
  size = os.path.getsize(path)
12
  if size <= MAX_SIZE:
@@ -21,6 +46,7 @@ def split_audio(path):
21
  parts.append(fn)
22
  return parts
23
 
 
24
  def transcribe_core(path, model="whisper-1"):
25
  if path.lower().endswith(".mp4"):
26
  fixed = path[:-4] + ".m4a"
@@ -34,34 +60,59 @@ def transcribe_core(path, model="whisper-1"):
34
  raw = []
35
  for c in chunks:
36
  with open(c, "rb") as af:
37
- txt = client.audio.transcriptions.create(model=model, file=af, response_format="text")
 
 
 
 
38
  raw.append(txt)
39
  raw_txt = "\n".join(raw)
40
 
 
41
  conv = client.chat.completions.create(
42
  model="gpt-4o-mini",
43
  messages=[
44
  {"role":"system","content":"你是嚴格的繁體中文轉換器"},
45
  {"role":"user","content":f"將以下內容轉為台灣繁體,不意譯:\n{raw_txt}"}
46
- ], temperature=0.0)
 
47
  trad = conv.choices[0].message.content.strip()
48
 
 
49
  summ = client.chat.completions.create(
50
  model="gpt-4o-mini",
51
  messages=[
52
  {"role":"system","content":"你是繁體摘要助手"},
53
  {"role":"user","content":f"用條列或一句話摘要:\n{trad}"}
54
- ], temperature=0.2)
 
55
  return trad, summ.choices[0].message.content.strip()
56
 
 
 
 
57
  def transcribe(password, file):
58
  if password.strip() != PASSWORD:
59
  return "❌ 密碼錯誤", "", ""
60
  if not file:
61
  return "⚠️ 未選擇檔案", "", ""
 
 
 
 
 
 
 
 
 
 
62
  text, summary = transcribe_core(file.name)
63
  return "✅ 完成", text, summary
64
 
 
 
 
 
65
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
66
  gr.Markdown("## 🎧 LINE 語音轉錄與摘要(Hugging Face 版)")
67
  pw = gr.Textbox(label="密碼", type="password")
@@ -74,5 +125,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
74
 
75
  app = demo
76
 
 
 
 
77
  if __name__ == "__main__":
78
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
+ import os, shutil, base64, io
2
  from pydub import AudioSegment
3
  from openai import OpenAI
4
  import gradio as gr
5
 
6
+ # ✅ 強制允許 base64 虛擬檔案
7
+ if hasattr(gr, "processing_utils"):
8
+ try:
9
+ gr.processing_utils.ALLOW_DUPLICATE_UPLOADS = True
10
+ except Exception:
11
+ pass
12
+ try:
13
+ # 解除 Gradio FileData 的 path 限制
14
+ import gradio.processing_utils as pu
15
+ def _dummy_check_allowed(*a, **kw): return True
16
+ pu._check_allowed = _dummy_check_allowed
17
+ print("🔓 已解除 Gradio 上傳路徑限制")
18
+ except Exception as e:
19
+ print(f"⚠️ 無法解除限制: {e}")
20
+
21
+ # ======================================================
22
+ # 🔐 設定區
23
+ # ======================================================
24
  PASSWORD = os.getenv("APP_PASSWORD", "chou")
25
  MAX_SIZE = 25 * 1024 * 1024
26
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
27
 
28
+ print("===== 🚀 啟動中 =====")
29
+ print(f"APP_PASSWORD: {'✅ 已載入' if PASSWORD else '❌ 未載入'}")
30
+ print(f"目前密碼內容:{PASSWORD}")
31
+
32
+ # ======================================================
33
+ # 🎧 音訊轉錄核心
34
+ # ======================================================
35
  def split_audio(path):
36
  size = os.path.getsize(path)
37
  if size <= MAX_SIZE:
 
46
  parts.append(fn)
47
  return parts
48
 
49
+
50
  def transcribe_core(path, model="whisper-1"):
51
  if path.lower().endswith(".mp4"):
52
  fixed = path[:-4] + ".m4a"
 
60
  raw = []
61
  for c in chunks:
62
  with open(c, "rb") as af:
63
+ txt = client.audio.transcriptions.create(
64
+ model=model,
65
+ file=af,
66
+ response_format="text"
67
+ )
68
  raw.append(txt)
69
  raw_txt = "\n".join(raw)
70
 
71
+ # === 簡轉繁 ===
72
  conv = client.chat.completions.create(
73
  model="gpt-4o-mini",
74
  messages=[
75
  {"role":"system","content":"你是嚴格的繁體中文轉換器"},
76
  {"role":"user","content":f"將以下內容轉為台灣繁體,不意譯:\n{raw_txt}"}
77
+ ],
78
+ temperature=0.0)
79
  trad = conv.choices[0].message.content.strip()
80
 
81
+ # === 摘要 ===
82
  summ = client.chat.completions.create(
83
  model="gpt-4o-mini",
84
  messages=[
85
  {"role":"system","content":"你是繁體摘要助手"},
86
  {"role":"user","content":f"用條列或一句話摘要:\n{trad}"}
87
+ ],
88
+ temperature=0.2)
89
  return trad, summ.choices[0].message.content.strip()
90
 
91
+ # ======================================================
92
+ # 💬 介面邏輯
93
+ # ======================================================
94
  def transcribe(password, file):
95
  if password.strip() != PASSWORD:
96
  return "❌ 密碼錯誤", "", ""
97
  if not file:
98
  return "⚠️ 未選擇檔案", "", ""
99
+
100
+ # ⚙️ 若 file 為 base64 資料
101
+ if hasattr(file, "data") and isinstance(file.data, str) and file.data.startswith("data:audio"):
102
+ base64_str = file.data.split(",")[1]
103
+ audio_bytes = base64.b64decode(base64_str)
104
+ temp_path = "uploaded_audio.m4a"
105
+ with open(temp_path, "wb") as f:
106
+ f.write(audio_bytes)
107
+ file.name = temp_path
108
+
109
  text, summary = transcribe_core(file.name)
110
  return "✅ 完成", text, summary
111
 
112
+
113
+ # ======================================================
114
+ # 🖥️ Gradio 介面
115
+ # ======================================================
116
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
117
  gr.Markdown("## 🎧 LINE 語音轉錄與摘要(Hugging Face 版)")
118
  pw = gr.Textbox(label="密碼", type="password")
 
125
 
126
  app = demo
127
 
128
+ # ======================================================
129
+ # 🚀 啟動
130
+ # ======================================================
131
  if __name__ == "__main__":
132
  demo.launch(server_name="0.0.0.0", server_port=7860)