alielfilali01 commited on
Commit
64bfc65
·
verified ·
1 Parent(s): fdf12dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -33
app.py CHANGED
@@ -1,31 +1,29 @@
1
  import gradio as gr
2
  import json
3
- import requests
4
  import numpy as np
5
  import matplotlib.pyplot as plt
6
  import seaborn as sns
7
  from io import BytesIO
8
 
9
  # -------------------------------
10
- # 1. Configuration and Data Loading
11
  # -------------------------------
12
- # URL to the JSON file (the URL below resolves to the raw file)
13
- DATA_URL = "https://huggingface.co/spaces/alielfilali01/3C3H-HeatMap/resolve/main/files/aragen_v1_results.json"
14
-
15
- # Define the metrics order (6 dimensions)
16
- METRICS = ["Correctness", "Completeness", "Conciseness", "Helpfulness", "Honesty", "Harmlessness"]
17
-
18
- def load_data(url=DATA_URL):
19
- response = requests.get(url)
20
- data = response.json()
21
- # Filter out any non-model entries (e.g. timestamp entries)
22
  model_data = [entry for entry in data if "Meta" in entry]
23
  return model_data
24
 
25
  # Load the JSON data once when the app starts
26
- DATA = load_data()
27
 
28
- # Extract model names for the dropdown based on the JSON "Meta" field
29
  def get_model_names(data):
30
  model_names = [entry["Meta"]["Model Name"] for entry in data]
31
  return model_names
@@ -33,38 +31,39 @@ def get_model_names(data):
33
  MODEL_NAMES = get_model_names(DATA)
34
 
35
  # -------------------------------
36
- # 2. Heatmap Generation Functions
37
  # -------------------------------
 
 
 
38
  def generate_heatmap_image(model_entry):
39
  """
40
- Given a model entry from the JSON data, this function extracts the 6 metrics,
41
- computes a 6x6 similarity matrix using the definition: similarity = 1 - |v_i - v_j|,
42
- and returns the heatmap image as bytes.
43
  """
44
  scores = model_entry["claude-3.5-sonnet Scores"]["3C3H Scores"]
45
- # Create a vector with the metrics in the defined order
46
  v = np.array([scores[m] for m in METRICS])
47
- # Compute the 6x6 similarity matrix
48
  matrix = 1 - np.abs(np.subtract.outer(v, v))
49
-
50
- # Create a mask for the upper triangle (diagonal remains visible)
51
  mask = np.triu(np.ones_like(matrix, dtype=bool), k=1)
52
 
53
  plt.figure(figsize=(6, 5))
54
- ax = sns.heatmap(matrix,
55
- mask=mask,
56
- annot=True,
57
- fmt=".2f",
58
- cmap="viridis",
59
- xticklabels=METRICS,
60
- yticklabels=METRICS,
61
- cbar_kws={"label": "Similarity"})
62
  plt.title(f"Confusion Matrix for Model: {model_entry['Meta']['Model Name']}")
63
  plt.xlabel("Metrics")
64
  plt.ylabel("Metrics")
65
  plt.tight_layout()
66
 
67
- # Save the figure to a bytes buffer
68
  buf = BytesIO()
69
  plt.savefig(buf, format="png")
70
  plt.close()
@@ -73,8 +72,8 @@ def generate_heatmap_image(model_entry):
73
 
74
  def generate_heatmaps(selected_model_names):
75
  """
76
- Filters the global DATA for entries matching the selected model names,
77
- generates a heatmap for each one, and returns a list of image bytes.
78
  """
79
  filtered_entries = [entry for entry in DATA if entry["Meta"]["Model Name"] in selected_model_names]
80
  images = []
 
1
  import gradio as gr
2
  import json
3
+ import os
4
  import numpy as np
5
  import matplotlib.pyplot as plt
6
  import seaborn as sns
7
  from io import BytesIO
8
 
9
  # -------------------------------
10
+ # 1. Load Results from Local File
11
  # -------------------------------
12
+ def load_results():
13
+ # Get the directory of the current file
14
+ current_dir = os.path.dirname(os.path.abspath(__file__))
15
+ # Construct the path to the JSON file (assumes file is stored in "files/aragen_v1_results.json")
16
+ results_file = os.path.join(current_dir, "files", "aragen_v1_results.json")
17
+ with open(results_file, "r") as f:
18
+ data = json.load(f)
19
+ # Filter out any non-model entries (e.g., timestamp entries)
 
 
20
  model_data = [entry for entry in data if "Meta" in entry]
21
  return model_data
22
 
23
  # Load the JSON data once when the app starts
24
+ DATA = load_results()
25
 
26
+ # Extract model names for the dropdown from the JSON "Meta" field
27
  def get_model_names(data):
28
  model_names = [entry["Meta"]["Model Name"] for entry in data]
29
  return model_names
 
31
  MODEL_NAMES = get_model_names(DATA)
32
 
33
  # -------------------------------
34
+ # 2. Define Metrics and Heatmap Generation Functions
35
  # -------------------------------
36
+ # Define the six metrics in the desired order.
37
+ METRICS = ["Correctness", "Completeness", "Conciseness", "Helpfulness", "Honesty", "Harmlessness"]
38
+
39
  def generate_heatmap_image(model_entry):
40
  """
41
+ For a given model entry, extract the six metrics and compute a 6x6 similarity matrix
42
+ using the definition: similarity = 1 - |v_i - v_j|, then return the heatmap image as bytes.
 
43
  """
44
  scores = model_entry["claude-3.5-sonnet Scores"]["3C3H Scores"]
45
+ # Create a vector with the metrics in the defined order.
46
  v = np.array([scores[m] for m in METRICS])
47
+ # Compute the 6x6 similarity matrix.
48
  matrix = 1 - np.abs(np.subtract.outer(v, v))
49
+ # Create a mask for the upper triangle (keeping the diagonal visible).
 
50
  mask = np.triu(np.ones_like(matrix, dtype=bool), k=1)
51
 
52
  plt.figure(figsize=(6, 5))
53
+ sns.heatmap(matrix,
54
+ mask=mask,
55
+ annot=True,
56
+ fmt=".2f",
57
+ cmap="viridis",
58
+ xticklabels=METRICS,
59
+ yticklabels=METRICS,
60
+ cbar_kws={"label": "Similarity"})
61
  plt.title(f"Confusion Matrix for Model: {model_entry['Meta']['Model Name']}")
62
  plt.xlabel("Metrics")
63
  plt.ylabel("Metrics")
64
  plt.tight_layout()
65
 
66
+ # Save the plot to a bytes buffer.
67
  buf = BytesIO()
68
  plt.savefig(buf, format="png")
69
  plt.close()
 
72
 
73
  def generate_heatmaps(selected_model_names):
74
  """
75
+ Filter the global DATA for entries matching the selected model names,
76
+ generate a heatmap for each, and return a list of image bytes.
77
  """
78
  filtered_entries = [entry for entry in DATA if entry["Meta"]["Model Name"] in selected_model_names]
79
  images = []