Spaces:
Build error
Build error
clementsan commited on
Commit ·
9733941
1
Parent(s): 1da1e92
Enable use of document references
Browse files100 unmodified lines1011021031041051066 unmodified lines11311411511611711811942 unmodified lines16216316416516616716816917017117215 unmodified lines1881891901911921931944 unmodified lines19920020120220320420516 unmodified lines2222232242252262272 unmodified lines230231232233234235236237238239240241242100 unmodified linesprogress(0.75, desc="Defining buffer memory...")memory = ConversationBufferMemory(memory_key="chat_history",return_messages=True)# retriever=vector_db.as_retriever(search_type="similarity", search_kwargs={'k': 3})6 unmodified lineschain_type="stuff",memory=memory,# combine_docs_chain_kwargs={"prompt": your_prompt})# return_source_documents=True,# return_generated_question=True,# verbose=True,)42 unmodified lines# Generate response using QA chainresponse = qa_chain({"question": message, "chat_history": formatted_chat_history})# return response['answer']# Append user message and response to chat historynew_history = history + [(message, response["answer"])]return gr.update(value=""), new_historydef upload_file(file_obj):15 unmodified lines"""<center><h2>PDF-based chatbot (powered by LangChain and open-source LLMs)</center></h2><h3>Ask any questions about your PDF documents, along with follow-ups</h3><b>Note:</b> This AI assistant performs retrieval-augmented generation from your PDF documents. \When generating answers, it takes past questions into account (via conversational memory), and points to specific document sources for clarity purposes</i><br><b>Warning:</b> This space uses the free CPU Basic hardware from Hugging Face. Some steps and LLM models used below (free inference endpoints) can take some time to generate a reply.""")with gr.Tab("Step 1 - Document pre-processing"):4 unmodified linesdb_btn = gr.Radio(["ChromaDB"], label="Vector database type", value = "ChromaDB", type="index", info="Choose your vector database")with gr.Accordion("Advanced options - Document text splitter", open=False):with gr.Row():slider_chunk_size = gr.Slider(minimum = 100, maximum = 1000, value=500, step=20, label="Chunk size", info="Chunk size", interactive=True)with gr.Row():slider_chunk_overlap = gr.Slider(minimum = 10, maximum = 200, value=40, step=10, label="Chunk overlap", info="Chunk overlap", interactive=True)with gr.Row():16 unmodified lineswith gr.Tab("Step 3 - Conversation with chatbot"):chatbot = gr.Chatbot(height=300)with gr.Row():msg = gr.Textbox(placeholder="Type message", container=True)with gr.Row():2 unmodified lines# Preprocessing events#upload_btn.upload(upload_file, inputs=[upload_btn], outputs=[document])db_btn.click(initialize_database, inputs=[document, slider_chunk_size, slider_chunk_overlap], outputs=[vector_db, db_progress])qachain_btn.click(initialize_LLM, inputs=[llm_btn, slider_temperature, slider_maxtokens, slider_topk, vector_db], outputs=[llm_progress]).then(lambda: None, None, chatbot, queue=False)# Chatbot eventsmsg.submit(conversation, [msg, chatbot], [msg, chatbot], queue=False)submit_btn.click(conversation, inputs=[msg, chatbot], outputs=[msg, chatbot], queue=False)clear_btn.click(lambda: None, None, chatbot, queue=False)demo.queue().launch(debug=True)100 unmodified lines1011021031041051061076 unmodified lines11411511611711811912042 unmodified lines16316416516616716816917017117217317417517617717817918018118215 unmodified lines1981992002012022032044 unmodified lines20921021121221321421516 unmodified lines2322332342352362372382392402412422432442 unmodified lines247248249250251252253254255256257258259260261262263264265266267268269270271272273274275100 unmodified linesprogress(0.75, desc="Defining buffer memory...")memory = ConversationBufferMemory(memory_key="chat_history",output_key='answer',return_messages=True)# retriever=vector_db.as_retriever(search_type="similarity", search_kwargs={'k': 3})6 unmodified lineschain_type="stuff",memory=memory,# combine_docs_chain_kwargs={"prompt": your_prompt})return_source_documents=True,# return_generated_question=True,# verbose=True,)42 unmodified lines# Generate response using QA chainresponse = qa_chain({"question": message, "chat_history": formatted_chat_history})response_answer = response["answer"]response_sources = response["source_documents"]response_source1 = response_sources[0].page_content.strip()response_source2 = response_sources[1].page_content.strip()# Langchain sources are zero-basedresponse_source1_page = response_sources[0].metadata["page"] + 1response_source2_page = response_sources[1].metadata["page"] + 1# print ('chat response: ', response_answer)# print('DB source', response_sources)# Append user message and response to chat historynew_history = history + [(message, response_answer)]# return gr.update(value=""), new_history, response_sources[0], response_sources[1]return gr.update(value=""), new_history, response_source1, response_source1_page, response_source2, response_source2_pagedef upload_file(file_obj):15 unmodified lines"""<center><h2>PDF-based chatbot (powered by LangChain and open-source LLMs)</center></h2><h3>Ask any questions about your PDF documents, along with follow-ups</h3><b>Note:</b> This AI assistant performs retrieval-augmented generation from your PDF documents. \When generating answers, it takes past questions into account (via conversational memory), and includes document references for clarity purposes.</i><br><b>Warning:</b> This space uses the free CPU Basic hardware from Hugging Face. Some steps and LLM models used below (free inference endpoints) can take some time to generate a reply.""")with gr.Tab("Step 1 - Document pre-processing"):4 unmodified linesdb_btn = gr.Radio(["ChromaDB"], label="Vector database type", value = "ChromaDB", type="index", info="Choose your vector database")with gr.Accordion("Advanced options - Document text splitter", open=False):with gr.Row():slider_chunk_size = gr.Slider(minimum = 100, maximum = 1000, value=600, step=20, label="Chunk size", info="Chunk size", interactive=True)with gr.Row():slider_chunk_overlap = gr.Slider(minimum = 10, maximum = 200, value=40, step=10, label="Chunk overlap", info="Chunk overlap", interactive=True)with gr.Row():16 unmodified lineswith gr.Tab("Step 3 - Conversation with chatbot"):chatbot = gr.Chatbot(height=300)with gr.Accordion("Advanced - Document references", open=False):with gr.Row():doc_source1 = gr.Textbox(label="Reference 1", lines=2, container=True, scale=20)source1_page = gr.Number(label="Page", scale=1)with gr.Row():doc_source2 = gr.Textbox(label="Reference 2", lines=2, container=True, scale=20)source2_page = gr.Number(label="Page", scale=1)with gr.Row():msg = gr.Textbox(placeholder="Type message", container=True)with gr.Row():2 unmodified lines# Preprocessing events#upload_btn.upload(upload_file, inputs=[upload_btn], outputs=[document])db_btn.click(initialize_database, \inputs=[document, slider_chunk_size, slider_chunk_overlap], \outputs=[vector_db, db_progress])qachain_btn.click(initialize_LLM, \inputs=[llm_btn, slider_temperature, slider_maxtokens, slider_topk, vector_db], \outputs=[llm_progress]).then(lambda:[None,"",0,"",0], \inputs=None, \outputs=[chatbot, doc_source1, source1_page, doc_source2, source2_page], \queue=False)# Chatbot eventsmsg.submit(conversation, \inputs=[msg, chatbot], \outputs=[msg, chatbot, doc_source1, source1_page, doc_source2, source2_page], \queue=False)submit_btn.click(conversation, \inputs=[msg, chatbot], \outputs=[msg, chatbot, doc_source1, source1_page, doc_source2, source2_page], \queue=False)clear_btn.click(lambda:[None,"",0,"",0], \inputs=None, \outputs=[chatbot, doc_source1, source1_page, doc_source2, source2_page], \queue=False)demo.queue().launch(debug=True)
app.py+44-11