Liam Dyer commited on
Commit
c1d7645
1 Parent(s): 6605a5a
Files changed (1) hide show
  1. app.py +29 -15
app.py CHANGED
@@ -2,6 +2,8 @@ import spaces
2
  import gradio as gr
3
  import surya.detection as detection
4
  import surya.layout as layout
 
 
5
 
6
 
7
  # Monkey patch to prevent spawning processes
@@ -46,27 +48,39 @@ layout.batch_layout_detection = batch_layout_detection
46
  from marker.convert import convert_single_pdf
47
  from marker.models import load_all_models
48
 
49
- model_lst = load_all_models()
50
 
51
 
52
  @spaces.GPU
53
- def convert(file_path):
54
- print(file_path)
55
- global model_lst
56
-
57
- full_text, images, meta = convert_single_pdf(
58
- file_path,
59
- model_lst,
60
- max_pages=None,
61
- langs=None,
62
- batch_multiplier=16,
63
- )
 
 
 
 
 
64
 
65
- return full_text
66
 
67
 
68
  gr.Interface(
69
  convert,
70
- inputs=gr.File(label="PDF file", type="filepath"),
71
- outputs=gr.Markdown(label="Markdown"),
 
 
 
 
 
 
 
72
  ).launch()
 
2
  import gradio as gr
3
  import surya.detection as detection
4
  import surya.layout as layout
5
+ import os
6
+ import base64
7
 
8
 
9
  # Monkey patch to prevent spawning processes
 
48
  from marker.convert import convert_single_pdf
49
  from marker.models import load_all_models
50
 
51
+ model_list = load_all_models()
52
 
53
 
54
  @spaces.GPU
55
+ def convert(pdf_file, extract_images):
56
+ global model_list
57
+
58
+ full_text, images, out_meta = convert_single_pdf(pdf_file, model_list)
59
+ image_data = {}
60
+ if extract_images:
61
+ for filename, image in images.items():
62
+ image.save(filename, "PNG")
63
+
64
+ with open(filename, "rb") as f:
65
+ image_bytes = f.read()
66
+
67
+ image_base64 = base64.b64encode(image_bytes).decode("utf-8")
68
+ image_data[filename] = image_base64
69
+
70
+ os.remove(filename)
71
 
72
+ return full_text, out_meta, image_data
73
 
74
 
75
  gr.Interface(
76
  convert,
77
+ inputs=[
78
+ gr.File(label="Upload PDF", type="filepath"),
79
+ gr.Checkbox(label="Extract Images"),
80
+ ],
81
+ outputs=[
82
+ gr.Text(label="Markdown"),
83
+ gr.JSON(label="Metadata"),
84
+ gr.JSON(label="Images"),
85
+ ],
86
  ).launch()