-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
285 lines (210 loc) · 6.84 KB
/
Copy pathapp.py
File metadata and controls
285 lines (210 loc) · 6.84 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
import os
import sys
import asyncio
import streamlit as st
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from mcp_use import MCPAgent, MCPClient
from st_social_media_links import SocialMediaIcons
# ==================================================
# CONFIGURACIÓN WINDOWS
# ==================================================
if sys.platform.startswith("win"):
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
# ==================================================
# VARIABLES DE ENTORNO
# ==================================================
load_dotenv()
key = os.getenv("OPENAI_API_KEY")
# ==================================================
# CONFIGURACIÓN STREAMLIT
# ==================================================
st.set_page_config(
page_title="MCP Data Analytics",
page_icon="📊",
layout="wide",
initial_sidebar_state="expanded"
)
# ==================================================
# CONFIGURACIÓN MCP SERVER
# ==================================================
ruta_servidor = r"C:\Users\EdwinQuintero\Documents\Anaconda 3\mcp-data-analytics-server\server.py"
config = {
"mcpServers": {
"mi-servidor-local": {
"command": "python",
"args": [ruta_servidor],
"cwd": os.path.dirname(ruta_servidor)
}
}
}
# ==================================================
# INICIALIZAR CLIENTE MCP Y LLM
# ==================================================
client = MCPClient.from_dict(config)
llm = ChatOpenAI(
model="gpt-4o-mini",
temperature=0
)
agent = MCPAgent(
llm=llm,
client=client,
max_steps=50
)
# ==================================================
# SIDEBAR
# ==================================================
with st.sidebar:
st.title("⚙️ Sistema")
st.subheader("Estado General")
if key:
st.success("✅ OpenAI Conectado")
else:
st.error("❌ OpenAI API Key no encontrada")
if os.path.exists(ruta_servidor):
st.success("✅ MCP Server Detectado")
else:
st.error("❌ MCP Server No Encontrado")
st.info("🤖 Modelo: GPT-4o-mini")
st.divider()
st.subheader("📁 Gestión de Archivos")
uploaded_file = st.file_uploader(
"Subir archivo",
type=["csv", "xlsx", "pdf", "txt"]
)
if uploaded_file:
temp_path = f"temp_{uploaded_file.name}"
with open(temp_path, "wb") as f:
f.write(uploaded_file.getbuffer())
st.success("Archivo cargado")
st.caption(temp_path)
st.divider()
st.subheader("🛠 Herramientas Disponibles")
st.markdown("""
**📁 Archivos**
- analizar_archivo
- crear_archivo
- leer_documento
**📊 Datos**
- analizar_datos
- tabla_dinamica_avanzada
- convertir_formato_datos
**📈 Visualización**
- crear_visualizacion
**🌐 Web**
- buscar_repositorios_github
- extraer_contenido_web
- descargar_archivo_web
""")
st.divider()
st.subheader("📊 Estadísticas")
col1, col2 = st.columns(2)
with col1:
st.metric("Tools", "10")
with col2:
st.metric("LLM", "GPT-4o")
# ==================================================
# CONTENIDO PRINCIPAL
# ==================================================
st.title("📊 MCP Data Analytics Platform")
st.caption(
"Plataforma Inteligente de Análisis de Datos impulsada por MCP, FastMCP y OpenAI"
)
imagen_url = (
"https://anhanguera.s3.amazonaws.com/wp-content/uploads/"
"2024/09/capa-homem-com-mao-estendida-04-09-2024-66d8f14be6cc4.webp"
)
st.image(imagen_url, width="stretch")
# ==================================================
# EJEMPLOS
# ==================================================
with st.expander("💡 Ejemplos de consultas para probar"):
st.markdown("""
### 📊 Análisis de Datos
- Analiza el archivo temp_datos.csv y muéstrame estadísticas
- Resume el archivo CSV cargado
### 📈 Visualización
- Crea un gráfico de barras del archivo cargado
- Genera un histograma de las ventas
### 🌐 GitHub
- Busca repositorios de Python para análisis de datos
- Busca repositorios de IA generativa
### 📄 Documentos
- Lee el PDF cargado
- Resume el contenido del documento
""")
# ==================================================
# CONSULTA PRINCIPAL
# ==================================================
st.subheader("💬 Consulta Inteligente")
consulta = st.text_area(
"Ingresa tu consulta",
height=120,
placeholder="Ejemplo: Busca repositorios de Python para análisis de datos"
)
col1, col2 = st.columns([1, 1])
with col1:
ejecutar = st.button(
"🚀 Ejecutar Consulta",
use_container_width=True
)
with col2:
limpiar = st.button(
"🗑️ Limpiar Temporales",
use_container_width=True
)
# ==================================================
# EJECUCIÓN DEL AGENTE
# ==================================================
if ejecutar and consulta:
with st.spinner("Procesando consulta..."):
try:
resultado = asyncio.run(agent.run(consulta))
st.success("✅ Consulta completada")
st.subheader("Resultado")
if isinstance(resultado, dict):
st.json(resultado)
else:
st.write(resultado)
except Exception as e:
st.error(f"❌ Error: {str(e)}")
with st.expander("Ver detalles técnicos"):
st.code(str(e))
# ==================================================
# LIMPIAR ARCHIVOS TEMPORALES
# ==================================================
if limpiar:
archivos_temp = [
f for f in os.listdir(".")
if f.startswith("temp_")
]
if not archivos_temp:
st.info("No existen archivos temporales")
for archivo in archivos_temp:
try:
os.remove(archivo)
st.success(f"Eliminado: {archivo}")
except Exception:
st.warning(f"No se pudo eliminar: {archivo}")
# ==================================================
# FOOTER
# ==================================================
st.markdown("---")
st.markdown(
"""
<div style="text-align:center">
<strong>Desarrollador:</strong> Edwin Quintero Alzate<br>
<strong>Email:</strong> egqa1975@gmail.com
</div>
""",
unsafe_allow_html=True
)
social_media_links = [
"https://www.facebook.com/edwin.quinteroalzate",
"https://www.linkedin.com/in/edwinquintero0329/",
"https://github.com/Edwin1719"
]
social_media_icons = SocialMediaIcons(
social_media_links
)
social_media_icons.render()