Sakil commited on
Commit
cfa7d92
·
1 Parent(s): 155c883

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(model='TheBloke/Llama-2-7B-Chat-GPTQ',max_new_tokens=512,temperature=0.1,gpu_layers=50)
16
+ return llm
17
+
18
+ st.title("Chat with CSV using Llama2 🦙🦜")
19
+ st.markdown("<h3 style='text-align: center; color: white;'>Built by <a href=https://github.com/Sakil786'>Llama-2-7B-Chat ❤️ </a></h3>", unsafe_allow_html=True)
20
+
21
+ uploaded_file = st.sidebar.file_uploader("Upload your Data", type="csv")
22
+
23
+ if uploaded_file :
24
+ #use tempfile because CSVLoader only accepts a file_path
25
+ with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
26
+ tmp_file.write(uploaded_file.getvalue())
27
+ tmp_file_path = tmp_file.name
28
+
29
+ loader = CSVLoader(file_path=tmp_file_path, encoding="utf-8", csv_args={
30
+ 'delimiter': ','})
31
+ data = loader.load()
32
+ #st.json(data)
33
+ embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2',
34
+ model_kwargs={'device': 'cpu'})
35
+
36
+ db = FAISS.from_documents(data, embeddings)
37
+ db.save_local(DB_FAISS_PATH)
38
+ llm = load_llm()
39
+ chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=db.as_retriever())
40
+
41
+ def conversational_chat(query):
42
+ result = chain({"question": query, "chat_history": st.session_state['history']})
43
+ st.session_state['history'].append((query, result["answer"]))
44
+ return result["answer"]
45
+
46
+ if 'history' not in st.session_state:
47
+ st.session_state['history'] = []
48
+
49
+ if 'generated' not in st.session_state:
50
+ st.session_state['generated'] = ["Hello ! Ask me anything about " + uploaded_file.name + " 🤗"]
51
+
52
+ if 'past' not in st.session_state:
53
+ st.session_state['past'] = ["Hey ! 👋"]
54
+
55
+ #container for the chat history
56
+ response_container = st.container()
57
+ #container for the user's text input
58
+ container = st.container()
59
+
60
+ with container:
61
+ with st.form(key='my_form', clear_on_submit=True):
62
+
63
+ user_input = st.text_input("Query:", placeholder="Talk to your csv data here (:", key='input')
64
+ submit_button = st.form_submit_button(label='Send')
65
+
66
+ if submit_button and user_input:
67
+ output = conversational_chat(user_input)
68
+
69
+ st.session_state['past'].append(user_input)
70
+ st.session_state['generated'].append(output)
71
+
72
+ if st.session_state['generated']:
73
+ with response_container:
74
+ for i in range(len(st.session_state['generated'])):
75
+ message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="big-smile")
76
+ message(st.session_state["generated"][i], key=str(i), avatar_style="thumbs")