thohan88 commited on
Commit
7a3e326
·
1 Parent(s): a807d0a

Rework and remove extras

Browse files
Files changed (4) hide show
  1. .gitattributes +0 -34
  2. .vscode/settings.json +0 -6
  3. requirements.txt +0 -4
  4. tools/pdf2svg.py +134 -64
.gitattributes DELETED
@@ -1,34 +0,0 @@
1
- *.7z filter=lfs diff=lfs merge=lfs -text
2
- *.arrow filter=lfs diff=lfs merge=lfs -text
3
- *.bin filter=lfs diff=lfs merge=lfs -text
4
- *.bz2 filter=lfs diff=lfs merge=lfs -text
5
- *.ckpt filter=lfs diff=lfs merge=lfs -text
6
- *.ftz filter=lfs diff=lfs merge=lfs -text
7
- *.gz filter=lfs diff=lfs merge=lfs -text
8
- *.h5 filter=lfs diff=lfs merge=lfs -text
9
- *.joblib filter=lfs diff=lfs merge=lfs -text
10
- *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
- *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
- *.model filter=lfs diff=lfs merge=lfs -text
13
- *.msgpack filter=lfs diff=lfs merge=lfs -text
14
- *.npy filter=lfs diff=lfs merge=lfs -text
15
- *.npz filter=lfs diff=lfs merge=lfs -text
16
- *.onnx filter=lfs diff=lfs merge=lfs -text
17
- *.ot filter=lfs diff=lfs merge=lfs -text
18
- *.parquet filter=lfs diff=lfs merge=lfs -text
19
- *.pb filter=lfs diff=lfs merge=lfs -text
20
- *.pickle filter=lfs diff=lfs merge=lfs -text
21
- *.pkl filter=lfs diff=lfs merge=lfs -text
22
- *.pt filter=lfs diff=lfs merge=lfs -text
23
- *.pth filter=lfs diff=lfs merge=lfs -text
24
- *.rar filter=lfs diff=lfs merge=lfs -text
25
- *.safetensors filter=lfs diff=lfs merge=lfs -text
26
- saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
- *.tar.* filter=lfs diff=lfs merge=lfs -text
28
- *.tflite filter=lfs diff=lfs merge=lfs -text
29
- *.tgz filter=lfs diff=lfs merge=lfs -text
30
- *.wasm filter=lfs diff=lfs merge=lfs -text
31
- *.xz filter=lfs diff=lfs merge=lfs -text
32
- *.zip filter=lfs diff=lfs merge=lfs -text
33
- *.zst filter=lfs diff=lfs merge=lfs -text
34
- *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
.vscode/settings.json DELETED
@@ -1,6 +0,0 @@
1
- {
2
- "[python]": {
3
- "editor.defaultFormatter": "ms-python.black-formatter"
4
- },
5
- "python.formatting.provider": "none"
6
- }
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,5 +1 @@
1
- duckdb
2
- faicons
3
- pandas
4
- shinywidgets
5
  shiny==1.4.0
 
 
 
 
 
1
  shiny==1.4.0
tools/pdf2svg.py CHANGED
@@ -25,15 +25,14 @@ def mod_pdf2svg_ui():
25
  "Upload a PDF file to convert a selected range of pages into separate SVG files.",
26
  class_="text-center text-muted mb-5",
27
  ),
28
- ui.layout_sidebar(
29
- ui.sidebar(
30
- ui.input_file("pdf_file", "Upload file", accept=".pdf"),
31
- ui.output_ui("page_selection_ui"),
32
- class_="p-0 bg-light border rounded-3",
33
- width=250,
34
  ),
35
- ui.div(ui.output_ui("conversion_output_ui")),
36
  ),
 
 
37
  )
38
  )
39
 
@@ -97,16 +96,76 @@ def mod_pdf2svg_server(input, output, session, www_dir):
97
  @output
98
  @render.ui
99
  def page_selection_ui():
100
- if not input.pdf_file() or total_pages.get() == 0:
101
  return None
102
 
103
- return ui.div(
104
- ui.input_numeric("first_page", "First page", value=1, min=1, max=total_pages.get()),
105
- ui.input_numeric("last_page", "Last page", value=total_pages.get(), min=1, max=total_pages.get()),
106
- ui.input_action_button("start_conversion", "Convert", class_="btn-primary w-100"),
107
- class_="p-3"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  )
109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
  @reactive.effect
112
  @reactive.event(input.start_conversion)
@@ -122,10 +181,10 @@ def mod_pdf2svg_server(input, output, session, www_dir):
122
  if not first_page or not last_page or first_page > last_page:
123
  ui.notification_show("Invalid page range selected.", type="error")
124
  return
125
-
126
  is_converting.set(True)
127
  conversion_results.set([])
128
-
129
  pages_to_convert = range(first_page, last_page + 1)
130
  num_pages_to_convert = len(pages_to_convert)
131
 
@@ -145,9 +204,18 @@ def mod_pdf2svg_server(input, output, session, www_dir):
145
  f"--pages={page_num}",
146
  ]
147
  try:
148
- subprocess.run(
149
- cmd, check=True, capture_output=True, text=True, timeout=30
 
 
 
 
 
150
  )
 
 
 
 
151
  if svg_path.exists():
152
  conversion_results.set(
153
  conversion_results.get()
@@ -170,8 +238,7 @@ def mod_pdf2svg_server(input, output, session, www_dir):
170
  except Exception as e:
171
  ui.notification_show(f"Error on page {page_num}: {e}", type="error")
172
  break
173
- await asyncio.sleep(0.01)
174
-
175
  is_converting.set(False)
176
  if not conversion_results.get():
177
  ui.notification_show(
@@ -190,7 +257,7 @@ def mod_pdf2svg_server(input, output, session, www_dir):
190
  class_="text-center text-muted m-5",
191
  ),
192
  class_="border rounded-3 d-grid align-content-center",
193
- style="min-height: 400px;",
194
  )
195
 
196
  if is_converting.get():
@@ -200,7 +267,7 @@ def mod_pdf2svg_server(input, output, session, www_dir):
200
  class_="text-center text-muted m-5",
201
  ),
202
  class_="border rounded-3 d-grid align-content-center",
203
- style="min-height: 400px;",
204
  )
205
 
206
  results = conversion_results.get()
@@ -211,78 +278,81 @@ def mod_pdf2svg_server(input, output, session, www_dir):
211
  class_="text-center text-muted m-5",
212
  ),
213
  class_="border rounded-3 d-grid align-content-center",
214
- style="min-height: 400px;",
215
  )
216
 
217
  header = ui.div(
218
- ui.h4(
219
- f"Successfully converted {len(results)} pages",
220
- class_="alert-heading fw-bold",
221
- ),
222
- ui.p(
223
- "Original file: ",
224
  ui.span(
225
- f"{pdf_filename.get()}.pdf",
226
- class_="badge bg-light text-muted fw-normal",
227
  ),
228
  ),
229
  ui.div(
230
  ui.download_button(
231
- "download_zip",
232
- "Download All as .ZIP",
233
- class_="btn btn-success btn-sm",
234
  ),
235
  ui.input_action_button(
236
- "remove_pdf", "Remove", class_="btn btn-danger btn-sm"
 
 
237
  ),
238
- class_="d-flex gap-2",
239
  ),
240
- class_="alert alert-success",
241
  )
242
 
243
-
244
- cards = [
245
- ui.card(
246
  ui.div(
247
- {"class": "d-flex flex-column", "style": "height: 100%;"},
248
- ui.tags.img(
249
- src=svg["url"],
250
- class_="bg-white img-fluid",
251
- style="max-height: 350px; object-fit: contain;",
252
- ),
253
- ui.div(
254
- ui.h6(f"Page {svg['page']}", class_="fw-bold mb-1"),
255
- ui.span(
256
- f"{svg['size'] / 1024:.1f} KB",
257
- class_="small text-muted mb-2",
258
  ),
259
- ui.a(
260
- "Download",
261
- href=svg["url"],
262
- download=svg["filename"],
263
- class_="btn btn-sm btn-dark",
 
 
 
 
 
 
 
264
  ),
265
- class_="mt-auto d-flex flex-column gap-1",
266
  ),
267
- ),
268
- class_="shadow-sm p-2",
269
- )
270
- for svg in results
271
- ]
272
 
273
- return ui.div(header, ui.layout_column_wrap(*cards, width="180px"))
274
 
275
  @reactive.effect
276
  @reactive.event(input.remove_pdf)
277
  def remove_current_pdf():
 
278
  is_removed.set(True)
 
 
279
  conversion_results.set([])
280
  pdf_filename.set(None)
281
  pdf_path_r.set(None)
282
  total_pages.set(0)
 
 
283
  if session_dir.exists():
284
  shutil.rmtree(session_dir, ignore_errors=True)
285
- ui.notification_show("Removed the PDF and all associated files.", duration=3)
 
 
286
 
287
  @session.on_ended
288
  def on_end():
@@ -301,4 +371,4 @@ def mod_pdf2svg_server(input, output, session, www_dir):
301
  for svg_file in results:
302
  zipf.write(svg_file["path"], svg_file["filename"])
303
  with open(zip_path, "rb") as f:
304
- yield f.read()
 
25
  "Upload a PDF file to convert a selected range of pages into separate SVG files.",
26
  class_="text-center text-muted mb-5",
27
  ),
28
+ ui.card(
29
+ ui.input_file(
30
+ "pdf_file", "Upload PDF file", accept=".pdf", width="100%"
 
 
 
31
  ),
32
+ class_="shadow-sm",
33
  ),
34
+ ui.output_ui("page_selection_ui"),
35
+ ui.div(ui.output_ui("conversion_output_ui"), class_="mt-4"),
36
  )
37
  )
38
 
 
96
  @output
97
  @render.ui
98
  def page_selection_ui():
99
+ if not input.pdf_file() or total_pages.get() == 0 or is_removed.get():
100
  return None
101
 
102
+ return ui.card(
103
+ ui.div(
104
+ {"class": "row g-3"},
105
+ ui.div(
106
+ {"class": "col-4"},
107
+ ui.input_numeric(
108
+ "first_page",
109
+ "From page",
110
+ value=1,
111
+ min=1,
112
+ max=total_pages.get(),
113
+ width="100%", # fill column
114
+ ).add_class("mb-0"),
115
+ ),
116
+ ui.div(
117
+ {"class": "col-4"},
118
+ ui.input_numeric(
119
+ "last_page",
120
+ "To page",
121
+ value=total_pages.get(),
122
+ min=1,
123
+ max=total_pages.get(),
124
+ width="100%", # fill column
125
+ ).add_class("mb-0"),
126
+ ),
127
+ ui.div(
128
+ {"class": "col-4 d-flex align-items-end"},
129
+ ui.input_action_button(
130
+ "start_conversion",
131
+ "Convert",
132
+ class_="btn btn-primary w-100 p-2",
133
+ ),
134
+ ),
135
+ ),
136
+ class_="mt-3 shadow-sm p-0",
137
  )
138
 
139
+ if not input.pdf_file() or total_pages.get() == 0 or is_removed.get():
140
+ return None
141
+
142
+ return ui.card(
143
+ ui.div(
144
+ {"class": "d-flex align-items-end gap-2"},
145
+ ui.input_numeric(
146
+ "first_page",
147
+ "From",
148
+ value=1,
149
+ min=1,
150
+ max=total_pages.get(),
151
+ width="120px",
152
+ ).add_class("m-0"),
153
+ ui.input_numeric(
154
+ "last_page",
155
+ "To",
156
+ value=total_pages.get(),
157
+ min=1,
158
+ max=total_pages.get(),
159
+ width="120px",
160
+ ).add_class("m-0"),
161
+ ui.input_action_button(
162
+ "start_conversion",
163
+ "Convert",
164
+ class_="btn btn-primary p-2",
165
+ ),
166
+ ),
167
+ class_="mt-3 shadow-sm p-0",
168
+ )
169
 
170
  @reactive.effect
171
  @reactive.event(input.start_conversion)
 
181
  if not first_page or not last_page or first_page > last_page:
182
  ui.notification_show("Invalid page range selected.", type="error")
183
  return
184
+
185
  is_converting.set(True)
186
  conversion_results.set([])
187
+
188
  pages_to_convert = range(first_page, last_page + 1)
189
  num_pages_to_convert = len(pages_to_convert)
190
 
 
204
  f"--pages={page_num}",
205
  ]
206
  try:
207
+ proc = await asyncio.create_subprocess_exec(
208
+ *cmd,
209
+ stdout=asyncio.subprocess.PIPE,
210
+ stderr=asyncio.subprocess.PIPE,
211
+ )
212
+ stdout, stderr = await asyncio.wait_for(
213
+ proc.communicate(), timeout=30
214
  )
215
+
216
+ if proc.returncode != 0:
217
+ raise RuntimeError(stderr.decode())
218
+
219
  if svg_path.exists():
220
  conversion_results.set(
221
  conversion_results.get()
 
238
  except Exception as e:
239
  ui.notification_show(f"Error on page {page_num}: {e}", type="error")
240
  break
241
+
 
242
  is_converting.set(False)
243
  if not conversion_results.get():
244
  ui.notification_show(
 
257
  class_="text-center text-muted m-5",
258
  ),
259
  class_="border rounded-3 d-grid align-content-center",
260
+ style="min-height: 300px;",
261
  )
262
 
263
  if is_converting.get():
 
267
  class_="text-center text-muted m-5",
268
  ),
269
  class_="border rounded-3 d-grid align-content-center",
270
+ style="min-height: 300px;",
271
  )
272
 
273
  results = conversion_results.get()
 
278
  class_="text-center text-muted m-5",
279
  ),
280
  class_="border rounded-3 d-grid align-content-center",
281
+ style="min-height: 300px;",
282
  )
283
 
284
  header = ui.div(
285
+ ui.div(
286
+ ui.strong(f"Converted {len(results)} pages"),
 
 
 
 
287
  ui.span(
288
+ f" from {pdf_filename.get()}.pdf",
289
+ class_="ms-2 text-muted",
290
  ),
291
  ),
292
  ui.div(
293
  ui.download_button(
294
+ "download_zip", "Download all", class_="btn btn-sm btn-success"
 
 
295
  ),
296
  ui.input_action_button(
297
+ "remove_pdf",
298
+ "Start over",
299
+ class_="btn btn-sm btn-danger ms-2",
300
  ),
 
301
  ),
302
+ class_="alert alert-success d-flex justify-content-between align-items-center shadow-sm",
303
  )
304
 
305
+ cards = ui.div(
306
+ {"class": "row row-cols-2 row-cols-md-4 g-3"},
307
+ *[
308
  ui.div(
309
+ {"class": "col"},
310
+ ui.card(
311
+ ui.tags.img(
312
+ src=svg["url"],
313
+ class_="card-img-top bg-white",
314
+ style="max-height: 200px; object-fit: contain;",
 
 
 
 
 
315
  ),
316
+ ui.card_body(
317
+ ui.h6(f"Page {svg['page']}", class_="card-title"),
318
+ ui.p(
319
+ f"{svg['size'] / 1024:.1f} KB",
320
+ class_="card-text small text-muted",
321
+ ),
322
+ ui.a(
323
+ "Download",
324
+ href=svg["url"],
325
+ download=svg["filename"],
326
+ class_="btn btn-sm btn-dark",
327
+ ),
328
  ),
329
+ class_="h-100 shadow-sm",
330
  ),
331
+ )
332
+ for svg in results
333
+ ],
334
+ )
 
335
 
336
+ return ui.div(header, cards)
337
 
338
  @reactive.effect
339
  @reactive.event(input.remove_pdf)
340
  def remove_current_pdf():
341
+ # Set a flag to indicate removal, which will hide the output UI
342
  is_removed.set(True)
343
+
344
+ # Reset all reactive values to their initial state
345
  conversion_results.set([])
346
  pdf_filename.set(None)
347
  pdf_path_r.set(None)
348
  total_pages.set(0)
349
+
350
+ # Clean up the session directory on the server
351
  if session_dir.exists():
352
  shutil.rmtree(session_dir, ignore_errors=True)
353
+
354
+ # Notify the user that the process is complete
355
+ ui.notification_show("Removed files. Ready for a new conversion.", duration=3)
356
 
357
  @session.on_ended
358
  def on_end():
 
371
  for svg_file in results:
372
  zipf.write(svg_file["path"], svg_file["filename"])
373
  with open(zip_path, "rb") as f:
374
+ yield f.read()