Shivanand Roy commited on
Commit ·
bc69d30
1
Parent(s): f98d774
Added application file
Browse filesFiles changed (9)
- DockerfileAdded+11
- app.pyAdded+145
- assets/ChatPDF.jpgAdded
- assets/ChatPDFAvatar.jpgAdded
- assets/ChatPDFAvatar.pngAdded
- assets/ChatPDFLogo.pngAdded
- assets/ChatPDFLogoV2.pngAdded
- assets/avatar.jpgAdded
- chainlit.mdAdded+19
1234567891011FROM python:3.9RUN useradd -m -u 1000 userUSER userENV HOME=/home/user \ PATH=/home/user/.local/bin:$PATHWORKDIR $HOME/appCOPY --chown=user . $HOME/appCOPY ./requirements.txt ~/app/requirements.txtRUN pip install -r requirements.txtCOPY . .CMD ["chainlit", "run", "app.py", "--port", "7860"]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145from langchain.embeddings.openai import OpenAIEmbeddingsfrom langchain.text_splitter import RecursiveCharacterTextSplitter, CharacterTextSplitterfrom langchain.vectorstores import Chromafrom langchain.chains import RetrievalQAWithSourcesChainfrom langchain.memory import ConversationBufferWindowMemoryfrom langchain.chains import ConversationalRetrievalChainfrom langchain.chat_models import ChatOpenAIfrom langchain.prompts.chat import ( ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate,)from langchain.document_loaders import PyPDFLoaderimport osimport chainlit as clfrom langchain.prompts import PromptTemplate
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
system_template = """Use the following pieces of context to answer the users question.If you don't know the answer, just say that you don't know, don't try to make up an answer.ALWAYS return a "SOURCES" part in your answer.The "SOURCES" part should be a reference to the source of the document from which you got your answer.
Example of your response should be:
```The answer is fooSOURCES: xyz```
Begin!----------------{summaries}"""messages = [ SystemMessagePromptTemplate.from_template(system_template), HumanMessagePromptTemplate.from_template("{question}"),]prompt = ChatPromptTemplate.from_messages(messages)chain_type_kwargs = {"prompt": prompt}
@cl.on_chat_startasync def start(): await cl.Avatar( name="ChatPDF", url="https://avatars.githubusercontent.com/u/128686189?s=400&u=a1d1553023f8ea0921fba0debbe92a8c5f840dd9&v=4", # path = r'assets/ChatPDFAvatar.jpg' ).send()
@cl.langchain_factory(use_async=True)async def init(): files = None
# Wait for the user to upload a file while files == None: files = await cl.AskFileMessage( content="Hey, Welcome to ChatPDF!\n\nChatPDF is a smart, user-friendly tool that integrates state-of-the-art AI models with text extraction and embedding capabilities to create a unique, conversational interaction with your PDF documents.\n\nSimply upload your PDF, ask your questions, and ChatPDF will deliver the most relevant answers directly from your document.\n\nPlease upload a PDF file to begin!", accept=["application/pdf"] ).send()
file = files[0]
msg = cl.Message(content=f'''Processing "{file.name}"...''') await msg.send()
# with open(os.path.join(file.name), "wb") as f: f.write(file.content)
print(file.name) loader = PyPDFLoader(file.name) pages = loader.load_and_split()
# add page split info # Initialize a dictionary to keep track of duplicate page numbers page_counts = {}
for document in pages: page_number = document.metadata['page']
# If this is the first occurrence of this page number, initialize its count to 1 # Otherwise, increment the count for this page number page_counts[page_number] = page_counts.get(page_number, 0) + 1
# Create the page split info string page_split_info = f"Page-{page_number+1}.{page_counts[page_number]}"
# Add the page split info to the document's metadata document.metadata['page_split_info'] = page_split_info
# Create a Chroma vector store embeddings = OpenAIEmbeddings() docsearch = await cl.make_async(Chroma.from_documents)( pages, embeddings )
# define memory memory = ConversationBufferWindowMemory( k=5, memory_key='chat_history', return_messages=True, output_key='answer' ) # Create a chain that uses the Chroma vector store chain = ConversationalRetrievalChain.from_llm( ChatOpenAI(temperature=0, model="gpt-3.5-turbo-16k", streaming=True), chain_type="stuff", retriever=docsearch.as_retriever(search_kwargs={'k':5}), memory=memory, return_source_documents=True, )
# Save the metadata and texts in the user session # cl.user_session.set("metadatas", metadatas) cl.user_session.set("texts", pages)
# Let the user know that the system is ready await msg.update(content=f''' "{file.name}" processed. You can now ask questions!''')
return chain
@cl.langchain_postprocessasync def process_response(res): answer = res["answer"] source_documents = res['source_documents'] content = [source_documents[i].page_content for i in range(len(source_documents))] name = [source_documents[i].metadata['page_split_info'] for i in range(len(source_documents))] source_elements = [ cl.Text(content=content[i], name=name[i]) for i in range(len(source_documents)) ] if source_documents: answer += f"\n\nSources: {', '.join([source_documents[i].metadata['page_split_info'] for i in range(len(source_documents))])}" else: answer += "\n\nNo sources found" await cl.Message(content=answer, elements=source_elements).send() # await cl.Message(content=answer).send()12345678910111213141516171819# ChatPDF - Conversation Style Question Answering with PDFs
ChatPDF is an application designed to provide super fast, conversation-style question answering from any PDF documents.

ChatPDF is a smart, user-friendly tool that integrates state-of-the-art AI models with text extraction and embedding capabilities to create a unique, conversational interaction with your PDF documents.
Simply upload your PDF, ask your questions, and ChatPDF will deliver the most relevant answers directly from your document.
## Features
1. PDF text extraction: ChatPDF accepts PDF files, from which it extracts the text in an intelligent manner.2. Document chunking: The extracted text is split into manageable chunks, enabling efficient processing.3. Text embedding: The chunks are embedded using sophisticated natural language processing techniques and stored in a vector database.4. Fast querying: Upon user query, the application swiftly identifies the most relevant document chunks from the vector database.5. AI-powered answers: The selected chunks are then passed to a large language model (LLM), which generates detailed, coherent responses.
DockerfileAdded+11
app.pyAdded+145
assets/ChatPDF.jpgAdded

assets/ChatPDFAvatar.jpgAdded
assets/ChatPDFAvatar.pngAdded
assets/ChatPDFLogo.pngAdded

assets/ChatPDFLogoV2.pngAdded

assets/avatar.jpgAdded
chainlit.mdAdded+19