Spaces:
Runtime error
Runtime error
import spaces | |
import os | |
import gradio as gr | |
from pdf2image import convert_from_path | |
from byaldi import RAGMultiModalModel | |
from transformers import Qwen2VLForConditionalGeneration, AutoProcessor | |
from qwen_vl_utils import process_vision_info | |
import torch | |
import torchvision | |
import subprocess | |
# Run the commands from setup.sh to install poppler-utils | |
def install_poppler(): | |
try: | |
subprocess.run(["pdfinfo"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
except FileNotFoundError: | |
print("Poppler not found. Installing...") | |
# Run the setup commands | |
subprocess.run("apt-get update", shell=True) | |
subprocess.run("apt-get install -y poppler-utils", shell=True) | |
# Call the Poppler installation check | |
install_poppler() | |
# Install flash-attn if not already installed | |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True) | |
# Load the RAG Model and the Qwen2-VL-2B-Instruct model | |
RAG = RAGMultiModalModel.from_pretrained("vidore/colpali") | |
model = Qwen2VLForConditionalGeneration.from_pretrained("Qwen/Qwen2-VL-2B-Instruct", | |
trust_remote_code=True, torch_dtype=torch.bfloat16).cuda().eval() | |
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct", trust_remote_code=True) | |
def process_pdf_and_query(pdf_file, user_query): | |
# Convert the PDF to images | |
images = convert_from_path(pdf_file.name) | |
num_images = len(images) | |
# Indexing the PDF in RAG | |
RAG.index( | |
input_path=pdf_file.name, | |
index_name="image_index", # index will be saved at index_root/index_name/ | |
store_collection_with_index=False, | |
overwrite=True | |
) | |
# Search the query in the RAG model | |
results = RAG.search(user_query, k=1) | |
if not results: | |
return "No results found.", num_images | |
# Retrieve the page number and process image | |
image_index = results[0]["page_num"] - 1 | |
messages = [ | |
{ | |
"role": "user", | |
"content": [ | |
{ | |
"type": "image", | |
"image": images[image_index], | |
}, | |
{"type": "text", "text": user_query}, | |
], | |
} | |
] | |
# Generate text with the Qwen model | |
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) | |
image_inputs, video_inputs = process_vision_info(messages) | |
inputs = processor( | |
text=[text], | |
images=image_inputs, | |
videos=video_inputs, | |
padding=True, | |
return_tensors="pt", | |
) | |
inputs = inputs.to("cuda") | |
# Generate the output response | |
generated_ids = model.generate(**inputs, max_new_tokens=50) | |
generated_ids_trimmed = [ | |
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids) | |
] | |
output_text = processor.batch_decode( | |
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False | |
) | |
return output_text[0], num_images | |
pdf_input = gr.File(label="Upload PDF") | |
query_input = gr.Textbox(label="Enter your query", placeholder="Ask a question about the PDF") | |
output_text = gr.Textbox(label="Model Answer") | |
output_images = gr.Textbox(label="Number of Images in PDF") | |
# CSS styling | |
css = """ | |
body { | |
background-color: #282a36; | |
font-family: Arial, sans-serif; | |
color: #f8f8f2; | |
} | |
h1 { | |
text-align: center; | |
font-size: 2.5em; | |
font-weight: bold; | |
margin-bottom: 20px; | |
} | |
footer { | |
margin-top: 20px; | |
} | |
.duplicate-button { | |
text-align: center; | |
background-color: #50fa7b; | |
color: #282a36; | |
font-weight: bold; | |
border: none; | |
padding: 10px; | |
cursor: pointer; | |
} | |
""" | |
description = """ | |
### About Multimodal RAG | |
Multimodal Retrieval-Augmented Generation (RAG) integrates both images and text to provide more comprehensive and contextually accurate responses to user queries. It uses a retriever model like **ColPali** to search and retrieve relevant data and a large language model (LLM) like **Qwen/Qwen2-VL-2B-Instruct** to generate natural language answers based on the input. | |
In this demo, **ColPali** is used as a multimodal retriever, and the **Byaldi** library from answer.ai simplifies the use of ColPali. We are utilizing **Qwen2-VL-2B-Instruct** for text generation, enabling both text and image-based queries. | |
""" | |
footer = """ | |
<div style="text-align: center; margin-top: 20px;"> | |
<a href="https://www.linkedin.com/in/pejman-ebrahimi-4a60151a7/" target="_blank">LinkedIn</a> | | |
<a href="https://github.com/arad1367" target="_blank">GitHub</a> | | |
<a href="https://arad1367.pythonanywhere.com/" target="_blank">Live demo of my PhD defense</a> | | |
<a href="https://huggingface.co/Qwen/Qwen2-VL-2B-Instruct" target="_blank">Qwen/Qwen2-VL-2B-Instruct</a> | | |
<a href="https://github.com/AnswerDotAI/byaldi" target="_blank">Byaldi</a> | | |
<a href="https://github.com/illuin-tech/colpali" target="_blank">ColPali</a> | |
<br> | |
Made with π by <a href="https://github.com/arad1367" target="_blank">Pejman Ebrahimi</a> | |
</div> | |
""" | |
# Gradio Interface | |
with gr.Blocks(theme='freddyaboulton/dracula_revamped', css=css) as demo: | |
gr.Markdown("<h1>Multimodal RAG with Image Query</h1>") | |
gr.Markdown(description) | |
with gr.Row(): | |
pdf_input = gr.File(label="Upload PDF") | |
query_input = gr.Textbox(label="Enter your query", placeholder="Ask a question about the PDF") | |
output_text = gr.Textbox(label="Model Answer") | |
output_images = gr.Textbox(label="Number of Images in PDF") | |
gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button") | |
gr.HTML(footer) | |
demo.launch(debug=True) | |