Spaces:
Build error
Build error
File size: 1,784 Bytes
29eadb1 |
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 |
import os
os.system("pip install pandasai==1.5.21 pandas==2.2.2 gradio==4.21.0")
import gradio as gr
import pandas as pd
from pandasai import SmartDataframe
from pandasai.llm import OpenSourceLLM
df_global = None
def cargar_csv(file):
global df_global
try:
df_global = pd.read_csv(file.name)
return f"Archivo cargado con éxito. Filas: {len(df_global)}, Columnas: {len(df_global.columns)}"
except Exception as e:
return f"Error al cargar el archivo: {str(e)}"
def responder_pregunta(pregunta, token):
global df_global
if df_global is None:
return "Primero debes cargar un archivo CSV."
try:
llm = OpenSourceLLM(
model="mistralai/Mistral-7B-Instruct-v0.1",
api_token=token
)
sdf = SmartDataframe(df_global, config={"llm": llm})
respuesta = sdf.chat(pregunta)
return respuesta
except Exception as e:
return f"Ocurrió un error: {str(e)}"
with gr.Blocks() as demo:
gr.Markdown("# Analizador CSV con Preguntas en Lenguaje Natural")
with gr.Row():
archivo = gr.File(label="Sube tu archivo CSV", file_types=[".csv"])
salida_carga = gr.Textbox(label="Resultado de carga")
boton_carga = gr.Button("Cargar CSV")
with gr.Row():
pregunta = gr.Textbox(label="Escribe tu pregunta sobre los datos")
token = gr.Textbox(label="Token de Hugging Face (requerido)", type="password")
salida_respuesta = gr.Textbox(label="Respuesta")
boton_pregunta = gr.Button("Responder")
boton_carga.click(cargar_csv, inputs=[archivo], outputs=[salida_carga])
boton_pregunta.click(responder_pregunta, inputs=[pregunta, token], outputs=[salida_respuesta])
if __name__ == "__main__":
demo.launch()
|