codeblacks commited on
Commit
a7357eb
·
verified ·
1 Parent(s): 63316b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -5
app.py CHANGED
@@ -1,21 +1,32 @@
1
  from sentence_transformers import SentenceTransformer
2
  import gradio as gr
3
- import update_packages
4
  import numpy as np
5
 
6
  # Load the pre-trained model
7
  embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
8
 
9
  # Define the function to process requests
10
- def generate_embeddings(chunks):
 
 
 
 
11
  embeddings = embedding_model.encode(chunks, convert_to_tensor=False)
12
- shape = embeddings.shape
13
- return embeddings.tolist(), shape # Convert numpy array to list
 
 
 
 
 
 
 
14
 
15
  # Define the Gradio interface
16
  interface = gr.Interface(
17
  fn=generate_embeddings,
18
- inputs=gr.Textbox(lines=5, placeholder="Enter text chunks here...", type="text"),
19
  outputs=[gr.JSON(label="Embeddings"), gr.Label(label="Shape")],
20
  title="Sentence Transformer Embeddings",
21
  description="Generate embeddings for input text chunks."
 
1
  from sentence_transformers import SentenceTransformer
2
  import gradio as gr
3
+ import torch
4
  import numpy as np
5
 
6
  # Load the pre-trained model
7
  embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
8
 
9
  # Define the function to process requests
10
+ def generate_embeddings(text):
11
+ # Split the input text into chunks (if needed)
12
+ chunks = text.split('\n') # Assuming chunks are separated by new lines
13
+
14
+ # Encode the input chunks to get the embeddings
15
  embeddings = embedding_model.encode(chunks, convert_to_tensor=False)
16
+
17
+ # Convert the embeddings to a PyTorch tensor
18
+ embeddings_tensor = torch.tensor(embeddings)
19
+
20
+ # Add batch dimension to the tensor (if needed)
21
+ embeddings_tensor = embeddings_tensor.unsqueeze(0) # Uncomment if a batch dimension is required
22
+
23
+ # Return the embeddings tensor and its shape
24
+ return embeddings_tensor.tolist(), embeddings_tensor.shape
25
 
26
  # Define the Gradio interface
27
  interface = gr.Interface(
28
  fn=generate_embeddings,
29
+ inputs=gr.Textbox(lines=5, placeholder="Enter text chunks here..."),
30
  outputs=[gr.JSON(label="Embeddings"), gr.Label(label="Shape")],
31
  title="Sentence Transformer Embeddings",
32
  description="Generate embeddings for input text chunks."