erin99 commited on
Commit
8f4e86f
·
verified ·
1 Parent(s): b6a315a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -0
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List
2
+
3
+ import gradio as gr
4
+ from angle_emb import AnglE
5
+ from scipy import spatial
6
+
7
+ def cosine_similarity(vec1: List[int], vec2: List[int]):
8
+ """ Calculate cosine similarity between two vectors.
9
+
10
+ :param vec1: a list of integers
11
+ :param vec2: a list of integers
12
+ :return: a float value between 0 and 1, indicating the similarity between the two vectors.
13
+ """
14
+ return 1 - spatial.distance.cosine(vec1, vec2)
15
+
16
+
17
+ uae = AnglE.from_pretrained('WhereIsAI/UAE-Large-V1')
18
+ uae_code = AnglE.from_pretrained('WhereIsAI/UAE-Code-Large-V1')
19
+
20
+
21
+ def code_similarity(code1, code2, model):
22
+ if model == "UAE-Code-Large-V1":
23
+ v1 = uae_code.encode(code1)
24
+ v2 = uae_code.encode(code2)
25
+ else:
26
+ v1 = uae.encode(code1)
27
+ v2 = uae.encode(code2)
28
+ return cosine_similarity(v1, v2)
29
+
30
+
31
+ demo = gr.Interface(
32
+ code_similarity,
33
+ [
34
+ gr.Textbox(label='code 1'),
35
+ gr.Textbox(label='code 2'),
36
+ gr.CheckboxGroup(["UAE-Code-Large-V1", "UAE-Large-V1"], value="UAE-Code-Large-V1", label="model"),
37
+ ],
38
+ "float",
39
+ examples=[
40
+ # sample 1
41
+ ["""# Approach 2: Quicksort using list comprehension
42
+
43
+ def quicksort(arr):
44
+ if len(arr) <= 1:
45
+ return arr
46
+ else:
47
+ pivot = arr[0]
48
+ left = [x for x in arr[1:] if x < pivot]
49
+ right = [x for x in arr[1:] if x >= pivot]
50
+ return quicksort(left) + [pivot] + quicksort(right)
51
+
52
+ # Example usage
53
+ arr = [1, 7, 4, 1, 10, 9, -2]
54
+ sorted_arr = quicksort(arr)
55
+ print("Sorted Array in Ascending Order:")
56
+ print(sorted_arr)""", """def quick_sort(arr):
57
+ if len(arr) <= 1:
58
+ return arr
59
+ else:
60
+ return qsort([x for x in arr[1:] if x < arr[0]])
61
+ + [arr[0]]
62
+ + qsort([x for x in arr[1:] if x >= arr[0]])""", """UAE-Code-Large-V1"""
63
+ ],
64
+ # sample 2
65
+ ["""def bubblesort(elements):
66
+ # Looping from size of array from last index[-1] to index [0]
67
+ for n in range(len(elements)-1, 0, -1):
68
+ swapped = False
69
+ for i in range(n):
70
+ if elements[i] > elements[i + 1]:
71
+ swapped = True
72
+ # swapping data if the element is less than next element in the array
73
+ elements[i], elements[i + 1] = elements[i + 1], elements[i]
74
+ if not swapped:
75
+ # exiting the function if we didn't make a single swap
76
+ # meaning that the array is already sorted.
77
+ return
78
+
79
+ elements = [39, 12, 18, 85, 72, 10, 2, 18]
80
+
81
+ print("Unsorted list is,")
82
+ print(elements)
83
+ bubblesort(elements)
84
+ print("Sorted Array is, ")
85
+ print(elements)""", """def quick_sort(arr):
86
+ if len(arr) <= 1:
87
+ return arr
88
+ else:
89
+ return qsort([x for x in arr[1:] if x < arr[0]])
90
+ + [arr[0]]
91
+ + qsort([x for x in arr[1:] if x >= arr[0]])""", """UAE-Code-Large-V1"""
92
+ ],
93
+ ]
94
+ )
95
+
96
+ if __name__ == "__main__":
97
+ demo.launch()