Spaces:
Running
Running
Commit
·
81dc3a6
1
Parent(s):
9b2cf74
Add files
Browse files- data_utils.py +47 -0
- utils.py +81 -0
data_utils.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image, UnidentifiedImageError
|
2 |
+
from utils import *
|
3 |
+
|
4 |
+
|
5 |
+
def get_evaluation_data(ds):
|
6 |
+
evaluation_data = []
|
7 |
+
for i in range(len(ds)):
|
8 |
+
try:
|
9 |
+
img = ds[i]["image"]
|
10 |
+
thumbnail_img = img.copy()
|
11 |
+
thumbnail_img.thumbnail((256, 256))
|
12 |
+
evaluation_data.append({
|
13 |
+
"id": ds[i]["ex_id"],
|
14 |
+
"image_thumbnail": image_to_base64(thumbnail_img),
|
15 |
+
"image_full": image_to_base64(img),
|
16 |
+
"image_full_url": "https://huggingface.co/", # Dummy
|
17 |
+
"prompt": ds[i]["prompt"],
|
18 |
+
"category": ds[i]["category"]
|
19 |
+
})
|
20 |
+
except (UnidentifiedImageError, OSError, ValueError): # To handle .heic images -> can be removed when dataset is fixed
|
21 |
+
img = Image.new("RGB", (256, 256), color="white")
|
22 |
+
evaluation_data.append({
|
23 |
+
"id": i,
|
24 |
+
"image_thumbnail": image_to_base64(img),
|
25 |
+
"image_full": image_to_base64(img),
|
26 |
+
"image_full_url": "https://huggingface.co/", # Dummy
|
27 |
+
"prompt": "Dummy prompt",
|
28 |
+
"category": "Dummy category"
|
29 |
+
})
|
30 |
+
return evaluation_data
|
31 |
+
|
32 |
+
def get_model_names():
|
33 |
+
models = ["Qwen2.5-VL", "gemma-3"]
|
34 |
+
return models
|
35 |
+
|
36 |
+
def get_responses():
|
37 |
+
responses = {
|
38 |
+
"Qwen2.5-VL": {
|
39 |
+
0: "Laws of the Universe - Toro y Moi",
|
40 |
+
1: "Smile, have a nice day!",
|
41 |
+
},
|
42 |
+
"gemma-3": {
|
43 |
+
0: "Houdini - Dua Lipa",
|
44 |
+
1: "Smile, you're on camera!"
|
45 |
+
},
|
46 |
+
}
|
47 |
+
return responses
|
utils.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
import time
|
3 |
+
|
4 |
+
from io import BytesIO
|
5 |
+
|
6 |
+
def image_to_base64(image):
|
7 |
+
buffered = BytesIO()
|
8 |
+
image.save(buffered, format="PNG")
|
9 |
+
img_data = base64.b64encode(buffered.getvalue()).decode()
|
10 |
+
return img_data
|
11 |
+
|
12 |
+
def get_examples_by_category(evaluation_data, category):
|
13 |
+
return [ex["id"] for ex in evaluation_data if ex["category"] == category]
|
14 |
+
|
15 |
+
def display_model_responses_html(evaluation_data, responses, model, start_index=0, batch_size=5):
|
16 |
+
start_time = time.time()
|
17 |
+
html = ""
|
18 |
+
for ex in evaluation_data[start_index:start_index + batch_size]:
|
19 |
+
image_thumbnail = ex["image_thumbnail"]
|
20 |
+
image_full_url = ex["image_full_url"]
|
21 |
+
img_id = ex["id"]
|
22 |
+
prompt = ex["prompt"]
|
23 |
+
response = responses[model].get(ex["id"], "(No response)")
|
24 |
+
category = ex["category"]
|
25 |
+
|
26 |
+
html += f"""
|
27 |
+
<div style="display: flex; margin-bottom: 20px; align-items: flex-start;">
|
28 |
+
<a href="{image_full_url}" target="_blank">
|
29 |
+
<img src="data:image/png;base64,{image_thumbnail}" alt="Example Image"
|
30 |
+
style="height: 200px; margin-right: 20px; border-radius: 8px; cursor: zoom-in;"/>
|
31 |
+
<!--onclick="openImage(this.src)" /> -->
|
32 |
+
</a>
|
33 |
+
<div style="flex: 1; background-color: #f8f9fa; padding: 16px 20px; border-radius: 12px;
|
34 |
+
font-family: 'Segoe UI', sans-serif; box-shadow: 0 2px 6px rgba(0,0,0,0.05);">
|
35 |
+
<p style="margin: 0 0 10px; font-size: 14px; color: #6c757d;"><strong style="color: #343a40;">Category:</strong> {category}</p>
|
36 |
+
<p style="margin: 0 0 10px; font-size: 16px;"><strong style="color: #495057;">Prompt:</strong> {prompt}</p>
|
37 |
+
<p style="margin: 0; font-size: 16px;"><strong style="color: #495057;">Response:</strong> {response}</p>
|
38 |
+
</div>
|
39 |
+
</div>
|
40 |
+
<hr/>
|
41 |
+
"""
|
42 |
+
elapsed_time = time.time() - start_time
|
43 |
+
print(f"[TRACE] display_model_responses_html took {elapsed_time:.3f} seconds for {batch_size} items")
|
44 |
+
|
45 |
+
return html
|
46 |
+
|
47 |
+
def display_example_responses_html(evaluation_data, responses, models, example_id):
|
48 |
+
ex = next(e for e in evaluation_data if e["id"] == example_id)
|
49 |
+
image_thumbnail = ex["image_thumbnail"]
|
50 |
+
image_full_url = ex["image_full_url"]
|
51 |
+
|
52 |
+
category = ex["category"]
|
53 |
+
prompt = ex["prompt"]
|
54 |
+
|
55 |
+
responses_html = ""
|
56 |
+
for model in models:
|
57 |
+
response = responses[model].get(example_id, "(No response)")
|
58 |
+
responses_html += f'<p style="margin: 0 0 8px;"><strong style="color: #495057;">{model}:</strong> {response}</p>'
|
59 |
+
|
60 |
+
html = f"""
|
61 |
+
<div style="display: flex; margin-bottom: 20px; align-items: flex-start;">
|
62 |
+
<a href="{image_full_url}" target="_blank">
|
63 |
+
<img src="data:image/png;base64,{image_thumbnail}" alt="Example Image"
|
64 |
+
style="height: 200px; margin-right: 20px; border-radius: 8px; cursor: zoom-in;"/>
|
65 |
+
<!--onclick="openImage(this.src)" /> -->
|
66 |
+
</a>
|
67 |
+
<div style="flex: 1; background-color: #f8f9fa; padding: 16px 20px; border-radius: 12px;
|
68 |
+
font-family: 'Segoe UI', sans-serif; box-shadow: 0 2px 6px rgba(0,0,0,0.05);">
|
69 |
+
<p style="margin: 0 0 10px; font-size: 14px; color: #6c757d;">
|
70 |
+
<strong style="color: #343a40;">Category:</strong> {category}</p>
|
71 |
+
<p style="margin: 0 0 10px; font-size: 16px;">
|
72 |
+
<strong style="color: #495057;">Prompt:</strong> {prompt}</p>
|
73 |
+
<div>
|
74 |
+
<strong style="color: #495057; font-size: 16px;">Responses:</strong>
|
75 |
+
{responses_html}
|
76 |
+
</div>
|
77 |
+
</div>
|
78 |
+
</div>
|
79 |
+
<hr/>
|
80 |
+
"""
|
81 |
+
return html
|