Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 = 256,
|
19 |
+
temperature = 0.2
|
20 |
+
)
|
21 |
+
return llm
|
22 |
+
|
23 |
+
st.title("🦙Llama2🦜CSV🦙")
|
24 |
+
st.markdown("<h3 style='color: black;'>Harness the power of LLama2 with Langchain.</h3>", unsafe_allow_html=True)
|
25 |
+
st.markdown("<h4 style='color: black;'>Developed by <a href='https://github.com/rohan-shaw'>Rohan Shaw</a> with ❤️</h4>", unsafe_allow_html=True)
|
26 |
+
uploaded_file = st.sidebar.file_uploader("CSV file here", type="csv")
|
27 |
+
|
28 |
+
if uploaded_file :
|
29 |
+
with tempfile.NamedTemporaryFile(delete=False) as t:
|
30 |
+
t.write(uploaded_file.getvalue())
|
31 |
+
temp_path = t.name
|
32 |
+
|
33 |
+
loader = CSVLoader(file_path=temp_path, encoding="utf-8", csv_args={
|
34 |
+
'delimiter': ','})
|
35 |
+
data = loader.load()
|
36 |
+
#st.json(data)
|
37 |
+
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2',
|
38 |
+
model_kwargs={'device': 'cpu'})
|
39 |
+
|
40 |
+
db = FAISS.from_documents(data, embeddings)
|
41 |
+
db.save_local(DB_FAISS_PATH)
|
42 |
+
llm = load_llm()
|
43 |
+
chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=db.as_retriever())
|
44 |
+
|
45 |
+
def conversational_chat(query):
|
46 |
+
result = chain({"question": query, "chat_history": st.session_state['history']})
|
47 |
+
st.session_state['history'].append((query, result["answer"]))
|
48 |
+
return result["answer"]
|
49 |
+
|
50 |
+
if 'history' not in st.session_state:
|
51 |
+
st.session_state['history'] = []
|
52 |
+
|
53 |
+
if 'generated' not in st.session_state:
|
54 |
+
st.session_state['generated'] = ["Bhai, " + uploaded_file.name + " is file ke bare mein kuch bhi puch le aankh 👀 band karke answer dunga 🤔"]
|
55 |
+
|
56 |
+
if 'past' not in st.session_state:
|
57 |
+
st.session_state['past'] = ["Aur, bol kya hal chal ! 🖖"]
|
58 |
+
|
59 |
+
#container for the chat history
|
60 |
+
response_container = st.container()
|
61 |
+
#container for the user's text input
|
62 |
+
container = st.container()
|
63 |
+
|
64 |
+
with container:
|
65 |
+
with st.form(key='my_form', clear_on_submit=True):
|
66 |
+
|
67 |
+
user_input = st.text_input("Query:", placeholder="Apne CSV file ke data ke bare me yaha pe puch (:", key='input')
|
68 |
+
submit_button = st.form_submit_button(label='Send')
|
69 |
+
|
70 |
+
if submit_button and user_input:
|
71 |
+
output = conversational_chat(user_input)
|
72 |
+
|
73 |
+
st.session_state['past'].append(user_input)
|
74 |
+
st.session_state['generated'].append(output)
|
75 |
+
|
76 |
+
if st.session_state['generated']:
|
77 |
+
with response_container:
|
78 |
+
for i in range(len(st.session_state['generated'])):
|
79 |
+
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="human")
|
80 |
+
message(st.session_state["generated"][i], key=str(i), avatar_style="pixel-art-neutral")
|