Spaces:
Runtime error
Runtime error
Create App.py
Browse files
App.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import re
|
3 |
+
import os
|
4 |
+
from metrology_rag import MetrologyRAGSystem
|
5 |
+
|
6 |
+
rag_system = None
|
7 |
+
|
8 |
+
def strip_ansi_codes(text):
|
9 |
+
return re.sub(r'\x1B\[[0-?]*[ -/]*[@-~]', '', text)
|
10 |
+
|
11 |
+
def initialize_system(files):
|
12 |
+
global rag_system
|
13 |
+
try:
|
14 |
+
temp_dir = "/kaggle/working/temp_pdfs/"
|
15 |
+
os.makedirs(temp_dir, exist_ok=True)
|
16 |
+
|
17 |
+
for file in files:
|
18 |
+
file_path = os.path.join(temp_dir, os.path.basename(file.name))
|
19 |
+
with open(file_path, "wb") as f:
|
20 |
+
f.write(file.read())
|
21 |
+
|
22 |
+
rag_system = MetrologyRAGSystem()
|
23 |
+
rag_system.initialize_system(temp_dir)
|
24 |
+
return f"✅ Sistema inicializado com {len(rag_system.documents)} documentos!"
|
25 |
+
except Exception as e:
|
26 |
+
return f"❌ Erro: {str(e)}"
|
27 |
+
|
28 |
+
def answer_query(query):
|
29 |
+
if not rag_system:
|
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 |
+
def analyze_report(file):
|
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="Sistema Metrologia v2.1") as demo:
|
50 |
+
gr.Markdown("# 🔧 Sistema de Metrologia Inteligente")
|
51 |
+
|
52 |
+
with gr.Tab("Inicialização"):
|
53 |
+
gr.Markdown("### Carregar Documentos PDF")
|
54 |
+
file_input = gr.File(file_count="multiple", file_types=[".pdf"])
|
55 |
+
init_btn = gr.Button("Inicializar")
|
56 |
+
init_status = gr.Textbox(label="Status")
|
57 |
+
init_btn.click(initialize_system, file_input, init_status)
|
58 |
+
|
59 |
+
with gr.Tab("Consulta"):
|
60 |
+
gr.Markdown("### Consulta Técnica")
|
61 |
+
query_input = gr.Textbox(label="Sua consulta")
|
62 |
+
query_btn = gr.Button("Enviar")
|
63 |
+
response_output = gr.Markdown(label="Resposta")
|
64 |
+
query_btn.click(answer_query, query_input, response_output)
|
65 |
+
|
66 |
+
with gr.Tab("Análise"):
|
67 |
+
gr.Markdown("### Analisar Relatório")
|
68 |
+
report_input = gr.File(file_count="single", file_types=[".pdf"])
|
69 |
+
analyze_btn = gr.Button("Analisar")
|
70 |
+
analysis_output = gr.Markdown(label="Resultado")
|
71 |
+
analyze_btn.click(analyze_report, report_input, analysis_output)
|
72 |
+
|
73 |
+
demo.launch(debug=True)
|