Sergidev commited on
Commit
5c51be8
·
verified ·
1 Parent(s): 2371338
Files changed (1) hide show
  1. app.py +58 -21
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,68 @@ def get_embedding(text):
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()
 
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 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(strings):
36
+ embeddings = [get_embedding(text) for text in strings]
37
+ embeddings_3d = [reduce_to_3d(emb) for emb in embeddings]
 
 
 
 
 
 
 
 
38
 
39
+ fig = go.Figure()
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
  return fig
55
 
56
+ def add_string(strings):
57
+ strings.append("")
58
+ return strings, gr.Textbox.update(visible=True)
59
+
60
+ def remove_string(strings):
61
+ if len(strings) > 1:
62
+ strings.pop()
63
+ return strings, gr.Textbox.update(visible=len(strings) > 1)
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
+ textboxes = gr.Textbox(label="Text 1", elem_id="textbox_1")
73
+ add_button = gr.Button("Add String")
74
+ remove_button = gr.Button("Remove String")
75
+
76
+ with gr.Column(scale=2):
77
+ plot_output = gr.Plot()
78
+
79
+ submit_button = gr.Button("Compare Embeddings")
80
+
81
+ def update_textboxes(strings):
82
+ return [gr.Textbox.update(value=s, label=f"Text {i+1}", visible=True) for i, s in enumerate(strings)]
83
+
84
+ add_button.click(add_string, inputs=[string_inputs], outputs=[string_inputs, textboxes])
85
+ remove_button.click(remove_string, inputs=[string_inputs], outputs=[string_inputs, textboxes])
86
+
87
+ submit_button.click(
88
+ compare_embeddings,
89
+ inputs=[string_inputs],
90
+ outputs=[plot_output]
91
+ )
92
+
93
+ string_inputs.change(update_textboxes, inputs=[string_inputs], outputs=[textboxes])
94
 
95
  iface.launch()