Spaces:
Build error
Build error
nurindahpratiwi
commited on
Commit
·
d576cda
1
Parent(s):
650ecfb
first commit
Browse files- app.py +75 -0
- requirements.txt +11 -0
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 langchian.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 |
+
llm = CTransformers(
|
| 15 |
+
model= "TheBloke/Llama-2-7B-Chat-GGML",
|
| 16 |
+
max_new_tokens=512,
|
| 17 |
+
temperature=0.5
|
| 18 |
+
)
|
| 19 |
+
return llm
|
| 20 |
+
|
| 21 |
+
st.image("https://huggingface.co/spaces/wiwaaw/summary/resolve/main/banner.png")
|
| 22 |
+
st.title("Chat with CSV using Llama2")
|
| 23 |
+
|
| 24 |
+
uploaded_file = st.sidebar.file_uploader("Upload your data", type="csv")
|
| 25 |
+
if uploaded_file is not None:
|
| 26 |
+
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
|
| 27 |
+
tmp_file.write(uploaded_file.getvalue())
|
| 28 |
+
tmp_file_path = tmp_file.name
|
| 29 |
+
|
| 30 |
+
loader = CSVLoader(file_path=tmp_file_path, encoding="utf-8", csv_args={
|
| 31 |
+
'delimiter': ',', # default value
|
| 32 |
+
})
|
| 33 |
+
data = loader.load()
|
| 34 |
+
embeddings=HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2',
|
| 35 |
+
model_kwargs={'device': 'cpu'})
|
| 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'] = ''
|
| 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 |
+
user_input = st.text_input('Query:', placeholder="Talk to your csv data here:", key='input')
|
| 63 |
+
submit_button = st.form_submit_button(label='Send')
|
| 64 |
+
|
| 65 |
+
if submit_button and user_input:
|
| 66 |
+
output = conversational_chat(user_input)
|
| 67 |
+
|
| 68 |
+
st.session_state['past'].append(user_input)
|
| 69 |
+
st.session_state['generated'].append(output)
|
| 70 |
+
|
| 71 |
+
if st.session_state['generated']:
|
| 72 |
+
with response_container:
|
| 73 |
+
for i in range(len(st.session_state['generated'])):
|
| 74 |
+
message(st.session_state['past'][i], is_user=True, key=str(i) +'_user', avatar_style="big-smile")
|
| 75 |
+
message(st.session_state['generated'][i], key=str(i), avatar_style="thumbs")
|
requirements.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
streamlit-chat
|
| 3 |
+
pypdf
|
| 4 |
+
langchain
|
| 5 |
+
torch
|
| 6 |
+
accelerate
|
| 7 |
+
bitsandbytes
|
| 8 |
+
transformers
|
| 9 |
+
sentence_transformers
|
| 10 |
+
faiss_cpu
|
| 11 |
+
ctransformers
|