Yvann commited on
Commit
de38ee5
Β·
unverified Β·
2 Parent(s): ff11a0cba5532a

Merge pull request #10 from gabacode/feat/refactor-csv

Browse files
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
- st.markdown(
20
- "<h1 style='text-align: center;'>ChatBot-CSV, Talk with your csv-data ! πŸ’¬</h1>",
21
- unsafe_allow_html=True)
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
- if user_api_key == "":
31
-
32
- st.markdown(
33
- "<div style='text-align: center;'><h4>Enter your OpenAI API key to start chatting πŸ˜‰</h4></div>",
34
- unsafe_allow_html=True)
35
-
36
  else:
37
  os.environ["OPENAI_API_KEY"] = user_api_key
38
-
39
- uploaded_file = st.sidebar.file_uploader("upload", type="csv", label_visibility="hidden")
40
-
41
- if uploaded_file is not None:
42
- def show_user_file(uploaded_file):
43
- file_container = st.expander("Your CSV file :")
44
- shows = pd.read_csv(uploaded_file)
45
- uploaded_file.seek(0)
46
- file_container.write(shows)
47
-
48
- show_user_file(uploaded_file)
49
-
50
- else :
51
- st.sidebar.info(
52
- "πŸ‘† Upload your CSV file to get started, "
53
- "sample for try : [fishfry-locations.csv](https://drive.google.com/file/d/18i7tN2CqrmoouaSqm3hDfAk17hmWx94e/view?usp=sharing)"
54
- )
55
-
56
- if uploaded_file :
57
- try :
58
- async def storeDocEmbeds(file, filename):
59
-
60
- # Write the uploaded file to a temporary file
61
- with tempfile.NamedTemporaryFile(mode="wb", delete=False) as tmp_file:
62
- tmp_file.write(file)
63
- tmp_file_path = tmp_file.name
64
-
65
- # Load the data from the CSV file using Langchain
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
- about = st.sidebar.expander("About πŸ€–")
213
- about.write("#### ChatBot-CSV is an AI chatbot featuring conversational memory, designed to enable users to discuss their CSV data in a more intuitive manner. πŸ“„")
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())
 
src/embeddings/.gitkeep ADDED
File without changes
src/modules/chatbot.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
32
+ self.vectors = vectors
33
+
34
+ async def conversational_chat(self, query):
35
+ """
36
+ Starts a conversational chat with a model via Langchain
37
+ """
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"]})
46
+
47
+ st.session_state["history"].append((query, result["answer"]))
48
+
49
+ return result["answer"]
src/modules/embedder.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
8
+
9
+ class Embedder:
10
+ def __init__(self):
11
+ self.PATH = "embeddings"
12
+ self.createEmbeddingsDir()
13
+
14
+ def createEmbeddingsDir(self):
15
+ """
16
+ Creates a directory to store the embeddings vectors
17
+ """
18
+ if not os.path.exists(self.PATH):
19
+ os.mkdir(self.PATH)
20
+
21
+ async def storeDocEmbeds(self, file, filename):
22
+ """
23
+ Stores document embeddings using Langchain and FAISS
24
+ """
25
+ # Write the uploaded file to a temporary file
26
+ with tempfile.NamedTemporaryFile(mode="wb", delete=False) as tmp_file:
27
+ tmp_file.write(file)
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
35
+ embeddings = OpenAIEmbeddings()
36
+
37
+ # Store the embeddings vectors using FAISS
38
+ vectors = FAISS.from_documents(data, embeddings)
39
+ os.remove(tmp_file_path)
40
+
41
+ # Save the vectors to a pickle file
42
+ with open(f"{self.PATH}/{filename}.pkl", "wb") as f:
43
+ pickle.dump(vectors, f)
44
+
45
+ async def getDocEmbeds(self, file, filename):
46
+ """
47
+ Retrieves document embeddings
48
+ """
49
+ # Check if embeddings vectors have already been stored in a pickle file
50
+ if not os.path.isfile(f"{self.PATH}/{filename}.pkl"):
51
+ # If not, store the vectors using the storeDocEmbeds function
52
+ await self.storeDocEmbeds(file, filename)
53
+
54
+ # Load the vectors from the pickle file
55
+ with open(f"{self.PATH}/{filename}.pkl", "rb") as f:
56
+ vectors = pickle.load(f)
57
+
58
+ return vectors
src/modules/history.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from streamlit_chat import message
4
+
5
+
6
+ class ChatHistory:
7
+ def __init__(self):
8
+ self.history = st.session_state.get("history", [])
9
+ st.session_state["history"] = self.history
10
+
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:
25
+ self.initialize_assistant_history(uploaded_file)
26
+ if "user" not in st.session_state:
27
+ self.initialize_user_history()
28
+
29
+ def reset(self, uploaded_file):
30
+ st.session_state["history"] = []
31
+ self.initialize_user_history()
32
+ self.initialize_assistant_history(uploaded_file)
33
+ st.session_state["reset_chat"] = False
34
+
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:
53
+ self.history = f.read().splitlines()
54
+
55
+ def save(self):
56
+ with open(self.history_file, "w") as f:
57
+ f.write("\n".join(self.history))
src/modules/layout.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
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
src/modules/sidebar.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+
4
+ 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.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)
28
+
29
+ def model_selector(self):
30
+ model = st.selectbox(label="Model", options=self.MODEL_OPTIONS)
31
+ st.session_state["model"] = model
32
+
33
+ def temperature_slider(self):
34
+ temperature = st.slider(
35
+ label="Temperature",
36
+ min_value=self.TEMPERATURE_MIN_VALUE,
37
+ max_value=self.TEMPERATURE_MAX_VALUE,
38
+ value=self.TEMPERATURE_DEFAULT_VALUE,
39
+ step=self.TEMPERATURE_STEP,
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()
47
+ self.temperature_slider()
48
+ st.session_state.setdefault("model", self.MODEL_OPTIONS[0])
49
+ st.session_state.setdefault("temperature", self.TEMPERATURE_DEFAULT_VALUE)
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