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 text chunks from the payload
|
| 11 |
+
chunks = payload.get("inputs", [])
|
| 12 |
+
|
| 13 |
+
# Generate embeddings
|
| 14 |
+
embeddings = self.embedding_model.encode(chunks, convert_to_tensor=True)
|
| 15 |
+
|
| 16 |
+
# Prepare response
|
| 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
|