Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
import gradio as gr
|
3 |
+
from marker.markdown_extractor import MarkdownExtractorConfig, MarkdownExtractor
|
4 |
+
from pdf-extractor.pdf_extractor import PDFExtractorConfig, PDFExtractor
|
5 |
+
from gemini.gemini_extractor import GeminiExtractorConfig, GeminiExtractor
|
6 |
+
from openai.oai_extractor import OAIExtractorConfig, OAIExtractor
|
7 |
+
from indexify_extractor_sdk import Content
|
8 |
+
|
9 |
+
markdown_extractor = MarkdownExtractor()
|
10 |
+
pdf_extractor = PDFExtractor()
|
11 |
+
gemini_extractor = GeminiExtractor()
|
12 |
+
oai_extractor = OAIExtractor()
|
13 |
+
|
14 |
+
@spaces.GPU
|
15 |
+
def use_marker(pdf_filepath):
|
16 |
+
if pdf_filepath is None:
|
17 |
+
raise gr.Error("Please provide some input PDF: upload an PDF file")
|
18 |
+
|
19 |
+
with open(pdf_filepath, "rb") as f:
|
20 |
+
pdf_data = f.read()
|
21 |
+
|
22 |
+
content = Content(content_type="application/pdf", data=pdf_data)
|
23 |
+
config = MarkdownExtractorConfig(batch_multiplier=2)
|
24 |
+
|
25 |
+
result = markdown_extractor.extract(content, config)
|
26 |
+
return result
|
27 |
+
|
28 |
+
@spaces.GPU
|
29 |
+
def use_pdf_extractor(pdf_filepath):
|
30 |
+
if pdf_filepath is None:
|
31 |
+
raise gr.Error("Please provide some input PDF: upload an PDF file")
|
32 |
+
|
33 |
+
with open(pdf_filepath, "rb") as f:
|
34 |
+
pdf_data = f.read()
|
35 |
+
|
36 |
+
content = Content(content_type="application/pdf", data=pdf_data)
|
37 |
+
config = PDFExtractorConfig(output_types=["text", "table"])
|
38 |
+
|
39 |
+
result = pdf_extractor.extract(content, config)
|
40 |
+
return result
|
41 |
+
|
42 |
+
@spaces.GPU
|
43 |
+
def use_gemini(pdf_filepath, key):
|
44 |
+
if pdf_filepath is None:
|
45 |
+
raise gr.Error("Please provide some input PDF: upload an PDF file")
|
46 |
+
|
47 |
+
with open(pdf_filepath, "rb") as f:
|
48 |
+
pdf_data = f.read()
|
49 |
+
|
50 |
+
content = Content(content_type="application/pdf", data=pdf_data)
|
51 |
+
config = GeminiExtractorConfig(prompt="Extract all text from the document.", model_name="gemini-1.5-flash", key=key)
|
52 |
+
|
53 |
+
result = gemini_extractor.extract(content, config)
|
54 |
+
return result
|
55 |
+
|
56 |
+
@spaces.GPU
|
57 |
+
def use_openai(pdf_filepath, key):
|
58 |
+
if pdf_filepath is None:
|
59 |
+
raise gr.Error("Please provide some input PDF: upload an PDF file")
|
60 |
+
|
61 |
+
with open(pdf_filepath, "rb") as f:
|
62 |
+
pdf_data = f.read()
|
63 |
+
|
64 |
+
content = Content(content_type="application/pdf", data=pdf_data)
|
65 |
+
config = OAIExtractorConfig(prompt="Extract all text from the document.", model_name="gpt-4o", key=key)
|
66 |
+
|
67 |
+
result = oai_extractor.extract(content, config)
|
68 |
+
return result
|
69 |
+
|
70 |
+
with gr.Blocks(title="PDF data extraction with Marker & Indexify") as marker_demo:
|
71 |
+
gr.HTML("<h1 style='text-align: center'>PDF data extraction with Marker & <a href='https://getindexify.ai/'>Indexify</a></h1>")
|
72 |
+
gr.HTML("<p style='text-align: center'>Indexify is a scalable realtime and continuous indexing and structured extraction engine for unstructured data to build generative AI applications</p>")
|
73 |
+
gr.HTML("<h3 style='text-align: center'>If you like this demo, please ⭐ Star us on <a href='https://github.com/tensorlakeai/indexify' target='_blank'>GitHub</a>!</h3>")
|
74 |
+
gr.HTML("<h4 style='text-align: center'>Here's an example notebook that demonstrates how to build a continous <a href='https://github.com/tensorlakeai/indexify/blob/main/docs/docs/examples/SEC_10_K_docs.ipynb' target='_blank'>extraction pipleine</a> with Indexify</h4>")
|
75 |
+
|
76 |
+
with gr.Row():
|
77 |
+
with gr.Column():
|
78 |
+
gr.HTML(
|
79 |
+
"<p><b>Step 1:</b> Upload a PDF file from local storage.</p>"
|
80 |
+
|
81 |
+
"<p style='color: #A0A0A0;'>Use this demo for single PDF file only. "
|
82 |
+
"You can extract from PDF files continuously and try various other extractors locally with "
|
83 |
+
"<a href='https://getindexify.ai/'>Indexify</a>.</p>"
|
84 |
+
)
|
85 |
+
|
86 |
+
pdf_file = gr.File(type="filepath")
|
87 |
+
|
88 |
+
with gr.Column():
|
89 |
+
gr.HTML("<p><b>Step 2:</b> Run the extractor.</p>")
|
90 |
+
|
91 |
+
go_button = gr.Button(
|
92 |
+
value="Run extractor",
|
93 |
+
variant="primary",
|
94 |
+
)
|
95 |
+
|
96 |
+
model_output_text_box = gr.Textbox(
|
97 |
+
label="Extractor Output",
|
98 |
+
elem_id="model_output_text_box",
|
99 |
+
)
|
100 |
+
|
101 |
+
with gr.Row():
|
102 |
+
|
103 |
+
gr.HTML(
|
104 |
+
"<p style='text-align: center'>"
|
105 |
+
"Developed with 🫶 by <a href='https://getindexify.ai/' target='_blank'>Indexify</a> | "
|
106 |
+
"a <a href='https://www.tensorlake.ai/' target='_blank'>Tensorlake</a> product"
|
107 |
+
"</p>"
|
108 |
+
)
|
109 |
+
|
110 |
+
go_button.click(
|
111 |
+
fn=use_marker,
|
112 |
+
inputs = [pdf_file],
|
113 |
+
outputs = [model_output_text_box]
|
114 |
+
)
|
115 |
+
|
116 |
+
demo = gr.TabbedInterface([marker_demo, pdf_demo, gemini_demo, openai_demo], ["Marker Extractor", "PDF Extractor", "Gemini Extractor", "OpenAI Extractor"], theme=gr.themes.Soft())
|
117 |
+
|
118 |
+
demo.queue()
|
119 |
+
demo.launch()
|