Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -88,16 +88,81 @@ class PromptManager:
|
|
88 |
3. Mérito
|
89 |
4. Análise das provas
|
90 |
5. Pedidos
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
"""
|
92 |
}
|
93 |
|
94 |
return prompts.get(doc_type, "Tipo de documento não suportado")
|
95 |
|
96 |
class DocumentGenerator:
|
97 |
-
"""Gerencia a geração de documentos usando
|
98 |
|
99 |
def __init__(self):
|
100 |
-
|
|
|
101 |
self.prompt_manager = PromptManager()
|
102 |
|
103 |
def generate(self, doc_type: str, context: Dict[str, str]) -> str:
|
@@ -105,18 +170,28 @@ class DocumentGenerator:
|
|
105 |
try:
|
106 |
prompt = self.prompt_manager.get_base_prompt(doc_type, context)
|
107 |
|
|
|
108 |
result = self.client.predict(
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
|
|
|
|
113 |
)
|
114 |
|
115 |
return self._format_output(result)
|
116 |
|
117 |
except Exception as e:
|
118 |
-
|
119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
def _format_output(self, text: str) -> str:
|
122 |
"""Formata o texto gerado para melhor apresentação"""
|
@@ -135,7 +210,7 @@ class WebInterface:
|
|
135 |
with gr.Blocks(theme=gr.themes.Soft()) as self.app:
|
136 |
gr.Markdown("""
|
137 |
# Gerador de Documentos Criminais
|
138 |
-
### Powered by
|
139 |
""")
|
140 |
|
141 |
with gr.Row():
|
@@ -188,6 +263,12 @@ class WebInterface:
|
|
188 |
placeholder="Indique os fundamentos legais principais..."
|
189 |
)
|
190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
191 |
generate_btn = gr.Button(
|
192 |
"Gerar Documento",
|
193 |
variant="primary"
|
@@ -226,7 +307,7 @@ class WebInterface:
|
|
226 |
doc_type, client_name, process_number,
|
227 |
court, jurisdiction, facts, legal_basis
|
228 |
],
|
229 |
-
outputs=output
|
230 |
)
|
231 |
|
232 |
def _generate_document(
|
@@ -234,31 +315,46 @@ class WebInterface:
|
|
234 |
process_number: str, court: str,
|
235 |
jurisdiction: str, facts: str,
|
236 |
legal_basis: str
|
237 |
-
) ->
|
238 |
"""Gera o documento com os parâmetros fornecidos"""
|
239 |
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
257 |
|
258 |
def launch(self):
|
259 |
"""Inicia a interface web"""
|
260 |
self.app.launch(share=True)
|
261 |
|
262 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
263 |
interface = WebInterface()
|
264 |
interface.launch()
|
|
|
88 |
3. Mérito
|
89 |
4. Análise das provas
|
90 |
5. Pedidos
|
91 |
+
""",
|
92 |
+
|
93 |
+
"resposta_acusacao": f"""
|
94 |
+
Atue como um advogado criminalista e gere uma resposta à acusação:
|
95 |
+
|
96 |
+
DADOS DO PROCESSO:
|
97 |
+
Cliente: {context.get('client_name')}
|
98 |
+
Processo: {context.get('process_number')}
|
99 |
+
Tribunal: {context.get('court')}
|
100 |
+
|
101 |
+
FATOS:
|
102 |
+
{context.get('facts')}
|
103 |
+
|
104 |
+
TESE DEFENSIVA:
|
105 |
+
{context.get('legal_basis')}
|
106 |
+
|
107 |
+
Elabore uma resposta à acusação completa, incluindo:
|
108 |
+
1. Preliminares
|
109 |
+
2. Mérito
|
110 |
+
3. Requerimentos probatórios
|
111 |
+
4. Teses defensivas
|
112 |
+
5. Pedidos
|
113 |
+
""",
|
114 |
+
|
115 |
+
"recurso_sentido_estrito": f"""
|
116 |
+
Atue como um advogado criminalista e gere um recurso em sentido estrito:
|
117 |
+
|
118 |
+
DADOS DO PROCESSO:
|
119 |
+
Cliente: {context.get('client_name')}
|
120 |
+
Processo: {context.get('process_number')}
|
121 |
+
Tribunal: {context.get('court')}
|
122 |
+
|
123 |
+
FATOS:
|
124 |
+
{context.get('facts')}
|
125 |
+
|
126 |
+
FUNDAMENTOS RECURSAIS:
|
127 |
+
{context.get('legal_basis')}
|
128 |
+
|
129 |
+
Elabore um recurso em sentido estrito completo, incluindo:
|
130 |
+
1. Tempestividade
|
131 |
+
2. Cabimento
|
132 |
+
3. Fundamentação
|
133 |
+
4. Pedidos
|
134 |
+
""",
|
135 |
+
|
136 |
+
"apelacao_criminal": f"""
|
137 |
+
Atue como um advogado criminalista e gere uma apelação criminal:
|
138 |
+
|
139 |
+
DADOS DO PROCESSO:
|
140 |
+
Cliente: {context.get('client_name')}
|
141 |
+
Processo: {context.get('process_number')}
|
142 |
+
Tribunal: {context.get('court')}
|
143 |
+
|
144 |
+
FATOS E SENTENÇA:
|
145 |
+
{context.get('facts')}
|
146 |
+
|
147 |
+
RAZÕES RECURSAIS:
|
148 |
+
{context.get('legal_basis')}
|
149 |
+
|
150 |
+
Elabore uma apelação criminal completa, incluindo:
|
151 |
+
1. Tempestividade
|
152 |
+
2. Preliminares
|
153 |
+
3. Mérito recursal
|
154 |
+
4. Pedidos
|
155 |
"""
|
156 |
}
|
157 |
|
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 Mistral público"""
|
162 |
|
163 |
def __init__(self):
|
164 |
+
# Usando modelo Mistral público
|
165 |
+
self.client = Client("HuggingFaceH4/zephyr-7b-beta")
|
166 |
self.prompt_manager = PromptManager()
|
167 |
|
168 |
def generate(self, doc_type: str, context: Dict[str, str]) -> str:
|
|
|
170 |
try:
|
171 |
prompt = self.prompt_manager.get_base_prompt(doc_type, context)
|
172 |
|
173 |
+
# Configuração específica para o modelo
|
174 |
result = self.client.predict(
|
175 |
+
prompt, # prompt principal
|
176 |
+
max_new_tokens=2048, # limite de tokens de saída
|
177 |
+
temperature=0.7, # criatividade moderada
|
178 |
+
top_p=0.95, # mantém a coerência
|
179 |
+
repetition_penalty=1.15, # evita repetições
|
180 |
+
api_name="/generate" # endpoint específico
|
181 |
)
|
182 |
|
183 |
return self._format_output(result)
|
184 |
|
185 |
except Exception as e:
|
186 |
+
error_msg = str(e)
|
187 |
+
logger.error(f"Erro na geração do documento: {error_msg}")
|
188 |
+
|
189 |
+
if "quota" in error_msg.lower():
|
190 |
+
return "O serviço está temporariamente sobrecarregado. Por favor, tente novamente em alguns minutos."
|
191 |
+
elif "timeout" in error_msg.lower():
|
192 |
+
return "O servidor demorou muito para responder. Por favor, tente novamente."
|
193 |
+
else:
|
194 |
+
return f"Erro na geração do documento. Por favor, verifique os dados e tente novamente. Detalhes: {error_msg}"
|
195 |
|
196 |
def _format_output(self, text: str) -> str:
|
197 |
"""Formata o texto gerado para melhor apresentação"""
|
|
|
210 |
with gr.Blocks(theme=gr.themes.Soft()) as self.app:
|
211 |
gr.Markdown("""
|
212 |
# Gerador de Documentos Criminais
|
213 |
+
### Powered by AI Technology
|
214 |
""")
|
215 |
|
216 |
with gr.Row():
|
|
|
263 |
placeholder="Indique os fundamentos legais principais..."
|
264 |
)
|
265 |
|
266 |
+
# Status e botões
|
267 |
+
status = gr.Textbox(
|
268 |
+
label="Status",
|
269 |
+
interactive=False
|
270 |
+
)
|
271 |
+
|
272 |
generate_btn = gr.Button(
|
273 |
"Gerar Documento",
|
274 |
variant="primary"
|
|
|
307 |
doc_type, client_name, process_number,
|
308 |
court, jurisdiction, facts, legal_basis
|
309 |
],
|
310 |
+
outputs=[output, status]
|
311 |
)
|
312 |
|
313 |
def _generate_document(
|
|
|
315 |
process_number: str, court: str,
|
316 |
jurisdiction: str, facts: str,
|
317 |
legal_basis: str
|
318 |
+
) -> tuple:
|
319 |
"""Gera o documento com os parâmetros fornecidos"""
|
320 |
|
321 |
+
try:
|
322 |
+
# Validação básica
|
323 |
+
if not all([client_name, process_number, facts, legal_basis]):
|
324 |
+
return "Erro: Todos os campos obrigatórios devem ser preenchidos", "⚠️ Preencha todos os campos obrigatórios"
|
325 |
+
|
326 |
+
context = {
|
327 |
+
"client_name": client_name,
|
328 |
+
"process_number": process_number,
|
329 |
+
"court": court,
|
330 |
+
"jurisdiction": jurisdiction,
|
331 |
+
"facts": facts,
|
332 |
+
"legal_basis": legal_basis
|
333 |
+
}
|
334 |
+
|
335 |
+
# Atualiza status
|
336 |
+
yield "", "⏳ Gerando documento..."
|
337 |
+
|
338 |
+
# Gera documento
|
339 |
+
result = self.generator.generate(
|
340 |
+
doc_type.lower().replace(" ", "_"),
|
341 |
+
context
|
342 |
+
)
|
343 |
+
|
344 |
+
return result, "✅ Documento gerado com sucesso!"
|
345 |
+
|
346 |
+
except Exception as e:
|
347 |
+
logger.error(f"Erro: {str(e)}")
|
348 |
+
return "", f"❌ Erro: {str(e)}"
|
349 |
|
350 |
def launch(self):
|
351 |
"""Inicia a interface web"""
|
352 |
self.app.launch(share=True)
|
353 |
|
354 |
if __name__ == "__main__":
|
355 |
+
# Arquivo requirements.txt necessário:
|
356 |
+
# gradio==4.19.2
|
357 |
+
# gradio_client==0.10.1
|
358 |
+
|
359 |
interface = WebInterface()
|
360 |
interface.launch()
|