Update handler.py
Browse files- handler.py +24 -24
handler.py
CHANGED
@@ -1,26 +1,18 @@
|
|
1 |
from typing import Dict, Any
|
2 |
-
from
|
3 |
-
|
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 |
-
self.model_basename = "unsloth.Q8_0.gguf"
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
# Inicialização do modelo
|
18 |
-
self.model = Llama(
|
19 |
-
model_path=model_path,
|
20 |
-
n_threads=2,
|
21 |
-
n_batch=512,
|
22 |
-
n_gpu_layers=-1,
|
23 |
-
n_ctx=4096
|
24 |
)
|
25 |
|
26 |
# Template do prompt no formato Alpaca
|
@@ -46,25 +38,33 @@ Você é um assistente especializado em planejamento de compras públicas de aco
|
|
46 |
try:
|
47 |
# Extrai o texto da entrada
|
48 |
input_text = data.get("text", "")
|
49 |
-
if not input_text:
|
50 |
return {"error": "Input text is required"}
|
51 |
|
|
|
|
|
|
|
52 |
# Formata o prompt
|
53 |
formatted_prompt = self.prompt_template.format(input=input_text)
|
54 |
|
|
|
|
|
|
|
|
|
55 |
# Gera a resposta
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
temperature=0.5,
|
60 |
top_p=0.95,
|
61 |
top_k=50,
|
62 |
-
|
63 |
-
|
|
|
64 |
)
|
65 |
|
66 |
-
#
|
67 |
-
response_text =
|
68 |
|
69 |
# Processa a resposta para extrair apenas a parte após "### Response:"
|
70 |
if "### Response:" in response_text:
|
|
|
1 |
from typing import Dict, Any
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
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 |
+
# Inicialização do modelo e tokenizer
|
11 |
+
self.tokenizer = AutoTokenizer.from_pretrained(self.model_name_or_path)
|
12 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
13 |
+
self.model_name_or_path,
|
14 |
+
torch_dtype=torch.float16,
|
15 |
+
device_map="auto"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
)
|
17 |
|
18 |
# Template do prompt no formato Alpaca
|
|
|
38 |
try:
|
39 |
# Extrai o texto da entrada
|
40 |
input_text = data.get("text", "")
|
41 |
+
if not input_text or not self.validate_input(input_text):
|
42 |
return {"error": "Input text is required"}
|
43 |
|
44 |
+
# Pré-processa o texto
|
45 |
+
input_text = self.preprocess(input_text)
|
46 |
+
|
47 |
# Formata o prompt
|
48 |
formatted_prompt = self.prompt_template.format(input=input_text)
|
49 |
|
50 |
+
# Tokeniza o input
|
51 |
+
inputs = self.tokenizer(formatted_prompt, return_tensors="pt", truncation=True, max_length=4096)
|
52 |
+
inputs = inputs.to(self.model.device)
|
53 |
+
|
54 |
# Gera a resposta
|
55 |
+
outputs = self.model.generate(
|
56 |
+
**inputs,
|
57 |
+
max_new_tokens=2096,
|
58 |
temperature=0.5,
|
59 |
top_p=0.95,
|
60 |
top_k=50,
|
61 |
+
pad_token_id=self.tokenizer.pad_token_id,
|
62 |
+
eos_token_id=self.tokenizer.eos_token_id,
|
63 |
+
do_sample=True
|
64 |
)
|
65 |
|
66 |
+
# Decodifica a resposta
|
67 |
+
response_text = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
68 |
|
69 |
# Processa a resposta para extrair apenas a parte após "### Response:"
|
70 |
if "### Response:" in response_text:
|