Upload handler.py
Browse files- handler.py +64 -59
handler.py
CHANGED
|
@@ -2,65 +2,70 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
| 2 |
import torch
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
def
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
def predict(inputs, parameters=None):
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
return {"generated_text": generated_text}
|
|
|
|
| 2 |
import torch
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Create the required EndpointHandler class for Hugging Face Inference Endpoints
|
| 6 |
+
class EndpointHandler:
|
| 7 |
+
def __init__(self, path=""):
|
| 8 |
+
# Initialize model and tokenizer as None - will be loaded on first request
|
| 9 |
+
self.model = None
|
| 10 |
+
self.tokenizer = None
|
| 11 |
+
self.path = path
|
| 12 |
+
print(f"Initializing EndpointHandler with path: {path}")
|
| 13 |
|
| 14 |
+
def __call__(self, data, parameters=None):
|
| 15 |
+
# This will be called when the endpoint receives a request
|
| 16 |
+
return self.predict(data, parameters)
|
| 17 |
|
| 18 |
+
def load_model(self):
|
| 19 |
+
"""Initialize the model and tokenizer once"""
|
| 20 |
+
# Model repository ID
|
| 21 |
+
model_id = "EvolphTech/Wildnerve-tlm01_Hybrid_Model"
|
| 22 |
+
|
| 23 |
+
# Get token from environment (if needed for private models)
|
| 24 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 25 |
+
|
| 26 |
+
# Load model and tokenizer
|
| 27 |
+
self.model = AutoModelForCausalLM.from_pretrained(model_id, token=hf_token)
|
| 28 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_id, token=hf_token)
|
| 29 |
+
|
| 30 |
+
# Move to GPU if available
|
| 31 |
+
if torch.cuda.is_available():
|
| 32 |
+
self.model = self.model.to("cuda")
|
| 33 |
+
|
| 34 |
+
print("Model and tokenizer loaded successfully!")
|
| 35 |
|
| 36 |
+
def predict(self, inputs, parameters=None):
|
| 37 |
+
"""Primary inference function for the model"""
|
| 38 |
+
# Load model if not already loaded
|
| 39 |
+
if self.model is None or self.tokenizer is None:
|
| 40 |
+
self.load_model()
|
| 41 |
+
|
| 42 |
+
# Default parameters
|
| 43 |
+
max_length = parameters.get("max_length", 100) if parameters else 100
|
| 44 |
+
temperature = parameters.get("temperature", 0.7) if parameters else 0.7
|
| 45 |
+
|
| 46 |
+
# Process the input text
|
| 47 |
+
if isinstance(inputs, str):
|
| 48 |
+
text_input = inputs
|
| 49 |
+
elif isinstance(inputs, dict) and "inputs" in inputs:
|
| 50 |
+
text_input = inputs["inputs"]
|
| 51 |
+
else:
|
| 52 |
+
text_input = str(inputs)
|
| 53 |
+
|
| 54 |
+
# Tokenize inputs
|
| 55 |
+
inputs = self.tokenizer(text_input, return_tensors="pt")
|
| 56 |
+
input_ids = inputs["input_ids"].to(self.model.device)
|
| 57 |
+
|
| 58 |
+
# Generate text
|
| 59 |
+
with torch.no_grad():
|
| 60 |
+
outputs = self.model.generate(
|
| 61 |
+
input_ids,
|
| 62 |
+
max_length=max_length,
|
| 63 |
+
temperature=temperature,
|
| 64 |
+
do_sample=temperature > 0,
|
| 65 |
+
pad_token_id=self.tokenizer.eos_token_id
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
# Decode and return the generated text
|
| 69 |
+
generated_text = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 70 |
+
|
| 71 |
+
return {"generated_text": generated_text}
|
|
|
|
|
|