abhijsrwala commited on
Commit
73ece59
·
verified ·
1 Parent(s): b863f1a

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +32 -0
handler.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+
4
+ # Load model and tokenizer globally
5
+ MODEL_NAME = "abhijsrwala/lora_model"
6
+
7
+ def load_model():
8
+ # Load the model and tokenizer
9
+ model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
10
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
11
+ return model, tokenizer
12
+
13
+ # Load model once to avoid reloading on every request
14
+ model, tokenizer = load_model()
15
+
16
+ def handle_request(input_data):
17
+ """
18
+ Handles inference requests.
19
+ Args:
20
+ input_data (str): The input text prompt.
21
+ Returns:
22
+ str: The model's response.
23
+ """
24
+ # Tokenize the input text
25
+ inputs = tokenizer.encode(input_data, return_tensors="pt")
26
+
27
+ # Generate text
28
+ outputs = model.generate(inputs, max_length=200, num_return_sequences=1)
29
+
30
+ # Decode the output
31
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
32
+ return response