Commit ·
1841cd8
1
Parent(s): 57f5c2b
Update app.py
Browse files17 unmodified lines18192021222324252627282930313233343547 unmodified lines838485868788892 unmodified lines92939495969798991003 unmodified lines1041051061071081091103 unmodified lines1141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751762 unmodified lines17918018118218318418517 unmodified lines# subprocess.run('pip install llama-cpp-agent==0.2.10', shell=True)# hf_hub_download(# repo_id="QuantFactory/Meta-Llama-3-8B-Instruct-GGUF",# filename="Meta-Llama-3-8B-Instruct.Q8_0.gguf",# local_dir = "./models"# )hf_hub_download(repo_id="bartowski/Meta-Llama-3-70B-Instruct-GGUF",filename="Meta-Llama-3-70B-Instruct-Q3_K_M.gguf",local_dir = "./models")def process_document(pdf_path, page_ids=None):extracted_pages = extract_pages(pdf_path, page_numbers=page_ids)47 unmodified lines@spaces.GPU(duration=120)def deidentify_doc(pdftext, maxtokens, temperature, top_probability):# prompt = "In the following text replace any person name and any address with term [redacted], replace any Date of Birth and NHS number with term [redacted]"# prompt = """# Perform the following actions on given report:# 1. Replace any person names, age, date of birth, gender with term [redacted]2 unmodified lines# 4. Replace only the CALENDAR DATES of format 'day/month/year' with term [redacted]# """prompt = """Remove person names, any dates, date of birth, age, gender, registration numbers and any addresses from the following report."""# model = Llama(# model_path=model_id,3 unmodified lines# n_ctx=8192,# )chat_template = MessagesFormatterType.LLAMA_3llm = Llama(model_path="models/Meta-Llama-3-70B-Instruct-Q3_K_M.gguf",3 unmodified linesn_ctx=8192,)provider = LlamaCppPythonProvider(llm)agent = LlamaCppAgent(provider,system_prompt="You are a helpful assistant.",predefined_messages_formatter_type=chat_template,debug_output=True)settings = provider.get_provider_default_settings()settings.temperature = temperaturesettings.top_k = 40settings.top_p = top_probabilitysettings.max_tokens = maxtokenssettings.repeat_penalty = 1.1settings.stream = Truemessages = BasicChatHistory()stream = agent.get_chat_response(prompt + ' : ' + pdftext,llm_sampling_settings=settings,chat_history=messages,returns_streaming_generator=True,print_output=False)output = ""for op in stream:output += op# output = llm.create_chat_completion(# messages=[# {"role": "assistant", "content": prompt},# {# "role": "user",# "content": pdftext# }# ],# max_tokens=maxtokens,# temperature=temperature# )# output = output['choices'][0]['message']['content']# prompt = "Perform the following actions on given text: 1. Replace any person age with term [redacted] 2. DO NOT REPLACE ANY MEDICAL MEASUREMENTS 3. Replace only the CALENDAR DATES of format 'day/month/year' with term [redacted]"# output = model.create_chat_completion(# messages=[# {"role": "assistant", "content": prompt},# {# "role": "user",# "content": output# }# ],# max_tokens=maxtokens,# temperature=temperature# )# output = output['choices'][0]['message']['content']# print(prompt)# print(output)2 unmodified linesreturn outputdef pdf_to_text(files, maxtokens=2048, temperature=0, top_probability=0.95):print('Control 0-----------------------------------')files=[files]#remove laterfor file in files:file_name = os.path.basename(file)17 unmodified lines18192021222324252627282930313233343547 unmodified lines83848586878889902 unmodified lines939495969798991001013 unmodified lines1051061071081091101113 unmodified lines1151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761772 unmodified lines18018118218318418517 unmodified lines# subprocess.run('pip install llama-cpp-agent==0.2.10', shell=True)hf_hub_download(repo_id="QuantFactory/Meta-Llama-3-8B-Instruct-GGUF",filename="Meta-Llama-3-8B-Instruct.Q8_0.gguf",local_dir = "./models")# hf_hub_download(# repo_id="bartowski/Meta-Llama-3-70B-Instruct-GGUF",# filename="Meta-Llama-3-70B-Instruct-Q3_K_M.gguf",# local_dir = "./models"# )def process_document(pdf_path, page_ids=None):extracted_pages = extract_pages(pdf_path, page_numbers=page_ids)47 unmodified lines@spaces.GPU(duration=120)def deidentify_doc(pdftext, maxtokens, temperature, top_probability):prompt = "In the following text replace any person name and any address with term [redacted], replace any Date of Birth and NHS number with term [redacted]"# prompt = """# Perform the following actions on given report:# 1. Replace any person names, age, date of birth, gender with term [redacted]2 unmodified lines# 4. Replace only the CALENDAR DATES of format 'day/month/year' with term [redacted]# """# prompt = """# Remove person names, any dates, date of birth, age, gender, registration numbers and any addresses from the following report.# """# model = Llama(# model_path=model_id,3 unmodified lines# n_ctx=8192,# )# chat_template = MessagesFormatterType.LLAMA_3llm = Llama(model_path="models/Meta-Llama-3-70B-Instruct-Q3_K_M.gguf",3 unmodified linesn_ctx=8192,)# provider = LlamaCppPythonProvider(llm)# agent = LlamaCppAgent(# provider,# system_prompt="You are a helpful assistant.",# predefined_messages_formatter_type=chat_template,# debug_output=True# )# settings = provider.get_provider_default_settings()# settings.temperature = temperature# settings.top_k = 40# settings.top_p = top_probability# settings.max_tokens = maxtokens# settings.repeat_penalty = 1.1# settings.stream = True# messages = BasicChatHistory()# stream = agent.get_chat_response(# prompt + ' : ' + pdftext,# llm_sampling_settings=settings,# chat_history=messages,# returns_streaming_generator=True,# print_output=False# )# output = ""# for op in stream:# output += opoutput = llm.create_chat_completion(messages=[{"role": "assistant", "content": prompt},{"role": "user","content": pdftext}],max_tokens=maxtokens,temperature=temperature)output = output['choices'][0]['message']['content']prompt = "Perform the following actions on given text: 1. Replace any person age with term [redacted] 2. DO NOT REPLACE ANY MEDICAL MEASUREMENTS 3. Replace only the CALENDAR DATES of format 'day/month/year' with term [redacted]"output = model.create_chat_completion(messages=[{"role": "assistant", "content": prompt},{"role": "user","content": output}],max_tokens=maxtokens,temperature=temperature)output = output['choices'][0]['message']['content']# print(prompt)# print(output)2 unmodified linesreturn outputdef pdf_to_text(files, maxtokens=2048, temperature=0, top_probability=0.95):files=[files]#remove laterfor file in files:file_name = os.path.basename(file)
app.py+67-67