metrology_rag / app.py
DHEIVER's picture
Rename App.py to app.py
2ee5fb0 verified
raw
history blame
2.57 kB
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)