gautamraj8044 commited on
Commit
c60ffec
Β·
verified Β·
1 Parent(s): 53f0757

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from streamlit_chat import message
3
+ import tempfile
4
+ from langchain_huggingface import HuggingFaceEmbeddings
5
+ from langchain_community.vectorstores import FAISS
6
+ from langchain.chains import ConversationalRetrievalChain
7
+ from langchain_community.document_loaders import CSVLoader
8
+ from langchain_community.llms.ctransformers import CTransformers
9
+ from huggingface_hub import hf_hub_download
10
+
11
+ DB_FAISS_PATH = 'vectorstore/db_faiss'
12
+
13
+ #Loading the model
14
+ def load_llm():
15
+ model_path = hf_hub_download(repo_id="TheBloke/Llama-2-7b-chat-GGML", filename="llama-2-7b-chat.ggmlv3.q8_0.bin")
16
+ llm = CTransformers(
17
+ model = model_path,
18
+ model_type="llama",
19
+ max_new_tokens = 512,
20
+ temperature = 0.5
21
+ )
22
+ return llm
23
+
24
+ st.title("Chat with CSV using Llama2 πŸ¦™πŸ¦œ")
25
+ st.markdown("<h3 style='text-align: center; color: white;'>Built by <a href='https://github.com/AIAnytime'>AI Anytime with ❀️ </a></h3>", unsafe_allow_html=True)
26
+
27
+ uploaded_file = st.sidebar.file_uploader("Upload your Data", type="csv")
28
+
29
+ if uploaded_file :
30
+ #use tempfile because CSVLoader only accepts a file_path
31
+ with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
32
+ tmp_file.write(uploaded_file.getvalue())
33
+ tmp_file_path = tmp_file.name
34
+
35
+ loader = CSVLoader(file_path=tmp_file_path, encoding="utf-8", csv_args={
36
+ 'delimiter': ','})
37
+ data = loader.load()
38
+ #st.json(data)
39
+ embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2',
40
+ model_kwargs={'device': 'cpu'})
41
+
42
+ db = FAISS.from_documents(data, embeddings)
43
+ db.save_local(DB_FAISS_PATH)
44
+ llm = load_llm()
45
+ chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=db.as_retriever())
46
+
47
+ def conversational_chat(query):
48
+ result = chain({"question": query, "chat_history": st.session_state['history']})
49
+ st.session_state['history'].append((query, result["answer"]))
50
+ return result["answer"]
51
+
52
+ if 'history' not in st.session_state:
53
+ st.session_state['history'] = []
54
+
55
+ if 'generated' not in st.session_state:
56
+ st.session_state['generated'] = ["Hello ! Ask me anything about " + uploaded_file.name + " πŸ€—"]
57
+
58
+ if 'past' not in st.session_state:
59
+ st.session_state['past'] = ["Hey ! πŸ‘‹"]
60
+
61
+ #container for the chat history
62
+ response_container = st.container()
63
+ #container for the user's text input
64
+ container = st.container()
65
+
66
+ with container:
67
+ with st.form(key='my_form', clear_on_submit=True):
68
+
69
+ user_input = st.text_input("Query:", placeholder="Talk to your csv data here (:", key='input')
70
+ submit_button = st.form_submit_button(label='Send')
71
+
72
+ if submit_button and user_input:
73
+ output = conversational_chat(user_input)
74
+
75
+ st.session_state['past'].append(user_input)
76
+ st.session_state['generated'].append(output)
77
+
78
+ if st.session_state['generated']:
79
+ with response_container:
80
+ for i in range(len(st.session_state['generated'])):
81
+ message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="big-smile")
82
+ message(st.session_state["generated"][i], key=str(i), avatar_style="thumbs")