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,57 +27,32 @@ def get_embedding(text):
|
|
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(
|
36 |
-
|
37 |
-
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
fig
|
43 |
-
|
44 |
-
|
45 |
-
|
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 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
num_textboxes = gr.State(value=2)
|
65 |
-
|
66 |
-
with gr.Column() as textbox_container:
|
67 |
-
textboxes = [gr.Textbox(label=f"Text {i+1}") for i in range(10)] # Create 10 textboxes
|
68 |
-
for i in range(2, 10): # Hide textboxes 3-10 initially
|
69 |
-
textboxes[i].visible = False
|
70 |
-
|
71 |
-
with gr.Row():
|
72 |
-
add_btn = gr.Button("Add String")
|
73 |
-
remove_btn = gr.Button("Remove String")
|
74 |
-
|
75 |
-
plot_output = gr.Plot()
|
76 |
-
|
77 |
-
submit_btn = gr.Button("Submit")
|
78 |
-
clear_btn = gr.ClearButton(components=textboxes + [plot_output], value="Clear")
|
79 |
-
|
80 |
-
add_btn.click(add_textbox, inputs=[num_textboxes], outputs=[textboxes[-1], num_textboxes])
|
81 |
-
remove_btn.click(remove_textbox, inputs=[num_textboxes], outputs=[textboxes[-1], num_textboxes])
|
82 |
-
submit_btn.click(compare_embeddings, inputs=textboxes, outputs=[plot_output])
|
83 |
|
84 |
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()
|