kasterkeqi commited on
Commit
42eafc1
·
1 Parent(s): 7605d66

Added custom handler for Hugging Face Inference Endpoints

Browse files
Files changed (1) hide show
  1. handler.py +31 -0
handler.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+
4
+ MODEL_NAME = "lumolabs-ai/Lumo-8B-Instruct"
5
+
6
+ class ModelHandler:
7
+ def __init__(self):
8
+ """Load the model and tokenizer"""
9
+ self.device = "cuda" if torch.cuda.is_available() else "cpu"
10
+ print(f"Loading model on {self.device}...")
11
+
12
+ self.tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
13
+ self.model = AutoModelForCausalLM.from_pretrained(MODEL_NAME).to(self.device)
14
+ print("Model loaded successfully.")
15
+
16
+ def __call__(self, inputs):
17
+ """Handle inference requests"""
18
+ text = inputs.get("inputs", "")
19
+ if not text:
20
+ return {"error": "No input provided"}
21
+
22
+ # Tokenize input
23
+ input_tokens = self.tokenizer(text, return_tensors="pt").to(self.device)
24
+
25
+ # Generate output
26
+ with torch.no_grad():
27
+ output_tokens = self.model.generate(**input_tokens, max_length=200)
28
+
29
+ # Decode output
30
+ response = self.tokenizer.decode(output_tokens[0], skip_special_tokens=True)
31
+ return {"response": response}