v2p2
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import spaces
|
|
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,32 +28,56 @@ def get_embedding(text):
|
|
27 |
def reduce_to_3d(embedding):
|
28 |
return embedding[:3]
|
29 |
|
|
|
|
|
|
|
30 |
@spaces.GPU
|
31 |
-
def compare_embeddings(
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
emb1_3d = reduce_to_3d(emb1)
|
36 |
-
emb2_3d = reduce_to_3d(emb2)
|
37 |
|
38 |
-
fig = go.Figure(
|
39 |
-
|
40 |
-
|
41 |
-
])
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
fig.update_layout(scene=dict(xaxis_title='X', yaxis_title='Y', zaxis_title='Z'))
|
44 |
|
45 |
return fig
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
iface.launch()
|
|
|
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 |
def reduce_to_3d(embedding):
|
29 |
return embedding[:3]
|
30 |
|
31 |
+
def 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(*texts):
|
36 |
+
embeddings = [get_embedding(text) for text in texts if text.strip()]
|
37 |
+
embeddings_3d = [reduce_to_3d(emb) for emb in embeddings]
|
|
|
|
|
|
|
38 |
|
39 |
+
fig = go.Figure()
|
40 |
+
|
41 |
+
# Add black origin point
|
42 |
+
fig.add_trace(go.Scatter3d(x=[0], y=[0], z=[0], mode='markers', marker=dict(size=5, color='black'), name='Origin'))
|
43 |
+
|
44 |
+
# Add lines and points for each text
|
45 |
+
for i, emb in enumerate(embeddings_3d):
|
46 |
+
color = random_color()
|
47 |
+
fig.add_trace(go.Scatter3d(x=[0, emb[0]], y=[0, emb[1]], z=[0, emb[2]], mode='lines+markers',
|
48 |
+
line=dict(color=color), marker=dict(color=color), name=f'Text {i+1}'))
|
49 |
|
50 |
fig.update_layout(scene=dict(xaxis_title='X', yaxis_title='Y', zaxis_title='Z'))
|
51 |
|
52 |
return fig
|
53 |
|
54 |
+
def add_textbox(textboxes):
|
55 |
+
textboxes.append(gr.Textbox(label=f"Text {len(textboxes) + 1}"))
|
56 |
+
return textboxes
|
57 |
+
|
58 |
+
def remove_textbox(textboxes):
|
59 |
+
if len(textboxes) > 2:
|
60 |
+
textboxes.pop()
|
61 |
+
return textboxes
|
62 |
+
|
63 |
+
with gr.Blocks() as iface:
|
64 |
+
gr.Markdown("# 3D Embedding Comparison")
|
65 |
+
gr.Markdown("Compare the embeddings of multiple strings visualized in 3D space using Mistral 7B.")
|
66 |
+
|
67 |
+
with gr.Column():
|
68 |
+
textboxes = gr.List([gr.Textbox(label="Text 1"), gr.Textbox(label="Text 2")])
|
69 |
+
|
70 |
+
with gr.Row():
|
71 |
+
add_btn = gr.Button("Add String")
|
72 |
+
remove_btn = gr.Button("Remove String")
|
73 |
+
|
74 |
+
plot_output = gr.Plot()
|
75 |
+
|
76 |
+
submit_btn = gr.Button("Submit")
|
77 |
+
clear_btn = gr.ClearButton(components=[textboxes, plot_output], value="Clear")
|
78 |
+
|
79 |
+
add_btn.click(add_textbox, inputs=[textboxes], outputs=[textboxes])
|
80 |
+
remove_btn.click(remove_textbox, inputs=[textboxes], outputs=[textboxes])
|
81 |
+
submit_btn.click(compare_embeddings, inputs=[textboxes], outputs=[plot_output])
|
82 |
|
83 |
iface.launch()
|