bluenevus commited on
Commit
a736130
·
verified ·
1 Parent(s): ffef066

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -0
app.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import io
3
+ import os
4
+ import zipfile
5
+ from dash import Dash, dcc, html, Input, Output, State
6
+ import dash_bootstrap_components as dbc
7
+ from docx import Document
8
+ import markdown
9
+ import threading
10
+ import time
11
+
12
+ app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
13
+
14
+ app.layout = dbc.Container([
15
+ html.H1("Auto-Wiki", className="my-4"),
16
+ dcc.Upload(
17
+ id='upload-data',
18
+ children=html.Div([
19
+ 'Drag and Drop or ',
20
+ html.A('Select Files')
21
+ ]),
22
+ style={
23
+ 'width': '100%',
24
+ 'height': '60px',
25
+ 'lineHeight': '60px',
26
+ 'borderWidth': '1px',
27
+ 'borderStyle': 'dashed',
28
+ 'borderRadius': '5px',
29
+ 'textAlign': 'center',
30
+ 'margin': '10px'
31
+ },
32
+ multiple=True
33
+ ),
34
+ html.Div(id='upload-output'),
35
+ dbc.Progress(id="upload-progress", label="Upload Progress", style={"visibility": "hidden"}),
36
+ dbc.Progress(id="conversion-progress", label="Conversion Progress", style={"visibility": "hidden"}),
37
+ dbc.Button("Convert and Download", id="convert-button", color="primary", className="mt-3", disabled=True),
38
+ dcc.Download(id="download-zip")
39
+ ])
40
+
41
+ def process_docx(contents, filename):
42
+ content_type, content_string = contents.split(',')
43
+ decoded = base64.b64decode(content_string)
44
+ doc = Document(io.BytesIO(decoded))
45
+ full_text = []
46
+ for para in doc.paragraphs:
47
+ full_text.append(para.text)
48
+ return '\n\n'.join(full_text)
49
+
50
+ @app.callback(
51
+ [Output('upload-output', 'children'),
52
+ Output('convert-button', 'disabled'),
53
+ Output('upload-progress', 'value'),
54
+ Output('upload-progress', 'style'),
55
+ Output('conversion-progress', 'value'),
56
+ Output('conversion-progress', 'style'),
57
+ Output('download-zip', 'data')],
58
+ [Input('upload-data', 'contents'),
59
+ Input('upload-data', 'filename'),
60
+ Input('convert-button', 'n_clicks')],
61
+ [State('upload-data', 'contents'),
62
+ State('upload-data', 'filename')]
63
+ )
64
+ def update_output(list_of_contents, list_of_names, n_clicks, contents, filenames):
65
+ ctx = dash.callback_context
66
+ if not ctx.triggered:
67
+ return dash.no_update
68
+
69
+ if ctx.triggered[0]['prop_id'] == 'upload-data.contents':
70
+ if list_of_contents is not None:
71
+ children = [
72
+ html.Div([
73
+ html.H5(f"File: {name}"),
74
+ html.Hr()
75
+ ]) for name in list_of_names
76
+ ]
77
+ return children, False, 100, {"visibility": "visible"}, 0, {"visibility": "hidden"}, None
78
+ return dash.no_update
79
+
80
+ if ctx.triggered[0]['prop_id'] == 'convert-button.n_clicks':
81
+ if n_clicks is None:
82
+ return dash.no_update
83
+
84
+ if not contents:
85
+ return dash.no_update
86
+
87
+ def process_files():
88
+ processed_files = []
89
+ for i, (c, n) in enumerate(zip(contents, filenames)):
90
+ text = process_docx(c, n)
91
+ md = markdown.markdown(text)
92
+ processed_files.append((n.replace('.docx', '.md'), md))
93
+ time.sleep(0.1) # Simulate processing time
94
+ app.callback_context.response.set_data(f'{{"progress": {(i+1)/len(contents)*100}}}')
95
+
96
+ zip_buffer = io.BytesIO()
97
+ with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
98
+ for name, content in processed_files:
99
+ zip_file.writestr(name, content)
100
+
101
+ return zip_buffer.getvalue()
102
+
103
+ thread = threading.Thread(target=process_files)
104
+ thread.start()
105
+
106
+ return dash.no_update, True, 100, {"visibility": "visible"}, 0, {"visibility": "visible"}, dcc.send_bytes(process_files(), "converted_files.zip")
107
+
108
+ return dash.no_update
109
+
110
+ if __name__ == '__main__':
111
+ print("Starting the Dash application...")
112
+ app.run(debug=True, host='0.0.0.0', port=7860)
113
+ print("Dash application has finished running.")