Spaces:
Build error
Build error
Gabriele Arcangelo Scalici commited on
Commit Β·
ba5532a
1
Parent(s): f8c1bce
feat: refactor folder structure
Browse files- launch.sh +0 -40
- main.py +0 -127
- requirements.txt +0 -0
- src/chatbot_csv.py +45 -202
- {embeddings β src/embeddings}/.gitkeep +0 -0
- {modules β src/modules}/chatbot.py +24 -0
- {modules β src/modules}/embedder.py +2 -2
- {modules β src/modules}/history.py +16 -3
- {modules β src/modules}/layout.py +20 -5
- {modules β src/modules}/sidebar.py +9 -11
- src/modules/utils.py +62 -0
launch.sh
DELETED
|
@@ -1,40 +0,0 @@
|
|
| 1 |
-
#!/bin/bash
|
| 2 |
-
|
| 3 |
-
delimiters="------------------------------------------------------------"
|
| 4 |
-
|
| 5 |
-
echo $delimiters
|
| 6 |
-
echo "Launching π"
|
| 7 |
-
echo $delimiters
|
| 8 |
-
|
| 9 |
-
# Get current directory
|
| 10 |
-
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
| 11 |
-
|
| 12 |
-
# Create virtualenv
|
| 13 |
-
if [ ! -d "$DIR/venv" ]; then
|
| 14 |
-
python3 -m venv $DIR/venv
|
| 15 |
-
echo $delimiters
|
| 16 |
-
echo "Virtualenv created"
|
| 17 |
-
echo $delimiters
|
| 18 |
-
fi
|
| 19 |
-
|
| 20 |
-
# Activate env
|
| 21 |
-
source $DIR/venv/bin/activate
|
| 22 |
-
|
| 23 |
-
# Upgrade pip
|
| 24 |
-
pip install --upgrade pip
|
| 25 |
-
|
| 26 |
-
# Install dependencies
|
| 27 |
-
pip install -r $DIR/requirements.txt
|
| 28 |
-
|
| 29 |
-
# Clear terminal
|
| 30 |
-
clear
|
| 31 |
-
|
| 32 |
-
# Run program
|
| 33 |
-
streamlit run $DIR/main.py
|
| 34 |
-
|
| 35 |
-
# Deactivate virtualenv
|
| 36 |
-
deactivate
|
| 37 |
-
|
| 38 |
-
echo $delimiters
|
| 39 |
-
echo "See you soon π"
|
| 40 |
-
echo $delimiters
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
main.py
DELETED
|
@@ -1,127 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import streamlit as st
|
| 3 |
-
import asyncio
|
| 4 |
-
from dotenv import load_dotenv
|
| 5 |
-
|
| 6 |
-
from streamlit_chat import message
|
| 7 |
-
|
| 8 |
-
from modules.chatbot import Chatbot
|
| 9 |
-
from modules.embedder import Embedder
|
| 10 |
-
from modules.history import ChatHistory
|
| 11 |
-
from modules.layout import Layout
|
| 12 |
-
from modules.sidebar import Sidebar
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
load_dotenv()
|
| 16 |
-
|
| 17 |
-
st.set_page_config(layout="wide", page_icon="π¬", page_title="ChatBot-PDF")
|
| 18 |
-
|
| 19 |
-
# Load the OpenAI API key from the .env file or from the user's input
|
| 20 |
-
def load_api_key():
|
| 21 |
-
if os.path.exists(".env") and os.environ.get("OPENAI_API_KEY") is not None:
|
| 22 |
-
user_api_key = os.environ["OPENAI_API_KEY"]
|
| 23 |
-
st.sidebar.success("API key loaded from .env", icon="π")
|
| 24 |
-
else:
|
| 25 |
-
user_api_key = st.sidebar.text_input(
|
| 26 |
-
label="#### Your OpenAI API key π", placeholder="Paste your openAI API key, sk-", type="password"
|
| 27 |
-
)
|
| 28 |
-
if user_api_key:
|
| 29 |
-
st.sidebar.success("API key loaded", icon="π")
|
| 30 |
-
return user_api_key
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
# Handle the file upload and display the uploaded file
|
| 34 |
-
def handle_upload():
|
| 35 |
-
uploaded_file = st.sidebar.file_uploader("upload", type="pdf", label_visibility="collapsed")
|
| 36 |
-
if uploaded_file is not None:
|
| 37 |
-
file_container = st.expander("Your PDF file :")
|
| 38 |
-
file_container.write(uploaded_file)
|
| 39 |
-
else:
|
| 40 |
-
st.sidebar.info(
|
| 41 |
-
"π Upload your PDF file to get started, "
|
| 42 |
-
"sample for try : [file.pdf](https://github.com/gabacode/chatPDF/blob/main/file.pdf)"
|
| 43 |
-
)
|
| 44 |
-
st.session_state["reset_chat"] = True
|
| 45 |
-
return uploaded_file
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
# Set up the chatbot with the uploaded file, model, and temperature
|
| 49 |
-
async def setup_chatbot(uploaded_file, model, temperature):
|
| 50 |
-
embeds = Embedder()
|
| 51 |
-
with st.spinner("Processing..."):
|
| 52 |
-
uploaded_file.seek(0)
|
| 53 |
-
file = uploaded_file.read()
|
| 54 |
-
vectors = await embeds.getDocEmbeds(file, uploaded_file.name)
|
| 55 |
-
chatbot = Chatbot(model, temperature, vectors)
|
| 56 |
-
st.session_state["ready"] = True
|
| 57 |
-
return chatbot
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
async def main():
|
| 61 |
-
|
| 62 |
-
layout = Layout()
|
| 63 |
-
sidebar = Sidebar()
|
| 64 |
-
|
| 65 |
-
layout.display_header()
|
| 66 |
-
user_api_key = load_api_key()
|
| 67 |
-
|
| 68 |
-
if user_api_key == "":
|
| 69 |
-
layout.show_api_key_error()
|
| 70 |
-
else:
|
| 71 |
-
os.environ["OPENAI_API_KEY"] = user_api_key
|
| 72 |
-
uploaded_file = handle_upload()
|
| 73 |
-
|
| 74 |
-
if uploaded_file is not None:
|
| 75 |
-
history = ChatHistory()
|
| 76 |
-
sidebar.options()
|
| 77 |
-
try:
|
| 78 |
-
chatbot = await setup_chatbot(uploaded_file, st.session_state["model"], st.session_state["temperature"])
|
| 79 |
-
st.session_state["chatbot"] = chatbot
|
| 80 |
-
|
| 81 |
-
if st.session_state["ready"]:
|
| 82 |
-
# Create a containers for displaying the chat history
|
| 83 |
-
response_container = st.container()
|
| 84 |
-
container = st.container()
|
| 85 |
-
|
| 86 |
-
with container:
|
| 87 |
-
with st.form(key="my_form", clear_on_submit=True):
|
| 88 |
-
user_input = st.text_area(
|
| 89 |
-
"Query:",
|
| 90 |
-
placeholder="Ask me anything about the document...",
|
| 91 |
-
key="input",
|
| 92 |
-
label_visibility="collapsed",
|
| 93 |
-
)
|
| 94 |
-
submit_button = st.form_submit_button(label="Send")
|
| 95 |
-
|
| 96 |
-
if st.session_state["reset_chat"]:
|
| 97 |
-
history.reset(uploaded_file)
|
| 98 |
-
|
| 99 |
-
history.initialize(uploaded_file)
|
| 100 |
-
|
| 101 |
-
# If the user has submitted a query
|
| 102 |
-
if submit_button and user_input:
|
| 103 |
-
history.append("user", user_input)
|
| 104 |
-
output = await st.session_state["chatbot"].conversational_chat(user_input)
|
| 105 |
-
history.append("assistant", output)
|
| 106 |
-
|
| 107 |
-
# If there are generated messages to display
|
| 108 |
-
if st.session_state["assistant"]:
|
| 109 |
-
with response_container:
|
| 110 |
-
for i in range(len(st.session_state["assistant"])):
|
| 111 |
-
message(
|
| 112 |
-
st.session_state["user"][i],
|
| 113 |
-
is_user=True,
|
| 114 |
-
key=f"{i}_user",
|
| 115 |
-
avatar_style="big-smile",
|
| 116 |
-
)
|
| 117 |
-
message(st.session_state["assistant"][i], key=str(i), avatar_style="thumbs")
|
| 118 |
-
|
| 119 |
-
except Exception as e:
|
| 120 |
-
st.error(f"Error: {str(e)}")
|
| 121 |
-
|
| 122 |
-
sidebar.about()
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
# Run the main function using asyncio
|
| 126 |
-
if __name__ == "__main__":
|
| 127 |
-
asyncio.run(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
Binary files a/requirements.txt and b/requirements.txt differ
|
|
|
src/chatbot_csv.py
CHANGED
|
@@ -1,221 +1,64 @@
|
|
| 1 |
import os
|
| 2 |
-
import pickle
|
| 3 |
import streamlit as st
|
| 4 |
-
import tempfile
|
| 5 |
-
import pandas as pd
|
| 6 |
import asyncio
|
|
|
|
| 7 |
|
| 8 |
-
from streamlit_chat import message
|
| 9 |
-
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 10 |
-
from langchain.chat_models import ChatOpenAI
|
| 11 |
-
from langchain.chains import ConversationalRetrievalChain
|
| 12 |
-
from langchain.document_loaders.csv_loader import CSVLoader
|
| 13 |
-
from langchain.vectorstores import FAISS
|
| 14 |
-
from langchain.prompts.prompt import PromptTemplate
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
st.set_page_config(layout="wide", page_icon="π¬", page_title="ChatBot-CSV")
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
-
user_api_key = st.sidebar.text_input(
|
| 24 |
-
label="#### Your OpenAI API key π",
|
| 25 |
-
placeholder="Paste your openAI API key, sk-",
|
| 26 |
-
type="password")
|
| 27 |
|
| 28 |
async def main():
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
else:
|
| 37 |
os.environ["OPENAI_API_KEY"] = user_api_key
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
loader = CSVLoader(file_path=tmp_file_path, encoding="utf-8")
|
| 67 |
-
data = loader.load()
|
| 68 |
-
|
| 69 |
-
embeddings = OpenAIEmbeddings()
|
| 70 |
-
|
| 71 |
-
vectors = FAISS.from_documents(data, embeddings)
|
| 72 |
-
os.remove(tmp_file_path)
|
| 73 |
-
|
| 74 |
-
with open(filename + ".pkl", "wb") as f:
|
| 75 |
-
pickle.dump(vectors, f)
|
| 76 |
-
|
| 77 |
-
async def getDocEmbeds(file, filename):
|
| 78 |
-
|
| 79 |
-
if not os.path.isfile(filename + ".pkl"):
|
| 80 |
-
# If not, store the vectors using the storeDocEmbeds function
|
| 81 |
-
await storeDocEmbeds(file, filename)
|
| 82 |
-
|
| 83 |
-
with open(filename + ".pkl", "rb") as f:
|
| 84 |
-
#global vectors
|
| 85 |
-
vectors = pickle.load(f)
|
| 86 |
-
|
| 87 |
-
return vectors
|
| 88 |
-
|
| 89 |
-
async def conversational_chat(query):
|
| 90 |
-
|
| 91 |
-
# Use the Langchain ConversationalRetrievalChain to generate a response to the user's query
|
| 92 |
-
result = chain({"question": query, "chat_history": st.session_state['history']})
|
| 93 |
-
|
| 94 |
-
# Add the user's query and the chatbot's response to the chat history
|
| 95 |
-
st.session_state['history'].append((query, result["answer"]))
|
| 96 |
-
|
| 97 |
-
# You can print the chat history for debugging :
|
| 98 |
-
#print("Log: ")
|
| 99 |
-
#print(st.session_state['history'])
|
| 100 |
-
|
| 101 |
-
return result["answer"]
|
| 102 |
-
|
| 103 |
-
# Set up sidebar with various options
|
| 104 |
-
with st.sidebar.expander("π οΈ Settings", expanded=False):
|
| 105 |
-
|
| 106 |
-
# Add a button to reset the chat history
|
| 107 |
-
if st.button("Reset Chat"):
|
| 108 |
-
st.session_state['reset_chat'] = True
|
| 109 |
-
|
| 110 |
-
# Allow the user to select a chatbot model to use
|
| 111 |
-
MODEL = st.selectbox(label='Model', options=['gpt-3.5-turbo','gpt-4'])
|
| 112 |
-
|
| 113 |
-
if 'history' not in st.session_state:
|
| 114 |
-
st.session_state['history'] = []
|
| 115 |
-
|
| 116 |
-
if 'ready' not in st.session_state:
|
| 117 |
-
st.session_state['ready'] = False
|
| 118 |
-
|
| 119 |
-
if 'reset_chat' not in st.session_state:
|
| 120 |
-
st.session_state['reset_chat'] = False
|
| 121 |
-
|
| 122 |
-
if uploaded_file is not None:
|
| 123 |
-
|
| 124 |
-
# Display a spinner while processing the file
|
| 125 |
-
with st.spinner("Processing..."):
|
| 126 |
-
|
| 127 |
-
uploaded_file.seek(0)
|
| 128 |
-
file = uploaded_file.read()
|
| 129 |
-
|
| 130 |
-
# Generate embeddings vectors for the file
|
| 131 |
-
vectors = await getDocEmbeds(file, uploaded_file.name)
|
| 132 |
-
|
| 133 |
-
_template = """Given the following conversation and a follow-up question, rephrase the follow-up question to be a stand-alone question.
|
| 134 |
-
You can assume that the question is about the information in a CSV file.
|
| 135 |
-
Chat History:
|
| 136 |
-
{chat_history}
|
| 137 |
-
Follow-up entry: {question}
|
| 138 |
-
Standalone question:"""
|
| 139 |
-
CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template)
|
| 140 |
-
|
| 141 |
-
qa_template = """"You are an AI conversational assistant to answer questions based on information from a csv file.
|
| 142 |
-
You are given data from a csv file and a question, you must help the user find the information they need.
|
| 143 |
-
Only give responses for information you know about. Don't try to make up an answer.
|
| 144 |
-
Your answers should be short,friendly, in the same language.
|
| 145 |
-
question: {question}
|
| 146 |
-
=========
|
| 147 |
-
{context}
|
| 148 |
-
=======
|
| 149 |
-
"""
|
| 150 |
-
QA_PROMPT = PromptTemplate(template=qa_template, input_variables=["question", "context"])
|
| 151 |
-
|
| 152 |
-
chain = ConversationalRetrievalChain.from_llm(llm = ChatOpenAI(temperature=0.0,model_name=MODEL),
|
| 153 |
-
condense_question_prompt=CONDENSE_QUESTION_PROMPT,qa_prompt=QA_PROMPT,retriever=vectors.as_retriever())
|
| 154 |
-
|
| 155 |
-
# Set the "ready" flag to True now that the chatbot is ready to chat
|
| 156 |
-
st.session_state['ready'] = True
|
| 157 |
-
|
| 158 |
-
if st.session_state['ready']:
|
| 159 |
-
|
| 160 |
-
# If the chat history has not yet been initialized, initialize it now
|
| 161 |
-
if 'generated' not in st.session_state:
|
| 162 |
-
st.session_state['generated'] = ["Hello ! Ask me anything about " + uploaded_file.name + " π€"]
|
| 163 |
-
|
| 164 |
-
if 'past' not in st.session_state:
|
| 165 |
-
st.session_state['past'] = ["Hey ! π"]
|
| 166 |
-
|
| 167 |
-
#container for displaying the chat history
|
| 168 |
-
response_container = st.container()
|
| 169 |
-
|
| 170 |
-
#container for the user's text input
|
| 171 |
-
container = st.container()
|
| 172 |
-
|
| 173 |
-
with container:
|
| 174 |
-
|
| 175 |
-
# Create a form for the user to enter their query
|
| 176 |
-
with st.form(key='my_form', clear_on_submit=True):
|
| 177 |
-
|
| 178 |
-
user_input = st.text_input("Query:", placeholder="Talk about your csv data here (:", key='input')
|
| 179 |
-
submit_button = st.form_submit_button(label='Send')
|
| 180 |
-
|
| 181 |
-
# If the "reset_chat" flag has been set, reset the chat history and generated messages
|
| 182 |
-
if st.session_state['reset_chat']:
|
| 183 |
-
|
| 184 |
-
st.session_state['history'] = []
|
| 185 |
-
st.session_state['past'] = ["Hey ! π"]
|
| 186 |
-
st.session_state['generated'] = ["Hello ! Ask me anything about " + uploaded_file.name + " π€"]
|
| 187 |
-
response_container.empty()
|
| 188 |
-
st.session_state['reset_chat'] = False
|
| 189 |
-
|
| 190 |
-
if submit_button and user_input:
|
| 191 |
-
|
| 192 |
-
# Generate a response using the Langchain ConversationalRetrievalChain
|
| 193 |
-
output = await conversational_chat(user_input)
|
| 194 |
-
|
| 195 |
-
# Add the user's input and the chatbot's output to the chat history
|
| 196 |
-
st.session_state['past'].append(user_input)
|
| 197 |
-
st.session_state['generated'].append(output)
|
| 198 |
-
|
| 199 |
-
if st.session_state['generated']:
|
| 200 |
-
|
| 201 |
-
# Display the chat history
|
| 202 |
-
with response_container:
|
| 203 |
-
|
| 204 |
-
for i in range(len(st.session_state['generated'])):
|
| 205 |
-
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="big-smile")
|
| 206 |
-
message(st.session_state["generated"][i], key=str(i), avatar_style="thumbs")
|
| 207 |
-
|
| 208 |
|
| 209 |
except Exception as e:
|
| 210 |
st.error(f"Error: {str(e)}")
|
| 211 |
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
about.write("#### He employs large language models to provide users with seamless, context-aware natural language interactions for a better understanding of their CSV data. π")
|
| 215 |
-
about.write("#### Powered by [Langchain](https://github.com/hwchase17/langchain), [OpenAI](https://platform.openai.com/docs/models/gpt-3-5) and [Streamlit](https://github.com/streamlit/streamlit) β‘")
|
| 216 |
-
about.write("#### Source code : [yvann-hub/ChatBot-CSV](https://github.com/yvann-hub/ChatBot-CSV)")
|
| 217 |
|
| 218 |
-
#Run the main function using asyncio
|
| 219 |
if __name__ == "__main__":
|
| 220 |
asyncio.run(main())
|
| 221 |
-
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import streamlit as st
|
|
|
|
|
|
|
| 3 |
import asyncio
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
from modules.history import ChatHistory
|
| 8 |
+
from modules.layout import Layout
|
| 9 |
+
from modules.utils import Utilities
|
| 10 |
+
from modules.sidebar import Sidebar
|
| 11 |
|
|
|
|
| 12 |
|
| 13 |
+
def init():
|
| 14 |
+
load_dotenv()
|
| 15 |
+
st.set_page_config(layout="wide", page_icon="π¬", page_title="ChatBot-CSV")
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
async def main():
|
| 19 |
+
init()
|
| 20 |
+
layout, sidebar, utils = Layout(), Sidebar(), Utilities()
|
| 21 |
+
layout.show_header()
|
| 22 |
+
user_api_key = utils.load_api_key()
|
| 23 |
+
|
| 24 |
+
if not user_api_key:
|
| 25 |
+
layout.show_api_key_missing()
|
| 26 |
else:
|
| 27 |
os.environ["OPENAI_API_KEY"] = user_api_key
|
| 28 |
+
uploaded_file = utils.handle_upload()
|
| 29 |
+
|
| 30 |
+
if uploaded_file:
|
| 31 |
+
history = ChatHistory()
|
| 32 |
+
sidebar.show_options()
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
chatbot = await utils.setup_chatbot(
|
| 36 |
+
uploaded_file, st.session_state["model"], st.session_state["temperature"]
|
| 37 |
+
)
|
| 38 |
+
st.session_state["chatbot"] = chatbot
|
| 39 |
+
|
| 40 |
+
if st.session_state["ready"]:
|
| 41 |
+
response_container, prompt_container = st.container(), st.container()
|
| 42 |
+
|
| 43 |
+
with prompt_container:
|
| 44 |
+
is_ready, user_input = layout.prompt_form()
|
| 45 |
+
|
| 46 |
+
history.initialize(uploaded_file)
|
| 47 |
+
if st.session_state["reset_chat"]:
|
| 48 |
+
history.reset(uploaded_file)
|
| 49 |
+
|
| 50 |
+
if is_ready:
|
| 51 |
+
history.append("user", user_input)
|
| 52 |
+
output = await st.session_state["chatbot"].conversational_chat(user_input)
|
| 53 |
+
history.append("assistant", output)
|
| 54 |
+
|
| 55 |
+
history.generate_messages(response_container)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
st.error(f"Error: {str(e)}")
|
| 59 |
|
| 60 |
+
sidebar.about()
|
| 61 |
+
|
|
|
|
|
|
|
|
|
|
| 62 |
|
|
|
|
| 63 |
if __name__ == "__main__":
|
| 64 |
asyncio.run(main())
|
|
|
{embeddings β src/embeddings}/.gitkeep
RENAMED
|
File without changes
|
{modules β src/modules}/chatbot.py
RENAMED
|
@@ -1,9 +1,31 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from langchain.chat_models import ChatOpenAI
|
| 3 |
from langchain.chains import ConversationalRetrievalChain
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
class Chatbot:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def __init__(self, model_name, temperature, vectors):
|
| 8 |
self.model_name = model_name
|
| 9 |
self.temperature = temperature
|
|
@@ -16,6 +38,8 @@ class Chatbot:
|
|
| 16 |
|
| 17 |
chain = ConversationalRetrievalChain.from_llm(
|
| 18 |
llm=ChatOpenAI(model_name=self.model_name, temperature=self.temperature),
|
|
|
|
|
|
|
| 19 |
retriever=self.vectors.as_retriever(),
|
| 20 |
)
|
| 21 |
result = chain({"question": query, "chat_history": st.session_state["history"]})
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from langchain.chat_models import ChatOpenAI
|
| 3 |
from langchain.chains import ConversationalRetrievalChain
|
| 4 |
+
from langchain.prompts.prompt import PromptTemplate
|
| 5 |
|
| 6 |
|
| 7 |
class Chatbot:
|
| 8 |
+
_template = """Given the following conversation and a follow-up question, rephrase the follow-up question to be a stand-alone question.
|
| 9 |
+
You can assume that the question is about the information in a CSV file.
|
| 10 |
+
Chat History:
|
| 11 |
+
{chat_history}
|
| 12 |
+
Follow-up entry: {question}
|
| 13 |
+
Standalone question:"""
|
| 14 |
+
|
| 15 |
+
CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template)
|
| 16 |
+
|
| 17 |
+
qa_template = """"You are an AI conversational assistant to answer questions based on information from a csv file.
|
| 18 |
+
You are given data from a csv file and a question, you must help the user find the information they need.
|
| 19 |
+
Only give responses for information you know about. Don't try to make up an answer.
|
| 20 |
+
Your answers should be short,friendly, in the same language.
|
| 21 |
+
question: {question}
|
| 22 |
+
=========
|
| 23 |
+
{context}
|
| 24 |
+
=======
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
QA_PROMPT = PromptTemplate(template=qa_template, input_variables=["question", "context"])
|
| 28 |
+
|
| 29 |
def __init__(self, model_name, temperature, vectors):
|
| 30 |
self.model_name = model_name
|
| 31 |
self.temperature = temperature
|
|
|
|
| 38 |
|
| 39 |
chain = ConversationalRetrievalChain.from_llm(
|
| 40 |
llm=ChatOpenAI(model_name=self.model_name, temperature=self.temperature),
|
| 41 |
+
condense_question_prompt=self.CONDENSE_QUESTION_PROMPT,
|
| 42 |
+
qa_prompt=self.QA_PROMPT,
|
| 43 |
retriever=self.vectors.as_retriever(),
|
| 44 |
)
|
| 45 |
result = chain({"question": query, "chat_history": st.session_state["history"]})
|
{modules β src/modules}/embedder.py
RENAMED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import pickle
|
| 3 |
import tempfile
|
| 4 |
-
from langchain.document_loaders import
|
| 5 |
from langchain.vectorstores import FAISS
|
| 6 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 7 |
|
|
@@ -28,7 +28,7 @@ class Embedder:
|
|
| 28 |
tmp_file_path = tmp_file.name
|
| 29 |
|
| 30 |
# Load the data from the file using Langchain
|
| 31 |
-
loader =
|
| 32 |
data = loader.load_and_split()
|
| 33 |
|
| 34 |
# Create an embeddings object using Langchain
|
|
|
|
| 1 |
import os
|
| 2 |
import pickle
|
| 3 |
import tempfile
|
| 4 |
+
from langchain.document_loaders.csv_loader import CSVLoader
|
| 5 |
from langchain.vectorstores import FAISS
|
| 6 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 7 |
|
|
|
|
| 28 |
tmp_file_path = tmp_file.name
|
| 29 |
|
| 30 |
# Load the data from the file using Langchain
|
| 31 |
+
loader = CSVLoader(file_path=tmp_file_path, encoding="utf-8")
|
| 32 |
data = loader.load_and_split()
|
| 33 |
|
| 34 |
# Create an embeddings object using Langchain
|
{modules β src/modules}/history.py
RENAMED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
class ChatHistory:
|
|
@@ -10,14 +11,14 @@ class ChatHistory:
|
|
| 10 |
def default_greeting(self):
|
| 11 |
return "Hey ! π"
|
| 12 |
|
| 13 |
-
def default_prompt(self,
|
| 14 |
-
return f
|
| 15 |
|
| 16 |
def initialize_user_history(self):
|
| 17 |
st.session_state["user"] = [self.default_greeting()]
|
| 18 |
|
| 19 |
def initialize_assistant_history(self, uploaded_file):
|
| 20 |
-
st.session_state["assistant"] = [self.default_prompt(
|
| 21 |
|
| 22 |
def initialize(self, uploaded_file):
|
| 23 |
if "assistant" not in st.session_state:
|
|
@@ -34,6 +35,18 @@ class ChatHistory:
|
|
| 34 |
def append(self, mode, message):
|
| 35 |
st.session_state[mode].append(message)
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
def load(self):
|
| 38 |
if os.path.exists(self.history_file):
|
| 39 |
with open(self.history_file, "r") as f:
|
|
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
+
from streamlit_chat import message
|
| 4 |
|
| 5 |
|
| 6 |
class ChatHistory:
|
|
|
|
| 11 |
def default_greeting(self):
|
| 12 |
return "Hey ! π"
|
| 13 |
|
| 14 |
+
def default_prompt(self, topic):
|
| 15 |
+
return f"Hello ! Ask me anything about {topic} π€"
|
| 16 |
|
| 17 |
def initialize_user_history(self):
|
| 18 |
st.session_state["user"] = [self.default_greeting()]
|
| 19 |
|
| 20 |
def initialize_assistant_history(self, uploaded_file):
|
| 21 |
+
st.session_state["assistant"] = [self.default_prompt(uploaded_file.name)]
|
| 22 |
|
| 23 |
def initialize(self, uploaded_file):
|
| 24 |
if "assistant" not in st.session_state:
|
|
|
|
| 35 |
def append(self, mode, message):
|
| 36 |
st.session_state[mode].append(message)
|
| 37 |
|
| 38 |
+
def generate_messages(self, container):
|
| 39 |
+
if st.session_state["assistant"]:
|
| 40 |
+
with container:
|
| 41 |
+
for i in range(len(st.session_state["assistant"])):
|
| 42 |
+
message(
|
| 43 |
+
st.session_state["user"][i],
|
| 44 |
+
is_user=True,
|
| 45 |
+
key=f"{i}_user",
|
| 46 |
+
avatar_style="big-smile",
|
| 47 |
+
)
|
| 48 |
+
message(st.session_state["assistant"][i], key=str(i), avatar_style="thumbs")
|
| 49 |
+
|
| 50 |
def load(self):
|
| 51 |
if os.path.exists(self.history_file):
|
| 52 |
with open(self.history_file, "r") as f:
|
{modules β src/modules}/layout.py
RENAMED
|
@@ -2,26 +2,41 @@ import streamlit as st
|
|
| 2 |
|
| 3 |
|
| 4 |
class Layout:
|
| 5 |
-
def
|
| 6 |
"""
|
| 7 |
Displays the header of the app
|
| 8 |
"""
|
| 9 |
st.markdown(
|
| 10 |
"""
|
| 11 |
-
<h1 style='text-align: center;'>ChatBot-
|
| 12 |
""",
|
| 13 |
unsafe_allow_html=True,
|
| 14 |
)
|
| 15 |
|
| 16 |
-
def
|
| 17 |
"""
|
| 18 |
-
Displays
|
| 19 |
"""
|
| 20 |
st.markdown(
|
| 21 |
"""
|
| 22 |
<div style='text-align: center;'>
|
| 23 |
-
<h4>Enter your <a href="https://platform.openai.com/account/api-keys" target="_blank">OpenAI API key</a> to start chatting
|
| 24 |
</div>
|
| 25 |
""",
|
| 26 |
unsafe_allow_html=True,
|
| 27 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
|
| 4 |
class Layout:
|
| 5 |
+
def show_header(self):
|
| 6 |
"""
|
| 7 |
Displays the header of the app
|
| 8 |
"""
|
| 9 |
st.markdown(
|
| 10 |
"""
|
| 11 |
+
<h1 style='text-align: center;'>ChatBot-CSV, Talk with your csv-data ! π¬</h1>
|
| 12 |
""",
|
| 13 |
unsafe_allow_html=True,
|
| 14 |
)
|
| 15 |
|
| 16 |
+
def show_api_key_missing(self):
|
| 17 |
"""
|
| 18 |
+
Displays a message if the user has not entered an API key
|
| 19 |
"""
|
| 20 |
st.markdown(
|
| 21 |
"""
|
| 22 |
<div style='text-align: center;'>
|
| 23 |
+
<h4>Enter your <a href="https://platform.openai.com/account/api-keys" target="_blank">OpenAI API key</a> to start chatting π</h4>
|
| 24 |
</div>
|
| 25 |
""",
|
| 26 |
unsafe_allow_html=True,
|
| 27 |
)
|
| 28 |
+
|
| 29 |
+
def prompt_form(self):
|
| 30 |
+
"""
|
| 31 |
+
Displays the prompt form
|
| 32 |
+
"""
|
| 33 |
+
with st.form(key="my_form", clear_on_submit=True):
|
| 34 |
+
user_input = st.text_area(
|
| 35 |
+
"Query:",
|
| 36 |
+
placeholder="Ask me anything about the document...",
|
| 37 |
+
key="input",
|
| 38 |
+
label_visibility="collapsed",
|
| 39 |
+
)
|
| 40 |
+
submit_button = st.form_submit_button(label="Send")
|
| 41 |
+
is_ready = submit_button and user_input
|
| 42 |
+
return is_ready, user_input
|
{modules β src/modules}/sidebar.py
RENAMED
|
@@ -5,25 +5,23 @@ class Sidebar:
|
|
| 5 |
MODEL_OPTIONS = ["gpt-3.5-turbo"]
|
| 6 |
TEMPERATURE_MIN_VALUE = 0.0
|
| 7 |
TEMPERATURE_MAX_VALUE = 1.0
|
| 8 |
-
TEMPERATURE_DEFAULT_VALUE = 0.
|
| 9 |
TEMPERATURE_STEP = 0.01
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
def about(self):
|
| 15 |
about = st.sidebar.expander("About π€")
|
| 16 |
sections = [
|
| 17 |
-
"#### ChatBot-
|
| 18 |
-
"####
|
| 19 |
-
"#### It employs large language models to provide users with seamless, context-aware natural language interactions for a better understanding of their data. π",
|
| 20 |
"#### Powered by [Langchain](https://github.com/hwchase17/langchain), [OpenAI](https://platform.openai.com/docs/models/gpt-3-5) and [Streamlit](https://github.com/streamlit/streamlit) β‘",
|
| 21 |
-
"#### Source code : [
|
| 22 |
]
|
| 23 |
for section in sections:
|
| 24 |
about.write(section)
|
| 25 |
|
| 26 |
-
|
|
|
|
| 27 |
if st.button("Reset chat"):
|
| 28 |
st.session_state["reset_chat"] = True
|
| 29 |
st.session_state.setdefault("reset_chat", False)
|
|
@@ -42,7 +40,7 @@ class Sidebar:
|
|
| 42 |
)
|
| 43 |
st.session_state["temperature"] = temperature
|
| 44 |
|
| 45 |
-
def
|
| 46 |
with st.sidebar.expander("π οΈ Settings", expanded=False):
|
| 47 |
self.reset_chat_button()
|
| 48 |
self.model_selector()
|
|
|
|
| 5 |
MODEL_OPTIONS = ["gpt-3.5-turbo"]
|
| 6 |
TEMPERATURE_MIN_VALUE = 0.0
|
| 7 |
TEMPERATURE_MAX_VALUE = 1.0
|
| 8 |
+
TEMPERATURE_DEFAULT_VALUE = 0.0
|
| 9 |
TEMPERATURE_STEP = 0.01
|
| 10 |
|
| 11 |
+
@staticmethod
|
| 12 |
+
def about():
|
|
|
|
|
|
|
| 13 |
about = st.sidebar.expander("About π€")
|
| 14 |
sections = [
|
| 15 |
+
"#### ChatBot-CSV is an AI chatbot featuring conversational memory, designed to enable users to discuss their CSV data in a more intuitive manner. π",
|
| 16 |
+
"#### He employs large language models to provide users with seamless, context-aware natural language interactions for a better understanding of their CSV data. π",
|
|
|
|
| 17 |
"#### Powered by [Langchain](https://github.com/hwchase17/langchain), [OpenAI](https://platform.openai.com/docs/models/gpt-3-5) and [Streamlit](https://github.com/streamlit/streamlit) β‘",
|
| 18 |
+
"#### Source code : [yvann-hub/ChatBot-CSV](https://github.com/yvann-hub/ChatBot-CSV)",
|
| 19 |
]
|
| 20 |
for section in sections:
|
| 21 |
about.write(section)
|
| 22 |
|
| 23 |
+
@staticmethod
|
| 24 |
+
def reset_chat_button():
|
| 25 |
if st.button("Reset chat"):
|
| 26 |
st.session_state["reset_chat"] = True
|
| 27 |
st.session_state.setdefault("reset_chat", False)
|
|
|
|
| 40 |
)
|
| 41 |
st.session_state["temperature"] = temperature
|
| 42 |
|
| 43 |
+
def show_options(self):
|
| 44 |
with st.sidebar.expander("π οΈ Settings", expanded=False):
|
| 45 |
self.reset_chat_button()
|
| 46 |
self.model_selector()
|
src/modules/utils.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import streamlit as st
|
| 4 |
+
|
| 5 |
+
from modules.chatbot import Chatbot
|
| 6 |
+
from modules.embedder import Embedder
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class Utilities:
|
| 10 |
+
@staticmethod
|
| 11 |
+
def load_api_key():
|
| 12 |
+
"""
|
| 13 |
+
Loads the OpenAI API key from the .env file or from the user's input
|
| 14 |
+
and returns it
|
| 15 |
+
"""
|
| 16 |
+
if os.path.exists(".env") and os.environ.get("OPENAI_API_KEY") is not None:
|
| 17 |
+
user_api_key = os.environ["OPENAI_API_KEY"]
|
| 18 |
+
st.sidebar.success("API key loaded from .env", icon="π")
|
| 19 |
+
else:
|
| 20 |
+
user_api_key = st.sidebar.text_input(
|
| 21 |
+
label="#### Your OpenAI API key π", placeholder="Paste your openAI API key, sk-", type="password"
|
| 22 |
+
)
|
| 23 |
+
if user_api_key:
|
| 24 |
+
st.sidebar.success("API key loaded", icon="π")
|
| 25 |
+
return user_api_key
|
| 26 |
+
|
| 27 |
+
@staticmethod
|
| 28 |
+
def handle_upload():
|
| 29 |
+
"""
|
| 30 |
+
Handles the file upload and displays the uploaded file
|
| 31 |
+
"""
|
| 32 |
+
uploaded_file = st.sidebar.file_uploader("upload", type="csv", label_visibility="collapsed")
|
| 33 |
+
if uploaded_file is not None:
|
| 34 |
+
|
| 35 |
+
def show_user_file(uploaded_file):
|
| 36 |
+
file_container = st.expander("Your CSV file :")
|
| 37 |
+
shows = pd.read_csv(uploaded_file)
|
| 38 |
+
uploaded_file.seek(0)
|
| 39 |
+
file_container.write(shows)
|
| 40 |
+
|
| 41 |
+
show_user_file(uploaded_file)
|
| 42 |
+
else:
|
| 43 |
+
st.sidebar.info(
|
| 44 |
+
"π Upload your CSV file to get started, "
|
| 45 |
+
"sample for try : [fishfry-locations.csv](https://drive.google.com/file/d/18i7tN2CqrmoouaSqm3hDfAk17hmWx94e/view?usp=sharing)"
|
| 46 |
+
)
|
| 47 |
+
st.session_state["reset_chat"] = True
|
| 48 |
+
return uploaded_file
|
| 49 |
+
|
| 50 |
+
@staticmethod
|
| 51 |
+
async def setup_chatbot(uploaded_file, model, temperature):
|
| 52 |
+
"""
|
| 53 |
+
Sets up the chatbot with the uploaded file, model, and temperature
|
| 54 |
+
"""
|
| 55 |
+
embeds = Embedder()
|
| 56 |
+
with st.spinner("Processing..."):
|
| 57 |
+
uploaded_file.seek(0)
|
| 58 |
+
file = uploaded_file.read()
|
| 59 |
+
vectors = await embeds.getDocEmbeds(file, uploaded_file.name)
|
| 60 |
+
chatbot = Chatbot(model, temperature, vectors)
|
| 61 |
+
st.session_state["ready"] = True
|
| 62 |
+
return chatbot
|