File size: 3,980 Bytes
a736130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import base64
import io
import os
import zipfile
from dash import Dash, dcc, html, Input, Output, State
import dash_bootstrap_components as dbc
from docx import Document
import markdown
import threading
import time

app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])

app.layout = dbc.Container([
    html.H1("Auto-Wiki", className="my-4"),
    dcc.Upload(
        id='upload-data',
        children=html.Div([
            'Drag and Drop or ',
            html.A('Select Files')
        ]),
        style={
            'width': '100%',
            'height': '60px',
            'lineHeight': '60px',
            'borderWidth': '1px',
            'borderStyle': 'dashed',
            'borderRadius': '5px',
            'textAlign': 'center',
            'margin': '10px'
        },
        multiple=True
    ),
    html.Div(id='upload-output'),
    dbc.Progress(id="upload-progress", label="Upload Progress", style={"visibility": "hidden"}),
    dbc.Progress(id="conversion-progress", label="Conversion Progress", style={"visibility": "hidden"}),
    dbc.Button("Convert and Download", id="convert-button", color="primary", className="mt-3", disabled=True),
    dcc.Download(id="download-zip")
])

def process_docx(contents, filename):
    content_type, content_string = contents.split(',')
    decoded = base64.b64decode(content_string)
    doc = Document(io.BytesIO(decoded))
    full_text = []
    for para in doc.paragraphs:
        full_text.append(para.text)
    return '\n\n'.join(full_text)

@app.callback(
    [Output('upload-output', 'children'),
     Output('convert-button', 'disabled'),
     Output('upload-progress', 'value'),
     Output('upload-progress', 'style'),
     Output('conversion-progress', 'value'),
     Output('conversion-progress', 'style'),
     Output('download-zip', 'data')],
    [Input('upload-data', 'contents'),
     Input('upload-data', 'filename'),
     Input('convert-button', 'n_clicks')],
    [State('upload-data', 'contents'),
     State('upload-data', 'filename')]
)
def update_output(list_of_contents, list_of_names, n_clicks, contents, filenames):
    ctx = dash.callback_context
    if not ctx.triggered:
        return dash.no_update

    if ctx.triggered[0]['prop_id'] == 'upload-data.contents':
        if list_of_contents is not None:
            children = [
                html.Div([
                    html.H5(f"File: {name}"),
                    html.Hr()
                ]) for name in list_of_names
            ]
            return children, False, 100, {"visibility": "visible"}, 0, {"visibility": "hidden"}, None
        return dash.no_update

    if ctx.triggered[0]['prop_id'] == 'convert-button.n_clicks':
        if n_clicks is None:
            return dash.no_update

        if not contents:
            return dash.no_update

        def process_files():
            processed_files = []
            for i, (c, n) in enumerate(zip(contents, filenames)):
                text = process_docx(c, n)
                md = markdown.markdown(text)
                processed_files.append((n.replace('.docx', '.md'), md))
                time.sleep(0.1)  # Simulate processing time
                app.callback_context.response.set_data(f'{{"progress": {(i+1)/len(contents)*100}}}')

            zip_buffer = io.BytesIO()
            with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
                for name, content in processed_files:
                    zip_file.writestr(name, content)
            
            return zip_buffer.getvalue()

        thread = threading.Thread(target=process_files)
        thread.start()

        return dash.no_update, True, 100, {"visibility": "visible"}, 0, {"visibility": "visible"}, dcc.send_bytes(process_files(), "converted_files.zip")

    return dash.no_update

if __name__ == '__main__':
    print("Starting the Dash application...")
    app.run(debug=True, host='0.0.0.0', port=7860)
    print("Dash application has finished running.")