Delete app.py
Browse files
app.py
DELETED
@@ -1,81 +0,0 @@
|
|
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 |
-
|
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")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|