Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,8 @@ from PIL import Image
|
|
10 |
from torch.utils.data import DataLoader
|
11 |
from tqdm import tqdm
|
12 |
|
|
|
|
|
13 |
from pqdm.processes import pqdm
|
14 |
|
15 |
from colpali_engine.models import ColQwen2, ColQwen2Processor
|
@@ -42,7 +44,7 @@ DEFAULT_SYSTEM_PROMPT = """
|
|
42 |
Answer in the same language as the query.
|
43 |
"""
|
44 |
|
45 |
-
def query_gpt4o_mini(query, images, api_key, system_prompt=DEFAULT_SYSTEM_PROMPT):
|
46 |
"""Calls OpenAI's GPT-4o-mini with the query and image data."""
|
47 |
|
48 |
if api_key and api_key.startswith("sk"):
|
@@ -135,9 +137,15 @@ def extract_context(images, api_key, window=10):
|
|
135 |
prompt = "Give the general context about these pages."
|
136 |
window_contexts = []
|
137 |
|
138 |
-
args = [
|
139 |
-
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
# for i in tqdm(range(0, len(images), window), desc="Extracting context", total=len(images)//window):
|
143 |
# window_images = images[max(i-window+1, 0):i+1]
|
@@ -149,6 +157,8 @@ def extract_context(images, api_key, window=10):
|
|
149 |
context = window_contexts[i//window]
|
150 |
contexts.append(context)
|
151 |
|
|
|
|
|
152 |
assert len(contexts) == len(images)
|
153 |
return contexts
|
154 |
|
|
|
10 |
from torch.utils.data import DataLoader
|
11 |
from tqdm import tqdm
|
12 |
|
13 |
+
from functools import partial
|
14 |
+
|
15 |
from pqdm.processes import pqdm
|
16 |
|
17 |
from colpali_engine.models import ColQwen2, ColQwen2Processor
|
|
|
44 |
Answer in the same language as the query.
|
45 |
"""
|
46 |
|
47 |
+
def query_gpt4o_mini(query, images, api_key=None, system_prompt=DEFAULT_SYSTEM_PROMPT):
|
48 |
"""Calls OpenAI's GPT-4o-mini with the query and image data."""
|
49 |
|
50 |
if api_key and api_key.startswith("sk"):
|
|
|
137 |
prompt = "Give the general context about these pages."
|
138 |
window_contexts = []
|
139 |
|
140 |
+
args = [
|
141 |
+
{
|
142 |
+
'query': prompt,
|
143 |
+
'images': zip(images[max(i-window+1, 0):i+1], [None]*len(images[max(i-window+1, 0):i+1])),
|
144 |
+
'api_key': api_key,
|
145 |
+
'system_prompt': DEFAULT_CONTEXT_PROMPT
|
146 |
+
} for i in range(0, len(images), window)
|
147 |
+
]
|
148 |
+
window_contexts = pqdm(args, query_gpt4o_mini, n_jobs=8, argument_type='kwargs')
|
149 |
|
150 |
# for i in tqdm(range(0, len(images), window), desc="Extracting context", total=len(images)//window):
|
151 |
# window_images = images[max(i-window+1, 0):i+1]
|
|
|
157 |
context = window_contexts[i//window]
|
158 |
contexts.append(context)
|
159 |
|
160 |
+
print(f"Example context: {contexts[0]}")
|
161 |
+
|
162 |
assert len(contexts) == len(images)
|
163 |
return contexts
|
164 |
|