Spaces:
Build error
Build error
Update app.py
Browse files19 unmodified lines202122232425262724 unmodified lines5253545556575859606116 unmodified lines7879808182838485868788899091929394959697989910010110210310410510610756 unmodified lines16416516616716816917028 unmodified lines19920020120220320420520620720820921019 unmodified linesimport tqdmimport acceleratedefault_persist_directory = './chromaDB/'llm_name0 = "mistralai/Mixtral-8x7B-Instruct-v0.1"24 unmodified linesvectordb = Chroma.from_documents(documents=splits,embedding=embedding,client=new_client,collection_name=collection_name,persist_directory=default_persist_directory)return vectordb16 unmodified linesllm = HuggingFaceHub(repo_id=llm_model,model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k, "load_in_8bit": True})elif llm_model == "microsoft/phi-2":raise gr.Error("phi-2 model requires 'trust_remote_code=True', currently not supported by langchain HuggingFaceHub...")llm = HuggingFaceHub(repo_id=llm_model,model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k, "trust_remote_code": True, "torch_dtype": "auto"})elif llm_model == "TinyLlama/TinyLlama-1.1B-Chat-v1.0":llm = HuggingFaceHub(repo_id=llm_model,model_kwargs={"temperature": temperature, "max_new_tokens": 250, "top_k": top_k})elif llm_model == "meta-llama/Llama-2-7b-chat-hf":raise gr.Error("Llama-2-7b-chat-hf model requires a Pro subscription...")llm = HuggingFaceHub(repo_id=llm_model,model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k})else:llm = HuggingFaceHub(repo_id=llm_model,# model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k, "trust_remote_code": True, "torch_dtype": "auto"}model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k})progress(0.75, desc="Defining buffer memory...")memory = ConversationBufferMemory(56 unmodified linesdef conversation(qa_chain, message, history):formatted_chat_history = format_chat_history(message, history)#print("formatted_chat_history",formatted_chat_history)# Generate response using QA chainresponse = qa_chain({"question": message, "chat_history": formatted_chat_history})response_answer = response["answer"]28 unmodified linescollection_name = gr.State()gr.Markdown("""<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 an output.<br>""")with gr.Tab("Step 1 - Document pre-processing"):with gr.Row():document = gr.Files(height=100, file_count="multiple", file_types=["pdf"], interactive=True, label="Upload your PDF documents (single or multiple)")19 unmodified lines2021222324252624 unmodified lines51525354555657585916 unmodified lines7677787980818256 unmodified lines13914014114214314414528 unmodified lines17417517617717817918019 unmodified linesimport tqdmimport acceleratedefault_persist_directory = './ChromaDB'llm_name0 = "mistralai/Mixtral-8x7B-Instruct-v0.1"24 unmodified linesvectordb = Chroma.from_documents(documents=splits,embedding=embedding,persist_directory="./chroma_db",client=new_client,collection_name=collection_name)return vectordb16 unmodified linesllm = HuggingFaceHub(repo_id=llm_model,model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k, "load_in_8bit": True})progress(0.75, desc="Defining buffer memory...")memory = ConversationBufferMemory(56 unmodified linesdef conversation(qa_chain, message, history):formatted_chat_history = format_chat_history(message, history)#print("formatted_chat_history",formatted_chat_history)formatted_chat_history = ""# Generate response using QA chainresponse = qa_chain({"question": message, "chat_history": formatted_chat_history})response_answer = response["answer"]28 unmodified linescollection_name = gr.State()gr.Markdown("""<center><h2>ChatPDF</center></h2>""")with gr.Tab("Step 1 - Document pre-processing"):with gr.Row():document = gr.Files(height=100, file_count="multiple", file_types=["pdf"], interactive=True, label="Upload your PDF documents (single or multiple)")
app.py+6-36