Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import necessary libraries
|
2 |
+
import streamlit as st
|
3 |
+
from streamlit_chat import message
|
4 |
+
import tempfile
|
5 |
+
from langchain.document_loaders.csv_loader import CSVLoader
|
6 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
7 |
+
from langchain.vectorstores import FAISS
|
8 |
+
from langchain.llms import CTransformers
|
9 |
+
from langchain.chains import ConversationalRetrievalChain
|
10 |
+
|
11 |
+
import sys
|
12 |
+
sys.path.append(r"vectorstore/db_faiss")
|
13 |
+
import dataset_utils
|
14 |
+
|
15 |
+
# Define the path for generated embeddings
|
16 |
+
DB_FAISS_PATH = 'vectorstore/db_faiss'
|
17 |
+
|
18 |
+
# Load the model of choice
|
19 |
+
def load_llm():
|
20 |
+
llm = CTransformers(
|
21 |
+
model="llama-2-7b-chat.ggmlv3.q8_0.bin",
|
22 |
+
model_type="llama",
|
23 |
+
max_new_tokens=512,
|
24 |
+
temperature=0.5
|
25 |
+
)
|
26 |
+
return llm
|
27 |
+
|
28 |
+
# Set the title for the Streamlit app
|
29 |
+
st.title("Llama2 Chat CSV - π¦π¦")
|
30 |
+
|
31 |
+
# Create a file uploader in the sidebar
|
32 |
+
uploaded_file = st.sidebar.file_uploader("Upload File", type="csv")
|
33 |
+
|
34 |
+
# Handle file upload
|
35 |
+
if uploaded_file:
|
36 |
+
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
|
37 |
+
tmp_file.write(uploaded_file.getvalue())
|
38 |
+
tmp_file_path = tmp_file.name
|
39 |
+
|
40 |
+
# Load CSV data using CSVLoader
|
41 |
+
loader = CSVLoader(file_path=tmp_file_path, encoding="utf-8", csv_args={'delimiter': ','})
|
42 |
+
data = loader.load()
|
43 |
+
|
44 |
+
# Create embeddings using Sentence Transformers
|
45 |
+
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2', model_kwargs={'device': 'cpu'})
|
46 |
+
|
47 |
+
# Create a FAISS vector store and save embeddings
|
48 |
+
db = FAISS.from_documents(data, embeddings)
|
49 |
+
db.save_local(DB_FAISS_PATH)
|
50 |
+
|
51 |
+
# Load the language model
|
52 |
+
llm = load_llm()
|
53 |
+
|
54 |
+
# Create a conversational chain
|
55 |
+
chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=db.as_retriever())
|
56 |
+
|
57 |
+
# Function for conversational chat
|
58 |
+
def conversational_chat(query):
|
59 |
+
result = chain({"question": query, "chat_history": st.session_state['history']})
|
60 |
+
st.session_state['history'].append((query, result["answer"]))
|
61 |
+
return result["answer"]
|
62 |
+
|
63 |
+
# Initialize chat history
|
64 |
+
if 'history' not in st.session_state:
|
65 |
+
st.session_state['history'] = []
|
66 |
+
|
67 |
+
# Initialize messages
|
68 |
+
if 'generated' not in st.session_state:
|
69 |
+
st.session_state['generated'] = ["Hello ! Ask me(LLAMA2) about " + uploaded_file.name + " π€"]
|
70 |
+
|
71 |
+
if 'past' not in st.session_state:
|
72 |
+
st.session_state['past'] = ["Hey ! π"]
|
73 |
+
|
74 |
+
# Create containers for chat history and user input
|
75 |
+
response_container = st.container()
|
76 |
+
container = st.container()
|
77 |
+
|
78 |
+
# User input form
|
79 |
+
with container:
|
80 |
+
with st.form(key='my_form', clear_on_submit=True):
|
81 |
+
user_input = st.text_input("Query:", placeholder="Talk to csv data π (:", key='input')
|
82 |
+
submit_button = st.form_submit_button(label='Send')
|
83 |
+
|
84 |
+
if submit_button and user_input:
|
85 |
+
output = conversational_chat(user_input)
|
86 |
+
st.session_state['past'].append(user_input)
|
87 |
+
st.session_state['generated'].append(output)
|
88 |
+
|
89 |
+
# Display chat history
|
90 |
+
if st.session_state['generated']:
|
91 |
+
with response_container:
|
92 |
+
for i in range(len(st.session_state['generated'])):
|
93 |
+
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="big-smile")
|
94 |
+
message(st.session_state["generated"][i], key=str(i), avatar_style="thumbs")
|