File size: 2,992 Bytes
6f053b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
import streamlit as st
import os
import sys

# Ensure src/infrastructure is in sys.path to allow imports if running from root
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(current_dir)

from advanced_broker_vehicular import preparar_rag, clasificar_intencion, manejar_saludo, manejar_emergencia

# Page configuration
st.set_page_config(page_title="Copiloto de Seguros", page_icon="🚗", layout="centered")

# Helper function to cache the RAG chain
@st.cache_resource(show_spinner="Inicializando sistema de seguros...")
def get_cached_rag_chain():
    return preparar_rag()

# Initialize session state for chat history
if "messages" not in st.session_state:
    st.session_state.messages = []

# Load the RAG chain (cached)
try:
    st.session_state.rag_chain = get_cached_rag_chain()
except Exception as e:
    st.error(f"Error al cargar el sistema de seguros: {e}")
    st.session_state.rag_chain = None


# Sidebar
st.sidebar.title("🚗 Copiloto de Seguros")
st.sidebar.markdown("""
Asistente inteligente para consultas sobre seguros vehiculares.
- **Consultas**: Pregunta por coberturas, precios, etc.
- **Emergencias**: Reporta siniestros.
- **Comparaciones**: Compara aseguradoras.
""")

# Display chat messages
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

# Chat input
if prompt := st.chat_input("Escribe tu consulta aquí..."):
    # Add user message to chat history
    st.session_state.messages.append({"role": "user", "content": prompt})
    with st.chat_message("user"):
        st.markdown(prompt)

    # Process response
    with st.chat_message("assistant"):
        message_placeholder = st.empty()
        full_response = ""
        
        # 1. Classify intention
        intencion = clasificar_intencion(prompt)
        
        response_text = ""
        
        if intencion == "SALUDO":
            response_text = manejar_saludo()
        
        elif intencion == "EMERGENCIA":
            response_text = manejar_emergencia()
            
        elif intencion == "CONSULTA":
            if st.session_state.rag_chain:
                message_placeholder.markdown("🔍 Analizando pólizas...")
                try:
                    res = st.session_state.rag_chain.invoke({"query": prompt})
                    response_text = res['result']
                except Exception as e:
                    response_text = f"Error al consultar la base de conocimiento: {str(e)}"
            else:
                response_text = "⚠️ El sistema de consultas no está disponible (PDFs no cargados)."
        
        else:
            response_text = "No estoy seguro de cómo ayudarte con eso. Intenta preguntar sobre seguros, emergencias o salúdame."

        message_placeholder.markdown(response_text)
        
        # Add assistant response to chat history
        st.session_state.messages.append({"role": "assistant", "content": response_text})