Spaces:
Sleeping
Sleeping
Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import logging
|
3 |
+
from datetime import datetime
|
4 |
+
from analyzers.gemini_analyzer import GeminiAnalyzer
|
5 |
+
from analyzers.ner_analyzer import NERAnalyzer
|
6 |
+
from docling.document_converter import DocumentConverter
|
7 |
+
|
8 |
+
# Configuração de logging
|
9 |
+
logging.basicConfig(
|
10 |
+
level=logging.INFO,
|
11 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
12 |
+
handlers=[
|
13 |
+
logging.FileHandler(f'contract_analyzer_{datetime.now().strftime("%Y%m%d")}.log'),
|
14 |
+
logging.StreamHandler()
|
15 |
+
]
|
16 |
+
)
|
17 |
+
logger = logging.getLogger(__name__)
|
18 |
+
|
19 |
+
class ContractAnalyzer:
|
20 |
+
def __init__(self):
|
21 |
+
self.gemini_analyzer = GeminiAnalyzer()
|
22 |
+
self.ner_analyzer = NERAnalyzer()
|
23 |
+
self.document_converter = DocumentConverter()
|
24 |
+
|
25 |
+
def analyze_contract(self, file, analysis_type: str):
|
26 |
+
logger.info(f"Iniciando análise do arquivo usando {analysis_type}: {file.name}")
|
27 |
+
|
28 |
+
try:
|
29 |
+
result = self.document_converter.convert(file.name)
|
30 |
+
document_text = result.document.export_to_markdown()
|
31 |
+
|
32 |
+
if analysis_type == "Gemini":
|
33 |
+
analysis_result = self.gemini_analyzer.analyze(document_text)
|
34 |
+
output = self.gemini_analyzer.format_output(analysis_result)
|
35 |
+
else: # NER
|
36 |
+
entities = self.ner_analyzer.extract_entities(document_text)
|
37 |
+
representatives = self.ner_analyzer.extract_representatives(entities)
|
38 |
+
output = self.ner_analyzer.format_output(representatives)
|
39 |
+
|
40 |
+
return document_text, output
|
41 |
+
|
42 |
+
except Exception as e:
|
43 |
+
logger.error(f"Erro durante análise do contrato: {str(e)}")
|
44 |
+
return "", f"Erro ao processar o arquivo: {str(e)}"
|
45 |
+
|
46 |
+
def create_interface():
|
47 |
+
analyzer = ContractAnalyzer()
|
48 |
+
try:
|
49 |
+
logger.info("Iniciando configuração da interface Gradio")
|
50 |
+
iface = gr.Interface(
|
51 |
+
fn=analyzer.analyze_contract,
|
52 |
+
inputs=[
|
53 |
+
"file",
|
54 |
+
gr.Radio(
|
55 |
+
choices=["Gemini", "NER"],
|
56 |
+
label="Tipo de Análise",
|
57 |
+
value="Gemini"
|
58 |
+
)
|
59 |
+
],
|
60 |
+
outputs=[
|
61 |
+
gr.Textbox(label="Texto do Contrato"),
|
62 |
+
gr.Textbox(label="Resultado da Análise")
|
63 |
+
],
|
64 |
+
title="Analisador de Contratos Sociais",
|
65 |
+
description="Este aplicativo analisa contratos sociais usando Gemini ou NER para identificar representantes legais.",
|
66 |
+
)
|
67 |
+
logger.info("Interface Gradio configurada com sucesso")
|
68 |
+
return iface
|
69 |
+
except Exception as e:
|
70 |
+
logger.error(f"Erro ao configurar interface Gradio: {str(e)}")
|
71 |
+
raise
|
72 |
+
|
73 |
+
if __name__ == "__main__":
|
74 |
+
logger.info("Iniciando aplicação")
|
75 |
+
iface = create_interface()
|
76 |
+
iface.launch()
|