Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,7 @@ from langchain.vectorstores import FAISS
|
|
7 |
from langchain.chains import ConversationalRetrievalChain
|
8 |
from langchain.chat_models import ChatOpenAI
|
9 |
from langchain.memory import ConversationBufferMemory
|
10 |
-
|
11 |
from langchain.prompts import PromptTemplate
|
12 |
|
13 |
|
@@ -16,6 +16,7 @@ openai_api_key = os.environ.get("OPENAI_API_KEY")
|
|
16 |
|
17 |
class AdvancedPdfChatbot:
|
18 |
def __init__(self, openai_api_key):
|
|
|
19 |
os.environ["OPENAI_API_KEY"] = openai_api_key
|
20 |
self.embeddings = OpenAIEmbeddings()
|
21 |
self.text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
@@ -56,9 +57,9 @@ You are a file-based knowledge assistant that interacts with users like ChatGPT.
|
|
56 |
User: "Can you summarize the main argument from the introduction of the file?"
|
57 |
Response: "Sure! The introduction discusses [key points] and highlights the central argument that [main idea]. This can be found on page 2 under the heading 'Introduction'."
|
58 |
User: "Where can I find the definition of 'symbolic interactionism' in the document?"
|
59 |
-
Response: "The definition of 'symbolic interactionism' appears on page 12 under the subheading 'Key Theoretical Concepts'."
|
60 |
User: "Explain the concept of 'cognitive dissonance' as it is presented in the document."
|
61 |
-
Response: "In the document, 'cognitive dissonance' is defined as [definition from the file]. It appears in the context of [brief explanation] and can be found on page 15 under the section 'Theoretical Foundations'."
|
62 |
|
63 |
NOTE : DESCRIBE/SUMMARY should always return the overall summary of the documents in well documented and descriptions of the topic in great details.
|
64 |
|
@@ -72,12 +73,15 @@ NOTE : DESCRIBE/SUMMARY should always return the overall summary of the document
|
|
72 |
|
73 |
self.prompt = PromptTemplate(template=self.template, input_variables=["context", "question"])
|
74 |
|
75 |
-
|
|
|
76 |
loader = PyPDFLoader(pdf_path)
|
77 |
documents = loader.load()
|
78 |
texts = self.text_splitter.split_documents(documents)
|
79 |
self.db = FAISS.from_documents(texts, self.embeddings)
|
80 |
self.setup_conversation_chain()
|
|
|
|
|
81 |
|
82 |
def setup_conversation_chain(self):
|
83 |
self.qa_chain = ConversationalRetrievalChain.from_llm(
|
@@ -96,16 +100,23 @@ NOTE : DESCRIBE/SUMMARY should always return the overall summary of the document
|
|
96 |
# Initialize the chatbot
|
97 |
pdf_chatbot = AdvancedPdfChatbot(openai_api_key)
|
98 |
|
|
|
|
|
99 |
def upload_pdf(pdf_file):
|
100 |
if pdf_file is None:
|
101 |
return "Please upload a PDF file."
|
102 |
-
file_path = pdf_file.name
|
|
|
|
|
|
|
103 |
pdf_chatbot.load_and_process_pdf(file_path)
|
104 |
return "PDF uploaded and processed successfully. You can now start chatting!"
|
105 |
|
|
|
|
|
106 |
def respond(message, history):
|
107 |
bot_message = pdf_chatbot.chat(message)
|
108 |
-
history.append((message, bot_message))
|
109 |
return "", history
|
110 |
|
111 |
def clear_chatbot():
|
|
|
7 |
from langchain.chains import ConversationalRetrievalChain
|
8 |
from langchain.chat_models import ChatOpenAI
|
9 |
from langchain.memory import ConversationBufferMemory
|
10 |
+
import shutil
|
11 |
from langchain.prompts import PromptTemplate
|
12 |
|
13 |
|
|
|
16 |
|
17 |
class AdvancedPdfChatbot:
|
18 |
def __init__(self, openai_api_key):
|
19 |
+
self.memory = deque(maxlen=20)
|
20 |
os.environ["OPENAI_API_KEY"] = openai_api_key
|
21 |
self.embeddings = OpenAIEmbeddings()
|
22 |
self.text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
|
|
57 |
User: "Can you summarize the main argument from the introduction of the file?"
|
58 |
Response: "Sure! The introduction discusses [key points] and highlights the central argument that [main idea]. This can be found on page 2 under the heading 'Introduction'."
|
59 |
User: "Where can I find the definition of 'symbolic interactionism' in the document?"
|
60 |
+
Bot Response: "The definition of 'symbolic interactionism' appears on page 12 under the subheading 'Key Theoretical Concepts'."
|
61 |
User: "Explain the concept of 'cognitive dissonance' as it is presented in the document."
|
62 |
+
Bot Response: "In the document, 'cognitive dissonance' is defined as [definition from the file]. It appears in the context of [brief explanation] and can be found on page 15 under the section 'Theoretical Foundations'."
|
63 |
|
64 |
NOTE : DESCRIBE/SUMMARY should always return the overall summary of the documents in well documented and descriptions of the topic in great details.
|
65 |
|
|
|
73 |
|
74 |
self.prompt = PromptTemplate(template=self.template, input_variables=["context", "question"])
|
75 |
|
76 |
+
def load_and_process_pdf(self, pdf_path):
|
77 |
+
try:
|
78 |
loader = PyPDFLoader(pdf_path)
|
79 |
documents = loader.load()
|
80 |
texts = self.text_splitter.split_documents(documents)
|
81 |
self.db = FAISS.from_documents(texts, self.embeddings)
|
82 |
self.setup_conversation_chain()
|
83 |
+
except Exception as e:
|
84 |
+
return f"An error occurred while processing the PDF: {e}"
|
85 |
|
86 |
def setup_conversation_chain(self):
|
87 |
self.qa_chain = ConversationalRetrievalChain.from_llm(
|
|
|
100 |
# Initialize the chatbot
|
101 |
pdf_chatbot = AdvancedPdfChatbot(openai_api_key)
|
102 |
|
103 |
+
|
104 |
+
|
105 |
def upload_pdf(pdf_file):
|
106 |
if pdf_file is None:
|
107 |
return "Please upload a PDF file."
|
108 |
+
file_path = f"uploads/{pdf_file.name}"
|
109 |
+
with open(file_path, "wb") as f:
|
110 |
+
shutil.copyfileobj(pdf_file, f)
|
111 |
+
pdf_chatbot.memory.clear() # Clears past memory before loading a new file
|
112 |
pdf_chatbot.load_and_process_pdf(file_path)
|
113 |
return "PDF uploaded and processed successfully. You can now start chatting!"
|
114 |
|
115 |
+
|
116 |
+
|
117 |
def respond(message, history):
|
118 |
bot_message = pdf_chatbot.chat(message)
|
119 |
+
history.append((f"User: {message}", f"Bot: {bot_message}"))
|
120 |
return "", history
|
121 |
|
122 |
def clear_chatbot():
|