Commit ·
ea0dc72
1
Parent(s): e65eb2d
Update
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ from pdfminer.high_level import extract_pages
|
|
| 2 |
from pdfminer.layout import LTTextContainer
|
| 3 |
from tqdm import tqdm
|
| 4 |
import re
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
import os
|
| 7 |
from llama_cpp import Llama
|
|
@@ -12,8 +13,6 @@ import transformers
|
|
| 12 |
import accelerate
|
| 13 |
import torch
|
| 14 |
|
| 15 |
-
# HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
| 16 |
-
|
| 17 |
def process_document(pdf_path, page_ids=None):
|
| 18 |
|
| 19 |
extracted_pages = extract_pages(pdf_path, page_numbers=page_ids)
|
|
@@ -116,48 +115,42 @@ def deidentify_doc(pdftext="", prompt="", maxtokens=600, temperature=0.0001, top
|
|
| 116 |
|
| 117 |
return output
|
| 118 |
|
| 119 |
-
def
|
| 120 |
-
|
| 121 |
-
os.makedirs(dir)
|
| 122 |
-
|
| 123 |
-
def pdf_to_text(files, output_folder, prompt="", maxtokens=600, temperature=1.2, top_probability=0.95):
|
| 124 |
-
if not output_folder:
|
| 125 |
-
output_folder='C:/'
|
| 126 |
-
output_folder = output_folder.replace('\\','/')
|
| 127 |
for file in files:
|
| 128 |
file_name = os.path.basename(file)
|
| 129 |
file_name_splt = file_name.split('.')
|
| 130 |
print('File name is ', file_name)
|
| 131 |
-
print('output folder is ', output_folder)
|
| 132 |
if(len(file_name_splt)>1 and file_name_splt[1]=='pdf'):
|
| 133 |
page2content = process_document(file, page_ids=[0])
|
| 134 |
pdftext = page2content[1]
|
| 135 |
if(pdftext):
|
| 136 |
anonymized_text = deidentify_doc(pdftext, prompt, maxtokens, temperature, top_probability)
|
| 137 |
-
filename = file_name_splt[0]+'.txt'
|
| 138 |
-
filename = filename.replace(' ','')
|
| 139 |
# html = txt_to_html(display_text)
|
| 140 |
# with open('out.html', "w", encoding="utf-8") as file:
|
| 141 |
# file.write(html)
|
| 142 |
-
with open(
|
| 143 |
# Write some text to the file
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
|
| 151 |
# model = GPT4All("Meta-Llama-3-8B-Instruct.Q4_0.gguf", n_threads=8, device='gpu')
|
| 152 |
# model.chat_session()
|
| 153 |
|
| 154 |
-
model_id = "Meta-Llama-3-8B-Instruct"
|
| 155 |
-
model = transformers.pipeline(
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
)
|
| 161 |
|
| 162 |
temp_slider = gr.Slider(minimum=0, maximum=2, value=0.2, label="Temperature Value")
|
| 163 |
prob_slider = gr.Slider(minimum=0, maximum=1, value=0.95, label="Max Probability Value")
|
|
@@ -167,8 +160,8 @@ output_text = gr.Textbox()
|
|
| 167 |
output_path_component = gr.File(label="Select Output Path")
|
| 168 |
iface = gr.Interface(
|
| 169 |
fn = pdf_to_text,
|
| 170 |
-
inputs = ['files'
|
| 171 |
-
outputs=
|
| 172 |
title='COBIx Endoscopy Report De-Identification',
|
| 173 |
description="This application assists to remove personal information from the uploaded clinical report",
|
| 174 |
theme=gr.themes.Soft(),
|
|
|
|
| 2 |
from pdfminer.layout import LTTextContainer
|
| 3 |
from tqdm import tqdm
|
| 4 |
import re
|
| 5 |
+
from zipfile import ZipFile
|
| 6 |
import gradio as gr
|
| 7 |
import os
|
| 8 |
from llama_cpp import Llama
|
|
|
|
| 13 |
import accelerate
|
| 14 |
import torch
|
| 15 |
|
|
|
|
|
|
|
| 16 |
def process_document(pdf_path, page_ids=None):
|
| 17 |
|
| 18 |
extracted_pages = extract_pages(pdf_path, page_numbers=page_ids)
|
|
|
|
| 115 |
|
| 116 |
return output
|
| 117 |
|
| 118 |
+
def pdf_to_text(files, prompt="", maxtokens=600, temperature=1.2, top_probability=0.95):
|
| 119 |
+
output_files = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
for file in files:
|
| 121 |
file_name = os.path.basename(file)
|
| 122 |
file_name_splt = file_name.split('.')
|
| 123 |
print('File name is ', file_name)
|
|
|
|
| 124 |
if(len(file_name_splt)>1 and file_name_splt[1]=='pdf'):
|
| 125 |
page2content = process_document(file, page_ids=[0])
|
| 126 |
pdftext = page2content[1]
|
| 127 |
if(pdftext):
|
| 128 |
anonymized_text = deidentify_doc(pdftext, prompt, maxtokens, temperature, top_probability)
|
|
|
|
|
|
|
| 129 |
# html = txt_to_html(display_text)
|
| 130 |
# with open('out.html', "w", encoding="utf-8") as file:
|
| 131 |
# file.write(html)
|
| 132 |
+
with open(file_name_splt[0]+'.txt', 'w') as outputfile:
|
| 133 |
# Write some text to the file
|
| 134 |
+
outputfile.write(anonymized_text)
|
| 135 |
+
output_files.append(outputfile)
|
| 136 |
+
zipf = ZipFile('anonymized_reports', 'w')
|
| 137 |
+
for file in files:
|
| 138 |
+
zipf.write(file, os.path.basename(file))
|
| 139 |
+
return 'anonymized_reports'
|
| 140 |
|
| 141 |
+
model_id = "Meta-Llama-3-8B-Instruct.Q5_K_M.gguf"
|
| 142 |
+
model = Llama(model_path=model_id, n_ctx=2048, n_threads=8, n_gpu_layers=32, n_batch=64)
|
| 143 |
|
| 144 |
# model = GPT4All("Meta-Llama-3-8B-Instruct.Q4_0.gguf", n_threads=8, device='gpu')
|
| 145 |
# model.chat_session()
|
| 146 |
|
| 147 |
+
# model_id = "D:/llama/meta-llama/Meta-Llama-3-8B-Instruct"
|
| 148 |
+
# model = transformers.pipeline(
|
| 149 |
+
# "text-generation",
|
| 150 |
+
# model=model_id,
|
| 151 |
+
# model_kwargs={"torch_dtype": torch.bfloat16},
|
| 152 |
+
# device="cpu",
|
| 153 |
+
# )
|
| 154 |
|
| 155 |
temp_slider = gr.Slider(minimum=0, maximum=2, value=0.2, label="Temperature Value")
|
| 156 |
prob_slider = gr.Slider(minimum=0, maximum=1, value=0.95, label="Max Probability Value")
|
|
|
|
| 160 |
output_path_component = gr.File(label="Select Output Path")
|
| 161 |
iface = gr.Interface(
|
| 162 |
fn = pdf_to_text,
|
| 163 |
+
inputs = ['files'],
|
| 164 |
+
outputs='file',
|
| 165 |
title='COBIx Endoscopy Report De-Identification',
|
| 166 |
description="This application assists to remove personal information from the uploaded clinical report",
|
| 167 |
theme=gr.themes.Soft(),
|