rianders commited on
Commit
fa613e1
·
verified ·
1 Parent(s): 677aef8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -65
app.py CHANGED
@@ -1,85 +1,66 @@
1
  import streamlit as st
2
- from database_utils import init_db, save_embeddings_to_db, get_all_embeddings, fetch_data_as_csv
3
  from transformers import BertModel, BertTokenizer
4
  from sklearn.decomposition import PCA
5
  import plotly.graph_objs as go
6
  import numpy as np
7
 
8
  # BERT embeddings function
9
- def get_bert_embeddings(words):
10
  tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
11
  model = BertModel.from_pretrained('bert-base-uncased')
12
- embeddings = []
13
- for word in words:
14
- inputs = tokenizer(word, return_tensors='pt')
15
- outputs = model(**inputs)
16
- mean_embedding = outputs.last_hidden_state[0].mean(dim=0).detach().numpy()
17
- embeddings.append(mean_embedding)
18
- if len(embeddings) > 0:
19
- pca = PCA(n_components=3)
20
- reduced_embeddings = pca.fit_transform(np.array(embeddings))
21
- return reduced_embeddings
22
- return []
23
 
24
- def plot_interactive_bert_embeddings(embeddings, words):
25
- if len(words) < 4:
26
- st.error("Please provide at least 4 words/phrases for effective visualization.")
27
- return None
28
- data = []
29
- for i, word in enumerate(words):
30
- trace = go.Scatter3d(
31
- x=[embeddings[i][0]],
32
- y=[embeddings[i][1]],
33
- z=[embeddings[i][2]],
34
- mode='markers+text',
35
- text=[word],
36
- name=word
37
- )
38
- data.append(trace)
39
- layout = go.Layout(
40
- title='3D Scatter Plot of BERT Embeddings',
41
- scene=dict(
42
- xaxis=dict(title='PCA Component 1'),
43
- yaxis=dict(title='PCA Component 2'),
44
- zaxis=dict(title='PCA Component 3')
45
- ),
46
- autosize=False,
47
- width=800,
48
- height=600
49
- )
50
- fig = go.Figure(data=data, layout=layout)
51
- return fig
52
-
53
- def process_and_visualize_words(words):
54
- embeddings = get_bert_embeddings(words)
55
- if embeddings.size > 0:
56
- for word, embedding in zip(words, embeddings):
57
- save_embeddings_to_db(word, embedding)
58
- st.success("Sentences added and embeddings saved!")
59
- embeddings, sentences = get_all_embeddings()
60
- fig = plot_interactive_bert_embeddings(np.vstack(embeddings), sentences)
61
- if fig is not None:
62
- st.plotly_chart(fig, use_container_width=True)
63
  else:
64
- st.error("Failed to generate embeddings. Ensure your sentences are correctly formatted.")
65
 
66
  def main():
67
  st.title("BERT Embeddings Visualization - Community Edition")
68
 
69
- init_db_message = init_db()
70
- st.text(init_db_message)
71
 
72
- new_sentences = st.text_input("Enter at least three words/phrases, comma-separated:", "Example: apple, banana, orange")
73
- if st.button("Add and Visualize Sentences"):
74
- words = [word.strip() for word in new_sentences.split(',')]
75
- if len(words) < 3:
76
- st.error("Please enter at least three words/phrases separated by commas.")
77
- else:
78
- process_and_visualize_words(words)
 
 
79
 
80
- if st.button("Download Database as CSV"):
81
- csv = fetch_data_as_csv()
82
- st.download_button(label="Download CSV", data=csv, file_name='embeddings.csv', mime='text/csv')
83
 
84
  if __name__ == "__main__":
85
  main()
 
1
  import streamlit as st
2
+ from database_utils import init_db, save_embeddings_to_db, get_all_embeddings, fetch_data_as_csv, clear_all_entries
3
  from transformers import BertModel, BertTokenizer
4
  from sklearn.decomposition import PCA
5
  import plotly.graph_objs as go
6
  import numpy as np
7
 
8
  # BERT embeddings function
9
+ def get_bert_embeddings(word):
10
  tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
11
  model = BertModel.from_pretrained('bert-base-uncased')
12
+ inputs = tokenizer(word, return_tensors='pt')
13
+ outputs = model(**inputs)
14
+ mean_embedding = outputs.last_hidden_state[0].mean(dim=0).detach().numpy()
15
+ pca = PCA(n_components=3)
16
+ reduced_embedding = pca.fit_transform(np.array([mean_embedding])) # Embedding needs to be in a 2D array
17
+ return reduced_embedding[0] # Return the first and only item
 
 
 
 
 
18
 
19
+ def plot_interactive_bert_embeddings():
20
+ embeddings, sentences = get_all_embeddings()
21
+ if len(sentences) >= 3:
22
+ fig = go.Figure(data=[
23
+ go.Scatter3d(
24
+ x=[emb[0]],
25
+ y=[emb[1]],
26
+ z=[emb[2]],
27
+ mode='markers+text',
28
+ text=[sent],
29
+ name=sent
30
+ ) for emb, sent in zip(embeddings, sentences)
31
+ ], layout=go.Layout(
32
+ title='3D Scatter Plot of BERT Embeddings',
33
+ scene=dict(
34
+ xaxis=dict(title='PCA Component 1'),
35
+ yaxis=dict(title='PCA Component 2'),
36
+ zaxis=dict(title='PCA Component 3')
37
+ ),
38
+ autosize=False,
39
+ width=800,
40
+ height=600
41
+ ))
42
+ st.plotly_chart(fig, use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  else:
44
+ st.error("Please add at least three different words/phrases to visualize.")
45
 
46
  def main():
47
  st.title("BERT Embeddings Visualization - Community Edition")
48
 
49
+ init_db()
 
50
 
51
+ new_word = st.text_input("Enter a new word or phrase:")
52
+ if st.button("Add Word/Phrase"):
53
+ if new_word:
54
+ embedding = get_bert_embeddings(new_word)
55
+ save_embeddings_to_db(new_word, embedding)
56
+ st.success(f"Added: {new_word}")
57
+
58
+ if st.button("Visualize Embeddings"):
59
+ plot_interactive_bert_embeddings()
60
 
61
+ if st.button("Clear All Entries"):
62
+ clear_all_entries() # This function needs to be defined in database_utils.py to delete all records
63
+ st.success("All entries have been cleared.")
64
 
65
  if __name__ == "__main__":
66
  main()