Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,111 +1,70 @@
|
|
1 |
import gradio as gr
|
2 |
-
import re
|
3 |
import os
|
|
|
4 |
import numpy as np
|
5 |
from metrology_rag import MetrologyRAGSystem
|
6 |
|
7 |
-
|
8 |
-
footer {visibility: hidden}
|
9 |
-
.markdown {max-width: 95% !important}
|
10 |
-
"""
|
11 |
-
|
12 |
-
class SpaceInterface:
|
13 |
def __init__(self):
|
14 |
-
|
15 |
-
self.
|
16 |
-
|
17 |
-
def initialize_system(self, files):
|
18 |
-
try:
|
19 |
-
temp_dir = "/tmp/metrology_pdfs"
|
20 |
-
os.makedirs(temp_dir, exist_ok=True)
|
21 |
-
|
22 |
-
# Correção necessária aqui
|
23 |
-
for file in files:
|
24 |
-
file_path = os.path.join(temp_dir, os.path.basename(file.name))
|
25 |
-
# Usar open() no arquivo temporário
|
26 |
-
with open(file.name, "rb") as f:
|
27 |
-
content = f.read()
|
28 |
-
with open(file_path, "wb") as dest_file:
|
29 |
-
dest_file.write(content)
|
30 |
-
|
31 |
-
self.rag_system = MetrologyRAGSystem()
|
32 |
-
self.rag_system.initialize_system(temp_dir)
|
33 |
-
return f"✅ Sistema inicializado com {len(self.rag_system.documents)} documentos!"
|
34 |
-
except Exception as e:
|
35 |
-
return f"❌ Erro: {str(e)}"
|
36 |
-
|
37 |
-
def process_query(self, query):
|
38 |
-
if not self.rag_system:
|
39 |
-
return "⚠️ Inicialize o sistema primeiro com documentos PDF!"
|
40 |
|
|
|
41 |
try:
|
42 |
response = self.rag_system.generate_technical_response(query)
|
43 |
-
self.current_response = response
|
44 |
return self._clean_response(response)
|
45 |
except Exception as e:
|
46 |
-
return f"Erro: {str(e)}"
|
47 |
|
48 |
-
def
|
49 |
-
if not self.rag_system or not self.current_response:
|
50 |
-
return None, "⚠️ Execute uma consulta primeiro!"
|
51 |
-
|
52 |
try:
|
53 |
-
|
54 |
-
self.
|
55 |
-
return report_path, "✅ Relatório gerado com sucesso!"
|
56 |
except Exception as e:
|
57 |
-
return
|
58 |
|
59 |
def _clean_response(self, text):
|
60 |
return re.sub(r'\x1B\[[0-?]*[ -/]*[@-~]', '', text)
|
61 |
|
62 |
-
|
63 |
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
-
with gr.
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
init_status = gr.Textbox(label="Status", interactive=False)
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
report_input = gr.Textbox(
|
86 |
-
label="Título do Relatório",
|
87 |
-
placeholder="Digite o título do relatório..."
|
88 |
-
)
|
89 |
-
report_btn = gr.Button("Gerar Relatório")
|
90 |
-
report_download = gr.File(label="Download do Relatório")
|
91 |
-
report_status = gr.Markdown()
|
92 |
-
|
93 |
-
init_btn.click(
|
94 |
-
interface.initialize_system,
|
95 |
-
inputs=file_input,
|
96 |
-
outputs=init_status
|
97 |
-
)
|
98 |
|
99 |
query_btn.click(
|
100 |
-
|
101 |
inputs=query_input,
|
102 |
outputs=response_output
|
103 |
)
|
104 |
|
105 |
-
|
106 |
-
|
107 |
inputs=report_input,
|
108 |
-
outputs=
|
109 |
)
|
110 |
|
111 |
-
demo.launch(
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import os
|
3 |
+
import re
|
4 |
import numpy as np
|
5 |
from metrology_rag import MetrologyRAGSystem
|
6 |
|
7 |
+
class CalibrationAssistant:
|
|
|
|
|
|
|
|
|
|
|
8 |
def __init__(self):
|
9 |
+
# Inicializa com documentos pré-carregados
|
10 |
+
self.rag_system = MetrologyRAGSystem()
|
11 |
+
self.rag_system.initialize_system("rag_documents")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
def technical_query(self, query):
|
14 |
try:
|
15 |
response = self.rag_system.generate_technical_response(query)
|
|
|
16 |
return self._clean_response(response)
|
17 |
except Exception as e:
|
18 |
+
return f"Erro na consulta: {str(e)}"
|
19 |
|
20 |
+
def analyze_report(self, file):
|
|
|
|
|
|
|
21 |
try:
|
22 |
+
analysis = self.rag_system.analyze_metrology_report(file.name)
|
23 |
+
return self._clean_response(analysis)
|
|
|
24 |
except Exception as e:
|
25 |
+
return f"Erro na análise: {str(e)}"
|
26 |
|
27 |
def _clean_response(self, text):
|
28 |
return re.sub(r'\x1B\[[0-?]*[ -/]*[@-~]', '', text)
|
29 |
|
30 |
+
assistant = CalibrationAssistant()
|
31 |
|
32 |
+
css = """
|
33 |
+
footer {visibility: hidden}
|
34 |
+
.markdown {max-width: 95% !important}
|
35 |
+
"""
|
36 |
+
|
37 |
+
with gr.Blocks(css=css, title="Assistente de Metrologia") as demo:
|
38 |
+
gr.Markdown("# 🔧 Assistente de Metrologia Certificada")
|
39 |
|
40 |
+
with gr.Tab("Consulta Técnica"):
|
41 |
+
gr.Markdown("### Sistema RAG com Documentação Pré-Carregada")
|
42 |
+
query_input = gr.Textbox(
|
43 |
+
label="Faça sua consulta técnica",
|
44 |
+
placeholder="Ex: Qual o procedimento para calibração de micrômetros?..."
|
45 |
+
)
|
46 |
+
query_btn = gr.Button("Enviar")
|
47 |
+
response_output = gr.Markdown(label="Resposta Técnica Certificada")
|
|
|
48 |
|
49 |
+
with gr.Tab("Análise de Relatório"):
|
50 |
+
gr.Markdown("### Verificação de Conformidade de Relatórios")
|
51 |
+
report_input = gr.File(
|
52 |
+
label="Carregue seu relatório de calibração (PDF)",
|
53 |
+
file_types=[".pdf"]
|
54 |
+
)
|
55 |
+
analyze_btn = gr.Button("Analisar")
|
56 |
+
analysis_output = gr.Markdown(label="Resultado da Análise")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
query_btn.click(
|
59 |
+
assistant.technical_query,
|
60 |
inputs=query_input,
|
61 |
outputs=response_output
|
62 |
)
|
63 |
|
64 |
+
analyze_btn.click(
|
65 |
+
assistant.analyze_report,
|
66 |
inputs=report_input,
|
67 |
+
outputs=analysis_output
|
68 |
)
|
69 |
|
70 |
+
demo.launch()
|