cos_sim / app.py
johnnyfivefingers's picture
Update app.py
cc8c2c8
import gradio as gr
from sklearn.metrics.pairwise import cosine_similarity
#from sklearn.metrics.pairwise import euclidean_distances
def greet(array_of_vectors):
# [[0 1],[0 5]]
array_of_vectors = array_of_vectors.replace("[[", "")
array_of_vectors = array_of_vectors.replace("]]", "")
array_of_vectors = array_of_vectors.replace("[", "")
array_of_vectors = array_of_vectors.replace("]", "")
cleaned = []
vecs = array_of_vectors.split(",")
for vec in vecs:
arr = vec.split(" ")
int_arr = []
for a in arr:
int_arr.append(int(a))
cleaned.append(int_arr)
similarity_matrix2 = cosine_similarity([[0,4]], [[0,0], [0,4], [0,19]])
similarity_matrix = cosine_similarity([[0,4]], cleaned)
# similarity_matrix = euclidean_distances(cleaned, cleaned)
results = ''
for matrix in similarity_matrix:
results = results + str(matrix) + "\n"
results = results + "cleaned:" + str(cleaned) + "\n"
results = results + "sim:" + str(similarity_matrix) + "\n"
results = results + "sim2:" + str(similarity_matrix2) + "\n"
return results
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
iface.launch()