Update app.py
Browse files
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 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
# 最後嘗試:直接當作路徑字串
|
| 70 |
try:
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
| 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.
|
| 282 |
label="Audio File",
|
| 283 |
-
|
| 284 |
-
|
| 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(
|