-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
59 lines (47 loc) · 1.91 KB
/
app.py
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
import os
import streamlit as st
from langchain.agents import create_csv_agent
from langchain.llms import OpenAI
from streamlit_chat import message
st.sidebar.title("OpenAI API Key")
api_key = st.sidebar.text_input("Enter your API key:", type="password")
uploaded_file = st.sidebar.file_uploader("Upload a CSV file", type=["csv"])
st.sidebar.title("Follow Us")
st.sidebar.markdown(
"""
* [Web](https://ngmi.ai/)
* [Mastodon](https://mastodon.online/@ngmi)
* [GitHub](https://github.com/ngmisl)
"""
)
st.sidebar.title("Our Product")
st.sidebar.markdown(
"[The Ultimate 5 ChatGPT Prompts: Simplify Your AI Experience](https://ngmi.gumroad.com/l/nobsprompts)"
)
if api_key:
os.environ["OPENAI_API_KEY"] = api_key
st.title("CSV Agent Interaction")
if uploaded_file:
st.write("CSV file uploaded successfully!")
agent = create_csv_agent(
OpenAI(temperature=0, client=any), uploaded_file, verbose=True
)
# Initialize the chat history in the session_state if it doesn't exist
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
user_input = st.text_input("Enter your question:", key="input_field")
if user_input:
answer = agent.run(user_input)
# Add the question and answer to the chat_history
st.session_state.chat_history.append(("user", user_input))
st.session_state.chat_history.append(("agent", answer))
# Display the chat_history in a chat-like format using streamlit-chat
for i, (sender, message_text) in enumerate(st.session_state.chat_history):
if sender == "user":
message(message_text, is_user=True, key=f"{i}_user")
else:
message(message_text, key=f"{i}")
else:
st.write("Please upload a CSV file.")
else:
st.sidebar.error("Please enter your OpenAI API key.")