jchen8000 commited on
Commit
9994f95
·
verified ·
1 Parent(s): c0a84b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -12
app.py CHANGED
@@ -15,6 +15,9 @@ from langchain_core.runnables import RunnablePassthrough
15
  # Initialize the FAISS vector store
16
  vector_store = None
17
 
 
 
 
18
  template = \
19
  """Use the following pieces of context to answer the question at the end.
20
  If you don't know the answer, just say that you don't know, don't try to make up an answer.
@@ -47,19 +50,24 @@ def index_pdf(pdf):
47
 
48
  return "PDF indexed successfully!"
49
 
50
- # # Function to handle chatbot queries
51
- # def chatbot_query(query):
52
- # if vector_store is None:
53
- # return "Please upload and index a PDF first."
54
-
55
- # # Create a retrieval-based QA chain
56
- # retriever = vector_store.as_retriever()
57
- # qa_chain = RetrievalQA(llm=OpenAI(), retriever=retriever)
58
-
59
- # # Get the response from the QA chain
60
- # response = qa_chain.run(query)
61
 
62
- # return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
 
65
  def format_docs(docs):
@@ -104,8 +112,10 @@ with gr.Blocks(theme="Nymbo/Alyx_Theme") as demo:
104
  with gr.Tab("Indexing"):
105
  pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
106
  index_button = gr.Button("Index PDF")
 
107
  index_output = gr.Textbox(label="Indexing Status")
108
  index_button.click(index_pdf, inputs=pdf_input, outputs=index_output)
 
109
 
110
  with gr.Tab("Chatbot"):
111
  # query_input = gr.Textbox(label="Enter your question")
 
15
  # Initialize the FAISS vector store
16
  vector_store = None
17
 
18
+ # Sample PDF file
19
+ sample_filename = "Attention Is All You Need.pdf"
20
+
21
  template = \
22
  """Use the following pieces of context to answer the question at the end.
23
  If you don't know the answer, just say that you don't know, don't try to make up an answer.
 
50
 
51
  return "PDF indexed successfully!"
52
 
53
+ def load_sample_pdf():
54
+ global vector_store
 
 
 
 
 
 
 
 
 
55
 
56
+ # Load the PDF
57
+ loader = PyPDFLoader(sample_filename)
58
+ documents = loader.load()
59
+
60
+ # Split the documents into chunks
61
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
62
+ texts = text_splitter.split_documents(documents)
63
+
64
+ # Embed the chunks
65
+ embeddings = HuggingFaceEmbeddings(model_name="bert-base-uncased", encode_kwargs={"normalize_embeddings": True})
66
+
67
+ # Store the embeddings in the vector store
68
+ vector_store = FAISS.from_documents(texts, embeddings)
69
+
70
+ return "Sample PDF indexed successfully!"
71
 
72
 
73
  def format_docs(docs):
 
112
  with gr.Tab("Indexing"):
113
  pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
114
  index_button = gr.Button("Index PDF")
115
+ load_sample = gr.Button("Or load "Attention Is All You Need.pdf" as a Sample")
116
  index_output = gr.Textbox(label="Indexing Status")
117
  index_button.click(index_pdf, inputs=pdf_input, outputs=index_output)
118
+ load_sample.click(load_sample_pdf, inputs=None, outputs=index_output)
119
 
120
  with gr.Tab("Chatbot"):
121
  # query_input = gr.Textbox(label="Enter your question")