jeysshon commited on
Commit
b09aade
verified
1 Parent(s): 88b8de7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -43
app.py CHANGED
@@ -1,55 +1,31 @@
1
  import streamlit as st
2
  import pandas as pd
3
- import requests
4
- from dotenv import load_dotenv
5
- import os
6
- import numpy as np
7
 
8
- # Cargar variables de entorno
9
- load_dotenv()
 
10
 
11
- # Usa tu clave API de Gemini aqu铆
12
- gemini_api_key = os.getenv('GEMINI_API_KEY')
13
 
14
- st.title("ChatBot para Archivos Excel")
15
- st.subheader("Tecnolog铆as utilizadas: Streamlit - por https://github.com/jaglinux", divider='rainbow')
16
-
17
- # Cargar archivo subido
18
- uploaded_file = st.file_uploader("Elige un archivo", type=['csv', 'xlsx'])
19
  if uploaded_file is None:
20
  df = pd.read_csv("titanic.csv")
21
- st.write("Archivo predeterminado cargado: titanic.csv")
22
  else:
 
23
  if uploaded_file.name.endswith(".csv"):
24
  df = pd.read_csv(uploaded_file)
25
  elif uploaded_file.name.endswith(".xlsx"):
26
  df = pd.read_excel(uploaded_file)
27
- st.dataframe(df, height=200)
28
-
29
- # Limpiar el DataFrame: reemplazar NaN y valores infinitos
30
- df.replace([np.inf, -np.inf], np.nan, inplace=True)
31
-
32
- # Convertir el DataFrame a un diccionario compatible con JSON
33
- df_dict = df.fillna('').to_dict(orient='records')
34
-
35
- # Funci贸n para enviar consultas a la API de Gemini (ejemplo gen茅rico)
36
- def query_gemini_api(question, api_key):
37
- headers = {
38
- 'Authorization': f'Bearer {api_key}',
39
- 'Content-Type': 'application/json'
40
- }
41
- data = {
42
- 'question': question,
43
- 'context': df_dict # Enviar el DataFrame como un diccionario
44
- }
45
- response = requests.post('https://api.gemini.example.com/ask', headers=headers, json=data)
46
- return response.json()
47
-
48
- # Capturar pregunta del usuario
49
- if question := st.chat_input("Haz una pregunta sobre el archivo CSV/XLSX proporcionado"):
50
- if not gemini_api_key:
51
- st.error("La clave API de Gemini no est谩 configurada. Por favor, verifica tu archivo .env.")
52
- else:
53
- response = query_gemini_api(question, gemini_api_key)
54
- st.chat_message("user").markdown(question)
55
- st.chat_message("assistant").markdown(response['answer']) # Ajusta seg煤n la respuesta de la API de Gemini
 
1
  import streamlit as st
2
  import pandas as pd
 
 
 
 
3
 
4
+ from langchain.agents.agent_types import AgentType
5
+ from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent
6
+ from langchain_openai import ChatOpenAI
7
 
 
 
8
 
9
+ uploaded_file = st.file_uploader("Choose a file", type=['csv','xlsx'])
 
 
 
 
10
  if uploaded_file is None:
11
  df = pd.read_csv("titanic.csv")
12
+ st.write("Default file uploaded, titanic.csv")
13
  else:
14
+ # Can be used wherever a "file-like" object is accepted:
15
  if uploaded_file.name.endswith(".csv"):
16
  df = pd.read_csv(uploaded_file)
17
  elif uploaded_file.name.endswith(".xlsx"):
18
  df = pd.read_excel(uploaded_file)
19
+ st.dataframe(df, height=5)
20
+
21
+ agent = create_pandas_dataframe_agent(
22
+ ChatOpenAI(temperature=0),
23
+ df,
24
+ verbose=True,
25
+ agent_type=AgentType.OPENAI_FUNCTIONS,
26
+ )
27
+ if question := st.chat_input("Ask Question to the csv/xlsx provided"):
28
+ response = agent.invoke(question)
29
+ print(response['output'])
30
+ st.chat_message("user").markdown(question)
31
+ st.chat_message("assistant").markdown(response['output'])