Commit ·
3e5cc1b
1
Parent(s): aa75b1b
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,6 @@ subprocess.run('pip install llama-cpp-agent==0.2.10', shell=True)
|
|
| 13 |
|
| 14 |
from llama_cpp import Llama
|
| 15 |
|
| 16 |
-
|
| 17 |
def process_document(pdf_path, page_ids=None):
|
| 18 |
extracted_pages = extract_pages(pdf_path, page_numbers=page_ids)
|
| 19 |
|
|
@@ -65,8 +64,10 @@ def txt_to_html(text):
|
|
| 65 |
return html_content
|
| 66 |
|
| 67 |
|
| 68 |
-
def deidentify_doc(pdftext
|
| 69 |
-
prompt = "Perform the following actions on given text: 1. Replace any person names with term [redacted] 2. Replace any person addresses with term [redacted]"
|
|
|
|
|
|
|
| 70 |
output = model.create_chat_completion(
|
| 71 |
messages=[
|
| 72 |
{"role": "assistant", "content": prompt},
|
|
@@ -80,32 +81,32 @@ def deidentify_doc(pdftext="", maxtokens=600, temperature=1.2, top_probability=0
|
|
| 80 |
)
|
| 81 |
output = output['choices'][0]['message']['content']
|
| 82 |
|
| 83 |
-
print(prompt)
|
| 84 |
-
print(output)
|
| 85 |
-
print('-------------------------------------------------------')
|
| 86 |
-
|
| 87 |
-
prompt = "Perform the following actions on given text: 1. Replace NHS number and Case note number and Date of Birth with term [redacted] 2. Replace only the CALENDAR DATES of format 'day/month/year' with term [redacted] 3. DO NOT REPLACE ANY MEDICAL MEASUREMENTS"
|
| 88 |
-
output = model.create_chat_completion(
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
)
|
| 99 |
-
output = output['choices'][0]['message']['content']
|
| 100 |
-
|
| 101 |
-
print(prompt)
|
| 102 |
-
print(output)
|
| 103 |
-
print('-------------------------------------------------------')
|
| 104 |
|
| 105 |
return output
|
| 106 |
|
| 107 |
@spaces.GPU(duration=120)
|
| 108 |
-
def pdf_to_text(files, output_folder, maxtokens=2048, temperature=0, top_probability=0.95):
|
| 109 |
files=[files]#remove later
|
| 110 |
for file in files:
|
| 111 |
file_name = os.path.basename(file)
|
|
@@ -117,11 +118,12 @@ def pdf_to_text(files, output_folder, maxtokens=2048, temperature=0, top_probabi
|
|
| 117 |
pdftext = page2content[1]
|
| 118 |
# pdftext = file # remove later
|
| 119 |
if (pdftext): #shift this if block to right later
|
| 120 |
-
anonymized_text = deidentify_doc(pdftext, maxtokens, temperature, top_probability)
|
| 121 |
return anonymized_text
|
| 122 |
|
| 123 |
|
| 124 |
-
model_id = "Meta-Llama-3-8B-Instruct.Q8_0.gguf"
|
|
|
|
| 125 |
model = Llama(model_path=model_id, n_ctx=2048, n_threads=8, n_gpu_layers=-1, n_batch=128)
|
| 126 |
|
| 127 |
css = ".gradio-container {background: 'logo.png'}"
|
|
@@ -134,7 +136,7 @@ output_text = gr.Textbox()
|
|
| 134 |
output_path_component = gr.File(label="Select Output Path")
|
| 135 |
iface = gr.Interface(
|
| 136 |
fn=pdf_to_text,
|
| 137 |
-
inputs=['file', input_folder_text],
|
| 138 |
# inputs=["textbox", input_folder_text, "textbox", max_tokens, temp_slider, prob_slider],
|
| 139 |
outputs=output_text,
|
| 140 |
title='COBIx Endoscopy Report De-Identification',
|
|
|
|
| 13 |
|
| 14 |
from llama_cpp import Llama
|
| 15 |
|
|
|
|
| 16 |
def process_document(pdf_path, page_ids=None):
|
| 17 |
extracted_pages = extract_pages(pdf_path, page_numbers=page_ids)
|
| 18 |
|
|
|
|
| 64 |
return html_content
|
| 65 |
|
| 66 |
|
| 67 |
+
def deidentify_doc(pdftext, prompt, maxtokens, temperature, top_probability):
|
| 68 |
+
# prompt = "Perform the following actions on given text: 1. Replace any person names with term [redacted] 2. Replace any person addresses with term [redacted]"
|
| 69 |
+
print('Prompt is ',prompt)
|
| 70 |
+
print('-------------------------------------------')
|
| 71 |
output = model.create_chat_completion(
|
| 72 |
messages=[
|
| 73 |
{"role": "assistant", "content": prompt},
|
|
|
|
| 81 |
)
|
| 82 |
output = output['choices'][0]['message']['content']
|
| 83 |
|
| 84 |
+
# print(prompt)
|
| 85 |
+
# print(output)
|
| 86 |
+
# print('-------------------------------------------------------')
|
| 87 |
+
|
| 88 |
+
# prompt = "Perform the following actions on given text: 1. Replace NHS number and Case note number and Date of Birth with term [redacted] 2. Replace only the CALENDAR DATES of format 'day/month/year' with term [redacted] 3. DO NOT REPLACE ANY MEDICAL MEASUREMENTS"
|
| 89 |
+
# output = model.create_chat_completion(
|
| 90 |
+
# messages=[
|
| 91 |
+
# {"role": "assistant", "content": prompt},
|
| 92 |
+
# {
|
| 93 |
+
# "role": "user",
|
| 94 |
+
# "content": output
|
| 95 |
+
# }
|
| 96 |
+
# ],
|
| 97 |
+
# max_tokens=maxtokens,
|
| 98 |
+
# temperature=temperature
|
| 99 |
+
# )
|
| 100 |
+
# output = output['choices'][0]['message']['content']
|
| 101 |
+
|
| 102 |
+
# print(prompt)
|
| 103 |
+
# print(output)
|
| 104 |
+
# print('-------------------------------------------------------')
|
| 105 |
|
| 106 |
return output
|
| 107 |
|
| 108 |
@spaces.GPU(duration=120)
|
| 109 |
+
def pdf_to_text(files, output_folder, prompt, maxtokens=2048, temperature=0, top_probability=0.95):
|
| 110 |
files=[files]#remove later
|
| 111 |
for file in files:
|
| 112 |
file_name = os.path.basename(file)
|
|
|
|
| 118 |
pdftext = page2content[1]
|
| 119 |
# pdftext = file # remove later
|
| 120 |
if (pdftext): #shift this if block to right later
|
| 121 |
+
anonymized_text = deidentify_doc(pdftext, prompt, maxtokens, temperature, top_probability)
|
| 122 |
return anonymized_text
|
| 123 |
|
| 124 |
|
| 125 |
+
# model_id = "Meta-Llama-3-8B-Instruct.Q8_0.gguf"
|
| 126 |
+
model_id = "Meta-Llama-3-70B-Instruct-IQ3_XXS.gguf"
|
| 127 |
model = Llama(model_path=model_id, n_ctx=2048, n_threads=8, n_gpu_layers=-1, n_batch=128)
|
| 128 |
|
| 129 |
css = ".gradio-container {background: 'logo.png'}"
|
|
|
|
| 136 |
output_path_component = gr.File(label="Select Output Path")
|
| 137 |
iface = gr.Interface(
|
| 138 |
fn=pdf_to_text,
|
| 139 |
+
inputs=['file', input_folder_text, "textbox"],
|
| 140 |
# inputs=["textbox", input_folder_text, "textbox", max_tokens, temp_slider, prob_slider],
|
| 141 |
outputs=output_text,
|
| 142 |
title='COBIx Endoscopy Report De-Identification',
|