File size: 1,318 Bytes
e8c87b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b3d9e5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import gradio as gr
from converter import MarkdownToDocxConverter
from datetime import datetime
import os

converter = MarkdownToDocxConverter()

def convert_markdown_to_docx(markdown_text: str, file_input: str):
    if file_input:
        with open(file_input, "r") as file:
            markdown_text = file.read()

    
    output_dir = "output"
    os.makedirs(output_dir, exist_ok=True)
    output_filename = f"output_{datetime.now().strftime('%Y%m%d_%H%M%S')}.docx"
    converter.convert(markdown_text, os.path.join(output_dir, output_filename))
    return os.path.join(output_dir, output_filename)

demo = gr.Blocks(title="Markdown to DOCX Converter")

with demo:
    gr.Markdown("# Markdown to DOCX Converter")
    with gr.Row():
        with gr.Column():
            with gr.Tab("Текст"):
                markdown_input = gr.TextArea(label="Markdown Input", value="")
            with gr.Tab("Файл (в приоритете)"):
                file_input = gr.File(label="Markdown Input")
        with gr.Column():
            gr.Markdown("Output:")
            docx_output = gr.File(label="DOCX Output")
    convert_button = gr.Button("Convert")
    convert_button.click(convert_markdown_to_docx, inputs=[markdown_input, file_input], outputs=docx_output)


if __name__ == "__main__":
    demo.launch()