Views
No views yet
EndpointHandler is a Python class that processes image and text data to generate embeddings and similarity scores using the ColQwen2 model—a visual retriever based on Qwen2-VL-2B-Instruct with the ColBERT strategy. This handler is optimized for retrieving documents and visual information based on their visual and textual features.EndpointHandler for processing PDF documents and text. PDF pages are converted to base64 images, which are then passed as input alongside text data to the handler.1import torch
2from pdf2image import convert_from_path
3import base64
4from io import BytesIO
5import requests
6
7# Function to convert PIL Image to base64 string
8def pil_image_to_base64(image):
9 """Converts a PIL Image to a base64 encoded string."""
10 buffer = BytesIO()
11 image.save(buffer, format="PNG")
12 return base64.b64encode(buffer.getvalue()).decode()
13
14# Function to convert PDF pages to base64 images
15def convert_pdf_to_base64_images(pdf_path):
16 """Converts PDF pages to base64 encoded images."""
17 pages = convert_from_path(pdf_path)
18 return [pil_image_to_base64(page) for page in pages]
19
20# Function to send payload to API and retrieve response
21def query_api(payload, api_url, headers):
22 """Sends a POST request to the API and returns the response."""
23 response = requests.post(api_url, headers=headers, json=payload)
24 return response.json()
25
26# Main execution
27if __name__ == "__main__":
28 # Convert PDF pages to base64 encoded images
29 encoded_images = convert_pdf_to_base64_images('document.pdf')
30
31 # Prepare payload
32 payload = {
33 "inputs": [],
34 "image": encoded_images,
35 "text": ["example query text"]
36 }
37
38 # API configuration
39 API_URL = "https://your-api-url"
40 headers = {
41 "Accept": "application/json",
42 "Authorization": "Bearer your_access_token",
43 "Content-Type": "application/json"
44 }
45
46 # Query the API and get output
47 output = query_api(payload=payload, api_url=API_URL, headers=headers)
48 print(output)EndpointHandler expects a dictionary containing:4.1{
2 "image": ["base64_image_string_1", "base64_image_string_2"],
3 "text": ["sample text 1", "sample text 2"],
4 "batch_size": 4
5}1{
2 "image": [[0.12, 0.34, ...], [0.56, 0.78, ...]],
3 "text": [[0.11, 0.22, ...], [0.33, 0.44, ...]],
4 "scores": [[0.87, 0.45], [0.23, 0.67]]
5}