Spaces:
Running
Running
Clement Vachet commited on
Commit ·
577e81d
1
Parent(s): 799497c
style: clean code
Browse files35 unmodified lines363738394041424344454647484927 unmodified lines7778798081828384858687888990919293949596979899100101102165 unmodified lines26826927027127227327427527627727827928028128228313 unmodified lines29729829930030130230343 unmodified lines34734834935035135235335 unmodified lines# Load PDF document and create doc splitsdef load_doc(list_file_path, chunk_size, chunk_overlap):# Processing for one document only# loader = PyPDFLoader(file_path)# pages = loader.load()loaders = [PyPDFLoader(x) for x in list_file_path]pages = []for loader in loaders:pages.extend(loader.load())# text_splitter = RecursiveCharacterTextSplitter(chunk_size = 600, chunk_overlap = 50)text_splitter = RecursiveCharacterTextSplitter(chunk_size = chunk_size,chunk_overlap = chunk_overlap)27 unmodified lines# Initialize langchain LLM chaindef initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db, progress=gr.Progress()):progress(0.1, desc="Initializing HF tokenizer...")# HuggingFacePipeline uses local model# Note: it will download model locally...# tokenizer=AutoTokenizer.from_pretrained(llm_model)# progress(0.5, desc="Initializing HF pipeline...")# pipeline=transformers.pipeline(# "text-generation",# model=llm_model,# tokenizer=tokenizer,# torch_dtype=torch.bfloat16,# trust_remote_code=True,# device_map="auto",# # max_length=1024,# max_new_tokens=max_tokens,# do_sample=True,# top_k=top_k,# num_return_sequences=1,# eos_token_id=tokenizer.eos_token_id# )# llm = HuggingFacePipeline(pipeline=pipeline, model_kwargs={'temperature': temperature})# HuggingFaceHub uses HF inference endpointsprogress(0.5, desc="Initializing HF Hub...")# Use of trust_remote_code as model_kwargs165 unmodified linesreturn qa_chain, gr.update(value=""), new_history, response_source1, response_source1_page, response_source2, response_source2_page, response_source3, response_source3_pagedef upload_file(file_obj):list_file_path = []for idx, file in enumerate(file_obj):file_path = file_obj.namelist_file_path.append(file_path)# print(file_path)# initialize_database(file_path, progress)return list_file_pathdef demo():with gr.Blocks(theme="base") as demo:vector_db = gr.State()13 unmodified lineswith gr.Tab("Step 1 - Upload PDF"):with gr.Row():document = gr.File(height=200, file_count="multiple", file_types=[".pdf"], interactive=True, label="Upload your PDF documents (single or multiple)")# upload_btn = gr.UploadButton("Loading document...", height=100, file_count="multiple", file_types=["pdf"], scale=1)with gr.Tab("Step 2 - Process document"):with gr.Row():43 unmodified linesclear_btn = gr.ClearButton(components=[msg, chatbot], value="Clear conversation")# 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, collection_name, db_progress])35 unmodified lines3637383940414243444527 unmodified lines737475767778165 unmodified lines24424524624724824913 unmodified lines26326426526626726843 unmodified lines31231331431531631735 unmodified lines# Load PDF document and create doc splitsdef load_doc(list_file_path, chunk_size, chunk_overlap):loaders = [PyPDFLoader(x) for x in list_file_path]pages = []for loader in loaders:pages.extend(loader.load())text_splitter = RecursiveCharacterTextSplitter(chunk_size = chunk_size,chunk_overlap = chunk_overlap)27 unmodified lines# Initialize langchain LLM chaindef initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db, progress=gr.Progress()):progress(0.1, desc="Initializing HF tokenizer...")# HuggingFaceHub uses HF inference endpointsprogress(0.5, desc="Initializing HF Hub...")# Use of trust_remote_code as model_kwargs165 unmodified linesreturn qa_chain, gr.update(value=""), new_history, response_source1, response_source1_page, response_source2, response_source2_page, response_source3, response_source3_pagedef demo():with gr.Blocks(theme="base") as demo:vector_db = gr.State()13 unmodified lineswith gr.Tab("Step 1 - Upload PDF"):with gr.Row():document = gr.File(height=200, file_count="multiple", file_types=[".pdf"], interactive=True, label="Upload your PDF documents (single or multiple)")with gr.Tab("Step 2 - Process document"):with gr.Row():43 unmodified linesclear_btn = gr.ClearButton(components=[msg, chatbot], value="Clear conversation")# Preprocessing eventsdb_btn.click(initialize_database, \inputs=[document, slider_chunk_size, slider_chunk_overlap], \outputs=[vector_db, collection_name, db_progress])
app.py-36