shreyasiv commited on
Commit
ed88d16
·
1 Parent(s): b2e7963
Files changed (1) hide show
  1. tuto_chatbot_csv.py +83 -0
tuto_chatbot_csv.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pip install streamlit langchain openai faiss-cpu tiktoken
2
+
3
+ import streamlit as st
4
+ from streamlit_chat import message
5
+ from langchain.embeddings.openai import OpenAIEmbeddings
6
+ from langchain.chat_models import ChatOpenAI
7
+ from langchain.chains import ConversationalRetrievalChain
8
+ from langchain.document_loaders.csv_loader import CSVLoader
9
+ from langchain.vectorstores import FAISS
10
+ import tempfile
11
+ from htmlTemplates import css, bot_template, user_template
12
+ from dotenv import load_dotenv
13
+ import os
14
+
15
+ load_dotenv('.env')
16
+
17
+ # Access the API key
18
+ api_key = os.getenv('API_KEY')
19
+ #user_api_key = "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"
20
+ #st.sidebar.text_input(
21
+ #label="#### Your OpenAI API key 👇",
22
+ #placeholder="Paste your openAI API key, sk-",
23
+ #type="password")
24
+ #logo=
25
+ uploaded_file = st.sidebar.file_uploader(" UPLOAD YOUR FILE", type="csv")
26
+
27
+ if uploaded_file :
28
+ with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
29
+ tmp_file.write(uploaded_file.getvalue())
30
+ tmp_file_path = tmp_file.name
31
+
32
+ loader = CSVLoader(file_path=tmp_file_path, encoding="utf-8")
33
+ data = loader.load()
34
+
35
+ embeddings = OpenAIEmbeddings(openai_api_key=api_key)
36
+ vectors = FAISS.from_documents(data, embeddings)
37
+
38
+ chain = ConversationalRetrievalChain.from_llm(llm=ChatOpenAI(temperature=0.0, model_name='gpt-3.5-turbo', openai_api_key=api_key),
39
+ retriever=vectors.as_retriever())
40
+
41
+ def conversational_chat(query):
42
+
43
+ result = chain({"question": query, "chat_history": st.session_state['history']})
44
+ st.session_state['history'].append((query, result["answer"]))
45
+
46
+ return result["answer"]
47
+
48
+ if 'history' not in st.session_state:
49
+ st.session_state['history'] = []
50
+
51
+ if 'generated' not in st.session_state:
52
+ st.session_state['generated'] = ["Hello! Ask me anything about " + uploaded_file.name]
53
+
54
+ if 'past' not in st.session_state:
55
+ st.session_state['past'] = ["Hey!"]
56
+
57
+ #container for the chat history
58
+ response_container = st.container()
59
+ #container for the user's text input
60
+ container = st.container()
61
+
62
+ # Apply CSS styles
63
+ st.write(css, unsafe_allow_html=True)
64
+
65
+ with container:
66
+ with st.form(key='my_form', clear_on_submit=True):
67
+
68
+ user_input = st.text_input("Query:", placeholder="Talk about your csv data here", key='input')
69
+ submit_button = st.form_submit_button(label='Send')
70
+
71
+ if submit_button and user_input:
72
+ output = conversational_chat(user_input)
73
+
74
+ st.session_state['past'].append(user_input)
75
+ st.session_state['generated'].append(output)
76
+
77
+ if st.session_state['generated']:
78
+ with response_container:
79
+ for i in range(len(st.session_state['generated'])):
80
+ message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="big-smile")
81
+ message(st.session_state["generated"][i], key=str(i), avatar_style="thumbs")
82
+
83
+ #streamlit run tuto_chatbot_csv.py