Instructions to use joaojanini/splade_sparse_vector_pinecone with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use joaojanini/splade_sparse_vector_pinecone with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("fill-mask", model="joaojanini/splade_sparse_vector_pinecone")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForMaskedLM tokenizer = AutoTokenizer.from_pretrained("joaojanini/splade_sparse_vector_pinecone") model = AutoModelForMaskedLM.from_pretrained("joaojanini/splade_sparse_vector_pinecone", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Commit ·
3b7c54f
1
Parent(s): 4c37e72
Update handler.py
Browse files- handler.py +8 -8
handler.py
CHANGED
|
@@ -3,13 +3,13 @@ from transformers import AutoTokenizer, AutoModelForMaskedLM
|
|
| 3 |
import torch
|
| 4 |
|
| 5 |
class EndpointHandler:
|
| 6 |
-
def __init__(self,
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
# Load tokenizer and model
|
| 9 |
-
self.tokenizer = AutoTokenizer.from_pretrained(
|
| 10 |
-
self.model = AutoModelForMaskedLM.from_pretrained(
|
| 11 |
-
self.max_seq_length = max_seq_length
|
| 12 |
-
self.device = device
|
| 13 |
|
| 14 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 15 |
"""
|
|
@@ -29,7 +29,7 @@ class EndpointHandler:
|
|
| 29 |
return_tensors="pt",
|
| 30 |
padding=True,
|
| 31 |
truncation=True,
|
| 32 |
-
max_length=self.
|
| 33 |
).to(self.device)
|
| 34 |
with torch.no_grad():
|
| 35 |
logits = self.model(**inputs).logits
|
|
@@ -48,4 +48,4 @@ class EndpointHandler:
|
|
| 48 |
{"indices": nz_tokens.tolist(), "values": nz_weights.tolist()}
|
| 49 |
)
|
| 50 |
|
| 51 |
-
return output[0] if isinstance(inputs, str) else output
|
|
|
|
| 3 |
import torch
|
| 4 |
|
| 5 |
class EndpointHandler:
|
| 6 |
+
def __init__(self, path: str = ""):
|
| 7 |
+
# Device to use for inference. "cuda" if CUDA is available, else "cpu"
|
| 8 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 9 |
+
|
| 10 |
# Load tokenizer and model
|
| 11 |
+
self.tokenizer = AutoTokenizer.from_pretrained(path)
|
| 12 |
+
self.model = AutoModelForMaskedLM.from_pretrained(path).to(self.device)
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 15 |
"""
|
|
|
|
| 29 |
return_tensors="pt",
|
| 30 |
padding=True,
|
| 31 |
truncation=True,
|
| 32 |
+
max_length=self.model.config.max_position_embeddings, # Max length according to the model configuration
|
| 33 |
).to(self.device)
|
| 34 |
with torch.no_grad():
|
| 35 |
logits = self.model(**inputs).logits
|
|
|
|
| 48 |
{"indices": nz_tokens.tolist(), "values": nz_weights.tolist()}
|
| 49 |
)
|
| 50 |
|
| 51 |
+
return output[0] if isinstance(inputs, str) else output
|