Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,102 @@
|
|
1 |
import streamlit as st
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 HuggingFaceEmbeddings
|
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 |
+
import os
|
13 |
+
# from transformers import T5Tokenizer, T5ForConditionalGeneration
|
14 |
+
# from langchain.callbacks import get_openai_callback
|
15 |
+
hub_token = os.environ["HUGGINGFACE_HUB_TOKEN"]
|
16 |
+
|
17 |
+
def split_pdfs(pdf_docs):
|
18 |
+
"""Splits a list of PDF documents into smaller chunks.
|
19 |
+
|
20 |
+
Args:
|
21 |
+
pdf_docs: A list of PDF documents.
|
22 |
+
|
23 |
+
Returns:
|
24 |
+
A list of lists of PDF documents, where each sublist contains a smaller chunk of the original PDF documents.
|
25 |
+
"""
|
26 |
+
|
27 |
+
pdf_chunks = []
|
28 |
+
for pdf_doc in pdf_docs:
|
29 |
+
# Split the PDF document into pages.
|
30 |
+
pdf_reader = PdfReader(pdf_doc)
|
31 |
+
pdf_pages = pdf_reader.pages
|
32 |
+
|
33 |
+
# Split the PDF pages into chunks.
|
34 |
+
pdf_chunks.append([])
|
35 |
+
for pdf_page in pdf_pages:
|
36 |
+
# Add the PDF page to the current chunk.
|
37 |
+
pdf_chunks[-1].append(pdf_page)
|
38 |
+
|
39 |
+
# If the chunk is too large, start a new chunk.
|
40 |
+
if len(pdf_chunks[-1]) >= 10:
|
41 |
+
pdf_chunks.append([])
|
42 |
+
|
43 |
+
return pdf_chunks
|
44 |
+
|
45 |
+
def generate_response(pdf_chunks, llm_model):
|
46 |
+
"""Generates a response to a query using a list of PDF documents and an LLM model.
|
47 |
+
|
48 |
+
Args:
|
49 |
+
pdf_chunks: A list of lists of PDF documents, where each sublist contains a smaller chunk of the original PDF documents.
|
50 |
+
llm_model: An LLM model.
|
51 |
+
|
52 |
+
Returns:
|
53 |
+
A response to the query.
|
54 |
+
"""
|
55 |
+
|
56 |
+
# Generate a summary of each PDF chunk.
|
57 |
+
pdf_summaries = []
|
58 |
+
for pdf_chunk in pdf_chunks:
|
59 |
+
# Generate a summary of the PDF chunk.
|
60 |
+
pdf_summary = llm_model.generate(
|
61 |
+
prompt=f"Summarize the following text:\n{get_pdf_text(pdf_chunk)}",
|
62 |
+
max_new_tokens=100
|
63 |
+
)
|
64 |
+
|
65 |
+
# Add the summary to the list of summaries.
|
66 |
+
pdf_summaries.append(pdf_summary)
|
67 |
+
|
68 |
+
# Generate a response to the query using the summaries of the PDF chunks.
|
69 |
+
response = llm_model.generate(
|
70 |
+
prompt=f"Answer the following question using the following summaries:\n{get_text_chunks(pdf_summaries)}\n\nQuestion:",
|
71 |
+
max_new_tokens=200
|
72 |
+
)
|
73 |
+
|
74 |
+
return response
|
75 |
+
|
76 |
+
def main():
|
77 |
+
load_dotenv()
|
78 |
+
st.set_page_config(page_title="Chat with multiple PDFs", page_icon=":books:")
|
79 |
+
st.write(css, unsafe_allow_html=True)
|
80 |
+
|
81 |
+
# Load the LLM model.
|
82 |
+
llm_model = HuggingFaceHub(repo_id="mistralai/Mistral-7B-v0.1", huggingfacehub_api_token=hub_token)
|
83 |
+
|
84 |
+
if "conversation" not in st.session_state:
|
85 |
+
st.session_state.conversation = None
|
86 |
+
|
87 |
+
if "chat_history" not in st.session_state:
|
88 |
+
st.session_state.chat_history = None
|
89 |
+
|
90 |
+
st.header("Chat with multiple PDFs :books:")
|
91 |
+
user_question = st.text_input("Ask a question about your documents:")
|
92 |
+
|
93 |
+
# If the user asked a question, generate a response.
|
94 |
+
if user_question:
|
95 |
+
# Split the PDF documents into smaller chunks.
|
96 |
+
pdf_chunks = split_pdfs(st.session_state.pdf_docs)
|
97 |
+
|
98 |
+
# Generate a response to the query.
|
99 |
+
response = generate_response(pdf_chunks, llm_model)
|
100 |
+
|
101 |
+
st.write(response)
|
102 |
+
|