Aliibraheem516 commited on
Commit
a2e06e0
·
verified ·
1 Parent(s): ea855fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -33
app.py CHANGED
@@ -27,40 +27,51 @@ Helpful answer:
27
  prompt = PromptTemplate(template=template, input_variables=["context", "question"])
28
 
29
 
30
- # Load and process the PDF
31
- loader = PyPDFLoader(pdf_file.name)
32
- pdf_data = loader.load()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
- # Split the text into chunks
35
- text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
36
- docs = text_splitter.split_documents(pdf_data)
 
 
 
 
37
 
38
- # Create a Chroma vector store
39
- embeddings = HuggingFaceEmbeddings(model_name="embaas/sentence-transformers-multilingual-e5-base")
40
- db = Chroma.from_documents(docs, embeddings)
41
 
42
- # Initialize message history for conversation
43
- message_history = ChatMessageHistory()
44
-
45
- # Memory for conversational context
46
- memory = ConversationBufferMemory(
47
- memory_key="chat_history",
48
- output_key="answer",
49
- chat_memory=message_history,
50
- return_messages=True,
51
- )
52
-
53
- # Create a chain that uses the Chroma vector store
54
- chain = ConversationalRetrievalChain.from_llm(
55
- llm=llm,
56
- chain_type="stuff",
57
- retriever=db.as_retriever(),
58
- memory=memory,
59
- return_source_documents=False,
60
- combine_docs_chain_kwargs={'prompt': prompt}
61
- )
62
-
63
- # Process the question
64
- res = chain({"question": question})
65
- answer = res["answer"]
66
 
 
27
  prompt = PromptTemplate(template=template, input_variables=["context", "question"])
28
 
29
 
30
+ def process_pdf_and_question(pdf_file,question):
31
+ loader = PyPDFLoader(pdf_file.name)
32
+ pdf_data = loader.load()
33
+
34
+ # Split the text into chunks
35
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
36
+ docs = text_splitter.split_documents(pdf_data)
37
+
38
+ # Create a Chroma vector store
39
+ embeddings = HuggingFaceEmbeddings(model_name="embaas/sentence-transformers-multilingual-e5-base")
40
+ db = Chroma.from_documents(docs, embeddings)
41
+
42
+ # Initialize message history for conversation
43
+ message_history = ChatMessageHistory()
44
+
45
+ # Memory for conversational context
46
+ memory = ConversationBufferMemory(
47
+ memory_key="chat_history",
48
+ output_key="answer",
49
+ chat_memory=message_history,
50
+ return_messages=True,
51
+ )
52
+
53
+ # Create a chain that uses the Chroma vector store
54
+ chain = ConversationalRetrievalChain.from_llm(
55
+ llm=llm,
56
+ chain_type="stuff",
57
+ retriever=db.as_retriever(),
58
+ memory=memory,
59
+ return_source_documents=False,
60
+ combine_docs_chain_kwargs={'prompt': prompt}
61
+ )
62
+
63
+ # Process the question
64
+ res = chain({"question": question})
65
+ return res["answer"]
66
 
67
+ app=gr.interface(fn=process_pdf_and_ask_question,
68
+ inputs=[gr.File(file_count="single", type="filepath"), gr.Textbox(lines=2, placeholder="Ask a question...")],
69
+ outputs="text",
70
+ title="PDF Q&A",
71
+ description="Upload a PDF and ask questions about it.",
72
+
73
+ )
74
 
75
+ app.launch()
 
 
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77