Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -43,12 +43,10 @@ def process_pdf_and_query(pdf_file, user_query):
|
|
43 |
overwrite=True
|
44 |
)
|
45 |
|
46 |
-
# Search the query in the RAG model
|
47 |
results = RAG.search(user_query, k=1)
|
48 |
if not results:
|
49 |
return "No results found.", num_images
|
50 |
|
51 |
-
# Retrieve the page number and process image
|
52 |
image_index = results[0]["page_num"] - 1
|
53 |
messages = [
|
54 |
{
|
@@ -63,7 +61,6 @@ def process_pdf_and_query(pdf_file, user_query):
|
|
63 |
}
|
64 |
]
|
65 |
|
66 |
-
|
67 |
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
68 |
image_inputs, video_inputs = process_vision_info(messages)
|
69 |
inputs = processor(
|
@@ -85,7 +82,6 @@ def process_pdf_and_query(pdf_file, user_query):
|
|
85 |
|
86 |
return output_text[0], num_images
|
87 |
|
88 |
-
|
89 |
css = """
|
90 |
.duplicate-button {
|
91 |
background-color: #6272a4;
|
@@ -104,6 +100,12 @@ css = """
|
|
104 |
border-radius: 10px;
|
105 |
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
106 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
"""
|
108 |
|
109 |
explanation = """
|
@@ -126,18 +128,18 @@ footer = """
|
|
126 |
</div>
|
127 |
"""
|
128 |
|
129 |
-
pdf_input = gr.File(label="Upload PDF")
|
130 |
-
query_input = gr.Textbox(label="Enter your query", placeholder="Ask a question about the PDF")
|
131 |
-
output_text = gr.Textbox(label="Model Answer")
|
132 |
-
output_images = gr.Textbox(label="Number of Images in PDF")
|
133 |
duplicate_button = gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")
|
134 |
|
135 |
# Launch the Gradio app
|
136 |
demo = gr.Interface(
|
137 |
fn=process_pdf_and_query,
|
138 |
-
inputs=[pdf_input, query_input],
|
139 |
-
outputs=[output_text, output_images],
|
140 |
-
title="
|
141 |
theme='freddyaboulton/dracula_revamped',
|
142 |
css=css,
|
143 |
description=explanation,
|
@@ -145,7 +147,13 @@ demo = gr.Interface(
|
|
145 |
)
|
146 |
|
147 |
with demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
gr.HTML(footer)
|
149 |
duplicate_button
|
150 |
-
|
151 |
-
demo.launch(debug=True)
|
|
|
43 |
overwrite=True
|
44 |
)
|
45 |
|
|
|
46 |
results = RAG.search(user_query, k=1)
|
47 |
if not results:
|
48 |
return "No results found.", num_images
|
49 |
|
|
|
50 |
image_index = results[0]["page_num"] - 1
|
51 |
messages = [
|
52 |
{
|
|
|
61 |
}
|
62 |
]
|
63 |
|
|
|
64 |
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
65 |
image_inputs, video_inputs = process_vision_info(messages)
|
66 |
inputs = processor(
|
|
|
82 |
|
83 |
return output_text[0], num_images
|
84 |
|
|
|
85 |
css = """
|
86 |
.duplicate-button {
|
87 |
background-color: #6272a4;
|
|
|
100 |
border-radius: 10px;
|
101 |
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
102 |
}
|
103 |
+
.title {
|
104 |
+
font-size: 30px;
|
105 |
+
font-weight: bold;
|
106 |
+
text-align: center;
|
107 |
+
margin-bottom: 20px;
|
108 |
+
}
|
109 |
"""
|
110 |
|
111 |
explanation = """
|
|
|
128 |
</div>
|
129 |
"""
|
130 |
|
131 |
+
pdf_input = gr.File(label="Upload PDF") # Single PDF file input
|
132 |
+
query_input = gr.Textbox(label="Enter your query", placeholder="Ask a question about the PDF") # User query input
|
133 |
+
output_text = gr.Textbox(label="Model Answer") # Output for the model's answer
|
134 |
+
output_images = gr.Textbox(label="Number of Images in PDF") # Output for number of images
|
135 |
duplicate_button = gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")
|
136 |
|
137 |
# Launch the Gradio app
|
138 |
demo = gr.Interface(
|
139 |
fn=process_pdf_and_query,
|
140 |
+
inputs=[pdf_input, query_input], # List of inputs
|
141 |
+
outputs=[output_text, output_images], # List of outputs
|
142 |
+
title="",
|
143 |
theme='freddyaboulton/dracula_revamped',
|
144 |
css=css,
|
145 |
description=explanation,
|
|
|
147 |
)
|
148 |
|
149 |
with demo:
|
150 |
+
gr.HTML("""
|
151 |
+
<div class='title'>
|
152 |
+
Multimodal RAG with Image Query -
|
153 |
+
<a href="https://github.com/arad1367" target="_blank" style="color: #ff79c6; text-decoration: none;">
|
154 |
+
Pejman Ebrahimi
|
155 |
+
</a>
|
156 |
+
</div>
|
157 |
+
""")
|
158 |
gr.HTML(footer)
|
159 |
duplicate_button
|
|
|
|