Update app.py
Browse files
app.py
CHANGED
@@ -1,211 +1,155 @@
|
|
1 |
-
# -*- coding: utf-8 -*-
|
2 |
-
"""
|
3 |
-
[Martinez-Gil2024] Augmenting the Interpretability of GraphCodeBERT for Code Similarity Tasks, arXiv preprint arXiv:2410.05275, 2024
|
4 |
-
|
5 |
-
@author: Jorge Martinez-Gil
|
6 |
-
"""
|
7 |
-
|
8 |
-
import
|
9 |
-
|
10 |
-
from sklearn.decomposition import PCA
|
11 |
-
import
|
12 |
-
import
|
13 |
-
import
|
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 |
-
for j in range(low, high):
|
93 |
-
if arr[j] <= pivot:
|
94 |
-
i
|
95 |
-
arr[i], arr[j] = arr[j], arr[i]
|
96 |
-
arr[i+1], arr[high] = arr[high], arr[i+1]
|
97 |
-
return (i+1)
|
98 |
-
|
99 |
-
def quick_sort(arr, low, high):
|
100 |
-
if low < high:
|
101 |
-
pi = partition(arr, low, high)
|
102 |
-
quick_sort(arr, low, pi-1)
|
103 |
-
quick_sort(arr, pi+1, high)
|
104 |
-
return arr
|
105 |
-
|
106 |
-
}
|
107 |
-
|
108 |
-
#
|
109 |
-
def get_token_embeddings(code):
|
110 |
-
inputs = tokenizer(code, return_tensors="pt", max_length=512, truncation=True, padding=True)
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
plt.ylabel('')
|
157 |
-
plt.grid(False)
|
158 |
-
plt.legend()
|
159 |
-
|
160 |
-
# Save the figure as a high-quality PNG file
|
161 |
-
output_file = os.path.join(output_dir, f"{algo1_name}_vs_{algo2_name}_tokens_2d_pca.png")
|
162 |
-
plt.savefig(output_file, format='png', dpi=300, bbox_inches='tight')
|
163 |
-
|
164 |
-
# Show the plot
|
165 |
-
plt.close()
|
166 |
-
|
167 |
-
print("All pairwise comparison images have been generated.")
|
168 |
-
|
169 |
-
|
170 |
-
import gradio as gr
|
171 |
-
from io import BytesIO
|
172 |
-
from PIL import Image
|
173 |
-
|
174 |
-
def compare_algorithms(algo1_name, algo2_name):
|
175 |
-
algo1_code = sorting_algorithms[algo1_name]
|
176 |
-
algo2_code = sorting_algorithms[algo2_name]
|
177 |
-
|
178 |
-
# Get token embeddings
|
179 |
-
algo1_embeddings, algo1_tokens = get_token_embeddings(algo1_code)
|
180 |
-
algo2_embeddings, algo2_tokens = get_token_embeddings(algo2_code)
|
181 |
-
|
182 |
-
# Combine and reduce
|
183 |
-
all_embeddings = np.concatenate((algo1_embeddings, algo2_embeddings), axis=0)
|
184 |
-
pca = PCA(n_components=2)
|
185 |
-
embeddings_2d = pca.fit_transform(all_embeddings)
|
186 |
-
|
187 |
-
# Plot
|
188 |
-
plt.figure(figsize=(6, 5), dpi=150)
|
189 |
-
plt.scatter(embeddings_2d[:len(algo1_tokens), 0], embeddings_2d[:len(algo1_tokens), 1], color='red', s=20, label=algo1_name)
|
190 |
-
plt.scatter(embeddings_2d[len(algo1_tokens):, 0], embeddings_2d[len(algo1_tokens):, 1], color='blue', s=20, label=algo2_name)
|
191 |
-
plt.xticks([]); plt.yticks([]); plt.grid(False); plt.legend()
|
192 |
-
|
193 |
-
# Save to BytesIO
|
194 |
-
buf = BytesIO()
|
195 |
-
plt.savefig(buf, format='png', bbox_inches='tight')
|
196 |
-
plt.close()
|
197 |
-
buf.seek(0)
|
198 |
-
return Image.open(buf)
|
199 |
-
|
200 |
-
interface = gr.Interface(
|
201 |
-
fn=compare_algorithms,
|
202 |
-
inputs=[
|
203 |
-
gr.Dropdown(choices=list(sorting_algorithms.keys()), label="Algorithm 1"),
|
204 |
-
gr.Dropdown(choices=list(sorting_algorithms.keys()), label="Algorithm 2")
|
205 |
-
],
|
206 |
-
outputs=gr.Image(type="pil", label="Token PCA Plot"),
|
207 |
-
title="Code Similarity Visualization with GraphCodeBERT"
|
208 |
-
)
|
209 |
-
|
210 |
-
if __name__ == "__main__":
|
211 |
-
interface.launch()
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""
|
3 |
+
[Martinez-Gil2024] Augmenting the Interpretability of GraphCodeBERT for Code Similarity Tasks, arXiv preprint arXiv:2410.05275, 2024
|
4 |
+
|
5 |
+
@author: Jorge Martinez-Gil
|
6 |
+
"""
|
7 |
+
|
8 |
+
import numpy as np
|
9 |
+
import matplotlib.pyplot as plt
|
10 |
+
from sklearn.decomposition import PCA
|
11 |
+
from transformers import RobertaTokenizer, RobertaModel
|
12 |
+
import torch
|
13 |
+
import gradio as gr
|
14 |
+
from io import BytesIO
|
15 |
+
from PIL import Image
|
16 |
+
|
17 |
+
# Load GraphCodeBERT model
|
18 |
+
tokenizer = RobertaTokenizer.from_pretrained("microsoft/graphcodebert-base")
|
19 |
+
model = RobertaModel.from_pretrained("microsoft/graphcodebert-base")
|
20 |
+
|
21 |
+
# Define sorting algorithms as strings
|
22 |
+
sorting_algorithms = {
|
23 |
+
"Bubble_Sort": """
|
24 |
+
def bubble_sort(arr):
|
25 |
+
n = len(arr)
|
26 |
+
for i in range(n):
|
27 |
+
for j in range(0, n-i-1):
|
28 |
+
if arr[j] > arr[j+1]:
|
29 |
+
arr[j], arr[j+1] = arr[j+1], arr[j]
|
30 |
+
return arr
|
31 |
+
""",
|
32 |
+
|
33 |
+
"Selection_Sort": """
|
34 |
+
def selection_sort(arr):
|
35 |
+
for i in range(len(arr)):
|
36 |
+
min_idx = i
|
37 |
+
for j in range(i+1, len(arr)):
|
38 |
+
if arr[j] < arr[min_idx]:
|
39 |
+
min_idx = j
|
40 |
+
arr[i], arr[min_idx] = arr[min_idx], arr[i]
|
41 |
+
return arr
|
42 |
+
""",
|
43 |
+
|
44 |
+
"Insertion_Sort": """
|
45 |
+
def insertion_sort(arr):
|
46 |
+
for i in range(1, len(arr)):
|
47 |
+
key = arr[i]
|
48 |
+
j = i-1
|
49 |
+
while j >= 0 and key < arr[j]:
|
50 |
+
arr[j + 1] = arr[j]
|
51 |
+
j -= 1
|
52 |
+
arr[j + 1] = key
|
53 |
+
return arr
|
54 |
+
""",
|
55 |
+
|
56 |
+
"Merge_Sort": """
|
57 |
+
def merge_sort(arr):
|
58 |
+
if len(arr) > 1:
|
59 |
+
mid = len(arr) // 2
|
60 |
+
L = arr[:mid]
|
61 |
+
R = arr[mid:]
|
62 |
+
|
63 |
+
merge_sort(L)
|
64 |
+
merge_sort(R)
|
65 |
+
|
66 |
+
i = j = k = 0
|
67 |
+
while i < len(L) and j < len(R):
|
68 |
+
if L[i] < R[j]:
|
69 |
+
arr[k] = L[i]
|
70 |
+
i += 1
|
71 |
+
else:
|
72 |
+
arr[k] = R[j]
|
73 |
+
j += 1
|
74 |
+
k += 1
|
75 |
+
|
76 |
+
while i < len(L):
|
77 |
+
arr[k] = L[i]
|
78 |
+
i += 1
|
79 |
+
k += 1
|
80 |
+
|
81 |
+
while j < len(R):
|
82 |
+
arr[k] = R[j]
|
83 |
+
j += 1
|
84 |
+
k += 1
|
85 |
+
return arr
|
86 |
+
""",
|
87 |
+
|
88 |
+
"Quick_Sort": """
|
89 |
+
def partition(arr, low, high):
|
90 |
+
i = (low - 1)
|
91 |
+
pivot = arr[high]
|
92 |
+
for j in range(low, high):
|
93 |
+
if arr[j] <= pivot:
|
94 |
+
i += 1
|
95 |
+
arr[i], arr[j] = arr[j], arr[i]
|
96 |
+
arr[i+1], arr[high] = arr[high], arr[i+1]
|
97 |
+
return (i + 1)
|
98 |
+
|
99 |
+
def quick_sort(arr, low, high):
|
100 |
+
if low < high:
|
101 |
+
pi = partition(arr, low, high)
|
102 |
+
quick_sort(arr, low, pi - 1)
|
103 |
+
quick_sort(arr, pi + 1, high)
|
104 |
+
return arr
|
105 |
+
"""
|
106 |
+
}
|
107 |
+
|
108 |
+
# Get token embeddings for a code snippet
|
109 |
+
def get_token_embeddings(code):
|
110 |
+
inputs = tokenizer(code, return_tensors="pt", max_length=512, truncation=True, padding=True)
|
111 |
+
with torch.no_grad():
|
112 |
+
outputs = model(**inputs)
|
113 |
+
token_embeddings = outputs.last_hidden_state.squeeze(0).cpu().numpy()
|
114 |
+
tokens = tokenizer.convert_ids_to_tokens(inputs['input_ids'].squeeze())
|
115 |
+
return token_embeddings, tokens
|
116 |
+
|
117 |
+
# Compare two algorithms and return PCA scatter plot
|
118 |
+
def compare_algorithms(algo1_name, algo2_name):
|
119 |
+
code1 = sorting_algorithms[algo1_name]
|
120 |
+
code2 = sorting_algorithms[algo2_name]
|
121 |
+
|
122 |
+
emb1, tokens1 = get_token_embeddings(code1)
|
123 |
+
emb2, tokens2 = get_token_embeddings(code2)
|
124 |
+
|
125 |
+
combined = np.concatenate([emb1, emb2], axis=0)
|
126 |
+
pca = PCA(n_components=2)
|
127 |
+
coords = pca.fit_transform(combined)
|
128 |
+
|
129 |
+
plt.figure(figsize=(6, 5), dpi=150)
|
130 |
+
plt.scatter(coords[:len(tokens1), 0], coords[:len(tokens1), 1], color='red', label=algo1_name, s=20)
|
131 |
+
plt.scatter(coords[len(tokens1):, 0], coords[len(tokens1):, 1], color='blue', label=algo2_name, s=20)
|
132 |
+
plt.legend()
|
133 |
+
plt.xticks([]); plt.yticks([]); plt.grid(False)
|
134 |
+
|
135 |
+
buf = BytesIO()
|
136 |
+
plt.savefig(buf, format='png', bbox_inches='tight')
|
137 |
+
plt.close()
|
138 |
+
buf.seek(0)
|
139 |
+
return Image.open(buf)
|
140 |
+
|
141 |
+
# Gradio interface
|
142 |
+
interface = gr.Interface(
|
143 |
+
fn=compare_algorithms,
|
144 |
+
inputs=[
|
145 |
+
gr.Dropdown(choices=list(sorting_algorithms.keys()), label="Algorithm 1"),
|
146 |
+
gr.Dropdown(choices=list(sorting_algorithms.keys()), label="Algorithm 2")
|
147 |
+
],
|
148 |
+
outputs=gr.Image(type="pil", label="Token Embedding PCA"),
|
149 |
+
title="GraphCodeBERT Token Embedding Comparison",
|
150 |
+
description="Visual comparison of token-level embeddings from GraphCodeBERT for classical sorting algorithms."
|
151 |
+
)
|
152 |
+
|
153 |
+
if __name__ == "__main__":
|
154 |
+
interface.launch()
|
155 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|