MichaelChou0806 commited on
Commit
f317cf1
·
verified ·
1 Parent(s): d339fc0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -11
app.py CHANGED
@@ -179,16 +179,16 @@ def transcribe_web(password, audio_file):
179
  # 驗證密碼
180
  if not password:
181
  print("[WEB] ❌ 密碼為空")
182
- return "❌ Please enter password", "", ""
183
 
184
  if password.strip() != PASSWORD:
185
  print(f"[WEB] ❌ 密碼錯誤")
186
- return "❌ Incorrect password", "", ""
187
 
188
  # 檢查檔案
189
  if not audio_file:
190
  print("[WEB] ❌ 未上傳檔案")
191
- return "⚠️ Please upload audio file", "", ""
192
 
193
  try:
194
  # 處理檔案
@@ -196,6 +196,9 @@ def transcribe_web(password, audio_file):
196
  path = _extract_effective_path(audio_file)
197
  print(f"[WEB] ✅ 檔案: {path}")
198
 
 
 
 
199
  # 轉錄
200
  print(f"[WEB] 開始轉錄...")
201
  text, summary = transcribe_core(path)
@@ -205,13 +208,13 @@ def transcribe_web(password, audio_file):
205
  status = f"✅ Completed! ({char_count} chars)"
206
 
207
  print(f"[WEB] ✅ 成功\n")
208
- return status, text, summary
209
 
210
  except Exception as e:
211
  import traceback
212
  error_msg = traceback.format_exc()
213
  print(f"❌ [WEB] 錯誤:\n{error_msg}\n")
214
- return f"❌ Error: {str(e)}", "", ""
215
 
216
  # ====== FastAPI 應用 ======
217
  fastapi_app = FastAPI()
@@ -348,10 +351,18 @@ with gr.Blocks(
348
  placeholder="Enter password"
349
  )
350
 
351
- # 檔案上傳 - 使用最基本的 File 組件
352
- audio_input = gr.File(
353
- label="Audio File (MP3, M4A, WAV, etc.)",
354
- type="filepath"
 
 
 
 
 
 
 
 
355
  )
356
 
357
  # 提交按鈕
@@ -406,11 +417,18 @@ with gr.Blocks(
406
  ```
407
  """)
408
 
409
- # 事件綁定
 
 
 
 
 
 
 
410
  submit_btn.click(
411
  fn=transcribe_web,
412
  inputs=[password_input, audio_input],
413
- outputs=[status_output, transcription_output, summary_output]
414
  )
415
 
416
  # ====== 掛載到 FastAPI ======
 
179
  # 驗證密碼
180
  if not password:
181
  print("[WEB] ❌ 密碼為空")
182
+ return "", "❌ Please enter password", "", ""
183
 
184
  if password.strip() != PASSWORD:
185
  print(f"[WEB] ❌ 密碼錯誤")
186
+ return "", "❌ Incorrect password", "", ""
187
 
188
  # 檢查檔案
189
  if not audio_file:
190
  print("[WEB] ❌ 未上傳檔案")
191
+ return "", "⚠️ Please upload audio file", "", ""
192
 
193
  try:
194
  # 處理檔案
 
196
  path = _extract_effective_path(audio_file)
197
  print(f"[WEB] ✅ 檔案: {path}")
198
 
199
+ # 顯示檔案名稱
200
+ file_name = os.path.basename(path)
201
+
202
  # 轉錄
203
  print(f"[WEB] 開始轉錄...")
204
  text, summary = transcribe_core(path)
 
208
  status = f"✅ Completed! ({char_count} chars)"
209
 
210
  print(f"[WEB] ✅ 成功\n")
211
+ return file_name, status, text, summary
212
 
213
  except Exception as e:
214
  import traceback
215
  error_msg = traceback.format_exc()
216
  print(f"❌ [WEB] 錯誤:\n{error_msg}\n")
217
+ return "", f"❌ Error: {str(e)}", "", ""
218
 
219
  # ====== FastAPI 應用 ======
220
  fastapi_app = FastAPI()
 
351
  placeholder="Enter password"
352
  )
353
 
354
+ # 檔案上傳 - 使用最基本的 UploadButton
355
+ audio_input = gr.UploadButton(
356
+ label="📁 Choose Audio File",
357
+ file_types=["audio/*", ".mp3", ".m4a", ".wav", ".ogg", ".webm", ".mp4"],
358
+ file_count="single"
359
+ )
360
+
361
+ # 顯示已選擇的檔案
362
+ file_display = gr.Textbox(
363
+ label="Selected File",
364
+ interactive=False,
365
+ placeholder="No file selected"
366
  )
367
 
368
  # 提交按鈕
 
417
  ```
418
  """)
419
 
420
+ # 事件綁定 - UploadButton 會在選擇檔案時自動觸發
421
+ audio_input.upload(
422
+ fn=lambda f: os.path.basename(f) if f else "No file",
423
+ inputs=audio_input,
424
+ outputs=file_display
425
+ )
426
+
427
+ # 提交按鈕綁定
428
  submit_btn.click(
429
  fn=transcribe_web,
430
  inputs=[password_input, audio_input],
431
+ outputs=[file_display, status_output, transcription_output, summary_output]
432
  )
433
 
434
  # ====== 掛載到 FastAPI ======