rishiraj commited on
Commit
630c062
·
verified ·
1 Parent(s): 6523ead

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import gradio as gr
3
+ from gemini.gemini_extractor import GeminiExtractorConfig, GeminiExtractor
4
+ from oai.oai_extractor import OAIExtractorConfig, OAIExtractor
5
+ from indexify_extractor_sdk import Content
6
+
7
+ gemini_extractor = GeminiExtractor()
8
+ oai_extractor = OAIExtractor()
9
+
10
+ def use_gemini(pdf_filepath, key):
11
+ if pdf_filepath is None:
12
+ raise gr.Error("Please provide some input PDF: upload a PDF file")
13
+ with open(pdf_filepath, "rb") as f:
14
+ pdf_data = f.read()
15
+ content = Content(content_type="application/pdf", data=pdf_data)
16
+ config = GeminiExtractorConfig(prompt="Extract all text from the document.", model_name="gemini-1.5-flash", key=key)
17
+ result = gemini_extractor.extract(content, config)
18
+ return result
19
+
20
+ with gr.Blocks(title="PDF data extraction with Gemini & Indexify") as gemini_demo:
21
+ gr.HTML("<h1 style='text-align: center'>PDF data extraction with Gemini & <a href='https://getindexify.ai/'>Indexify</a></h1>")
22
+ 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>")
23
+ 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>")
24
+ gr.HTML("<h4 style='text-align: center'>Here's an example notebook that demonstrates how to build a continuous <a href='https://github.com/tensorlakeai/indexify/blob/main/docs/docs/examples/multimodal_gemini.ipynb' target='_blank'>extraction pipeline</a> with Indexify</h4>")
25
+
26
+ with gr.Row():
27
+ with gr.Column():
28
+ gr.HTML(
29
+ "<p><b>Step 1:</b> Upload a PDF file from local storage.</p>"
30
+ "<p style='color: #A0A0A0;'>Use this demo for single PDF file only. "
31
+ "You can extract from PDF files continuously and try various other extractors locally with "
32
+ "<a href='https://getindexify.ai/'>Indexify</a>.</p>"
33
+ )
34
+ pdf_file = gr.File(type="filepath")
35
+ gr.HTML("<p><b>Step 2:</b> Enter your API key.</p>")
36
+ key = gr.Textbox(info="Please enter your GEMINI_API_KEY", label="Key:")
37
+ with gr.Column():
38
+ gr.HTML("<p><b>Step 3:</b> Run the extractor.</p>")
39
+ go_button = gr.Button(value="Run extractor", variant="primary")
40
+ model_output_text_box = gr.Textbox(label="Extractor Output", elem_id="model_output_text_box")
41
+
42
+ with gr.Row():
43
+ gr.HTML("<p style='text-align: center'>Developed with 🫶 by <a href='https://getindexify.ai/' target='_blank'>Indexify</a> | a <a href='https://www.tensorlake.ai/' target='_blank'>Tensorlake</a> product</p>")
44
+
45
+ go_button.click(fn=use_gemini, inputs=[pdf_file, key], outputs=[model_output_text_box])
46
+
47
+ def use_openai(pdf_filepath, key):
48
+ if pdf_filepath is None:
49
+ raise gr.Error("Please provide some input PDF: upload a PDF file")
50
+ with open(pdf_filepath, "rb") as f:
51
+ pdf_data = f.read()
52
+ content = Content(content_type="application/pdf", data=pdf_data)
53
+ config = OAIExtractorConfig(prompt="Extract all text from the document.", model_name="gpt-4o", key=key)
54
+ result = oai_extractor.extract(content, config)
55
+ return result
56
+
57
+ with gr.Blocks(title="PDF data extraction with OpenAI & Indexify") as openai_demo:
58
+ gr.HTML("<h1 style='text-align: center'>PDF data extraction with OpenAI & <a href='https://getindexify.ai/'>Indexify</a></h1>")
59
+ 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>")
60
+ 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>")
61
+ gr.HTML("<h4 style='text-align: center'>Here's an example notebook that demonstrates how to build a continuous <a href='https://github.com/tensorlakeai/indexify/blob/main/docs/docs/examples/multimodal_openai.ipynb' target='_blank'>extraction pipeline</a> with Indexify</h4>")
62
+
63
+ with gr.Row():
64
+ with gr.Column():
65
+ gr.HTML(
66
+ "<p><b>Step 1:</b> Upload a PDF file from local storage.</p>"
67
+ "<p style='color: #A0A0A0;'>Use this demo for single PDF file only. "
68
+ "You can extract from PDF files continuously and try various other extractors locally with "
69
+ "<a href='https://getindexify.ai/'>Indexify</a>.</p>"
70
+ )
71
+ pdf_file = gr.File(type="filepath")
72
+ gr.HTML("<p><b>Step 2:</b> Enter your API key.</p>")
73
+ key = gr.Textbox(info="Please enter your OPENAI_API_KEY", label="Key:")
74
+ with gr.Column():
75
+ gr.HTML("<p><b>Step 3:</b> Run the extractor.</p>")
76
+ go_button = gr.Button(value="Run extractor", variant="primary")
77
+ model_output_text_box = gr.Textbox(label="Extractor Output", elem_id="model_output_text_box")
78
+
79
+ with gr.Row():
80
+ gr.HTML("<p style='text-align: center'>Developed with 🫶 by <a href='https://getindexify.ai/' target='_blank'>Indexify</a> | a <a href='https://www.tensorlake.ai/' target='_blank'>Tensorlake</a> product</p>")
81
+
82
+ go_button.click(fn=use_openai, inputs=[pdf_file, key], outputs=[model_output_text_box])
83
+
84
+ demo = gr.TabbedInterface([gemini_demo, openai_demo], ["Gemini Extractor", "OpenAI Extractor"], theme=gr.themes.Soft())
85
+
86
+ demo.queue()
87
+ demo.launch()