Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,155 @@
|
|
1 |
-
import os
|
2 |
-
import getpass
|
3 |
import streamlit as st
|
4 |
-
from
|
5 |
-
from
|
6 |
-
from langchain.
|
7 |
-
from langchain.
|
8 |
-
from langchain import
|
9 |
-
from langchain.
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
# load huggingface api key
|
16 |
-
hubtok = os.environ["HUGGINGFACE_HUB_TOKEN"]
|
17 |
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
25 |
|
26 |
-
# st.write(pages)
|
27 |
|
28 |
-
|
29 |
-
|
|
|
30 |
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
repo_id = "tiiuae/falcon-7b"
|
35 |
-
llm = HuggingFaceHub(repo_id=repo_id, huggingfacehub_api_token=hubtok, model_kwargs={'temperature': 0.2,'max_length': 1000})
|
36 |
|
37 |
-
|
38 |
-
|
|
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
from PyPDF2 import PdfReader
|
4 |
+
from langchain.text_splitter import CharacterTextSplitter
|
5 |
+
from langchain.embeddings import OpenAIEmbeddings, HuggingFaceInstructEmbeddings
|
6 |
+
from langchain.vectorstores import FAISS
|
7 |
+
from langchain.chat_models import ChatOpenAI
|
8 |
+
from langchain.memory import ConversationBufferMemory
|
9 |
+
from langchain.chains import ConversationalRetrievalChain
|
10 |
+
from htmlTemplates import css, bot_template, user_template
|
11 |
+
from langchain.llms import HuggingFaceHub
|
12 |
+
from langchain.callbacks import get_openai_callback
|
13 |
|
14 |
+
def get_pdf_text(pdf_docs):
|
15 |
+
text = ""
|
16 |
+
for pdf in pdf_docs:
|
17 |
+
pdf_reader = PdfReader(pdf)
|
18 |
+
for page in pdf_reader.pages:
|
19 |
+
text += page.extract_text()
|
20 |
+
return text
|
21 |
|
|
|
|
|
22 |
|
23 |
+
def get_text_chunks(text):
|
24 |
+
text_splitter = CharacterTextSplitter(
|
25 |
+
separator="\n",
|
26 |
+
chunk_size=1000,
|
27 |
+
chunk_overlap=200,
|
28 |
+
length_function=len
|
29 |
+
)
|
30 |
+
chunks = text_splitter.split_text(text)
|
31 |
+
return chunks
|
32 |
|
33 |
|
34 |
+
def get_vectorstore(text_chunks):
|
35 |
+
# embeddings = OpenAIEmbeddings()
|
36 |
+
embeddings = HuggingFaceInstructEmbeddings(model_name="hkunlp/instructor-xl")
|
37 |
+
vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
|
38 |
+
return vectorstore
|
39 |
|
|
|
40 |
|
41 |
+
def get_conversation_chain(vectorstore):
|
42 |
+
# llm = ChatOpenAI(model_name="gpt-3.5-turbo-16k")
|
43 |
+
llm = HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature":0.5, "max_length":512})
|
44 |
|
45 |
+
memory = ConversationBufferMemory(
|
46 |
+
memory_key='chat_history', return_messages=True)
|
47 |
+
conversation_chain = ConversationalRetrievalChain.from_llm(
|
48 |
+
llm=llm,
|
49 |
+
retriever=vectorstore.as_retriever(),
|
50 |
+
memory=memory
|
51 |
+
)
|
52 |
+
return conversation_chain
|
53 |
|
|
|
|
|
54 |
|
55 |
+
def handle_userinput(user_question):
|
56 |
+
response = st.session_state.conversation({'question': user_question})
|
57 |
+
st.session_state.chat_history = response['chat_history']
|
58 |
|
59 |
+
for i, message in enumerate(st.session_state.chat_history):
|
60 |
+
if i % 2 == 0:
|
61 |
+
st.write(user_template.replace(
|
62 |
+
"{{MSG}}", message.content), unsafe_allow_html=True)
|
63 |
+
else:
|
64 |
+
st.write(bot_template.replace(
|
65 |
+
"{{MSG}}", message.content), unsafe_allow_html=True)
|
66 |
+
|
67 |
+
|
68 |
+
def main():
|
69 |
+
load_dotenv()
|
70 |
+
st.set_page_config(page_title="Chat with multiple PDFs",
|
71 |
+
page_icon=":books:")
|
72 |
+
st.write(css, unsafe_allow_html=True)
|
73 |
+
|
74 |
+
if "conversation" not in st.session_state:
|
75 |
+
st.session_state.conversation = None
|
76 |
+
if "chat_history" not in st.session_state:
|
77 |
+
st.session_state.chat_history = None
|
78 |
+
|
79 |
+
st.header("Chat with multiple PDFs :books:")
|
80 |
+
user_question = st.text_input("Ask a question about your documents:")
|
81 |
+
if user_question:
|
82 |
+
handle_userinput(user_question)
|
83 |
+
|
84 |
+
with st.sidebar:
|
85 |
+
st.subheader("Your documents")
|
86 |
+
pdf_docs = st.file_uploader(
|
87 |
+
"Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
|
88 |
+
if st.button("Process"):
|
89 |
+
if(len(pdf_docs) == 0):
|
90 |
+
st.error("Please upload at least one PDF")
|
91 |
+
else:
|
92 |
+
with st.spinner("Processing"):
|
93 |
+
# get pdf text
|
94 |
+
raw_text = get_pdf_text(pdf_docs)
|
95 |
+
|
96 |
+
# get the text chunks
|
97 |
+
text_chunks = get_text_chunks(raw_text)
|
98 |
+
|
99 |
+
# create vector store
|
100 |
+
vectorstore = get_vectorstore(text_chunks)
|
101 |
+
|
102 |
+
# create conversation chain
|
103 |
+
st.session_state.conversation = get_conversation_chain(
|
104 |
+
vectorstore)
|
105 |
+
|
106 |
+
if __name__ == '__main__':
|
107 |
+
main()
|
108 |
+
|
109 |
+
|
110 |
+
|
111 |
+
|
112 |
+
|
113 |
+
|
114 |
+
# import os
|
115 |
+
# import getpass
|
116 |
+
# import streamlit as st
|
117 |
+
# from langchain.document_loaders import PyPDFLoader
|
118 |
+
# from langchain.text_splitter import RecursiveCharacterTextSplitter
|
119 |
+
# from langchain.embeddings import HuggingFaceEmbeddings
|
120 |
+
# from langchain.vectorstores import Chroma
|
121 |
+
# from langchain import HuggingFaceHub
|
122 |
+
# from langchain.chains import RetrievalQA
|
123 |
+
# # __import__('pysqlite3')
|
124 |
+
# # import sys
|
125 |
+
# # sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')
|
126 |
+
|
127 |
+
|
128 |
+
# # load huggingface api key
|
129 |
+
# hubtok = os.environ["HUGGINGFACE_HUB_TOKEN"]
|
130 |
+
|
131 |
+
# # use streamlit file uploader to ask user for file
|
132 |
+
# # file = st.file_uploader("Upload PDF")
|
133 |
+
|
134 |
+
|
135 |
+
# path = "Geeta.pdf"
|
136 |
+
# loader = PyPDFLoader(path)
|
137 |
+
# pages = loader.load()
|
138 |
+
|
139 |
+
# # st.write(pages)
|
140 |
+
|
141 |
+
# splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=20)
|
142 |
+
# docs = splitter.split_documents(pages)
|
143 |
+
|
144 |
+
# embeddings = HuggingFaceEmbeddings()
|
145 |
+
# doc_search = Chroma.from_documents(docs, embeddings)
|
146 |
+
|
147 |
+
# repo_id = "tiiuae/falcon-7b"
|
148 |
+
# llm = HuggingFaceHub(repo_id=repo_id, huggingfacehub_api_token=hubtok, model_kwargs={'temperature': 0.2,'max_length': 1000})
|
149 |
+
|
150 |
+
# from langchain.schema import retriever
|
151 |
+
# retireval_chain = RetrievalQA.from_chain_type(llm, chain_type="stuff", retriever=doc_search.as_retriever())
|
152 |
+
|
153 |
+
# if query := st.chat_input("Enter a question: "):
|
154 |
+
# with st.chat_message("assistant"):
|
155 |
+
# st.write(retireval_chain.run(query))
|