MichaelChou0806 commited on
Commit
b1c58b3
·
verified ·
1 Parent(s): 6ddb6b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -17
app.py CHANGED
@@ -38,10 +38,16 @@ def _dataurl_to_file(data_url: str, orig_name: str | None = None) -> str:
38
  def _extract_effective_path(file_obj) -> str:
39
  """從各種格式中提取有效檔案路徑"""
40
  print(f"[DEBUG] 檔案物件類型: {type(file_obj)}")
 
 
 
 
 
41
 
42
  # 如果是字串路徑
43
  if isinstance(file_obj, str):
44
  s = file_obj.strip().strip('"')
 
45
  if s.startswith("data:"):
46
  return _dataurl_to_file(s, None)
47
  if os.path.isfile(s):
@@ -50,30 +56,41 @@ def _extract_effective_path(file_obj) -> str:
50
  # 如果是字典
51
  if isinstance(file_obj, dict):
52
  print(f"[DEBUG] 字典 keys: {list(file_obj.keys())}")
 
 
53
  data = file_obj.get("data")
54
  if isinstance(data, str) and data.startswith("data:"):
55
  return _dataurl_to_file(data, file_obj.get("orig_name"))
56
- p = str(file_obj.get("path") or "").strip().strip('"')
57
- if p and os.path.isfile(p):
58
- return p
59
-
60
- # 如果是物件,嘗試獲取 path name 屬性
61
- if hasattr(file_obj, 'name') and file_obj.name:
62
- if os.path.isfile(file_obj.name):
63
- return file_obj.name
 
64
 
65
- if hasattr(file_obj, 'path') and file_obj.path:
66
- if os.path.isfile(file_obj.path):
67
- return file_obj.path
 
 
 
 
 
 
68
 
69
  # 最後嘗試:直接當作路徑字串
70
  try:
71
- if os.path.isfile(str(file_obj)):
72
- return str(file_obj)
 
 
73
  except:
74
  pass
75
 
76
- raise FileNotFoundError(f"Cannot parse uploaded file: {file_obj}")
77
 
78
  def split_audio(path):
79
  """將音訊檔案分割成多個小於 25MB 的片段"""
@@ -278,10 +295,10 @@ with gr.Blocks(title="Audio Transcription", theme=gr.themes.Soft()) as demo:
278
  placeholder="Enter password"
279
  )
280
 
281
- audio_input = gr.Audio(
282
  label="Audio File",
283
- type="filepath",
284
- sources=["upload"]
285
  )
286
 
287
  submit_btn = gr.Button(
 
38
  def _extract_effective_path(file_obj) -> str:
39
  """從各種格式中提取有效檔案路徑"""
40
  print(f"[DEBUG] 檔案物件類型: {type(file_obj)}")
41
+ print(f"[DEBUG] 檔案物件內容: {file_obj}")
42
+
43
+ # 處理 None
44
+ if file_obj is None:
45
+ raise FileNotFoundError("File object is None")
46
 
47
  # 如果是字串路徑
48
  if isinstance(file_obj, str):
49
  s = file_obj.strip().strip('"')
50
+ print(f"[DEBUG] 字串路徑: {s}")
51
  if s.startswith("data:"):
52
  return _dataurl_to_file(s, None)
53
  if os.path.isfile(s):
 
56
  # 如果是字典
57
  if isinstance(file_obj, dict):
58
  print(f"[DEBUG] 字典 keys: {list(file_obj.keys())}")
59
+
60
+ # 嘗試 data URL
61
  data = file_obj.get("data")
62
  if isinstance(data, str) and data.startswith("data:"):
63
  return _dataurl_to_file(data, file_obj.get("orig_name"))
64
+
65
+ # 嘗試 path
66
+ for key in ["path", "name", "file", "filepath"]:
67
+ p = file_obj.get(key)
68
+ if p and isinstance(p, str):
69
+ p = p.strip().strip('"')
70
+ if os.path.isfile(p):
71
+ print(f"[DEBUG] 找到有效路徑 (key={key}): {p}")
72
+ return p
73
 
74
+ # 如果是物件,嘗試獲取屬性
75
+ for attr in ["name", "path", "file", "filepath"]:
76
+ if hasattr(file_obj, attr):
77
+ p = getattr(file_obj, attr, None)
78
+ if p and isinstance(p, str):
79
+ p = p.strip().strip('"')
80
+ if os.path.isfile(p):
81
+ print(f"[DEBUG] 找到有效路徑 (attr={attr}): {p}")
82
+ return p
83
 
84
  # 最後嘗試:直接當作路徑字串
85
  try:
86
+ path_str = str(file_obj).strip().strip('"')
87
+ if os.path.isfile(path_str):
88
+ print(f"[DEBUG] 直接轉換為路徑: {path_str}")
89
+ return path_str
90
  except:
91
  pass
92
 
93
+ raise FileNotFoundError(f"Cannot parse uploaded file: {type(file_obj)} - {file_obj}")
94
 
95
  def split_audio(path):
96
  """將音訊檔案分割成多個小於 25MB 的片段"""
 
295
  placeholder="Enter password"
296
  )
297
 
298
+ audio_input = gr.File(
299
  label="Audio File",
300
+ file_types=["audio", ".mp3", ".m4a", ".wav", ".ogg", ".webm", ".mp4"],
301
+ file_count="single"
302
  )
303
 
304
  submit_btn = gr.Button(