mouliraj56 commited on
Commit
349a420
Β·
1 Parent(s): e00f925

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("<h3 style='text-align: center; color: white;'>Built by <a href='https://github.com/AIAnytime'>AI Anytime with ❀️ </a></h3>", 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
+