├── README.md ├── requirements.txt ├── LICENSE └── app.py /README.md: -------------------------------------------------------------------------------- 1 | # ChatCSV-Llama2-Chatbot 2 | ChatCSV bot using Llama 2, Sentence Transformers, CTransformers, Langchain, and Streamlit. 3 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | streamlit 2 | pypdf 3 | langchain 4 | torch 5 | accelerate 6 | bitsandbytes 7 | transformers 8 | sentence_transformers 9 | faiss_cpu -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 AI Anytime 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | import streamlit as st 2 | from streamlit_chat import message 3 | import tempfile 4 | from langchain.document_loaders.csv_loader import CSVLoader 5 | from langchain.embeddings import HuggingFaceEmbeddings 6 | from langchain.vectorstores import FAISS 7 | from langchain.llms import CTransformers 8 | from langchain.chains import ConversationalRetrievalChain 9 | 10 | DB_FAISS_PATH = 'vectorstore/db_faiss' 11 | 12 | #Loading the model 13 | def load_llm(): 14 | # Load the locally downloaded model here 15 | llm = CTransformers( 16 | model = "llama-2-7b-chat.ggmlv3.q8_0.bin", 17 | model_type="llama", 18 | max_new_tokens = 512, 19 | temperature = 0.5 20 | ) 21 | return llm 22 | 23 | st.title("Chat with CSV using Llama2 🦙🦜") 24 | st.markdown("

Built by AI Anytime with ❤️

", unsafe_allow_html=True) 25 | 26 | uploaded_file = st.sidebar.file_uploader("Upload your Data", type="csv") 27 | 28 | if uploaded_file : 29 | #use tempfile because CSVLoader only accepts a file_path 30 | with tempfile.NamedTemporaryFile(delete=False) as tmp_file: 31 | tmp_file.write(uploaded_file.getvalue()) 32 | tmp_file_path = tmp_file.name 33 | 34 | loader = CSVLoader(file_path=tmp_file_path, encoding="utf-8", csv_args={ 35 | 'delimiter': ','}) 36 | data = loader.load() 37 | #st.json(data) 38 | embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2', 39 | model_kwargs={'device': 'cpu'}) 40 | 41 | db = FAISS.from_documents(data, embeddings) 42 | db.save_local(DB_FAISS_PATH) 43 | llm = load_llm() 44 | chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=db.as_retriever()) 45 | 46 | def conversational_chat(query): 47 | result = chain({"question": query, "chat_history": st.session_state['history']}) 48 | st.session_state['history'].append((query, result["answer"])) 49 | return result["answer"] 50 | 51 | if 'history' not in st.session_state: 52 | st.session_state['history'] = [] 53 | 54 | if 'generated' not in st.session_state: 55 | st.session_state['generated'] = ["Hello ! Ask me anything about " + uploaded_file.name + " 🤗"] 56 | 57 | if 'past' not in st.session_state: 58 | st.session_state['past'] = ["Hey ! 👋"] 59 | 60 | #container for the chat history 61 | response_container = st.container() 62 | #container for the user's text input 63 | container = st.container() 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 to 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 | 84 | 85 | 86 | 87 | 88 | --------------------------------------------------------------------------------