Spaces:
Running
Running
user
commited on
Commit
•
1a56877
1
Parent(s):
3fa1043
Allow input MD text
Browse files- example.md +1 -4
- main.py +67 -30
example.md
CHANGED
@@ -14,13 +14,10 @@ This document contains elements like headings, lists, and images.
|
|
14 |
- 456
|
15 |
- 789
|
16 |
|
17 |
-
##
|
18 |
|
19 |
1. 123
|
20 |
2. 456
|
21 |
3. 789
|
22 |
|
23 |
-
## Image Example
|
24 |
-
|
25 |
![Demo](demo.webp)
|
26 |
-
|
|
|
14 |
- 456
|
15 |
- 789
|
16 |
|
17 |
+
## Image Example
|
18 |
|
19 |
1. 123
|
20 |
2. 456
|
21 |
3. 789
|
22 |
|
|
|
|
|
23 |
![Demo](demo.webp)
|
|
main.py
CHANGED
@@ -1,9 +1,19 @@
|
|
1 |
import gradio as gr
|
2 |
import subprocess
|
3 |
import os
|
|
|
4 |
|
5 |
-
def convert_markdown_to_ppt(markdown_file, template_file=None):
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
output_path = os.path.splitext(input_path)[0] + ".pptx"
|
8 |
|
9 |
command = f"pandoc '{input_path}' -o '{output_path}'"
|
@@ -20,38 +30,65 @@ def convert_markdown_to_ppt(markdown_file, template_file=None):
|
|
20 |
|
21 |
return output_path
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
# 設定範例檔案的路徑
|
24 |
examples = [
|
25 |
["example.md", None], # 僅上傳 Markdown 檔案
|
26 |
["example.md", "template.pptx"] # 同時上傳 Markdown 檔案和模板
|
27 |
]
|
28 |
|
29 |
-
# 設定 Gradio
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
""
|
54 |
-
)
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import subprocess
|
3 |
import os
|
4 |
+
from datetime import datetime
|
5 |
|
6 |
+
def convert_markdown_to_ppt(markdown_file, markdown_text, input_type, template_file=None):
|
7 |
+
# 根據輸入類型處理 Markdown 檔案或字串
|
8 |
+
if input_type == "Upload Markdown File":
|
9 |
+
input_path = markdown_file.name
|
10 |
+
else: # "Enter Markdown Text"
|
11 |
+
# 將字串內容寫入暫存檔案
|
12 |
+
timestamp = datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
|
13 |
+
input_path = f"file_{timestamp}.md"
|
14 |
+
with open(input_path, "w") as file:
|
15 |
+
file.write(markdown_text)
|
16 |
+
|
17 |
output_path = os.path.splitext(input_path)[0] + ".pptx"
|
18 |
|
19 |
command = f"pandoc '{input_path}' -o '{output_path}'"
|
|
|
30 |
|
31 |
return output_path
|
32 |
|
33 |
+
def update_visibility(input_type):
|
34 |
+
# 根據選擇顯示/隱藏相應的輸入框
|
35 |
+
if input_type == "Upload Markdown File":
|
36 |
+
return gr.update(visible=True), gr.update(visible=False)
|
37 |
+
else:
|
38 |
+
return gr.update(visible=False), gr.update(visible=True)
|
39 |
+
|
40 |
# 設定範例檔案的路徑
|
41 |
examples = [
|
42 |
["example.md", None], # 僅上傳 Markdown 檔案
|
43 |
["example.md", "template.pptx"] # 同時上傳 Markdown 檔案和模板
|
44 |
]
|
45 |
|
46 |
+
# 設定 Gradio 介面
|
47 |
+
with gr.Blocks() as iface:
|
48 |
+
gr.Markdown("""
|
49 |
+
### Markdown to PPT Converter
|
50 |
+
|
51 |
+
上傳一個 Markdown 檔案,並選擇性地上傳 PPT 模板,將其轉換為 PPT 格式並下載。
|
52 |
+
Upload a Markdown file, and optionally upload a PPT template to convert it into PPT format and download.
|
53 |
+
|
54 |
+
### 使用步驟
|
55 |
+
1. **選擇輸入方式**:可以選擇「上傳 Markdown 檔案」或「輸入 Markdown 字串」。
|
56 |
+
2. **上傳或輸入 Markdown 檔案**:根據選擇進行上傳或輸入。
|
57 |
+
3. **選擇性上傳 PPT 模板**:如需指定模板,可點擊「PPT Template (Optional)」欄位上傳 PPT 模板檔案。否則將使用預設樣式轉換。
|
58 |
+
4. **點擊提交**:點擊「Submit」按鈕進行轉換。
|
59 |
+
5. **下載轉換結果**:轉換完成後,下載生成的 PPT 文件。
|
60 |
+
|
61 |
+
### Steps in English
|
62 |
+
1. **Choose Input Type**: You can select either "Upload Markdown File" or "Enter Markdown Text".
|
63 |
+
2. **Upload or Enter Markdown File**: Proceed with upload or text input based on your choice.
|
64 |
+
3. **Optionally upload a PPT template**: If you want to use a specific template, click on the "PPT Template (Optional)" field to upload a PPT template file. Otherwise, the default style will be used.
|
65 |
+
4. **Click Submit**: Press the "Submit" button to start the conversion.
|
66 |
+
5. **Download the result**: Once the conversion is complete, download the generated PPT file.
|
67 |
+
""")
|
68 |
+
|
69 |
+
input_type = gr.Radio(["Upload Markdown File", "Enter Markdown Text"], label="Input Type", value="Upload Markdown File")
|
70 |
+
markdown_file = gr.File(label="Markdown File", visible=True)
|
71 |
+
markdown_text = gr.Textbox(lines=10, placeholder="Enter Markdown content here...", visible=False)
|
72 |
+
template_file = gr.File(label="PPT Template (Optional)")
|
73 |
+
|
74 |
+
input_type.change(
|
75 |
+
update_visibility,
|
76 |
+
inputs=input_type,
|
77 |
+
outputs=[markdown_file, markdown_text]
|
78 |
+
)
|
79 |
+
|
80 |
+
submit_button = gr.Button("Submit")
|
81 |
+
output_file = gr.File()
|
82 |
+
|
83 |
+
submit_button.click(
|
84 |
+
convert_markdown_to_ppt,
|
85 |
+
inputs=[markdown_file, markdown_text, input_type, template_file],
|
86 |
+
outputs=output_file
|
87 |
+
)
|
88 |
+
|
89 |
+
gr.Examples(
|
90 |
+
examples=examples,
|
91 |
+
inputs=[markdown_file, template_file]
|
92 |
+
)
|
93 |
+
|
94 |
iface.launch()
|