-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
318 lines (259 loc) · 10.8 KB
/
app.py
File metadata and controls
318 lines (259 loc) · 10.8 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import streamlit as st
import os
from dotenv import load_dotenv
from agent import AIAssistant
# Cargar variables de entorno
load_dotenv()
def init_session_state():
"""Inicializa todas las variables de estado de sesión"""
if "messages" not in st.session_state:
st.session_state.messages = []
if "conversation_started" not in st.session_state:
st.session_state.conversation_started = False
if "assistant_initialized" not in st.session_state:
st.session_state.assistant_initialized = False
if "assistant" not in st.session_state:
st.session_state.assistant = None
if "pending_question" not in st.session_state:
st.session_state.pending_question = None
if "processing" not in st.session_state:
st.session_state.processing = False
def initialize_assistant():
"""Inicializa el asistente de IA"""
if not st.session_state.assistant_initialized:
try:
with st.spinner("🤖 Inicializando asistente de IA..."):
st.session_state.assistant = AIAssistant()
st.session_state.assistant_initialized = True
except Exception as e:
st.error(f"Error al inicializar el asistente: {str(e)}")
st.stop()
def display_chat_history():
"""Muestra el historial de chat"""
for message in st.session_state.messages:
with st.chat_message(message["role"]):
# Renderizar LaTeX si está presente
content = message["content"]
if "\\(" in content or "$$" in content:
st.markdown(content, unsafe_allow_html=True)
else:
st.markdown(content)
def process_user_input(user_input):
"""Procesa la entrada del usuario"""
if not st.session_state.assistant:
st.error("El asistente no está inicializado")
return None
try:
response = st.session_state.assistant.chat(user_input)
# Asegurar que la respuesta tenga fuentes si no las tiene
if "**Fuentes:**" not in response and "Fuentes:" not in response:
response += "\n\n**Fuentes:**\n- Basado en conocimiento general del curso"
return response
except Exception as e:
return f"**Error:** {str(e)}\n\n**Fuentes:**\n- Error en el procesamiento"
def main():
# Configuración de la página
st.set_page_config(
page_title="Asistente IA - Curso TEC",
page_icon="🤖",
layout="wide",
initial_sidebar_state="expanded"
)
# CSS personalizado para LaTeX y mejor UI
st.markdown("""
<style>
/* Soporte para LaTeX */
.katex { font-size: 1.1em !important; }
/* Mejorar apariencia del chat */
.stChatMessage {
padding: 1rem;
margin: 0.5rem 0;
}
/* Spinner personalizado */
.stSpinner > div {
text-align: center;
color: #ff6b6b;
}
/* Mejorar sidebar */
.css-1d391kg {
padding-top: 1rem;
}
/* Evitar que el spinner aparezca en el sidebar */
.css-1kyxreq .stSpinner {
display: none;
}
/* Estilos para fuentes */
.sources-section {
border-top: 1px solid #333;
padding-top: 1rem;
margin-top: 1rem;
background-color: rgba(0,0,0,0.1);
border-radius: 5px;
padding: 1rem;
}
</style>
<!-- MathJax para renderizar LaTeX -->
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<script>
window.MathJax = {
tex: {
inlineMath: [['\\(', '\\)']],
displayMath: [['$$', '$$']],
processEscapes: true
}
};
</script>
""", unsafe_allow_html=True)
# Inicializar estado de sesión PRIMERO
init_session_state()
# Título principal
st.title("🤖 Asistente de IA - Curso TEC")
st.markdown("*Especializado en Inteligencia Artificial*")
st.markdown("---")
# Sidebar con información
with st.sidebar:
st.header("📚 Información del Asistente")
# Verificar API Key
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
st.error("No se encontró OPENAI_API_KEY")
else:
st.success("API Key configurada")
st.markdown("""
**Capacidades:**
- 🔍 Búsqueda en apuntes del curso
- 📖 Consultas en Wikipedia
- 💬 Memoria de conversación
- 🎯 Respuestas especializadas en IA
- 📐 Soporte para fórmulas matemáticas (LaTeX)
- 📚 Fuentes siempre citadas
**Herramientas disponibles:**
- `rag_search`: Busca en apuntes del curso
- `wikipedia_search`: Busca información externa
""")
st.markdown("---")
# Botón para reiniciar conversación
if st.button("🔄 Nueva Conversación", type="secondary"):
st.session_state.messages = []
if st.session_state.assistant:
st.session_state.assistant.reset_memory()
st.session_state.conversation_started = False
st.session_state.pending_question = None
st.session_state.processing = False
st.rerun()
# Estadísticas de la conversación
if st.session_state.messages:
user_messages = len([m for m in st.session_state.messages if m["role"] == "user"])
st.metric("Mensajes enviados", user_messages)
st.markdown("---")
# Ejemplos de preguntas
st.markdown("### 💡 Preguntas de ejemplo:")
example_questions = [
"¿Qué es backpropagation?",
"¿Quién escribió los apuntes de la semana 7?",
"Explícame el proyecto II",
"¿Qué es la función sigmoide?",
"Busca información sobre Geoffrey Hinton en Wikipedia"
]
for i, question in enumerate(example_questions):
if st.button(f"📝 {question}", key=f"example_{i}", disabled=st.session_state.processing):
# Solo marcar la pregunta como pendiente si no estamos procesando
if not st.session_state.processing:
st.session_state.pending_question = question
st.rerun()
# Inicializar asistente
initialize_assistant()
# Mensaje de bienvenida
if not st.session_state.conversation_started:
welcome_message = """
¡Hola! 👋 Soy tu asistente especializado en el curso de **Inteligencia Artificial del TEC**.
**¿En qué puedo ayudarte?**
- 📚 Responder preguntas sobre conceptos del curso
- 📝 Explicar tareas y proyectos
- 🔍 Buscar información específica por semana o autor
- 🌐 Proporcionar información adicional de Wikipedia
- 📐 Explicar fórmulas matemáticas con notación LaTeX
**Instrucciones:**
- Haz preguntas específicas sobre el curso
- Si necesitas información externa, pídeme que busque en Wikipedia
- Puedo recordar nuestra conversación anterior
- Las fórmulas matemáticas se mostrarán correctamente
- Siempre citaré mis fuentes al final de cada respuesta
¡Pregúntame lo que necesites! 🚀
**Fuentes:**
- Mensaje de bienvenida del sistema
"""
st.session_state.messages.append({
"role": "assistant",
"content": welcome_message
})
st.session_state.conversation_started = True
# Mostrar historial de chat
display_chat_history()
# Procesar pregunta pendiente del sidebar
if st.session_state.pending_question and not st.session_state.processing:
user_input = st.session_state.pending_question
st.session_state.pending_question = None
st.session_state.processing = True
# Agregar mensaje del usuario al historial
st.session_state.messages.append({
"role": "user",
"content": user_input
})
# Mostrar mensaje del usuario
with st.chat_message("user"):
st.markdown(user_input)
# Procesar y mostrar respuesta del asistente
with st.chat_message("assistant"):
# Crear placeholder vacío para evitar "fantasmas"
message_placeholder = st.empty()
with st.spinner("🤔 Pensando y buscando información..."):
response = process_user_input(user_input)
if response:
# Mostrar la respuesta final
if "\\(" in response or "$$" in response:
message_placeholder.markdown(response, unsafe_allow_html=True)
else:
message_placeholder.markdown(response)
# Agregar respuesta al historial
st.session_state.messages.append({
"role": "assistant",
"content": response
})
st.session_state.processing = False
st.rerun()
# Input del usuario - Deshabilitado durante procesamiento
user_input = st.chat_input("Escribe tu pregunta aquí...", disabled=st.session_state.processing)
# Procesar input del usuario desde el chat
if user_input and not st.session_state.processing:
st.session_state.processing = True
# Agregar mensaje del usuario al historial
st.session_state.messages.append({
"role": "user",
"content": user_input
})
# Mostrar mensaje del usuario
with st.chat_message("user"):
st.markdown(user_input)
# Procesar y mostrar respuesta del asistente
with st.chat_message("assistant"):
# Crear placeholder vacío para evitar "fantasmas"
message_placeholder = st.empty()
with st.spinner("🤔 Pensando y buscando información..."):
response = process_user_input(user_input)
if response:
# Mostrar la respuesta final
if "\\(" in response or "$$" in response:
message_placeholder.markdown(response, unsafe_allow_html=True)
else:
message_placeholder.markdown(response)
# Agregar respuesta al historial
st.session_state.messages.append({
"role": "assistant",
"content": response
})
st.session_state.processing = False
st.rerun()
if __name__ == "__main__":
main()