Spaces:
Runtime error
Runtime error
Add caching
Browse files- PDF_Reader.py +4 -1
- QA_Bot.py +6 -5
- app.py +0 -2
PDF_Reader.py
CHANGED
|
@@ -2,7 +2,9 @@ import PyPDF2
|
|
| 2 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 3 |
from langchain.embeddings import HuggingFaceBgeEmbeddings
|
| 4 |
from langchain.vectorstores import FAISS
|
|
|
|
| 5 |
|
|
|
|
| 6 |
def read_pdf(uploaded_file):
|
| 7 |
pdf_reader = PyPDF2.PdfReader(uploaded_file)
|
| 8 |
text = ""
|
|
@@ -10,6 +12,7 @@ def read_pdf(uploaded_file):
|
|
| 10 |
text += page.extract_text()
|
| 11 |
return text
|
| 12 |
|
|
|
|
| 13 |
def Chunks(docs):
|
| 14 |
text_splitter = RecursiveCharacterTextSplitter(
|
| 15 |
# Set a really small chunk size, just to show.
|
|
@@ -19,7 +22,7 @@ def Chunks(docs):
|
|
| 19 |
doc = text_splitter.split_text(docs)
|
| 20 |
return doc
|
| 21 |
|
| 22 |
-
|
| 23 |
def PDF_4_QA(file):
|
| 24 |
content = read_pdf(file)
|
| 25 |
pdf_chunks = Chunks(docs=content)
|
|
|
|
| 2 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 3 |
from langchain.embeddings import HuggingFaceBgeEmbeddings
|
| 4 |
from langchain.vectorstores import FAISS
|
| 5 |
+
import streamlit as st
|
| 6 |
|
| 7 |
+
@st.cache_data
|
| 8 |
def read_pdf(uploaded_file):
|
| 9 |
pdf_reader = PyPDF2.PdfReader(uploaded_file)
|
| 10 |
text = ""
|
|
|
|
| 12 |
text += page.extract_text()
|
| 13 |
return text
|
| 14 |
|
| 15 |
+
@st.cache_data
|
| 16 |
def Chunks(docs):
|
| 17 |
text_splitter = RecursiveCharacterTextSplitter(
|
| 18 |
# Set a really small chunk size, just to show.
|
|
|
|
| 22 |
doc = text_splitter.split_text(docs)
|
| 23 |
return doc
|
| 24 |
|
| 25 |
+
@st.cache_resource
|
| 26 |
def PDF_4_QA(file):
|
| 27 |
content = read_pdf(file)
|
| 28 |
pdf_chunks = Chunks(docs=content)
|
QA_Bot.py
CHANGED
|
@@ -2,8 +2,7 @@ import streamlit as st
|
|
| 2 |
from QnA import Q_A
|
| 3 |
import re,time
|
| 4 |
|
| 5 |
-
|
| 6 |
-
def QA_Bot(vectorstore):
|
| 7 |
st.title("Q&A Bot")
|
| 8 |
# Initialize chat history
|
| 9 |
if "messages" not in st.session_state:
|
|
@@ -20,8 +19,7 @@ def QA_Bot(vectorstore):
|
|
| 20 |
st.chat_message("user").markdown(prompt)
|
| 21 |
# Add user message to chat history
|
| 22 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 23 |
-
|
| 24 |
-
ai_response = Q_A(vectorstore,prompt)
|
| 25 |
response = f"Echo: {ai_response}"
|
| 26 |
# Display assistant response in chat message container
|
| 27 |
with st.chat_message("assistant"):
|
|
@@ -29,9 +27,12 @@ def QA_Bot(vectorstore):
|
|
| 29 |
full_response = ""
|
| 30 |
for chunk in re.split(r'(\s+)', response):
|
| 31 |
full_response += chunk + " "
|
| 32 |
-
time.sleep(0.01)
|
| 33 |
|
| 34 |
# Add a blinking cursor to simulate typing
|
| 35 |
message_placeholder.markdown(full_response + "▌")
|
| 36 |
# Add assistant response to chat history
|
| 37 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from QnA import Q_A
|
| 3 |
import re,time
|
| 4 |
|
| 5 |
+
def QA_Bot(_vectorstore):
|
|
|
|
| 6 |
st.title("Q&A Bot")
|
| 7 |
# Initialize chat history
|
| 8 |
if "messages" not in st.session_state:
|
|
|
|
| 19 |
st.chat_message("user").markdown(prompt)
|
| 20 |
# Add user message to chat history
|
| 21 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 22 |
+
ai_response = ai_proc(_vectorstore, prompt)
|
|
|
|
| 23 |
response = f"Echo: {ai_response}"
|
| 24 |
# Display assistant response in chat message container
|
| 25 |
with st.chat_message("assistant"):
|
|
|
|
| 27 |
full_response = ""
|
| 28 |
for chunk in re.split(r'(\s+)', response):
|
| 29 |
full_response += chunk + " "
|
|
|
|
| 30 |
|
| 31 |
# Add a blinking cursor to simulate typing
|
| 32 |
message_placeholder.markdown(full_response + "▌")
|
| 33 |
# Add assistant response to chat history
|
| 34 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 35 |
+
|
| 36 |
+
@st.cache_resource
|
| 37 |
+
def ai_proc(_vectorstore, prompt):
|
| 38 |
+
return Q_A(_vectorstore, prompt)
|
app.py
CHANGED
|
@@ -36,9 +36,7 @@ def main():
|
|
| 36 |
)
|
| 37 |
|
| 38 |
uploaded_file = st.sidebar.file_uploader("Choose a PDF file", type="pdf")
|
| 39 |
-
print("upload file before")
|
| 40 |
if uploaded_file is not None:
|
| 41 |
-
print("upload file not none")
|
| 42 |
# profiler = cProfile.Profile()
|
| 43 |
# profiler.enable()
|
| 44 |
st.sidebar.success("File uploaded successfully.")
|
|
|
|
| 36 |
)
|
| 37 |
|
| 38 |
uploaded_file = st.sidebar.file_uploader("Choose a PDF file", type="pdf")
|
|
|
|
| 39 |
if uploaded_file is not None:
|
|
|
|
| 40 |
# profiler = cProfile.Profile()
|
| 41 |
# profiler.enable()
|
| 42 |
st.sidebar.success("File uploaded successfully.")
|