codeblacks commited on
Commit
e569f68
1 Parent(s): c6cd033

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -15
app.py CHANGED
@@ -1,27 +1,21 @@
1
- from transformers import AutoTokenizer, AutoModel
2
- import torch
3
  import gradio as gr
 
4
 
5
- # Load the pre-trained paraphrase-mpnet-base-v2 model and tokenizer
6
- tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/paraphrase-mpnet-base-v2')
7
- model = AutoModel.from_pretrained('sentence-transformers/paraphrase-mpnet-base-v2')
8
 
9
- def get_mpnet_embeddings(sentences):
10
- # Tokenize input sentences
11
- inputs = tokenizer(sentences, return_tensors='pt', padding=True, truncation=True, max_length=512)
12
- # Get embeddings
13
- with torch.no_grad():
14
- outputs = model(**inputs)
15
- embeddings = outputs.last_hidden_state.mean(dim=1) # Mean pooling over the sequence
16
- return embeddings.numpy().tolist()
17
 
18
  # Define the Gradio interface
19
  interface = gr.Interface(
20
- fn=get_mpnet_embeddings, # Function to call
21
  inputs=gr.Textbox(lines=2, placeholder="Enter sentences here, one per line"), # Input component
22
  outputs=gr.JSON(), # Output component
23
  title="Sentence Embeddings with MPNet", # Interface title
24
- description="Enter sentences to get their embeddings with paraphrase-mpnet-base-v2 (up to 512 tokens)." # Description
25
  )
26
 
27
  # Launch the interface
 
 
 
1
  import gradio as gr
2
+ from sentence_transformers import SentenceTransformer
3
 
4
+ # Load the pre-trained paraphrase-mpnet-base-v2 model
5
+ model = SentenceTransformer('sentence-transformers/paraphrase-mpnet-base-v2')
 
6
 
7
+ def get_embeddings(sentences):
8
+ # Get embeddings for the input sentences
9
+ embeddings = model.encode(sentences)
10
+ return embeddings.tolist()
 
 
 
 
11
 
12
  # Define the Gradio interface
13
  interface = gr.Interface(
14
+ fn=get_embeddings, # Function to call
15
  inputs=gr.Textbox(lines=2, placeholder="Enter sentences here, one per line"), # Input component
16
  outputs=gr.JSON(), # Output component
17
  title="Sentence Embeddings with MPNet", # Interface title
18
+ description="Enter sentences to get their embeddings with paraphrase-mpnet-base-v2." # Description
19
  )
20
 
21
  # Launch the interface