Update handler.py
Browse files- handler.py +18 -12
handler.py
CHANGED
@@ -1,30 +1,33 @@
|
|
1 |
from typing import Dict, Any
|
2 |
-
from transformers import
|
3 |
import torch
|
4 |
|
5 |
class EndpointHandler:
|
6 |
def __init__(self, path=""):
|
7 |
# Configuração do modelo
|
8 |
-
self.model_name_or_path = "souzat19/Llama3.1_fn14133.29122024"
|
9 |
|
10 |
# Detecta se GPU está disponível
|
11 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
print(f"Using device: {self.device}")
|
13 |
|
14 |
print("Initializing tokenizer...")
|
15 |
-
|
16 |
-
self.tokenizer = AutoTokenizer.from_pretrained(
|
17 |
self.model_name_or_path,
|
18 |
trust_remote_code=True
|
19 |
)
|
20 |
|
21 |
print("Initializing model...")
|
22 |
-
|
23 |
-
self.model = AutoModelForCausalLM.from_pretrained(
|
24 |
self.model_name_or_path,
|
25 |
torch_dtype=torch.float32,
|
26 |
-
trust_remote_code=True
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
print("Model initialized successfully")
|
30 |
|
@@ -52,11 +55,12 @@ Você é um assistente especializado em planejamento de compras públicas de aco
|
|
52 |
formatted_prompt,
|
53 |
return_tensors="pt",
|
54 |
truncation=True,
|
55 |
-
max_length=4096
|
|
|
56 |
)
|
57 |
|
58 |
-
|
59 |
-
|
60 |
|
61 |
# Gera a resposta
|
62 |
with torch.no_grad():
|
@@ -66,7 +70,9 @@ Você é um assistente especializado em planejamento de compras públicas de aco
|
|
66 |
temperature=0.5,
|
67 |
top_p=0.95,
|
68 |
top_k=50,
|
69 |
-
do_sample=True
|
|
|
|
|
70 |
)
|
71 |
|
72 |
# Decodifica a resposta
|
|
|
1 |
from typing import Dict, Any
|
2 |
+
from transformers import LlamaForCausalLM, LlamaTokenizer
|
3 |
import torch
|
4 |
|
5 |
class EndpointHandler:
|
6 |
def __init__(self, path=""):
|
7 |
# Configuração do modelo
|
8 |
+
self.model_name_or_path = path or "souzat19/Llama3.1_fn14133.29122024"
|
9 |
|
10 |
# Detecta se GPU está disponível
|
11 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
print(f"Using device: {self.device}")
|
13 |
|
14 |
print("Initializing tokenizer...")
|
15 |
+
self.tokenizer = LlamaTokenizer.from_pretrained(
|
|
|
16 |
self.model_name_or_path,
|
17 |
trust_remote_code=True
|
18 |
)
|
19 |
|
20 |
print("Initializing model...")
|
21 |
+
self.model = LlamaForCausalLM.from_pretrained(
|
|
|
22 |
self.model_name_or_path,
|
23 |
torch_dtype=torch.float32,
|
24 |
+
trust_remote_code=True,
|
25 |
+
device_map="auto" if torch.cuda.is_available() else None,
|
26 |
+
local_files_only=True if path else False
|
27 |
+
)
|
28 |
+
|
29 |
+
if not torch.cuda.is_available():
|
30 |
+
self.model = self.model.to("cpu")
|
31 |
|
32 |
print("Model initialized successfully")
|
33 |
|
|
|
55 |
formatted_prompt,
|
56 |
return_tensors="pt",
|
57 |
truncation=True,
|
58 |
+
max_length=4096,
|
59 |
+
add_special_tokens=True
|
60 |
)
|
61 |
|
62 |
+
if torch.cuda.is_available():
|
63 |
+
inputs = {k: v.to(self.device) for k, v in inputs.items()}
|
64 |
|
65 |
# Gera a resposta
|
66 |
with torch.no_grad():
|
|
|
70 |
temperature=0.5,
|
71 |
top_p=0.95,
|
72 |
top_k=50,
|
73 |
+
do_sample=True,
|
74 |
+
pad_token_id=self.tokenizer.pad_token_id,
|
75 |
+
eos_token_id=self.tokenizer.eos_token_id
|
76 |
)
|
77 |
|
78 |
# Decodifica a resposta
|