Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -43,9 +43,8 @@ css_style = """
|
|
43 |
"""
|
44 |
|
45 |
def eliminar_proceso_pensamiento(texto):
|
46 |
-
|
47 |
-
|
48 |
-
return lineas[-1] if lineas else "Respuesta no disponible"
|
49 |
|
50 |
def get_pdf_text(pdf_docs):
|
51 |
text = ""
|
@@ -87,9 +86,9 @@ def get_conversational_chain():
|
|
87 |
|
88 |
def extract_metadata(vector_store):
|
89 |
metadata_questions = {
|
90 |
-
"title": "¿
|
91 |
"entity": "¿A qué entidad u organización pertenece este documento?",
|
92 |
-
"date": "¿
|
93 |
}
|
94 |
|
95 |
metadata = {}
|
@@ -107,6 +106,7 @@ def extract_metadata(vector_store):
|
|
107 |
return metadata
|
108 |
|
109 |
def mostrar_respuesta(texto):
|
|
|
110 |
with st.container():
|
111 |
st.markdown(f'<div class="response-box">{texto}</div>', unsafe_allow_html=True)
|
112 |
|
@@ -132,10 +132,6 @@ def main():
|
|
132 |
st.title("PDF Consultor 🔍")
|
133 |
st.markdown(css_style, unsafe_allow_html=True)
|
134 |
|
135 |
-
# Inicializa estado de sesión
|
136 |
-
if 'documento_cargado' not in st.session_state:
|
137 |
-
st.session_state.documento_cargado = False
|
138 |
-
|
139 |
# Sidebar - Carga de documentos
|
140 |
with st.sidebar:
|
141 |
st.markdown('<p class="step-number">1 Subir archivos</p>', unsafe_allow_html=True)
|
@@ -147,7 +143,7 @@ def main():
|
|
147 |
)
|
148 |
|
149 |
# Procesamiento automático al cargar documentos
|
150 |
-
if pdf_docs and not st.session_state.
|
151 |
with st.spinner("Analizando documento..."):
|
152 |
try:
|
153 |
raw_text = get_pdf_text(pdf_docs)
|
@@ -156,7 +152,7 @@ def main():
|
|
156 |
|
157 |
st.session_state.metadata = extract_metadata(vector_store)
|
158 |
st.session_state.vector_store = vector_store
|
159 |
-
st.session_state.
|
160 |
|
161 |
st.rerun()
|
162 |
|
@@ -184,23 +180,31 @@ def main():
|
|
184 |
st.markdown("---")
|
185 |
|
186 |
# Interfaz de consultas
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
|
205 |
if __name__ == "__main__":
|
206 |
main()
|
|
|
43 |
"""
|
44 |
|
45 |
def eliminar_proceso_pensamiento(texto):
|
46 |
+
"""Elimina todo contenido entre incluyendo las etiquetas"""
|
47 |
+
return re.sub(r'', '', texto, flags=re.DOTALL).strip()
|
|
|
48 |
|
49 |
def get_pdf_text(pdf_docs):
|
50 |
text = ""
|
|
|
86 |
|
87 |
def extract_metadata(vector_store):
|
88 |
metadata_questions = {
|
89 |
+
"title": "¿Cuál es el título principal del documento?",
|
90 |
"entity": "¿A qué entidad u organización pertenece este documento?",
|
91 |
+
"date": "¿Qué fecha de implantación se menciona en el documento?"
|
92 |
}
|
93 |
|
94 |
metadata = {}
|
|
|
106 |
return metadata
|
107 |
|
108 |
def mostrar_respuesta(texto):
|
109 |
+
"""Muestra la respuesta formateada en un contenedor especial"""
|
110 |
with st.container():
|
111 |
st.markdown(f'<div class="response-box">{texto}</div>', unsafe_allow_html=True)
|
112 |
|
|
|
132 |
st.title("PDF Consultor 🔍")
|
133 |
st.markdown(css_style, unsafe_allow_html=True)
|
134 |
|
|
|
|
|
|
|
|
|
135 |
# Sidebar - Carga de documentos
|
136 |
with st.sidebar:
|
137 |
st.markdown('<p class="step-number">1 Subir archivos</p>', unsafe_allow_html=True)
|
|
|
143 |
)
|
144 |
|
145 |
# Procesamiento automático al cargar documentos
|
146 |
+
if pdf_docs and not st.session_state.get('processed'):
|
147 |
with st.spinner("Analizando documento..."):
|
148 |
try:
|
149 |
raw_text = get_pdf_text(pdf_docs)
|
|
|
152 |
|
153 |
st.session_state.metadata = extract_metadata(vector_store)
|
154 |
st.session_state.vector_store = vector_store
|
155 |
+
st.session_state.processed = True
|
156 |
|
157 |
st.rerun()
|
158 |
|
|
|
180 |
st.markdown("---")
|
181 |
|
182 |
# Interfaz de consultas
|
183 |
+
with st.form("consulta_form"):
|
184 |
+
col1, col2 = st.columns([5, 1])
|
185 |
+
with col1:
|
186 |
+
user_question = st.text_input(
|
187 |
+
"Escribe tu pregunta:",
|
188 |
+
placeholder="Ej: ¿Qué normativa regula este proceso?",
|
189 |
+
label_visibility="collapsed"
|
190 |
+
)
|
191 |
+
with col2:
|
192 |
+
st.markdown("<br>", unsafe_allow_html=True)
|
193 |
+
enviar = st.form_submit_button("Enviar ▶")
|
194 |
+
|
195 |
+
botones_rapidos = st.columns(3)
|
196 |
+
with botones_rapidos[0]:
|
197 |
+
if st.form_submit_button("📝 Resumen ejecutivo"):
|
198 |
+
user_question = "Genera un resumen ejecutivo de máximo 3 párrafos"
|
199 |
+
with botones_rapidos[1]:
|
200 |
+
if st.form_submit_button("🏛️ Entidad relacionada"):
|
201 |
+
user_question = "¿A qué organización o entidad pertenece este documento?"
|
202 |
+
with botones_rapidos[2]:
|
203 |
+
if st.form_submit_button("📅 Fechas clave"):
|
204 |
+
user_question = "Lista las fechas importantes mencionadas en orden cronológico"
|
205 |
+
|
206 |
+
if user_question and enviar:
|
207 |
+
procesar_consulta(user_question)
|
208 |
|
209 |
if __name__ == "__main__":
|
210 |
main()
|