hexapi commited on
Commit
f2f9821
·
verified ·
1 Parent(s): bf5319e

Update app.py

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