File size: 2,573 Bytes
853a257
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import gradio as gr
import re
import os
from metrology_rag import MetrologyRAGSystem

rag_system = None

def strip_ansi_codes(text):
    return re.sub(r'\x1B\[[0-?]*[ -/]*[@-~]', '', text)

def initialize_system(files):
    global rag_system
    try:
        temp_dir = "/kaggle/working/temp_pdfs/"
        os.makedirs(temp_dir, exist_ok=True)
        
        for file in files:
            file_path = os.path.join(temp_dir, os.path.basename(file.name))
            with open(file_path, "wb") as f:
                f.write(file.read())
        
        rag_system = MetrologyRAGSystem()
        rag_system.initialize_system(temp_dir)
        return f"✅ Sistema inicializado com {len(rag_system.documents)} documentos!"
    except Exception as e:
        return f"❌ Erro: {str(e)}"

def answer_query(query):
    if not rag_system:
        return "⚠️ Inicialize o sistema primeiro!"
    try:
        response = rag_system.generate_technical_response(query)
        return strip_ansi_codes(response)
    except Exception as e:
        return f"Erro: {str(e)}"

def analyze_report(file):
    if not rag_system:
        return "⚠️ Sistema não inicializado!"
    try:
        temp_path = f"/kaggle/working/{os.path.basename(file.name)}"
        with open(temp_path, "wb") as f:
            f.write(file.read())
        analysis = rag_system.analyze_metrology_report(temp_path)
        return strip_ansi_codes(analysis)
    except Exception as e:
        return f"Erro: {str(e)}"

with gr.Blocks(title="Sistema Metrologia v2.1") as demo:
    gr.Markdown("# 🔧 Sistema de Metrologia Inteligente")
    
    with gr.Tab("Inicialização"):
        gr.Markdown("### Carregar Documentos PDF")
        file_input = gr.File(file_count="multiple", file_types=[".pdf"])
        init_btn = gr.Button("Inicializar")
        init_status = gr.Textbox(label="Status")
        init_btn.click(initialize_system, file_input, init_status)

    with gr.Tab("Consulta"):
        gr.Markdown("### Consulta Técnica")
        query_input = gr.Textbox(label="Sua consulta")
        query_btn = gr.Button("Enviar")
        response_output = gr.Markdown(label="Resposta")
        query_btn.click(answer_query, query_input, response_output)

    with gr.Tab("Análise"):
        gr.Markdown("### Analisar Relatório")
        report_input = gr.File(file_count="single", file_types=[".pdf"])
        analyze_btn = gr.Button("Analisar")
        analysis_output = gr.Markdown(label="Resultado")
        analyze_btn.click(analyze_report, report_input, analysis_output)

demo.launch(debug=True)