rianders commited on
Commit
2a28595
·
verified ·
1 Parent(s): 1b249fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -5
app.py CHANGED
@@ -17,14 +17,15 @@ def get_bert_embeddings(word):
17
 
18
  def plot_interactive_bert_embeddings():
19
  embeddings, sentences = get_all_embeddings()
20
- if len(sentences) >= 3:
21
- pca = PCA(n_components=3)
 
22
  reduced_embeddings = pca.fit_transform(np.array(embeddings))
23
  fig = go.Figure(data=[
24
  go.Scatter3d(
25
  x=[emb[0]],
26
- y=[emb[1]],
27
- z=[emb[2]],
28
  mode='markers+text',
29
  text=sent,
30
  name=sent
@@ -42,7 +43,8 @@ def plot_interactive_bert_embeddings():
42
  ))
43
  st.plotly_chart(fig, use_container_width=True)
44
  else:
45
- st.error("Please add at least three different words/phrases to visualize.")
 
46
 
47
 
48
  def main():
 
17
 
18
  def plot_interactive_bert_embeddings():
19
  embeddings, sentences = get_all_embeddings()
20
+ if len(sentences) > 0:
21
+ # Even if there's less than 3, PCA can still run with min(n_samples, n_features)
22
+ pca = PCA(n_components=min(3, len(sentences)))
23
  reduced_embeddings = pca.fit_transform(np.array(embeddings))
24
  fig = go.Figure(data=[
25
  go.Scatter3d(
26
  x=[emb[0]],
27
+ y=[emb[1] if len(emb) > 1 else 0], # Ensure there are enough dimensions
28
+ z=[emb[2] if len(emb) > 2 else 0],
29
  mode='markers+text',
30
  text=sent,
31
  name=sent
 
43
  ))
44
  st.plotly_chart(fig, use_container_width=True)
45
  else:
46
+ st.error("No data available for visualization.")
47
+
48
 
49
 
50
  def main():