Upload 3 files
Browse files
app.py
CHANGED
|
@@ -9,32 +9,43 @@ def subir_csv(file):
|
|
| 9 |
df_global = pd.read_csv(file.name)
|
| 10 |
return f"Archivo cargado con {len(df_global)} filas y {len(df_global.columns)} columnas."
|
| 11 |
|
| 12 |
-
def
|
| 13 |
global df_global
|
| 14 |
if df_global is None:
|
| 15 |
return "Primero debes subir un archivo CSV."
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
numeric_cols = df_global.select_dtypes(include='number')
|
| 19 |
if numeric_cols.empty:
|
| 20 |
return "No hay columnas numéricas para calcular el promedio."
|
| 21 |
return numeric_cols.mean().to_string()
|
| 22 |
-
elif "columnas" in pregunta.lower():
|
| 23 |
-
return f"Las columnas del archivo son: {', '.join(df_global.columns)}"
|
| 24 |
-
else:
|
| 25 |
-
return "Pregunta no reconocida. Prueba con: ¿Cuál es el promedio de las columnas?"
|
| 26 |
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
archivo = gr.File(label="Sube tu archivo CSV")
|
| 32 |
-
salida = gr.Textbox(label="Resultado de carga")
|
| 33 |
-
pregunta = gr.Textbox(label="Haz una pregunta sobre el CSV")
|
| 34 |
-
respuesta = gr.Textbox(label="Respuesta del análisis")
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|
| 40 |
-
|
|
|
|
| 9 |
df_global = pd.read_csv(file.name)
|
| 10 |
return f"Archivo cargado con {len(df_global)} filas y {len(df_global.columns)} columnas."
|
| 11 |
|
| 12 |
+
def responder_pregunta(pregunta):
|
| 13 |
global df_global
|
| 14 |
if df_global is None:
|
| 15 |
return "Primero debes subir un archivo CSV."
|
| 16 |
|
| 17 |
+
pregunta = pregunta.lower()
|
| 18 |
+
|
| 19 |
+
if "promedio" in pregunta:
|
| 20 |
numeric_cols = df_global.select_dtypes(include='number')
|
| 21 |
if numeric_cols.empty:
|
| 22 |
return "No hay columnas numéricas para calcular el promedio."
|
| 23 |
return numeric_cols.mean().to_string()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
elif "columnas" in pregunta:
|
| 26 |
+
return f"Las columnas del archivo son: {', '.join(df_global.columns)}"
|
| 27 |
|
| 28 |
+
elif "filas" in pregunta:
|
| 29 |
+
return f"El archivo contiene {len(df_global)} filas."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
else:
|
| 32 |
+
return "No entendí la pregunta. Prueba con: ¿Cuál es el promedio?, ¿Cuáles son las columnas?, ¿Cuántas filas hay?"
|
| 33 |
+
|
| 34 |
+
def app_interface(csv_file, pregunta):
|
| 35 |
+
mensaje = subir_csv(csv_file)
|
| 36 |
+
respuesta = responder_pregunta(pregunta)
|
| 37 |
+
return mensaje + "\n\n" + respuesta
|
| 38 |
+
|
| 39 |
+
iface = gr.Interface(
|
| 40 |
+
fn=app_interface,
|
| 41 |
+
inputs=[
|
| 42 |
+
gr.File(label="Sube tu archivo CSV"),
|
| 43 |
+
gr.Textbox(label="Pregunta sobre el archivo")
|
| 44 |
+
],
|
| 45 |
+
outputs=gr.Textbox(label="Respuesta"),
|
| 46 |
+
title="Analizador de CSV",
|
| 47 |
+
description="Sube un archivo CSV y haz preguntas como '¿Cuál es el promedio?' o '¿Cuáles son las columnas?'"
|
| 48 |
+
)
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
| 51 |
+
iface.launch()
|