Spaces:
Build error
Build error
clementsan commited on
Commit ·
00bd139
1
Parent(s): ceae871
Update qa_chain to gradio session state
Browse files106 unmodified lines1071081091101111121135 unmodified lines1191201211221231241251261271288 unmodified lines1371381391401411421431441451461471481491501511524 unmodified lines15715815916016116216312 unmodified lines1761771781791801811829 unmodified lines19219319419519619719853 unmodified lines252253254255256257258259260261262263264265266267268269270106 unmodified lines# retriever=vector_db.as_retriever(search_type="similarity", search_kwargs={'k': 3})retriever=vector_db.as_retriever()progress(0.8, desc="Defining retrieval chain...")global qa_chainqa_chain = ConversationalRetrievalChain.from_llm(llm,retriever=retriever,5 unmodified lines# verbose=True,)progress(0.9, desc="Done!")# return qa_chain# Initialize all elementsdef initialize_database(list_file_obj, chunk_size, chunk_overlap, progress=gr.Progress()):# Create list of documents (when valid)#file_path = file_obj.name8 unmodified linesvector_db = create_db(doc_splits)progress(0.9, desc="Done!")return vector_db, "Complete!"#return qa_chaindef initialize_LLM(llm_option, llm_temperature, max_tokens, top_k, vector_db, progress=gr.Progress()):print("llm_option",llm_option)llm_name = list_llm[llm_option]print("llm_name",llm_name)initialize_llmchain(llm_name, llm_temperature, max_tokens, top_k, vector_db, progress)return "Complete!"#return qa_chaindef format_chat_history(message, chat_history):4 unmodified linesreturn formatted_chat_historydef conversation(message, history):formatted_chat_history = format_chat_history(message, history)#print("formatted_chat_history",formatted_chat_history)12 unmodified lines# 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):9 unmodified linesdef demo():with gr.Blocks(theme="base") as demo:vector_db = gr.State()# qa_chain = gr.Variable()gr.Markdown("""<center><h2>PDF-based chatbot (powered by LangChain and open-source LLMs)</center></h2>53 unmodified linesoutputs=[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, \106 unmodified lines1071081091101111125 unmodified lines1181191201211221231241251261278 unmodified lines1361371381391401411421431441451461471481494 unmodified lines15415515615715815916012 unmodified lines1731741751761771781799 unmodified lines18919019119219319419553 unmodified lines249250251252253254255256257258259260261262263264265266267106 unmodified lines# retriever=vector_db.as_retriever(search_type="similarity", search_kwargs={'k': 3})retriever=vector_db.as_retriever()progress(0.8, desc="Defining retrieval chain...")qa_chain = ConversationalRetrievalChain.from_llm(llm,retriever=retriever,5 unmodified lines# verbose=True,)progress(0.9, desc="Done!")return qa_chain# Initialize databasedef initialize_database(list_file_obj, chunk_size, chunk_overlap, progress=gr.Progress()):# Create list of documents (when valid)#file_path = file_obj.name8 unmodified linesvector_db = create_db(doc_splits)progress(0.9, desc="Done!")return vector_db, "Complete!"def initialize_LLM(llm_option, llm_temperature, max_tokens, top_k, vector_db, progress=gr.Progress()):print("llm_option",llm_option)llm_name = list_llm[llm_option]print("llm_name",llm_name)qa_chain = initialize_llmchain(llm_name, llm_temperature, max_tokens, top_k, vector_db, progress)return qa_chain, "Complete!"def format_chat_history(message, chat_history):4 unmodified linesreturn formatted_chat_historydef conversation(qa_chain, message, history):formatted_chat_history = format_chat_history(message, history)#print("formatted_chat_history",formatted_chat_history)12 unmodified lines# 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 qa_chain, gr.update(value=""), new_history, response_source1, response_source1_page, response_source2, response_source2_pagedef upload_file(file_obj):9 unmodified linesdef demo():with gr.Blocks(theme="base") as demo:vector_db = gr.State()qa_chain = gr.State()gr.Markdown("""<center><h2>PDF-based chatbot (powered by LangChain and open-source LLMs)</center></h2>53 unmodified linesoutputs=[vector_db, db_progress])qachain_btn.click(initialize_LLM, \inputs=[llm_btn, slider_temperature, slider_maxtokens, slider_topk, vector_db], \outputs=[qa_chain, 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=[qa_chain, msg, chatbot], \outputs=[qa_chain, msg, chatbot, doc_source1, source1_page, doc_source2, source2_page], \queue=False)submit_btn.click(conversation, \inputs=[qa_chain, msg, chatbot], \outputs=[qa_chain, msg, chatbot, doc_source1, source1_page, doc_source2, source2_page], \queue=False)clear_btn.click(lambda:[None,"",0,"",0], \inputs=None, \
app.py+12-15