Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -158,43 +158,53 @@ class PromptManager:
|
|
158 |
return prompts.get(doc_type, "Tipo de documento não suportado")
|
159 |
|
160 |
class DocumentGenerator:
|
161 |
-
"""Gerencia a geração de documentos usando modelo
|
162 |
|
163 |
def __init__(self):
|
164 |
-
# Usando modelo
|
165 |
-
self.client = Client("
|
166 |
self.prompt_manager = PromptManager()
|
|
|
167 |
|
168 |
def generate(self, doc_type: str, context: Dict[str, str]) -> str:
|
169 |
"""Gera o documento baseado no tipo e contexto"""
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
|
|
|
|
|
|
|
|
195 |
|
196 |
def _format_output(self, text: str) -> str:
|
197 |
"""Formata o texto gerado para melhor apresentação"""
|
|
|
|
|
|
|
|
|
|
|
198 |
return text.strip()
|
199 |
|
200 |
class WebInterface:
|
|
|
158 |
return prompts.get(doc_type, "Tipo de documento não suportado")
|
159 |
|
160 |
class DocumentGenerator:
|
161 |
+
"""Gerencia a geração de documentos usando modelo BERT público"""
|
162 |
|
163 |
def __init__(self):
|
164 |
+
# Usando modelo BERT público e estável
|
165 |
+
self.client = Client("microsoft/phi-1_5") # Modelo mais leve e estável
|
166 |
self.prompt_manager = PromptManager()
|
167 |
+
self.max_retries = 3
|
168 |
|
169 |
def generate(self, doc_type: str, context: Dict[str, str]) -> str:
|
170 |
"""Gera o documento baseado no tipo e contexto"""
|
171 |
+
for attempt in range(self.max_retries):
|
172 |
+
try:
|
173 |
+
prompt = self.prompt_manager.get_base_prompt(doc_type, context)
|
174 |
+
|
175 |
+
# Configuração específica para o modelo
|
176 |
+
result = self.client.predict(
|
177 |
+
prompt, # texto principal
|
178 |
+
max_length=2048, # tamanho máximo
|
179 |
+
temperature=0.7, # criatividade moderada
|
180 |
+
top_p=0.95, # coerência
|
181 |
+
do_sample=True, # amostragem
|
182 |
+
api_name="/run" # endpoint padrão
|
183 |
+
)
|
184 |
+
|
185 |
+
return self._format_output(result)
|
186 |
+
|
187 |
+
except Exception as e:
|
188 |
+
error_msg = str(e)
|
189 |
+
logger.error(f"Tentativa {attempt + 1} falhou: {error_msg}")
|
190 |
+
|
191 |
+
if attempt < self.max_retries - 1:
|
192 |
+
continue
|
193 |
+
else:
|
194 |
+
if "quota" in error_msg.lower():
|
195 |
+
return "Serviço temporariamente indisponível. Por favor, tente novamente em alguns minutos."
|
196 |
+
elif "timeout" in error_msg.lower():
|
197 |
+
return "O servidor está demorando para responder. Por favor, tente novamente."
|
198 |
+
else:
|
199 |
+
return f"Não foi possível gerar o documento. Por favor, verifique os dados e tente novamente."
|
200 |
|
201 |
def _format_output(self, text: str) -> str:
|
202 |
"""Formata o texto gerado para melhor apresentação"""
|
203 |
+
if not isinstance(text, str):
|
204 |
+
if isinstance(text, (list, tuple)) and len(text) > 0:
|
205 |
+
text = str(text[0])
|
206 |
+
else:
|
207 |
+
text = str(text)
|
208 |
return text.strip()
|
209 |
|
210 |
class WebInterface:
|