Spaces:
Runtime error
Runtime error
Liam Dyer
commited on
huge
Browse files
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 |
-
|
| 50 |
|
| 51 |
|
| 52 |
@spaces.GPU
|
| 53 |
-
def convert(
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
return full_text
|
| 66 |
|
| 67 |
|
| 68 |
gr.Interface(
|
| 69 |
convert,
|
| 70 |
-
inputs=
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|