Update app.py
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)
|
@@ -28,31 +29,43 @@ def reduce_to_3d(embedding):
|
|
28 |
return embedding[:3]
|
29 |
|
30 |
@spaces.GPU
|
31 |
-
def compare_embeddings(
|
32 |
-
|
33 |
-
|
34 |
|
35 |
-
|
36 |
-
emb2_3d = reduce_to_3d(emb2)
|
37 |
|
38 |
-
|
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 |
-
gr.
|
51 |
-
gr.
|
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 numpy as np
|
7 |
|
8 |
model_name = "mistralai/Mistral-7B-v0.1"
|
9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
29 |
return embedding[:3]
|
30 |
|
31 |
@spaces.GPU
|
32 |
+
def compare_embeddings(*texts):
|
33 |
+
embeddings = [get_embedding(text) for text in texts]
|
34 |
+
embeddings_3d = [reduce_to_3d(emb) for emb in embeddings]
|
35 |
|
36 |
+
fig = go.Figure()
|
|
|
37 |
|
38 |
+
colors = ['red', 'blue', 'green', 'purple', 'orange', 'cyan', 'magenta', 'yellow']
|
39 |
+
|
40 |
+
for i, emb in enumerate(embeddings_3d):
|
41 |
+
color = colors[i % len(colors)]
|
42 |
+
fig.add_trace(go.Scatter3d(
|
43 |
+
x=[0, emb[0]], y=[0, emb[1]], z=[0, emb[2]],
|
44 |
+
mode='lines+markers',
|
45 |
+
name=f'Text {i+1}',
|
46 |
+
line=dict(color=color),
|
47 |
+
marker=dict(color=color)
|
48 |
+
))
|
49 |
|
50 |
fig.update_layout(scene=dict(xaxis_title='X', yaxis_title='Y', zaxis_title='Z'))
|
51 |
|
52 |
return fig
|
53 |
|
54 |
+
def update_interface(num_inputs):
|
55 |
+
with gr.Blocks() as new_interface:
|
56 |
+
text_inputs = [gr.Textbox(label=f"Text {i+1}") for i in range(num_inputs)]
|
57 |
+
output = gr.Plot()
|
58 |
+
submit_btn = gr.Button("Compare Embeddings")
|
59 |
+
submit_btn.click(fn=compare_embeddings, inputs=text_inputs, outputs=output)
|
60 |
+
return new_interface
|
61 |
+
|
62 |
+
with gr.Blocks() as iface:
|
63 |
+
gr.Markdown("# 3D Embedding Comparison")
|
64 |
+
gr.Markdown("Compare the embeddings of multiple strings visualized in 3D space using Mistral 7B.")
|
65 |
+
|
66 |
+
num_inputs = gr.Slider(minimum=2, maximum=10, step=1, value=2, label="Number of texts to compare")
|
67 |
+
dynamic_interface = gr.HTML()
|
68 |
+
|
69 |
+
num_inputs.change(fn=update_interface, inputs=[num_inputs], outputs=[dynamic_interface])
|
70 |
|
71 |
iface.launch()
|