jchen8000 commited on
Commit
aeb1340
·
verified ·
1 Parent(s): 4088d90

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from langchain.document_loaders import PyPDFLoader
3
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
4
+ from langchain.embeddings import OpenAIEmbeddings
5
+ from langchain.vectorstores import FAISS
6
+ from langchain.chains import RetrievalQA
7
+ from langchain.llms import OpenAI
8
+
9
+ # Initialize the FAISS vector store
10
+ vector_store = None
11
+
12
+ # Function to handle PDF upload and indexing
13
+ def index_pdf(pdf):
14
+ global vector_store
15
+
16
+ # Load the PDF
17
+ loader = PyPDFLoader(pdf.name)
18
+ documents = loader.load()
19
+
20
+ # Split the documents into chunks
21
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
22
+ texts = text_splitter.split_documents(documents)
23
+
24
+ # Embed the chunks and store them in the vector store
25
+ embeddings = OpenAIEmbeddings()
26
+ vector_store = FAISS.from_documents(texts, embeddings)
27
+
28
+ return "PDF indexed successfully!"
29
+
30
+ # Function to handle chatbot queries
31
+ def chatbot_query(query):
32
+ if vector_store is None:
33
+ return "Please upload and index a PDF first."
34
+
35
+ # Create a retrieval-based QA chain
36
+ retriever = vector_store.as_retriever()
37
+ qa_chain = RetrievalQA(llm=OpenAI(), retriever=retriever)
38
+
39
+ # Get the response from the QA chain
40
+ response = qa_chain.run(query)
41
+
42
+ return response
43
+
44
+ # Create the Gradio interface
45
+ with gr.Blocks() as demo:
46
+ with gr.Tab("Indexing"):
47
+ pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
48
+ index_button = gr.Button("Index PDF")
49
+ index_output = gr.Textbox(label="Indexing Status")
50
+
51
+ index_button.click(index_pdf, inputs=pdf_input, outputs=index_output)
52
+
53
+ with gr.Tab("Chatbot"):
54
+ query_input = gr.Textbox(label="Enter your question")
55
+ query_button = gr.Button("Submit")
56
+ query_output = gr.Textbox(label="Response")
57
+
58
+ query_button.click(chatbot_query, inputs=query_input, outputs=query_output)
59
+
60
+ # Launch the Gradio app
61
+ demo.launch()