Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,11 @@
|
|
1 |
import os
|
2 |
import torch
|
3 |
-
from datasets import load_dataset
|
4 |
-
from qdrant_client import QdrantClient
|
5 |
-
from qdrant_client.http import models
|
6 |
-
from tqdm import tqdm
|
7 |
-
from colpali_engine.models import ColQwen2, ColQwen2Processor
|
8 |
import gradio as gr
|
9 |
import requests
|
10 |
from PIL import Image
|
11 |
from io import BytesIO
|
12 |
-
|
13 |
-
|
14 |
-
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
|
15 |
|
16 |
# Initialize ColPali model and processor
|
17 |
model_name = "vidore/colqwen2-v0.1"
|
@@ -30,8 +24,7 @@ QDRANT_API_KEY = os.getenv("QDRANT_API_KEY")
|
|
30 |
qdrant_client = QdrantClient(url="https://davanstrien-qdrant-test.hf.space",
|
31 |
port=None, api_key=QDRANT_API_KEY, timeout=10)
|
32 |
|
33 |
-
|
34 |
-
collection_name = "song_sheets" # Replace with your actual collection name
|
35 |
|
36 |
def search_images_by_text(query_text, top_k=5):
|
37 |
# Process and encode the text query
|
@@ -52,19 +45,20 @@ def search_images_by_text(query_text, top_k=5):
|
|
52 |
|
53 |
return search_result
|
54 |
|
55 |
-
def modify_iiif_url(url,
|
|
|
56 |
parts = url.split('/')
|
57 |
size_index = -3
|
58 |
-
parts[size_index] = f"{
|
59 |
return '/'.join(parts)
|
60 |
|
61 |
-
def search_and_display(query, top_k,
|
62 |
results = search_images_by_text(query, top_k)
|
63 |
images = []
|
64 |
captions = []
|
65 |
|
66 |
for result in results.points:
|
67 |
-
modified_url = modify_iiif_url(result.payload['image_url'],
|
68 |
response = requests.get(modified_url)
|
69 |
img = Image.open(BytesIO(response.content)).convert("RGB")
|
70 |
images.append(img)
|
@@ -78,15 +72,14 @@ iface = gr.Interface(
|
|
78 |
inputs=[
|
79 |
gr.Textbox(label="Search Query"),
|
80 |
gr.Slider(minimum=1, maximum=20, step=1, label="Number of Results", value=5),
|
81 |
-
gr.Slider(minimum=
|
82 |
-
gr.Slider(minimum=100, maximum=1000, step=50, label="Image Height", value=300)
|
83 |
],
|
84 |
outputs=[
|
85 |
gr.Gallery(label="Search Results", show_label=False, columns=5, height="auto"),
|
86 |
gr.JSON(label="Captions")
|
87 |
],
|
88 |
-
title="Image Search with IIIF Resizing",
|
89 |
-
description="Enter a text query to search for images. You can adjust the number of results and the size of the returned images."
|
90 |
)
|
91 |
|
92 |
# Launch the Gradio interface
|
|
|
1 |
import os
|
2 |
import torch
|
|
|
|
|
|
|
|
|
|
|
3 |
import gradio as gr
|
4 |
import requests
|
5 |
from PIL import Image
|
6 |
from io import BytesIO
|
7 |
+
from qdrant_client import QdrantClient
|
8 |
+
from transformers import ColQwen2, ColQwen2Processor
|
|
|
9 |
|
10 |
# Initialize ColPali model and processor
|
11 |
model_name = "vidore/colqwen2-v0.1"
|
|
|
24 |
qdrant_client = QdrantClient(url="https://davanstrien-qdrant-test.hf.space",
|
25 |
port=None, api_key=QDRANT_API_KEY, timeout=10)
|
26 |
|
27 |
+
collection_name = "your_collection_name" # Replace with your actual collection name
|
|
|
28 |
|
29 |
def search_images_by_text(query_text, top_k=5):
|
30 |
# Process and encode the text query
|
|
|
45 |
|
46 |
return search_result
|
47 |
|
48 |
+
def modify_iiif_url(url, size_percent):
|
49 |
+
# Modify the IIIF URL to use percentage scaling
|
50 |
parts = url.split('/')
|
51 |
size_index = -3
|
52 |
+
parts[size_index] = f"pct:{size_percent}"
|
53 |
return '/'.join(parts)
|
54 |
|
55 |
+
def search_and_display(query, top_k, size_percent):
|
56 |
results = search_images_by_text(query, top_k)
|
57 |
images = []
|
58 |
captions = []
|
59 |
|
60 |
for result in results.points:
|
61 |
+
modified_url = modify_iiif_url(result.payload['image_url'], size_percent)
|
62 |
response = requests.get(modified_url)
|
63 |
img = Image.open(BytesIO(response.content)).convert("RGB")
|
64 |
images.append(img)
|
|
|
72 |
inputs=[
|
73 |
gr.Textbox(label="Search Query"),
|
74 |
gr.Slider(minimum=1, maximum=20, step=1, label="Number of Results", value=5),
|
75 |
+
gr.Slider(minimum=1, maximum=100, step=1, label="Image Size (%)", value=100)
|
|
|
76 |
],
|
77 |
outputs=[
|
78 |
gr.Gallery(label="Search Results", show_label=False, columns=5, height="auto"),
|
79 |
gr.JSON(label="Captions")
|
80 |
],
|
81 |
+
title="Image Search with IIIF Percentage Resizing",
|
82 |
+
description="Enter a text query to search for images. You can adjust the number of results and the size of the returned images as a percentage of the original size."
|
83 |
)
|
84 |
|
85 |
# Launch the Gradio interface
|