Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,73 +1,107 @@
|
|
1 |
import gradio as gr
|
2 |
import re
|
3 |
import os
|
|
|
4 |
from metrology_rag import MetrologyRAGSystem
|
5 |
|
6 |
-
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
def
|
12 |
-
|
13 |
-
|
14 |
-
temp_dir = "/kaggle/working/temp_pdfs/"
|
15 |
-
os.makedirs(temp_dir, exist_ok=True)
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
27 |
|
28 |
-
def
|
29 |
-
|
30 |
-
return "⚠️ Inicialize o sistema primeiro!"
|
31 |
-
try:
|
32 |
-
response = rag_system.generate_technical_response(query)
|
33 |
-
return strip_ansi_codes(response)
|
34 |
-
except Exception as e:
|
35 |
-
return f"Erro: {str(e)}"
|
36 |
|
37 |
-
|
38 |
-
if not rag_system:
|
39 |
-
return "⚠️ Sistema não inicializado!"
|
40 |
-
try:
|
41 |
-
temp_path = f"/kaggle/working/{os.path.basename(file.name)}"
|
42 |
-
with open(temp_path, "wb") as f:
|
43 |
-
f.write(file.read())
|
44 |
-
analysis = rag_system.analyze_metrology_report(temp_path)
|
45 |
-
return strip_ansi_codes(analysis)
|
46 |
-
except Exception as e:
|
47 |
-
return f"Erro: {str(e)}"
|
48 |
|
49 |
-
with gr.Blocks(title="
|
50 |
-
gr.Markdown("# 🔧
|
51 |
|
52 |
-
with gr.
|
53 |
-
gr.
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
demo.launch(debug=True)
|
|
|
1 |
import gradio as gr
|
2 |
import re
|
3 |
import os
|
4 |
+
import numpy as np
|
5 |
from metrology_rag import MetrologyRAGSystem
|
6 |
|
7 |
+
css = """
|
8 |
+
footer {visibility: hidden}
|
9 |
+
.markdown {max-width: 95% !important}
|
10 |
+
"""
|
11 |
|
12 |
+
class SpaceInterface:
|
13 |
+
def __init__(self):
|
14 |
+
self.rag_system = None
|
15 |
+
self.current_response = ""
|
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 |
+
for file in files:
|
23 |
+
file_path = os.path.join(temp_dir, os.path.basename(file.name))
|
24 |
+
with open(file_path, "wb") as f:
|
25 |
+
f.write(file.read())
|
26 |
+
|
27 |
+
self.rag_system = MetrologyRAGSystem()
|
28 |
+
self.rag_system.initialize_system(temp_dir)
|
29 |
+
return f"✅ Sistema inicializado com {len(self.rag_system.documents)} documentos!"
|
30 |
+
except Exception as e:
|
31 |
+
return f"❌ Erro: {str(e)}"
|
32 |
|
33 |
+
def process_query(self, query):
|
34 |
+
if not self.rag_system:
|
35 |
+
return "⚠️ Inicialize o sistema primeiro com documentos PDF!"
|
|
|
|
|
36 |
|
37 |
+
try:
|
38 |
+
response = self.rag_system.generate_technical_response(query)
|
39 |
+
self.current_response = response
|
40 |
+
return self._clean_response(response)
|
41 |
+
except Exception as e:
|
42 |
+
return f"Erro: {str(e)}"
|
43 |
+
|
44 |
+
def generate_report(self, report_title):
|
45 |
+
if not self.rag_system or not self.current_response:
|
46 |
+
return None, "⚠️ Execute uma consulta primeiro!"
|
47 |
|
48 |
+
try:
|
49 |
+
report_path = "/tmp/relatorio_tecnico.md"
|
50 |
+
self.rag_system.generate_report(report_title, self.current_response, report_path)
|
51 |
+
return report_path, "✅ Relatório gerado com sucesso!"
|
52 |
+
except Exception as e:
|
53 |
+
return None, f"❌ Falha: {str(e)}"
|
54 |
|
55 |
+
def _clean_response(self, text):
|
56 |
+
return re.sub(r'\x1B\[[0-?]*[ -/]*[@-~]', '', text)
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
+
interface = SpaceInterface()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
+
with gr.Blocks(css=css, title="Metrology Assistant") as demo:
|
61 |
+
gr.Markdown("# 🔧 Assistente de Metrologia Inteligente")
|
62 |
|
63 |
+
with gr.Row():
|
64 |
+
with gr.Column(scale=2):
|
65 |
+
file_input = gr.File(
|
66 |
+
file_count="multiple",
|
67 |
+
file_types=[".pdf"],
|
68 |
+
label="Upload de Documentos Técnicos"
|
69 |
+
)
|
70 |
+
init_btn = gr.Button("Inicializar Sistema")
|
71 |
+
init_status = gr.Textbox(label="Status", interactive=False)
|
72 |
+
|
73 |
+
with gr.Column(scale=3):
|
74 |
+
query_input = gr.Textbox(
|
75 |
+
label="Consulta Técnica",
|
76 |
+
placeholder="Digite sua pergunta sobre metrologia..."
|
77 |
+
)
|
78 |
+
query_btn = gr.Button("Enviar Consulta")
|
79 |
+
response_output = gr.Markdown(label="Resposta Técnica")
|
80 |
+
|
81 |
+
report_input = gr.Textbox(
|
82 |
+
label="Título do Relatório",
|
83 |
+
placeholder="Digite o título do relatório..."
|
84 |
+
)
|
85 |
+
report_btn = gr.Button("Gerar Relatório")
|
86 |
+
report_download = gr.File(label="Download do Relatório")
|
87 |
+
report_status = gr.Markdown()
|
88 |
|
89 |
+
init_btn.click(
|
90 |
+
interface.initialize_system,
|
91 |
+
inputs=file_input,
|
92 |
+
outputs=init_status
|
93 |
+
)
|
94 |
+
|
95 |
+
query_btn.click(
|
96 |
+
interface.process_query,
|
97 |
+
inputs=query_input,
|
98 |
+
outputs=response_output
|
99 |
+
)
|
100 |
+
|
101 |
+
report_btn.click(
|
102 |
+
interface.generate_report,
|
103 |
+
inputs=report_input,
|
104 |
+
outputs=[report_download, report_status]
|
105 |
+
)
|
106 |
|
107 |
demo.launch(debug=True)
|