Revert
Browse files
app.py
CHANGED
@@ -3,7 +3,6 @@ import spaces
|
|
3 |
import torch
|
4 |
from transformers import AutoTokenizer, AutoModel
|
5 |
import plotly.graph_objects as go
|
6 |
-
import random
|
7 |
|
8 |
model_name = "mistralai/Mistral-7B-v0.1"
|
9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
@@ -28,91 +27,32 @@ def get_embedding(text):
|
|
28 |
def reduce_to_3d(embedding):
|
29 |
return embedding[:3]
|
30 |
|
31 |
-
def generate_random_color():
|
32 |
-
return f'rgb({random.randint(0, 255)}, {random.randint(0, 255)}, {random.randint(0, 255)})'
|
33 |
-
|
34 |
@spaces.GPU
|
35 |
-
def compare_embeddings(
|
36 |
-
|
37 |
-
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
# Add origin point
|
42 |
-
fig.add_trace(go.Scatter3d(x=[0], y=[0], z=[0], mode='markers', name='Origin',
|
43 |
-
marker=dict(color='black', size=8)))
|
44 |
-
|
45 |
-
# Add lines and points for each string
|
46 |
-
for i, emb in enumerate(embeddings_3d):
|
47 |
-
color = generate_random_color()
|
48 |
-
fig.add_trace(go.Scatter3d(x=[0, emb[0]], y=[0, emb[1]], z=[0, emb[2]],
|
49 |
-
mode='lines+markers', name=f'Text {i+1}',
|
50 |
-
line=dict(color=color), marker=dict(color=color)))
|
51 |
-
|
52 |
-
fig.update_layout(scene=dict(xaxis_title='X', yaxis_title='Y', zaxis_title='Z'))
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
return string_list
|
59 |
-
|
60 |
-
def remove_string(string_list):
|
61 |
-
if len(string_list) > 1:
|
62 |
-
string_list.pop()
|
63 |
-
return string_list
|
64 |
-
|
65 |
-
with gr.Blocks(title="3D Embedding Comparison") as iface:
|
66 |
-
gr.Markdown("# 3D Embedding Comparison")
|
67 |
-
gr.Markdown("Compare the embeddings of multiple strings visualized in 3D space using Mistral 7B.")
|
68 |
-
|
69 |
-
with gr.Row():
|
70 |
-
with gr.Column(scale=1):
|
71 |
-
string_inputs = gr.State([""])
|
72 |
-
with gr.Column(variant="panel") as textbox_container:
|
73 |
-
textboxes = gr.Textbox(label="Text 1")
|
74 |
-
add_button = gr.Button("Add String")
|
75 |
-
remove_button = gr.Button("Remove String")
|
76 |
-
|
77 |
-
with gr.Column(scale=2):
|
78 |
-
plot_output = gr.Plot()
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
def update_textboxes(string_list):
|
83 |
-
components = []
|
84 |
-
for i, s in enumerate(string_list):
|
85 |
-
components.append(gr.Textbox(value=s, label=f"Text {i+1}"))
|
86 |
-
return gr.Column.update(visible=True, children=components)
|
87 |
-
|
88 |
-
add_button.click(
|
89 |
-
add_string,
|
90 |
-
inputs=[string_inputs],
|
91 |
-
outputs=[string_inputs]
|
92 |
-
).then(
|
93 |
-
update_textboxes,
|
94 |
-
inputs=[string_inputs],
|
95 |
-
outputs=[textbox_container]
|
96 |
-
)
|
97 |
-
|
98 |
-
remove_button.click(
|
99 |
-
remove_string,
|
100 |
-
inputs=[string_inputs],
|
101 |
-
outputs=[string_inputs]
|
102 |
-
).then(
|
103 |
-
update_textboxes,
|
104 |
-
inputs=[string_inputs],
|
105 |
-
outputs=[textbox_container]
|
106 |
-
)
|
107 |
|
108 |
-
|
109 |
-
processed_strings = [t.strip() for t in strings if t.strip()]
|
110 |
-
return compare_embeddings(processed_strings)
|
111 |
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
iface.launch()
|
|
|
3 |
import torch
|
4 |
from transformers import AutoTokenizer, AutoModel
|
5 |
import plotly.graph_objects as go
|
|
|
6 |
|
7 |
model_name = "mistralai/Mistral-7B-v0.1"
|
8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
27 |
def reduce_to_3d(embedding):
|
28 |
return embedding[:3]
|
29 |
|
|
|
|
|
|
|
30 |
@spaces.GPU
|
31 |
+
def compare_embeddings(text1, text2):
|
32 |
+
emb1 = get_embedding(text1)
|
33 |
+
emb2 = get_embedding(text2)
|
34 |
|
35 |
+
emb1_3d = reduce_to_3d(emb1)
|
36 |
+
emb2_3d = reduce_to_3d(emb2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
fig = go.Figure(data=[
|
39 |
+
go.Scatter3d(x=[0, emb1_3d[0]], y=[0, emb1_3d[1]], z=[0, emb1_3d[2]], mode='lines+markers', name='Text 1'),
|
40 |
+
go.Scatter3d(x=[0, emb2_3d[0]], y=[0, emb2_3d[1]], z=[0, emb2_3d[2]], mode='lines+markers', name='Text 2')
|
41 |
+
])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
fig.update_layout(scene=dict(xaxis_title='X', yaxis_title='Y', zaxis_title='Z'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
return fig
|
|
|
|
|
46 |
|
47 |
+
iface = gr.Interface(
|
48 |
+
fn=compare_embeddings,
|
49 |
+
inputs=[
|
50 |
+
gr.Textbox(label="Text 1"),
|
51 |
+
gr.Textbox(label="Text 2")
|
52 |
+
],
|
53 |
+
outputs=gr.Plot(),
|
54 |
+
title="3D Embedding Comparison",
|
55 |
+
description="Compare the embeddings of two strings visualized in 3D space using Mistral 7B."
|
56 |
+
)
|
57 |
|
58 |
iface.launch()
|