tbdavid2019 commited on
Commit
1e479c7
·
1 Parent(s): 8ec4ba9

更新 README.md 和 app.py,新增功能與錯誤處理,改善使用者體驗

Browse files
Files changed (3) hide show
  1. README.md +27 -5
  2. __pycache__/app.cpython-311.pyc +0 -0
  3. app.py +36 -15
README.md CHANGED
@@ -1,12 +1,34 @@
1
- ---
2
  title: Hg Markitdown
3
- emoji: 🚀
4
- colorFrom: purple
5
- colorTo: red
6
  sdk: gradio
7
  sdk_version: 5.25.0
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  title: Hg Markitdown
2
+ emoji:
3
+ colorFrom: blue
4
+ colorTo: green
5
  sdk: gradio
6
  sdk_version: 5.25.0
7
  app_file: app.py
8
  pinned: false
9
  ---
10
 
11
+ # MarkItDown 檔案轉 Markdown 工具
12
+
13
+ 這個專案提供一個基於 Gradio 的網頁介面,支援拖放或選取檔案(PDF、Word、Excel、PowerPoint、圖片、音訊等)並自動轉換為 Markdown 內容,方便快速複製使用。
14
+
15
+ ## 功能特色
16
+
17
+ - 支援拖曳或選擇檔案即時轉換
18
+ - 轉換結果以 Markdown 元件呈現,支援語法高亮
19
+ - 內建複製按鈕,可一鍵複製轉換後內容
20
+ - 可手動按「開始轉換」重新觸發、更新結果
21
+
22
+ ## 執行方式
23
+
24
+ ```bash
25
+ pip install -r requirements.txt
26
+ python app.py
27
+ ```
28
+
29
+ 執行後於終端機顯示的網址(預設 http://127.0.0.1:7860/)即可打開網頁並進行檔案上傳轉換。
30
+
31
+ ## 注意事項
32
+
33
+ - 請確認已安裝 `markitdown[all]` 所需的系統依賴與 NLP 模型。
34
+ - 若遇到特定檔案格式無法解析,請檢查檔案是否完整、未加密,或參考 MarkItDown 官方文件取得額外支援。
__pycache__/app.cpython-311.pyc ADDED
Binary file (1.55 kB). View file
 
app.py CHANGED
@@ -1,26 +1,47 @@
1
  import gradio as gr
2
  from markitdown import MarkItDown
3
- import os
4
- import shutil
5
- from uuid import uuid4
6
 
7
  md = MarkItDown()
8
 
9
- def convert_file_to_md(file):
 
 
 
10
  try:
11
- result = md.convert(file.name)
12
- content = result.text_content
13
  except Exception as e:
14
  content = f"❌ 轉換失敗:{str(e)}"
 
 
 
 
15
  return content
16
 
17
- demo = gr.Interface(
18
- fn=convert_file_to_md,
19
- inputs=gr.File(label="上傳支援格式檔案(如 PDF、Word、Excel 等)"),
20
- outputs=gr.Textbox(label="轉換後的 Markdown 結果", lines=25),
21
- title="📄 MarkItDown 文件轉 Markdown 線上工具",
22
- description="上傳你的檔案,我們將自動轉換為 Markdown 格式內容,支援 PDF、Word、Excel、PowerPoint、圖片、音訊等格式。",
23
- allow_flagging="never" # ← 加這行就不會出現 Flag 按鈕了
24
- )
25
 
26
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from markitdown import MarkItDown
 
 
 
3
 
4
  md = MarkItDown()
5
 
6
+ def convert_file_to_md(file_path: str) -> str:
7
+ if not file_path:
8
+ return "⚠️ 未收到檔案,請重新上傳。"
9
+
10
  try:
11
+ result = md.convert(file_path)
12
+ content = result.text_content or ""
13
  except Exception as e:
14
  content = f"❌ 轉換失敗:{str(e)}"
15
+
16
+ if not content.strip():
17
+ return "ℹ️ 轉換完成,但沒有可顯示的 Markdown 內容。"
18
+
19
  return content
20
 
 
 
 
 
 
 
 
 
21
 
22
+ with gr.Blocks(title="📄 MarkItDown 文件轉 Markdown 線上工具") as demo:
23
+ gr.Markdown("""
24
+ ## 📄 MarkItDown 文件轉 Markdown
25
+ 上傳支援格式檔案,系統會自動轉換為 Markdown 內容。也可以按下「開始轉換」手動觸發。
26
+ """)
27
+
28
+ with gr.Row():
29
+ file_input = gr.File(
30
+ label="拖曳或選擇支援格式檔案(PDF、Word、Excel、PowerPoint、圖片、音訊等)",
31
+ file_count="single",
32
+ type="filepath",
33
+ )
34
+ convert_button = gr.Button("開始轉換", variant="primary")
35
+
36
+ gr.Markdown("### 轉換後的 Markdown 結果")
37
+ output = gr.Markdown(
38
+ value="請上傳或轉換檔案,結果將顯示於此。",
39
+ show_copy_button=True,
40
+ )
41
+
42
+ file_input.upload(convert_file_to_md, inputs=file_input, outputs=output)
43
+ convert_button.click(convert_file_to_md, inputs=file_input, outputs=output)
44
+
45
+
46
+ if __name__ == "__main__":
47
+ demo.launch()