Create inference.py
Browse files- inference.py +23 -0
inference.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
class Model:
|
| 5 |
+
def __init__(self):
|
| 6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
+
|
| 8 |
+
self.tokenizer = AutoTokenizer.from_pretrained(".")
|
| 9 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
".",
|
| 11 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
| 12 |
+
device_map="auto"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
def __call__(self, prompt: str):
|
| 16 |
+
inputs = self.tokenizer(prompt, return_tensors="pt").to(self.model.device)
|
| 17 |
+
output_ids = self.model.generate(
|
| 18 |
+
**inputs,
|
| 19 |
+
max_new_tokens=128,
|
| 20 |
+
temperature=0.7,
|
| 21 |
+
do_sample=True
|
| 22 |
+
)
|
| 23 |
+
return self.tokenizer.decode(output_ids[0], skip_special_tokens=True)
|