nurindahpratiwi
commited on
Commit
β’
35dfdca
1
Parent(s):
3fb0a7c
first commit
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 = "TheBloke/Llama-2-7B-Chat-GGML",
|
17 |
+
max_new_tokens = 512,
|
18 |
+
temperature = 0.5
|
19 |
+
)
|
20 |
+
return llm
|
21 |
+
|
22 |
+
st.title("Chat with CSV using Llama2 π¦π¦")
|
23 |
+
|
24 |
+
uploaded_file = st.sidebar.file_uploader("Upload your Data", type="csv")
|
25 |
+
|
26 |
+
if uploaded_file :
|
27 |
+
#use tempfile because CSVLoader only accepts a file_path
|
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", csv_args={
|
33 |
+
'delimiter': ','})
|
34 |
+
data = loader.load()
|
35 |
+
#st.json(data)
|
36 |
+
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2',
|
37 |
+
model_kwargs={'device': 'cpu'})
|
38 |
+
|
39 |
+
db = FAISS.from_documents(data, embeddings)
|
40 |
+
db.save_local(DB_FAISS_PATH)
|
41 |
+
llm = load_llm()
|
42 |
+
chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=db.as_retriever())
|
43 |
+
|
44 |
+
def conversational_chat(query):
|
45 |
+
result = chain({"question": query, "chat_history": st.session_state['history']})
|
46 |
+
st.session_state['history'].append((query, result["answer"]))
|
47 |
+
return result["answer"]
|
48 |
+
|
49 |
+
if 'history' not in st.session_state:
|
50 |
+
st.session_state['history'] = []
|
51 |
+
|
52 |
+
if 'generated' not in st.session_state:
|
53 |
+
st.session_state['generated'] = ["Hello ! Ask me anything about " + uploaded_file.name + " π€"]
|
54 |
+
|
55 |
+
if 'past' not in st.session_state:
|
56 |
+
st.session_state['past'] = ["Hey ! π"]
|
57 |
+
|
58 |
+
#container for the chat history
|
59 |
+
response_container = st.container()
|
60 |
+
#container for the user's text input
|
61 |
+
container = st.container()
|
62 |
+
|
63 |
+
with container:
|
64 |
+
with st.form(key='my_form', clear_on_submit=True):
|
65 |
+
|
66 |
+
user_input = st.text_input("Query:", placeholder="Talk to your csv data here (:", key='input')
|
67 |
+
submit_button = st.form_submit_button(label='Send')
|
68 |
+
|
69 |
+
if submit_button and user_input:
|
70 |
+
output = conversational_chat(user_input)
|
71 |
+
|
72 |
+
st.session_state['past'].append(user_input)
|
73 |
+
st.session_state['generated'].append(output)
|
74 |
+
|
75 |
+
if st.session_state['generated']:
|
76 |
+
with response_container:
|
77 |
+
for i in range(len(st.session_state['generated'])):
|
78 |
+
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="big-smile")
|
79 |
+
message(st.session_state["generated"][i], key=str(i), avatar_style="thumbs")
|