Update app.py
Browse files
app.py
CHANGED
@@ -69,8 +69,9 @@ TEMPLATES = {
|
|
69 |
}
|
70 |
}
|
71 |
|
|
|
72 |
class EnhancedPromptGenerator:
|
73 |
-
def __init__(self, model_name: str = "
|
74 |
self.model_name = model_name
|
75 |
self.load_model()
|
76 |
|
@@ -83,7 +84,7 @@ class EnhancedPromptGenerator:
|
|
83 |
use_fast=True
|
84 |
)
|
85 |
self.model = AutoModelForCausalLM.from_pretrained(
|
86 |
-
|
87 |
torch_dtype=torch.float16,
|
88 |
device_map="auto",
|
89 |
low_cpu_mem_usage=True
|
@@ -94,9 +95,14 @@ class EnhancedPromptGenerator:
|
|
94 |
raise
|
95 |
|
96 |
def generate_with_model(self, prompt: str, config: PromptConfig = PromptConfig()) -> str:
|
97 |
-
"""Gera texto usando o modelo com configurações
|
98 |
try:
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
outputs = self.model.generate(
|
102 |
**inputs,
|
@@ -105,7 +111,9 @@ class EnhancedPromptGenerator:
|
|
105 |
top_p=config.top_p,
|
106 |
repetition_penalty=config.repetition_penalty,
|
107 |
do_sample=True,
|
108 |
-
pad_token_id=self.tokenizer.eos_token_id
|
|
|
|
|
109 |
)
|
110 |
|
111 |
return self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
@@ -113,7 +121,7 @@ class EnhancedPromptGenerator:
|
|
113 |
except Exception as e:
|
114 |
logging.error(f"Erro na geração: {e}")
|
115 |
return f"Erro na geração: {str(e)}"
|
116 |
-
|
117 |
generator = EnhancedPromptGenerator()
|
118 |
|
119 |
def get_suggestions(categoria: str, subcategoria: str) -> List[str]:
|
|
|
69 |
}
|
70 |
}
|
71 |
|
72 |
+
# Mudança principal na classe do gerador para usar um modelo mais leve
|
73 |
class EnhancedPromptGenerator:
|
74 |
+
def __init__(self, model_name: str = "neuralmind/bert-base-portuguese-cased"):
|
75 |
self.model_name = model_name
|
76 |
self.load_model()
|
77 |
|
|
|
84 |
use_fast=True
|
85 |
)
|
86 |
self.model = AutoModelForCausalLM.from_pretrained(
|
87 |
+
"microsoft/phi-2", # Modelo mais leve e eficiente
|
88 |
torch_dtype=torch.float16,
|
89 |
device_map="auto",
|
90 |
low_cpu_mem_usage=True
|
|
|
95 |
raise
|
96 |
|
97 |
def generate_with_model(self, prompt: str, config: PromptConfig = PromptConfig()) -> str:
|
98 |
+
"""Gera texto usando o modelo com configurações otimizadas"""
|
99 |
try:
|
100 |
+
# Adiciona contexto em português
|
101 |
+
enhanced_prompt = f"""
|
102 |
+
Por favor, gere um texto em português de acordo com as instruções:
|
103 |
+
{prompt}
|
104 |
+
"""
|
105 |
+
inputs = self.tokenizer(enhanced_prompt, return_tensors="pt").to(self.model.device)
|
106 |
|
107 |
outputs = self.model.generate(
|
108 |
**inputs,
|
|
|
111 |
top_p=config.top_p,
|
112 |
repetition_penalty=config.repetition_penalty,
|
113 |
do_sample=True,
|
114 |
+
pad_token_id=self.tokenizer.eos_token_id,
|
115 |
+
num_beams=4, # Melhor qualidade para português
|
116 |
+
early_stopping=True
|
117 |
)
|
118 |
|
119 |
return self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
121 |
except Exception as e:
|
122 |
logging.error(f"Erro na geração: {e}")
|
123 |
return f"Erro na geração: {str(e)}"
|
124 |
+
|
125 |
generator = EnhancedPromptGenerator()
|
126 |
|
127 |
def get_suggestions(categoria: str, subcategoria: str) -> List[str]:
|