File size: 4,335 Bytes
6a40ae3
 
64bfc65
6a40ae3
 
 
 
3070f01
6a40ae3
 
64bfc65
6a40ae3
64bfc65
 
 
 
 
 
 
6a40ae3
 
 
 
64bfc65
6a40ae3
64bfc65
6a40ae3
 
 
 
 
 
 
64bfc65
6a40ae3
64bfc65
 
 
cdd8617
6a40ae3
64bfc65
bd89ad5
6a40ae3
 
64bfc65
6a40ae3
64bfc65
6a40ae3
64bfc65
6a40ae3
 
692e84c
 
64bfc65
 
 
 
 
 
 
 
6a40ae3
 
 
 
 
64bfc65
6a40ae3
692e84c
6a40ae3
 
692e84c
7ce9f0f
d33c3e8
692e84c
 
 
 
 
d33c3e8
6a40ae3
 
 
64bfc65
bd89ad5
6a40ae3
 
7ce9f0f
 
 
 
6a40ae3
 
 
 
 
692e84c
 
 
 
 
 
 
21aa39f
4a44340
 
 
2028153
4a44340
 
b9c157e
6a40ae3
00f7c3d
4a44340
6a40ae3
 
692e84c
 
 
 
 
 
 
 
6a40ae3
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import gradio as gr
import json
import os
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from io import BytesIO
from PIL import Image

# -------------------------------
# 1. Load Results from Local File
# -------------------------------
def load_results():
    # Get the directory of the current file
    current_dir = os.path.dirname(os.path.abspath(__file__))
    results_file = os.path.join(current_dir, "files", "aragen_v1_results.json")
    with open(results_file, "r") as f:
        data = json.load(f)
    # Filter out any non-model entries (e.g., timestamp entries)
    model_data = [entry for entry in data if "Meta" in entry]
    return model_data

# Load the JSON data once when the app starts
DATA = load_results()

# Extract model names for the dropdown from the JSON "Meta" field
def get_model_names(data):
    model_names = [entry["Meta"]["Model Name"] for entry in data]
    return model_names

MODEL_NAMES = get_model_names(DATA)

# -------------------------------
# 2. Define Metrics and Heatmap Generation Functions
# -------------------------------
# Define the six metrics in the desired order.
METRICS = ["Correctness", "Completeness", "Conciseness", "Helpfulness", "Honesty", "Harmlessness"]

def generate_heatmap_image(model_entry):
    """
    For a given model entry, extract the six metrics and compute a 6x6 similarity matrix
    using the definition: similarity = 1 - |v_i - v_j|, then return the heatmap as a PIL image.
    """
    scores = model_entry["claude-3.5-sonnet Scores"]["3C3H Scores"]
    # Create a vector with the metrics in the defined order.
    v = np.array([scores[m] for m in METRICS])
    # Compute the 6x6 similarity matrix.
    matrix = 1 - np.abs(np.subtract.outer(v, v))
    # Create a mask for the upper triangle (keeping the diagonal visible).
    mask = np.triu(np.ones_like(matrix, dtype=bool), k=1)
    
    # Set a consistent figure size that will work well in the gallery
    plt.figure(figsize=(6, 5), dpi=100)
    sns.heatmap(matrix,
                mask=mask,
                annot=True,
                fmt=".2f",
                cmap="viridis",
                xticklabels=METRICS,
                yticklabels=METRICS,
                cbar_kws={"label": "Similarity"})
    plt.title(f"Confusion Matrix for Model: {model_entry['Meta']['Model Name']}")
    plt.xlabel("Metrics")
    plt.ylabel("Metrics")
    plt.tight_layout()
    
    # Save the plot to a bytes buffer.
    buf = BytesIO()
    plt.savefig(buf, format="png", bbox_inches="tight")
    plt.close()
    buf.seek(0)
    
    # Convert the buffer into a PIL Image.
    image = Image.open(buf).convert("RGB")
    
    # Resize the image to a reasonable fixed size for the gallery
    max_size = (800, 600)
    image.thumbnail(max_size, Image.Resampling.LANCZOS)
    
    return image

def generate_heatmaps(selected_model_names):
    """
    Filter the global DATA for entries matching the selected model names,
    generate a heatmap for each, and return a list of PIL images.
    """
    filtered_entries = [entry for entry in DATA if entry["Meta"]["Model Name"] in selected_model_names]
    images = []
    for entry in filtered_entries:
        img = generate_heatmap_image(entry)
        images.append(img)
    return images

# -------------------------------
# 3. Build the Gradio Interface
# -------------------------------
with gr.Blocks(css="""
    .gallery-item img {
        max-width: 100% !important;
        max-height: 100% !important;
        object-fit: contain !important;
    }
""") as demo:
    gr.HTML("""
    <center>
    <br></br>
    <h1>3C3H Heatmap Generator</h1>
    <h3>Select the models you want to compare and generate their heatmaps below.</h3>
    <br></br>
    </center>
    """)
    with gr.Row():
        default_models = ["silma-ai/SILMA-9B-Instruct-v1.0", "google/gemma-2-9b-it"]
        model_dropdown = gr.Dropdown(choices=MODEL_NAMES, label="Select Model(s)", multiselect=True, value=default_models)
    
    generate_btn = gr.Button("Generate Heatmaps")
    
    # Set height and columns for better display
    gallery = gr.Gallery(
        label="Heatmaps", 
        columns=2, 
        height="auto",
        object_fit="contain"
    )
    
    generate_btn.click(fn=generate_heatmaps, inputs=model_dropdown, outputs=gallery)

demo.launch()