Spaces:
Sleeping
Sleeping
srinidhidevaraj
commited on
Commit
•
99fde54
1
Parent(s):
0760b60
Update app.py
Browse files
app.py
CHANGED
@@ -1,158 +1,147 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import os
|
3 |
-
from langchain_groq import ChatGroq
|
4 |
-
from langchain_community.document_loaders import WebBaseLoader
|
5 |
-
from langchain_community.embeddings import OllamaEmbeddings
|
6 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
7 |
-
from langchain.chains.combine_documents import create_stuff_documents_chain
|
8 |
-
from langchain_core.prompts import ChatPromptTemplate
|
9 |
-
from langchain.chains import create_retrieval_chain
|
10 |
-
from langchain_community.vectorstores import FAISS
|
11 |
-
from langchain_community.document_loaders import PyPDFLoader
|
12 |
-
from langchain_community.document_loaders import PyPDFDirectoryLoader
|
13 |
-
from langchain_community.embeddings import HuggingFaceBgeEmbeddings
|
14 |
-
# from langchain.vectorstores.cassandra import Cassandra
|
15 |
-
from langchain_community.vectorstores import Cassandra
|
16 |
-
from langchain_community.llms import Ollama
|
17 |
-
from cassandra.auth import PlainTextAuthProvider
|
18 |
-
import tempfile
|
19 |
-
import cassio
|
20 |
-
from PyPDF2 import PdfReader
|
21 |
-
from cassandra.cluster import Cluster
|
22 |
-
import warnings
|
23 |
-
warnings.filterwarnings("ignore")
|
24 |
-
|
25 |
-
from dotenv import load_dotenv
|
26 |
-
import time
|
27 |
-
load_dotenv()
|
28 |
-
|
29 |
-
ASTRA_DB_SECURE_BUNDLE_PATH ='
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
)
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
from langchain_groq import ChatGroq
|
4 |
+
from langchain_community.document_loaders import WebBaseLoader
|
5 |
+
from langchain_community.embeddings import OllamaEmbeddings
|
6 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
7 |
+
from langchain.chains.combine_documents import create_stuff_documents_chain
|
8 |
+
from langchain_core.prompts import ChatPromptTemplate
|
9 |
+
from langchain.chains import create_retrieval_chain
|
10 |
+
from langchain_community.vectorstores import FAISS
|
11 |
+
from langchain_community.document_loaders import PyPDFLoader
|
12 |
+
from langchain_community.document_loaders import PyPDFDirectoryLoader
|
13 |
+
from langchain_community.embeddings import HuggingFaceBgeEmbeddings
|
14 |
+
# from langchain.vectorstores.cassandra import Cassandra
|
15 |
+
from langchain_community.vectorstores import Cassandra
|
16 |
+
from langchain_community.llms import Ollama
|
17 |
+
from cassandra.auth import PlainTextAuthProvider
|
18 |
+
import tempfile
|
19 |
+
import cassio
|
20 |
+
from PyPDF2 import PdfReader
|
21 |
+
from cassandra.cluster import Cluster
|
22 |
+
import warnings
|
23 |
+
warnings.filterwarnings("ignore")
|
24 |
+
|
25 |
+
from dotenv import load_dotenv
|
26 |
+
import time
|
27 |
+
load_dotenv()
|
28 |
+
|
29 |
+
ASTRA_DB_SECURE_BUNDLE_PATH ='secure-connect-pdf-query-db.zip'
|
30 |
+
|
31 |
+
cassio.init(token=ASTRA_DB_APPLICATION_TOKEN,database_id=ASTRA_DB_ID,secure_connect_bundle=ASTRA_DB_SECURE_BUNDLE_PATH)
|
32 |
+
|
33 |
+
cloud_config = {
|
34 |
+
'secure_connect_bundle': ASTRA_DB_SECURE_BUNDLE_PATH
|
35 |
+
}
|
36 |
+
|
37 |
+
def doc_loader(pdf_reader):
|
38 |
+
|
39 |
+
encode_kwargs = {'normalize_embeddings': True}
|
40 |
+
huggigface_embeddings=HuggingFaceBgeEmbeddings(
|
41 |
+
model_name='BAAI/bge-small-en-v1.5',
|
42 |
+
# model_name='sentence-transformers/all-MiniLM-16-v2',
|
43 |
+
model_kwargs={'device':'cpu'},
|
44 |
+
encode_kwargs=encode_kwargs)
|
45 |
+
|
46 |
+
|
47 |
+
loader=PyPDFLoader(pdf_reader)
|
48 |
+
documents=loader.load_and_split()
|
49 |
+
|
50 |
+
|
51 |
+
text_splitter=RecursiveCharacterTextSplitter(chunk_size=1000,chunk_overlap=200)
|
52 |
+
final_documents=text_splitter.split_documents(documents)
|
53 |
+
|
54 |
+
astrasession = Cluster(
|
55 |
+
cloud={"secure_connect_bundle": ASTRA_DB_SECURE_BUNDLE_PATH},
|
56 |
+
auth_provider=PlainTextAuthProvider("token", ASTRA_DB_APPLICATION_TOKEN),
|
57 |
+
).connect()
|
58 |
+
|
59 |
+
|
60 |
+
# Truncate the existing table
|
61 |
+
astrasession.execute(f'TRUNCATE {ASTRA_DB_KEYSPACE}.{ASTRA_DB_TABLE}')
|
62 |
+
|
63 |
+
astra_vector_store=Cassandra(
|
64 |
+
embedding=huggigface_embeddings,
|
65 |
+
table_name="qa_mini_demo",
|
66 |
+
session=astrasession,
|
67 |
+
keyspace=ASTRA_DB_KEYSPACE
|
68 |
+
)
|
69 |
+
|
70 |
+
|
71 |
+
astra_vector_store.add_documents(final_documents)
|
72 |
+
|
73 |
+
return astra_vector_store
|
74 |
+
|
75 |
+
def prompt_temp():
|
76 |
+
prompt=ChatPromptTemplate.from_template(
|
77 |
+
"""
|
78 |
+
Answer the question based on provided context only.
|
79 |
+
Your context retrieval mechanism works correclty but your are not providing answer from context.
|
80 |
+
Please provide the most accurate response based on question.
|
81 |
+
{context},
|
82 |
+
Questions:{input}
|
83 |
+
"""
|
84 |
+
)
|
85 |
+
|
86 |
+
return prompt
|
87 |
+
|
88 |
+
def generate_response(llm,prompt,user_input,vectorstore):
|
89 |
+
|
90 |
+
|
91 |
+
document_chain=create_stuff_documents_chain(llm,prompt)
|
92 |
+
retriever=vectorstore.as_retriever(search_type="similarity",search_kwargs={"k":5})
|
93 |
+
retrieval_chain=create_retrieval_chain(retriever,document_chain)
|
94 |
+
response=retrieval_chain.invoke({"input":user_input})
|
95 |
+
|
96 |
+
return response
|
97 |
+
# ['answer']
|
98 |
+
|
99 |
+
|
100 |
+
|
101 |
+
def main():
|
102 |
+
st.set_page_config(page_title='Chat Groq Demo')
|
103 |
+
st.header('Chat Groq Demo')
|
104 |
+
user_input=st.text_input('Enter the Prompt here')
|
105 |
+
file=st.file_uploader('Choose Invoice File',type='pdf')
|
106 |
+
|
107 |
+
|
108 |
+
submit = st.button("Submit")
|
109 |
+
st.session_state.submit_clicked = False
|
110 |
+
if submit :
|
111 |
+
st.session_state.submit_clicked = True
|
112 |
+
if user_input and file:
|
113 |
+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
114 |
+
temp_file.write(file.getbuffer())
|
115 |
+
file_path = temp_file.name
|
116 |
+
# with open(file.name, mode='wb') as w:
|
117 |
+
# # w.write(file.getvalue())
|
118 |
+
# w.write(file.getbuffer())
|
119 |
+
llm=ChatGroq(groq_api_key=groq_api_key,model_name="gemma-7b-it")
|
120 |
+
prompt=prompt_temp()
|
121 |
+
|
122 |
+
vectorstore=doc_loader(file_path)
|
123 |
+
|
124 |
+
|
125 |
+
response=generate_response(llm,prompt,user_input,vectorstore)
|
126 |
+
st.write(response['answer'])
|
127 |
+
|
128 |
+
with st.expander("Document Similarity Search"):
|
129 |
+
for i,doc in enumerate(response['context']):
|
130 |
+
st.write(doc.page_content)
|
131 |
+
st.write('---------------------------------')
|
132 |
+
|
133 |
+
|
134 |
+
|
135 |
+
if __name__=="__main__":
|
136 |
+
main()
|
137 |
+
|
138 |
+
|
139 |
+
|
140 |
+
|
141 |
+
|
142 |
+
|
143 |
+
|
144 |
+
|
145 |
+
|
146 |
+
|
147 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|