Spaces:
Sleeping
Sleeping
Pavithiran
commited on
Create model.py
Browse files
model.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sentence_transformers import SentenceTransformer
|
2 |
+
import torch
|
3 |
+
|
4 |
+
class Model:
|
5 |
+
def __init__(self):
|
6 |
+
# Load the pre-trained model
|
7 |
+
self.embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
8 |
+
|
9 |
+
def __call__(self, payload):
|
10 |
+
# Extract chunks from the payload
|
11 |
+
chunks = payload.get("chunks", [])
|
12 |
+
|
13 |
+
# Generate embeddings for chunks
|
14 |
+
embeddings = self.embedding_model.encode(chunks, convert_to_tensor=True)
|
15 |
+
|
16 |
+
# Prepare response with tensor embeddings
|
17 |
+
response = {
|
18 |
+
"embeddings": embeddings.tolist(), # Convert tensor to list for JSON serialization
|
19 |
+
"shape": list(embeddings.shape) # Return the shape of the embeddings tensor
|
20 |
+
}
|
21 |
+
return response
|