kambris commited on
Commit
6f973fa
·
verified ·
1 Parent(s): 17deefc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -3
app.py CHANGED
@@ -19,7 +19,8 @@ def generate_embeddings(texts):
19
 
20
  for text in texts:
21
  # Tokenize the text with truncation set to False
22
- tokens = bert_tokenizer.encode(text, truncation=False) # Do not truncate here
 
23
 
24
  # Split the tokens into chunks of size 512 (maximum length)
25
  chunked_texts = [tokens[i:i + 512] for i in range(0, len(tokens), 512)]
@@ -27,11 +28,11 @@ def generate_embeddings(texts):
27
  poem_embeddings = []
28
 
29
  for chunk in chunked_texts:
30
- # Prepare the input tensor for the model
31
  inputs = torch.tensor(chunk).unsqueeze(0) # Adding batch dimension
32
  with torch.no_grad():
33
  outputs = bert_model(inputs)
34
- # Get the embeddings from the last hidden state
35
  chunk_embedding = outputs.last_hidden_state.mean(dim=1).numpy()
36
 
37
  poem_embeddings.append(chunk_embedding)
 
19
 
20
  for text in texts:
21
  # Tokenize the text with truncation set to False
22
+ # We are using the BertTokenizer directly without using the pipeline
23
+ tokens = bert_tokenizer.encode(text, add_special_tokens=True, truncation=False, padding=False)
24
 
25
  # Split the tokens into chunks of size 512 (maximum length)
26
  chunked_texts = [tokens[i:i + 512] for i in range(0, len(tokens), 512)]
 
28
  poem_embeddings = []
29
 
30
  for chunk in chunked_texts:
31
+ # Convert the chunk to a tensor and prepare the input for BERT model
32
  inputs = torch.tensor(chunk).unsqueeze(0) # Adding batch dimension
33
  with torch.no_grad():
34
  outputs = bert_model(inputs)
35
+ # Get the embeddings from the last hidden state (mean of all token embeddings)
36
  chunk_embedding = outputs.last_hidden_state.mean(dim=1).numpy()
37
 
38
  poem_embeddings.append(chunk_embedding)