Spaces:
Runtime error
Runtime error
Commit
·
cb8cfbf
1
Parent(s):
0295b05
Upload 2 files
Browse files- app.py +92 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from langchain.agents import create_pandas_dataframe_agent
|
| 4 |
+
from langchain.llms import OpenAI
|
| 5 |
+
# Function to get dataset from user
|
| 6 |
+
def get_user_dataset():
|
| 7 |
+
st.sidebar.header("Upload Dataset")
|
| 8 |
+
uploaded_file = st.sidebar.file_uploader("Choose a CSV or Excel file", type=["csv", "xlsx"])
|
| 9 |
+
if uploaded_file is not None:
|
| 10 |
+
try:
|
| 11 |
+
if uploaded_file.name.endswith('csv'):
|
| 12 |
+
df = pd.read_csv(uploaded_file)
|
| 13 |
+
else:
|
| 14 |
+
df = pd.read_excel(uploaded_file)
|
| 15 |
+
return df
|
| 16 |
+
except Exception as e:
|
| 17 |
+
st.sidebar.error(f"Error: {e}")
|
| 18 |
+
return None
|
| 19 |
+
|
| 20 |
+
# Function to initialize or retrieve agent
|
| 21 |
+
def get_agent(df, openai_api_key):
|
| 22 |
+
if "agent" not in st.session_state:
|
| 23 |
+
agent = create_pandas_dataframe_agent(OpenAI(temperature=0, openai_api_key=openai_api_key), df, verbose=True)
|
| 24 |
+
st.session_state.agent = agent
|
| 25 |
+
return st.session_state.agent
|
| 26 |
+
|
| 27 |
+
# Main Streamlit app
|
| 28 |
+
def main():
|
| 29 |
+
st.set_page_config(
|
| 30 |
+
page_title="DataTalker: Have a Conversation with Your Dataset",
|
| 31 |
+
page_icon=":speech_balloon:",
|
| 32 |
+
)
|
| 33 |
+
st.title("Welcome to DataTalker")
|
| 34 |
+
# Provide user instructions
|
| 35 |
+
st.sidebar.header("Instructions")
|
| 36 |
+
#st.sidebar.markdown(
|
| 37 |
+
#"1. Use the sidebar to upload a CSV or Excel file containing your dataset.\n"
|
| 38 |
+
#"2. Once the dataset is uploaded, you can start chatting with it!\n"
|
| 39 |
+
#"3. Type your message in the chat input box and press Enter to send it.\n"
|
| 40 |
+
#"4. The assistant will respond based on the message you provide.\n"
|
| 41 |
+
#S)
|
| 42 |
+
st.sidebar.write("\n\n")
|
| 43 |
+
st.sidebar.markdown("**Get a free API key from OpenAI:**")
|
| 44 |
+
st.sidebar.markdown("* Create a [free account](https://platform.openai.com/signup?launch) or [login](https://platform.openai.com/login)")
|
| 45 |
+
st.sidebar.markdown("* Go to **Personal** and then **API keys**")
|
| 46 |
+
st.sidebar.markdown("* Create a new API")
|
| 47 |
+
st.sidebar.markdown("* Paste your API key in the text box")
|
| 48 |
+
st.sidebar.divider()
|
| 49 |
+
# Get OpenAI API Key from user
|
| 50 |
+
openai_api_key = st.sidebar.text_input("Enter your OpenAI API Key")
|
| 51 |
+
|
| 52 |
+
# Get or upload dataset
|
| 53 |
+
df = get_user_dataset()
|
| 54 |
+
if df is None:
|
| 55 |
+
return
|
| 56 |
+
|
| 57 |
+
# Initialize or retrieve agent
|
| 58 |
+
agent = get_agent(df, openai_api_key)
|
| 59 |
+
|
| 60 |
+
# Initialize chat history
|
| 61 |
+
if "messages" not in st.session_state:
|
| 62 |
+
st.session_state.messages = []
|
| 63 |
+
|
| 64 |
+
# Display chat messages from history on app rerun
|
| 65 |
+
for message in st.session_state.messages:
|
| 66 |
+
with st.chat_message(message["role"]):
|
| 67 |
+
st.markdown(message["content"])
|
| 68 |
+
|
| 69 |
+
# Accept user input
|
| 70 |
+
if prompt := st.chat_input("Ask a question about your dataset"):
|
| 71 |
+
# Add user message to chat history
|
| 72 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 73 |
+
# Display user message in chat message container
|
| 74 |
+
with st.chat_message("user"):
|
| 75 |
+
st.markdown(prompt)
|
| 76 |
+
|
| 77 |
+
# Display assistant response in chat message container
|
| 78 |
+
with st.chat_message("assistant"):
|
| 79 |
+
message_placeholder = st.empty()
|
| 80 |
+
full_response = ""
|
| 81 |
+
assistant_response = agent.run(prompt)
|
| 82 |
+
# Simulate stream of response with milliseconds delay
|
| 83 |
+
for chunk in assistant_response.split('n/'):
|
| 84 |
+
full_response += chunk + " "
|
| 85 |
+
# Add a blinking cursor to simulate typing
|
| 86 |
+
message_placeholder.markdown(full_response + "▌")
|
| 87 |
+
message_placeholder.markdown(full_response)
|
| 88 |
+
# Add assistant response to chat history
|
| 89 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 90 |
+
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
pandas
|
| 3 |
+
langchain
|