Commit ·
1841cd8
1
Parent(s): 57f5c2b
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,18 +18,18 @@ from llama_cpp_agent.chat_history.messages import Roles
|
|
| 18 |
# subprocess.run('pip install llama-cpp-agent==0.2.10', shell=True)
|
| 19 |
|
| 20 |
|
| 21 |
-
# hf_hub_download(
|
| 22 |
-
# repo_id="QuantFactory/Meta-Llama-3-8B-Instruct-GGUF",
|
| 23 |
-
# filename="Meta-Llama-3-8B-Instruct.Q8_0.gguf",
|
| 24 |
-
# local_dir = "./models"
|
| 25 |
-
# )
|
| 26 |
-
|
| 27 |
hf_hub_download(
|
| 28 |
-
repo_id="
|
| 29 |
-
filename="Meta-Llama-3-
|
| 30 |
local_dir = "./models"
|
| 31 |
)
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
def process_document(pdf_path, page_ids=None):
|
| 34 |
extracted_pages = extract_pages(pdf_path, page_numbers=page_ids)
|
| 35 |
|
|
@@ -83,7 +83,8 @@ def txt_to_html(text):
|
|
| 83 |
@spaces.GPU(duration=120)
|
| 84 |
def deidentify_doc(pdftext, maxtokens, temperature, top_probability):
|
| 85 |
|
| 86 |
-
|
|
|
|
| 87 |
# prompt = """
|
| 88 |
# Perform the following actions on given report:
|
| 89 |
# 1. Replace any person names, age, date of birth, gender with term [redacted]
|
|
@@ -92,9 +93,9 @@ def deidentify_doc(pdftext, maxtokens, temperature, top_probability):
|
|
| 92 |
# 4. Replace only the CALENDAR DATES of format 'day/month/year' with term [redacted]
|
| 93 |
# """
|
| 94 |
|
| 95 |
-
prompt = """
|
| 96 |
-
|
| 97 |
-
|
| 98 |
|
| 99 |
# model = Llama(
|
| 100 |
# model_path=model_id,
|
|
@@ -104,7 +105,7 @@ def deidentify_doc(pdftext, maxtokens, temperature, top_probability):
|
|
| 104 |
# n_ctx=8192,
|
| 105 |
# )
|
| 106 |
|
| 107 |
-
chat_template = MessagesFormatterType.LLAMA_3
|
| 108 |
|
| 109 |
llm = Llama(
|
| 110 |
model_path="models/Meta-Llama-3-70B-Instruct-Q3_K_M.gguf",
|
|
@@ -114,63 +115,63 @@ def deidentify_doc(pdftext, maxtokens, temperature, top_probability):
|
|
| 114 |
n_ctx=8192,
|
| 115 |
)
|
| 116 |
|
| 117 |
-
provider = LlamaCppPythonProvider(llm)
|
| 118 |
|
| 119 |
-
agent = LlamaCppAgent(
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
)
|
| 125 |
|
| 126 |
-
settings = provider.get_provider_default_settings()
|
| 127 |
-
settings.temperature = temperature
|
| 128 |
-
settings.top_k = 40
|
| 129 |
-
settings.top_p = top_probability
|
| 130 |
-
settings.max_tokens = maxtokens
|
| 131 |
-
settings.repeat_penalty = 1.1
|
| 132 |
-
settings.stream = True
|
| 133 |
-
|
| 134 |
-
messages = BasicChatHistory()
|
| 135 |
-
|
| 136 |
-
stream = agent.get_chat_response(
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
)
|
| 143 |
|
| 144 |
-
output = ""
|
| 145 |
-
for op in stream:
|
| 146 |
-
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
|
| 175 |
# print(prompt)
|
| 176 |
# print(output)
|
|
@@ -179,7 +180,6 @@ def deidentify_doc(pdftext, maxtokens, temperature, top_probability):
|
|
| 179 |
return output
|
| 180 |
|
| 181 |
def pdf_to_text(files, maxtokens=2048, temperature=0, top_probability=0.95):
|
| 182 |
-
print('Control 0-----------------------------------')
|
| 183 |
files=[files]#remove later
|
| 184 |
for file in files:
|
| 185 |
file_name = os.path.basename(file)
|
|
|
|
| 18 |
# subprocess.run('pip install llama-cpp-agent==0.2.10', shell=True)
|
| 19 |
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
hf_hub_download(
|
| 22 |
+
repo_id="QuantFactory/Meta-Llama-3-8B-Instruct-GGUF",
|
| 23 |
+
filename="Meta-Llama-3-8B-Instruct.Q8_0.gguf",
|
| 24 |
local_dir = "./models"
|
| 25 |
)
|
| 26 |
|
| 27 |
+
# hf_hub_download(
|
| 28 |
+
# repo_id="bartowski/Meta-Llama-3-70B-Instruct-GGUF",
|
| 29 |
+
# filename="Meta-Llama-3-70B-Instruct-Q3_K_M.gguf",
|
| 30 |
+
# local_dir = "./models"
|
| 31 |
+
# )
|
| 32 |
+
|
| 33 |
def process_document(pdf_path, page_ids=None):
|
| 34 |
extracted_pages = extract_pages(pdf_path, page_numbers=page_ids)
|
| 35 |
|
|
|
|
| 83 |
@spaces.GPU(duration=120)
|
| 84 |
def deidentify_doc(pdftext, maxtokens, temperature, top_probability):
|
| 85 |
|
| 86 |
+
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]"
|
| 87 |
+
|
| 88 |
# prompt = """
|
| 89 |
# Perform the following actions on given report:
|
| 90 |
# 1. Replace any person names, age, date of birth, gender with term [redacted]
|
|
|
|
| 93 |
# 4. Replace only the CALENDAR DATES of format 'day/month/year' with term [redacted]
|
| 94 |
# """
|
| 95 |
|
| 96 |
+
# prompt = """
|
| 97 |
+
# Remove person names, any dates, date of birth, age, gender, registration numbers and any addresses from the following report.
|
| 98 |
+
# """
|
| 99 |
|
| 100 |
# model = Llama(
|
| 101 |
# model_path=model_id,
|
|
|
|
| 105 |
# n_ctx=8192,
|
| 106 |
# )
|
| 107 |
|
| 108 |
+
# chat_template = MessagesFormatterType.LLAMA_3
|
| 109 |
|
| 110 |
llm = Llama(
|
| 111 |
model_path="models/Meta-Llama-3-70B-Instruct-Q3_K_M.gguf",
|
|
|
|
| 115 |
n_ctx=8192,
|
| 116 |
)
|
| 117 |
|
| 118 |
+
# provider = LlamaCppPythonProvider(llm)
|
| 119 |
|
| 120 |
+
# agent = LlamaCppAgent(
|
| 121 |
+
# provider,
|
| 122 |
+
# system_prompt="You are a helpful assistant.",
|
| 123 |
+
# predefined_messages_formatter_type=chat_template,
|
| 124 |
+
# debug_output=True
|
| 125 |
+
# )
|
| 126 |
|
| 127 |
+
# settings = provider.get_provider_default_settings()
|
| 128 |
+
# settings.temperature = temperature
|
| 129 |
+
# settings.top_k = 40
|
| 130 |
+
# settings.top_p = top_probability
|
| 131 |
+
# settings.max_tokens = maxtokens
|
| 132 |
+
# settings.repeat_penalty = 1.1
|
| 133 |
+
# settings.stream = True
|
| 134 |
+
|
| 135 |
+
# messages = BasicChatHistory()
|
| 136 |
+
|
| 137 |
+
# stream = agent.get_chat_response(
|
| 138 |
+
# prompt + ' : ' + pdftext,
|
| 139 |
+
# llm_sampling_settings=settings,
|
| 140 |
+
# chat_history=messages,
|
| 141 |
+
# returns_streaming_generator=True,
|
| 142 |
+
# print_output=False
|
| 143 |
+
# )
|
| 144 |
|
| 145 |
+
# output = ""
|
| 146 |
+
# for op in stream:
|
| 147 |
+
# output += op
|
| 148 |
|
| 149 |
+
output = llm.create_chat_completion(
|
| 150 |
+
messages=[
|
| 151 |
+
{"role": "assistant", "content": prompt},
|
| 152 |
+
{
|
| 153 |
+
"role": "user",
|
| 154 |
+
"content": pdftext
|
| 155 |
+
}
|
| 156 |
+
],
|
| 157 |
+
max_tokens=maxtokens,
|
| 158 |
+
temperature=temperature
|
| 159 |
+
)
|
| 160 |
+
output = output['choices'][0]['message']['content']
|
| 161 |
+
|
| 162 |
+
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]"
|
| 163 |
+
output = model.create_chat_completion(
|
| 164 |
+
messages=[
|
| 165 |
+
{"role": "assistant", "content": prompt},
|
| 166 |
+
{
|
| 167 |
+
"role": "user",
|
| 168 |
+
"content": output
|
| 169 |
+
}
|
| 170 |
+
],
|
| 171 |
+
max_tokens=maxtokens,
|
| 172 |
+
temperature=temperature
|
| 173 |
+
)
|
| 174 |
+
output = output['choices'][0]['message']['content']
|
| 175 |
|
| 176 |
# print(prompt)
|
| 177 |
# print(output)
|
|
|
|
| 180 |
return output
|
| 181 |
|
| 182 |
def pdf_to_text(files, maxtokens=2048, temperature=0, top_probability=0.95):
|
|
|
|
| 183 |
files=[files]#remove later
|
| 184 |
for file in files:
|
| 185 |
file_name = os.path.basename(file)
|