Spaces:
Running
Running
| import base64 | |
| import time | |
| from io import BytesIO | |
| def image_to_base64(image): | |
| buffered = BytesIO() | |
| image.save(buffered, format="PNG") | |
| img_data = base64.b64encode(buffered.getvalue()).decode() | |
| return img_data | |
| def get_examples_by_category(evaluation_data, category): | |
| return [ex["id"] for ex in evaluation_data if ex["category"] == category] | |
| def display_model_responses_html(evaluation_data, responses, model, start_index=0, batch_size=5): | |
| start_time = time.time() | |
| html = "" | |
| for ex in evaluation_data[start_index:start_index + batch_size]: | |
| image_thumbnail = ex["image_thumbnail"] | |
| image_full_url = ex["image_full_url"] | |
| img_id = ex["id"] | |
| prompt = ex["prompt"] | |
| response = responses[model].get(ex["id"], "(No response)") | |
| category = ex["category"] | |
| html += f""" | |
| <div style="display: flex; margin-bottom: 20px; align-items: flex-start;"> | |
| <a href="{image_full_url}" target="_blank"> | |
| <img src="data:image/png;base64,{image_thumbnail}" alt="Example Image" | |
| style="height: 200px; margin-right: 20px; border-radius: 8px; cursor: zoom-in;"/> | |
| <!--onclick="openImage(this.src)" /> --> | |
| </a> | |
| <div style="flex: 1; background-color: #f8f9fa; padding: 16px 20px; border-radius: 12px; | |
| font-family: 'Segoe UI', sans-serif; box-shadow: 0 2px 6px rgba(0,0,0,0.05);"> | |
| <p style="margin: 0 0 10px; font-size: 14px; color: #6c757d;"><strong style="color: #343a40;">Category:</strong> {category}</p> | |
| <p style="margin: 0 0 10px; font-size: 16px;"><strong style="color: #495057;">Prompt:</strong> {prompt}</p> | |
| <p style="margin: 0; font-size: 16px;"><strong style="color: #495057;">Response:</strong> {response}</p> | |
| </div> | |
| </div> | |
| <hr/> | |
| """ | |
| elapsed_time = time.time() - start_time | |
| print(f"[TRACE] display_model_responses_html took {elapsed_time:.3f} seconds for {batch_size} items") | |
| return html | |
| def display_example_responses_html(evaluation_data, responses, models, example_id): | |
| ex = next(e for e in evaluation_data if e["id"] == example_id) | |
| image_thumbnail = ex["image_thumbnail"] | |
| image_full_url = ex["image_full_url"] | |
| category = ex["category"] | |
| prompt = ex["prompt"] | |
| responses_html = "" | |
| for model in models: | |
| response = responses[model].get(example_id, "(No response)") | |
| responses_html += f'<p style="margin: 0 0 8px;"><strong style="color: #495057;">{model}:</strong> {response}</p>' | |
| html = f""" | |
| <div style="display: flex; margin-bottom: 20px; align-items: flex-start;"> | |
| <a href="{image_full_url}" target="_blank"> | |
| <img src="data:image/png;base64,{image_thumbnail}" alt="Example Image" | |
| style="height: 200px; margin-right: 20px; border-radius: 8px; cursor: zoom-in;"/> | |
| <!--onclick="openImage(this.src)" /> --> | |
| </a> | |
| <div style="flex: 1; background-color: #f8f9fa; padding: 16px 20px; border-radius: 12px; | |
| font-family: 'Segoe UI', sans-serif; box-shadow: 0 2px 6px rgba(0,0,0,0.05);"> | |
| <p style="margin: 0 0 10px; font-size: 14px; color: #6c757d;"> | |
| <strong style="color: #343a40;">Category:</strong> {category}</p> | |
| <p style="margin: 0 0 10px; font-size: 16px;"> | |
| <strong style="color: #495057;">Prompt:</strong> {prompt}</p> | |
| <div> | |
| <strong style="color: #495057; font-size: 16px;">Responses:</strong> | |
| {responses_html} | |
| </div> | |
| </div> | |
| </div> | |
| <hr/> | |
| """ | |
| return html |