Commit
•
7b7297d
1
Parent(s):
c75004b
Create handler.py
Browse files- handler.py +30 -0
handler.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import bitsandbytes as bnb
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM # Adjusted import for the correct model type
|
4 |
+
import os
|
5 |
+
from typing import Dict, List, Any
|
6 |
+
|
7 |
+
# Set the environment variable for the Hugging Face token
|
8 |
+
|
9 |
+
|
10 |
+
class EndpointHandler:
|
11 |
+
def __init__(self, model_path="thomasabebe/coolmodel"):
|
12 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_path)
|
13 |
+
self.model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=torch.float32) # Adjusted model class
|
14 |
+
|
15 |
+
def __call__(self, data):
|
16 |
+
# Extract inputs from the data
|
17 |
+
inputs = data.get("inputs", "")
|
18 |
+
|
19 |
+
# Preprocess the inputs
|
20 |
+
encoded_input = self.tokenizer(inputs, return_tensors='pt', padding=True, truncation=True)
|
21 |
+
|
22 |
+
# Get predictions
|
23 |
+
output = self.model(**encoded_input)
|
24 |
+
|
25 |
+
# Postprocess the outputs
|
26 |
+
prediction = output.logits.argmax(-1).tolist() # Convert the tensor to a list
|
27 |
+
|
28 |
+
# Return the result
|
29 |
+
return {"label": prediction}
|
30 |
+
|