MichaelChou0806 commited on
Commit
d178239
·
verified ·
1 Parent(s): fdb606f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -22
app.py CHANGED
@@ -19,9 +19,14 @@ ALERT_PASS = os.getenv("ALERT_PASS")
19
  # 📧 寄信通知
20
  # ========================
21
  def send_alert_email(session_id, reason):
 
22
  if not ALERT_EMAIL or not ALERT_PASS:
23
  return
24
- msg = MIMEText(f"Session {session_id} 已被鎖定。\n原因:{reason}\n時間:{time.ctime()}", "plain", "utf-8")
 
 
 
 
25
  msg["Subject"] = "⚠️ 語音轉錄系統警報"
26
  msg["From"] = ALERT_EMAIL
27
  msg["To"] = ALERT_EMAIL
@@ -115,7 +120,7 @@ def transcribe_core(path, model):
115
  full = "\n".join(txts)
116
  res = client.chat.completions.create(
117
  model="gpt-4o-mini",
118
- messages=[{"role":"user","content":f"請用繁體中文摘要以下內容:\n{full}"}],
119
  temperature=0.4,
120
  )
121
  summ = res.choices[0].message.content.strip()
@@ -127,15 +132,17 @@ def transcribe_core(path, model):
127
  def transcribe_with_password(session_id, password, file, model_choice):
128
  locked_flag, msg = check_lock(session_id)
129
  if locked_flag:
130
- return msg, "", ""
131
  if password != PASSWORD:
132
  cnt, msg2 = record_failed_attempt(session_id)
133
- return msg2 or f"密碼錯誤(第 {cnt} 次)", "", ""
134
  if not file:
135
- return "請上傳音訊檔。", "", ""
136
  clear_attempts(session_id)
137
  full, summ = transcribe_core(file, model_choice)
138
- return " 轉錄完成", full, summ
 
 
139
 
140
  def ask_about_transcript(full_text, q):
141
  if not full_text.strip():
@@ -144,7 +151,7 @@ def ask_about_transcript(full_text, q):
144
  return "請輸入問題"
145
  prompt = f"以下是轉錄內容:\n{full_text}\n\n問題:{q}\n請用繁體中文回答。"
146
  res = client.chat.completions.create(
147
- model="gpt-4o-mini", messages=[{"role":"user","content":prompt}], temperature=0.6)
148
  return res.choices[0].message.content.strip()
149
 
150
  # ========================
@@ -163,15 +170,16 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
163
  transcribe_btn = gr.Button("開始轉錄與摘要 🚀")
164
  status_box = gr.Textbox(label="狀態", interactive=False)
165
  transcript_box = gr.Textbox(label="完整轉錄文字", lines=10)
166
- copy_transcript = gr.Button("📋 複製")
167
  summary_box = gr.Textbox(label="摘要結果", lines=10)
168
- copy_summary = gr.Button("📋 複製")
 
 
 
169
 
170
  with gr.Accordion("💬 進一步問 AI", open=False):
171
  user_q = gr.Textbox(label="輸入問題", lines=2)
172
  ask_btn = gr.Button("詢問 AI 🤔")
173
  ai_reply = gr.Textbox(label="AI 回覆", lines=6)
174
- copy_reply = gr.Button("📋 複製")
175
 
176
  def init_session():
177
  import uuid
@@ -181,19 +189,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
181
  transcribe_btn.click(
182
  transcribe_with_password,
183
  [session_state, password_input, audio_input, model_choice],
184
- [status_box, transcript_box, summary_box],
185
  )
186
  ask_btn.click(ask_about_transcript, [transcript_box, user_q], [ai_reply])
187
 
188
- copy_js = """
189
- async (text) => {
190
- try { await navigator.clipboard.writeText(text);
191
- alert("✅ 已複製到剪貼簿!"); }
192
- catch(e){ alert("❌ 複製失敗:" + e); }
193
- }
194
- """
195
- copy_transcript.click(None, transcript_box, None, _js=copy_js)
196
- copy_summary.click(None, summary_box, None, _js=copy_js)
197
- copy_reply.click(None, ai_reply, None, _js=copy_js)
198
-
199
  demo.launch()
 
19
  # 📧 寄信通知
20
  # ========================
21
  def send_alert_email(session_id, reason):
22
+ """當使用者嘗試過多或被鎖定時寄出警報郵件"""
23
  if not ALERT_EMAIL or not ALERT_PASS:
24
  return
25
+ msg = MIMEText(
26
+ f"Session {session_id} 已被鎖定。\n原因:{reason}\n時間:{time.ctime()}",
27
+ "plain",
28
+ "utf-8"
29
+ )
30
  msg["Subject"] = "⚠️ 語音轉錄系統警報"
31
  msg["From"] = ALERT_EMAIL
32
  msg["To"] = ALERT_EMAIL
 
120
  full = "\n".join(txts)
121
  res = client.chat.completions.create(
122
  model="gpt-4o-mini",
123
+ messages=[{"role": "user", "content": f"請用繁體中文摘要以下內容:\n{full}"}],
124
  temperature=0.4,
125
  )
126
  summ = res.choices[0].message.content.strip()
 
132
  def transcribe_with_password(session_id, password, file, model_choice):
133
  locked_flag, msg = check_lock(session_id)
134
  if locked_flag:
135
+ return msg, "", "", None, None
136
  if password != PASSWORD:
137
  cnt, msg2 = record_failed_attempt(session_id)
138
+ return msg2 or f"密碼錯誤(第 {cnt} 次)", "", "", None, None
139
  if not file:
140
+ return "請上傳音訊檔。", "", "", None, None
141
  clear_attempts(session_id)
142
  full, summ = transcribe_core(file, model_choice)
143
+ open("transcript_full.txt", "w", encoding="utf-8").write(full)
144
+ open("transcript_summary.txt", "w", encoding="utf-8").write(summ)
145
+ return "✅ 轉錄完成", full, summ, "transcript_full.txt", "transcript_summary.txt"
146
 
147
  def ask_about_transcript(full_text, q):
148
  if not full_text.strip():
 
151
  return "請輸入問題"
152
  prompt = f"以下是轉錄內容:\n{full_text}\n\n問題:{q}\n請用繁體中文回答。"
153
  res = client.chat.completions.create(
154
+ model="gpt-4o-mini", messages=[{"role": "user", "content": prompt}], temperature=0.6)
155
  return res.choices[0].message.content.strip()
156
 
157
  # ========================
 
170
  transcribe_btn = gr.Button("開始轉錄與摘要 🚀")
171
  status_box = gr.Textbox(label="狀態", interactive=False)
172
  transcript_box = gr.Textbox(label="完整轉錄文字", lines=10)
 
173
  summary_box = gr.Textbox(label="摘要結果", lines=10)
174
+
175
+ with gr.Row():
176
+ download_full = gr.File(label="下載完整轉錄文字 (.txt)")
177
+ download_summary = gr.File(label="下載摘要結果 (.txt)")
178
 
179
  with gr.Accordion("💬 進一步問 AI", open=False):
180
  user_q = gr.Textbox(label="輸入問題", lines=2)
181
  ask_btn = gr.Button("詢問 AI 🤔")
182
  ai_reply = gr.Textbox(label="AI 回覆", lines=6)
 
183
 
184
  def init_session():
185
  import uuid
 
189
  transcribe_btn.click(
190
  transcribe_with_password,
191
  [session_state, password_input, audio_input, model_choice],
192
+ [status_box, transcript_box, summary_box, download_full, download_summary],
193
  )
194
  ask_btn.click(ask_about_transcript, [transcript_box, user_q], [ai_reply])
195
 
 
 
 
 
 
 
 
 
 
 
 
196
  demo.launch()