drkareemkamal commited on
Commit
821c158
·
verified ·
1 Parent(s): b0dcb44

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -0
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from streamlit_chat import message
3
+ import tempfile
4
+ #from langchain_community.documentloader.csv_loader import CSVLoader
5
+ from langchain_community.document_loaders.csv_loader import CSVLoader
6
+ from langchain_community.embeddings import HuggingFaceEmbeddings
7
+ #from langchain_community.embeddings import HuggingFaceBgeEmbeddings
8
+
9
+ from langchain_community.vectorstores import FAISS
10
+ #from langchain_community.llms import CTransformers
11
+ from langchain_community.llms.ctransformers import CTransformers
12
+
13
+ from langchain.chains.conversational_retrieval.base import ConversationalRetrievalChain
14
+
15
+ #from langchain.chains.conversational_retrieval.base import ConversationalRetreievalChain
16
+
17
+
18
+ DB_FAISS_PATH = 'vectorstore/db_faiss'
19
+
20
+ def load_llm():
21
+ # load model from hugging face repo
22
+ llm = CTransformers(
23
+ model = 'TheBloke/Llama-2-7B-Chat-GGML',
24
+ model_type = 'llma',
25
+ max_new_token = 512,
26
+ temperature = 0.5
27
+ )
28
+ return llm
29
+
30
+ st.title("Chat with CSV using Llma 2")
31
+ st.markdown("<h1 style='text-align: center; color: blue;'>Chat with your PDF 📄 </h1>", unsafe_allow_html=True)
32
+ st.markdown("<h3 style='text-align: center; color: grey;'>Built by <a href='https://github.com/DrKareemKAmal'>MindSparks ❤️ </a></h3>", unsafe_allow_html=True)
33
+
34
+ uploaded_file = st.sidebar.file_uploader('Upload your data', type=['csv'])
35
+
36
+ if uploaded_file:
37
+ with tempfile.NamedTemporaryFile(delete=False)as temp_file :
38
+ tempfile.write(uploaded_file.getvalue())
39
+ tempfile_path = tempfile.name
40
+
41
+ loader = CSVLoader(file_path = tempfile_path, encoding = 'utf-8',
42
+ csv_args = {'delimiter': ','} )
43
+ data = loader.load()
44
+ st.json(data)
45
+
46
+ embeddings = HuggingFaceEmbeddings(
47
+ model = 'all-MiniLM-L6-v2',
48
+ model_kwargs = {'device': 'cpu'}
49
+ )
50
+
51
+
52
+ db = FAISS.from_documents(data, embeddings)
53
+ db.save_local (DB_FAISS_PATH)
54
+ llm = load_llm()
55
+
56
+ chain = ConversationalRetrievalChain.from_llm(llm= llm , retriever = db.as_retriever())
57
+
58
+ def conversational_chat(query):
59
+ result = chain({"quetion": query ,
60
+ "chat_history": st.session_state['history']})
61
+ st.session_state['history'].append((query , result['answer']))
62
+ return result['answer']
63
+
64
+ if 'history' not in st.session_state :
65
+ st.session_state['history'] = []
66
+
67
+ if 'generated' not in st.session_state :
68
+ st.session_state['generated'] = ['Hello, Ask me anything about ' + uploaded_file.name]
69
+
70
+ if 'past' not in st.session_state :
71
+ st.session_state['past'] = ['Hey !']
72
+
73
+ # Container for the chat history
74
+ response_container = st.container()
75
+ container = st.container()
76
+
77
+ with container :
78
+ with st.form(key = 'mu_form',
79
+ clear_on_submit=True):
80
+ user_input = st.text_input('Query:', placeholder= "Talk to youur CSV Data here ")
81
+ submit_button = st.from_submit_button(label = 'chat')
82
+
83
+ if submit_button and user_input :
84
+ output = conversational_chat(user_input)
85
+
86
+ st.session_state['past'].append(user_input)
87
+ st.session_state['generated'].append(output)
88
+
89
+ if st.session_state['generated'] :
90
+ with response_container:
91
+ for i in range(len(st.session_state['generated'])):
92
+ message(st.session_state['past'][i], is_user = True , key=str(i) + '_user',
93
+ avatar_style='big-smile')
94
+ message(st.session_state['generated'][i], key = str(i), avatar_style='thumb')
95
+
96
+
97
+
98
+
99
+
100
+
101
+
102
+