srijaydeshpande commited on
Commit
067587e
1 Parent(s): 4958379

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -281
app.py CHANGED
@@ -85,53 +85,6 @@ def txt_to_html(text):
85
  def deidentify_doc(pdftext, maxtokens, temperature, top_probability):
86
 
87
  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]. Output the modified text."
88
-
89
- # model = Llama(
90
- # model_path=model_id,
91
- # flash_attn=True,
92
- # n_gpu_layers=81,
93
- # n_batch=1024,
94
- # n_ctx=8192,
95
- # )
96
-
97
- # chat_template = MessagesFormatterType.LLAMA_3
98
-
99
- # sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
100
-
101
- # llm = LLM(model="meta-llama/Meta-Llama-3-8B-Instruct")
102
- # outputs = llm.generate([prompt], sampling_params)
103
- # output = outputs[0].outputs[0].text
104
-
105
- # provider = LlamaCppPythonProvider(llm)
106
-
107
- # agent = LlamaCppAgent(
108
- # provider,
109
- # system_prompt="You are a helpful assistant.",
110
- # predefined_messages_formatter_type=chat_template,
111
- # debug_output=True
112
- # )
113
-
114
- # settings = provider.get_provider_default_settings()
115
- # settings.temperature = temperature
116
- # settings.top_k = 40
117
- # settings.top_p = top_probability
118
- # settings.max_tokens = maxtokens
119
- # settings.repeat_penalty = 1.1
120
- # settings.stream = True
121
-
122
- # messages = BasicChatHistory()
123
-
124
- # stream = agent.get_chat_response(
125
- # prompt + ' : ' + pdftext,
126
- # llm_sampling_settings=settings,
127
- # chat_history=messages,
128
- # returns_streaming_generator=True,
129
- # print_output=False
130
- # )
131
-
132
- # output = ""
133
- # for op in stream:
134
- # output += op
135
 
136
  llm = Llama(
137
  model_path="models/Meta-Llama-3-8B-Instruct.Q8_0.gguf",
@@ -156,9 +109,6 @@ def deidentify_doc(pdftext, maxtokens, temperature, top_probability):
156
 
157
  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 the modified text."
158
 
159
- # outputs = llm.generate([output], sampling_params)
160
- # output = outputs[0].outputs[0].text
161
-
162
  output = llm.create_chat_completion(
163
  messages=[
164
  {"role": "assistant", "content": prompt},
@@ -172,34 +122,24 @@ def deidentify_doc(pdftext, maxtokens, temperature, top_probability):
172
  )
173
  output = output['choices'][0]['message']['content']
174
 
175
- # print(prompt)
176
- # print(output)
177
- # print('-------------------------------------------------------')
178
-
179
  return output
180
 
181
  def pdf_to_text(files, maxtokens=2048, temperature=0, top_probability=0.95):
182
- files=[files]#remove later
183
  for file in files:
184
  if not file:
185
  return 'Please provide a valid PDF'
186
  file_name = os.path.basename(file)
187
  file_name_splt = file_name.split('.')
188
- # print('File name is ', file_name)
189
  if (len(file_name_splt) > 1 and file_name_splt[1] == 'pdf'):
190
  page2content = process_document(file, page_ids=[0])
191
  pdftext = page2content[1]
192
- print(pdftext)
193
- # pdftext = file # remove later
194
  if (pdftext): #shift this if block to right later
195
  anonymized_text = deidentify_doc(pdftext, maxtokens, temperature, top_probability)
196
  else:
197
  anonymized_text = 'PDF file appears to be corrupted.'
198
  return anonymized_text
199
 
200
- # model_id = "models/Meta-Llama-3-70B-Instruct-Q3_K_M.gguf"
201
- # model = Llama(model_path=model_id, n_ctx=2048, n_threads=8, n_gpu_layers=-1, n_batch=128)
202
-
203
  css = ".gradio-container {background: 'logo.png'}"
204
  temp_slider = gr.Slider(minimum=0, maximum=2, value=0.9, label="Temperature Value")
205
  prob_slider = gr.Slider(minimum=0, maximum=1, value=0.95, label="Max Probability Value")
@@ -217,223 +157,4 @@ iface = gr.Interface(
217
  description="This application assists to remove personal information from the uploaded clinical report",
218
  theme=gr.themes.Soft(),
219
  )
220
- iface.launch()
221
-
222
- # import spaces
223
- # import json
224
- # import subprocess
225
- # from llama_cpp import Llama
226
- # from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
227
- # from llama_cpp_agent.providers import LlamaCppPythonProvider
228
- # from llama_cpp_agent.chat_history import BasicChatHistory
229
- # from llama_cpp_agent.chat_history.messages import Roles
230
- # import gradio as gr
231
- # from huggingface_hub import hf_hub_download
232
-
233
- # hf_hub_download(
234
- # repo_id="bartowski/Meta-Llama-3-70B-Instruct-GGUF",
235
- # filename="Meta-Llama-3-70B-Instruct-Q3_K_M.gguf",
236
- # local_dir = "./models"
237
- # )
238
- # # hf_hub_download(
239
- # # repo_id="bartowski/Mistral-7B-Instruct-v0.3-GGUF",
240
- # # filename="Mistral-7B-Instruct-v0.3-f32.gguf",
241
- # # local_dir = "./models"
242
- # # )
243
-
244
- # css = """
245
- # .message-row {
246
- # justify-content: space-evenly !important;
247
- # }
248
- # .message-bubble-border {
249
- # border-radius: 6px !important;
250
- # }
251
- # .message-buttons-bot, .message-buttons-user {
252
- # right: 10px !important;
253
- # left: auto !important;
254
- # bottom: 2px !important;
255
- # }
256
- # .dark.message-bubble-border {
257
- # border-color: #343140 !important;
258
- # }
259
- # .dark.user {
260
- # background: #1e1c26 !important;
261
- # }
262
- # .dark.assistant.dark, .dark.pending.dark {
263
- # background: #16141c !important;
264
- # }
265
- # """
266
-
267
- # def get_messages_formatter_type(model_name):
268
- # if "Llama" in model_name:
269
- # return MessagesFormatterType.LLAMA_3
270
- # elif "Mistral" in model_name:
271
- # return MessagesFormatterType.MISTRAL
272
- # else:
273
- # raise ValueError(f"Unsupported model: {model_name}")
274
-
275
- # @spaces.GPU(duration=60)
276
- # def respond(
277
- # message,
278
- # history: list[tuple[str, str]],
279
- # model,
280
- # system_message,
281
- # max_tokens,
282
- # temperature,
283
- # top_p,
284
- # top_k,
285
- # repeat_penalty,
286
- # ):
287
- # chat_template = get_messages_formatter_type(model)
288
-
289
- # llm = Llama(
290
- # model_path=f"models/{model}",
291
- # flash_attn=True,
292
- # n_gpu_layers=81,
293
- # n_batch=1024,
294
- # n_ctx=8192,
295
- # )
296
- # provider = LlamaCppPythonProvider(llm)
297
-
298
- # agent = LlamaCppAgent(
299
- # provider,
300
- # system_prompt=f"{system_message}",
301
- # predefined_messages_formatter_type=chat_template,
302
- # debug_output=True
303
- # )
304
-
305
- # settings = provider.get_provider_default_settings()
306
- # settings.temperature = temperature
307
- # settings.top_k = top_k
308
- # settings.top_p = top_p
309
- # settings.max_tokens = max_tokens
310
- # settings.repeat_penalty = repeat_penalty
311
- # settings.stream = True
312
-
313
- # messages = BasicChatHistory()
314
-
315
- # for msn in history:
316
- # user = {
317
- # 'role': Roles.user,
318
- # 'content': msn[0]
319
- # }
320
- # assistant = {
321
- # 'role': Roles.assistant,
322
- # 'content': msn[1]
323
- # }
324
- # messages.add_message(user)
325
- # messages.add_message(assistant)
326
-
327
- # stream = agent.get_chat_response(
328
- # message,
329
- # llm_sampling_settings=settings,
330
- # chat_history=messages,
331
- # returns_streaming_generator=True,
332
- # print_output=False
333
- # )
334
-
335
- # outputs = ""
336
- # for output in stream:
337
- # outputs += output
338
- # yield outputs
339
-
340
- # PLACEHOLDER = """
341
- # <div class="message-bubble-border" style="display:flex; max-width: 600px; border-radius: 6px; border-width: 1px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); backdrop-filter: blur(10px);">
342
- # <figure style="margin: 0;">
343
- # <img src="https://huggingface.co/spaces/pabloce/llama-cpp-agent/resolve/main/llama.jpg" alt="Logo" style="width: 100%; height: 100%; border-radius: 8px;">
344
- # </figure>
345
- # <div style="padding: .5rem 1.5rem;">
346
- # <h2 style="text-align: left; font-size: 1.5rem; font-weight: 700; margin-bottom: 0.5rem;">llama-cpp-agent</h2>
347
- # <p style="text-align: left; font-size: 16px; line-height: 1.5; margin-bottom: 15px;">The llama-cpp-agent framework simplifies interactions with Large Language Models (LLMs), providing an interface for chatting, executing function calls, generating structured output, performing retrieval augmented generation, and processing text using agentic chains with tools.</p>
348
- # <div style="display: flex; justify-content: space-between; align-items: center;">
349
- # <div style="display: flex; flex-flow: column; justify-content: space-between;">
350
- # <span style="display: inline-flex; align-items: center; border-radius: 0.375rem; background-color: rgba(229, 70, 77, 0.1); padding: 0.1rem 0.75rem; font-size: 0.75rem; font-weight: 500; color: #f88181; margin-bottom: 2.5px;">
351
- # Mistral 7B Instruct v0.3
352
- # </span>
353
- # <span style="display: inline-flex; align-items: center; border-radius: 0.375rem; background-color: rgba(79, 70, 229, 0.1); padding: 0.1rem 0.75rem; font-size: 0.75rem; font-weight: 500; color: #60a5fa; margin-top: 2.5px;">
354
- # Meta Llama 3 70B Instruct
355
- # </span>
356
- # </div>
357
- # <div style="display: flex; justify-content: flex-end; align-items: center;">
358
- # <a href="https://discord.gg/sRMvWKrh" target="_blank" rel="noreferrer" style="padding: .5rem;">
359
- # <svg width="24" height="24" fill="currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 5 30.67 23.25">
360
- # <title>Discord</title>
361
- # <path d="M26.0015 6.9529C24.0021 6.03845 21.8787 5.37198 19.6623 5C19.3833 5.48048 19.0733 6.13144 18.8563 6.64292C16.4989 6.30193 14.1585 6.30193 11.8336 6.64292C11.6166 6.13144 11.2911 5.48048 11.0276 5C8.79575 5.37198 6.67235 6.03845 4.6869 6.9529C0.672601 12.8736 -0.41235 18.6548 0.130124 24.3585C2.79599 26.2959 5.36889 27.4739 7.89682 28.2489C8.51679 27.4119 9.07477 26.5129 9.55525 25.5675C8.64079 25.2265 7.77283 24.808 6.93587 24.312C7.15286 24.1571 7.36986 23.9866 7.57135 23.8161C12.6241 26.1255 18.0969 26.1255 23.0876 23.8161C23.3046 23.9866 23.5061 24.1571 23.7231 24.312C22.8861 24.808 22.0182 25.2265 21.1037 25.5675C21.5842 26.5129 22.1422 27.4119 22.7621 28.2489C25.2885 27.4739 27.8769 26.2959 30.5288 24.3585C31.1952 17.7559 29.4733 12.0212 26.0015 6.9529ZM10.2527 20.8402C8.73376 20.8402 7.49382 19.4608 7.49382 17.7714C7.49382 16.082 8.70276 14.7025 10.2527 14.7025C11.7871 14.7025 13.0425 16.082 13.0115 17.7714C13.0115 19.4608 11.7871 20.8402 10.2527 20.8402ZM20.4373 20.8402C18.9183 20.8402 17.6768 19.4608 17.6768 17.7714C17.6768 16.082 18.8873 14.7025 20.4373 14.7025C21.9717 14.7025 23.2271 16.082 23.1961 17.7714C23.1961 19.4608 21.9872 20.8402 20.4373 20.8402Z"></path>
362
- # </svg>
363
- # </a>
364
- # <a href="https://github.com/Maximilian-Winter/llama-cpp-agent" target="_blank" rel="noreferrer" style="padding: .5rem;">
365
- # <svg width="24" height="24" fill="currentColor" viewBox="3 3 18 18">
366
- # <title>GitHub</title>
367
- # <path d="M12 3C7.0275 3 3 7.12937 3 12.2276C3 16.3109 5.57625 19.7597 9.15374 20.9824C9.60374 21.0631 9.77249 20.7863 9.77249 20.5441C9.77249 20.3249 9.76125 19.5982 9.76125 18.8254C7.5 19.2522 6.915 18.2602 6.735 17.7412C6.63375 17.4759 6.19499 16.6569 5.8125 16.4378C5.4975 16.2647 5.0475 15.838 5.80124 15.8264C6.51 15.8149 7.01625 16.4954 7.18499 16.7723C7.99499 18.1679 9.28875 17.7758 9.80625 17.5335C9.885 16.9337 10.1212 16.53 10.38 16.2993C8.3775 16.0687 6.285 15.2728 6.285 11.7432C6.285 10.7397 6.63375 9.9092 7.20749 9.26326C7.1175 9.03257 6.8025 8.08674 7.2975 6.81794C7.2975 6.81794 8.05125 6.57571 9.77249 7.76377C10.4925 7.55615 11.2575 7.45234 12.0225 7.45234C12.7875 7.45234 13.5525 7.55615 14.2725 7.76377C15.9937 6.56418 16.7475 6.81794 16.7475 6.81794C17.2424 8.08674 16.9275 9.03257 16.8375 9.26326C17.4113 9.9092 17.76 10.7281 17.76 11.7432C17.76 15.2843 15.6563 16.0687 13.6537 16.2993C13.98 16.5877 14.2613 17.1414 14.2613 18.0065C14.2613 19.2407 14.25 20.2326 14.25 20.5441C14.25 20.7863 14.4188 21.0746 14.8688 20.9824C16.6554 20.364 18.2079 19.1866 19.3078 17.6162C20.4077 16.0457 20.9995 14.1611 21 12.2276C21 7.12937 16.9725 3 12 3Z"></path>
368
- # </svg>
369
- # </a>
370
- # </div>
371
- # </div>
372
- # </div>
373
- # </div>
374
- # """
375
-
376
- # demo = gr.ChatInterface(
377
- # respond,
378
- # additional_inputs=[
379
- # gr.Dropdown([
380
- # 'Meta-Llama-3-70B-Instruct-Q3_K_M.gguf',
381
- # 'Mistral-7B-Instruct-v0.3-f32.gguf'
382
- # ],
383
- # value="Meta-Llama-3-70B-Instruct-Q3_K_M.gguf",
384
- # label="Model"
385
- # ),
386
- # gr.Textbox(value="You are a helpful assistant.", label="System message"),
387
- # gr.Slider(minimum=1, maximum=4096, value=2048, step=1, label="Max tokens"),
388
- # gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
389
- # gr.Slider(
390
- # minimum=0.1,
391
- # maximum=1.0,
392
- # value=0.95,
393
- # step=0.05,
394
- # label="Top-p",
395
- # ),
396
- # gr.Slider(
397
- # minimum=0,
398
- # maximum=100,
399
- # value=40,
400
- # step=1,
401
- # label="Top-k",
402
- # ),
403
- # gr.Slider(
404
- # minimum=0.0,
405
- # maximum=2.0,
406
- # value=1.1,
407
- # step=0.1,
408
- # label="Repetition penalty",
409
- # ),
410
- # ],
411
- # theme=gr.themes.Soft(primary_hue="violet", secondary_hue="violet", neutral_hue="gray",font=[gr.themes.GoogleFont("Exo"), "ui-sans-serif", "system-ui", "sans-serif"]).set(
412
- # body_background_fill_dark="#16141c",
413
- # block_background_fill_dark="#16141c",
414
- # block_border_width="1px",
415
- # block_title_background_fill_dark="#1e1c26",
416
- # input_background_fill_dark="#292733",
417
- # button_secondary_background_fill_dark="#24212b",
418
- # border_color_accent_dark="#343140",
419
- # border_color_primary_dark="#343140",
420
- # background_fill_secondary_dark="#16141c",
421
- # color_accent_soft_dark="transparent",
422
- # code_background_fill_dark="#292733",
423
- # ),
424
- # css=css,
425
- # retry_btn="Retry",
426
- # undo_btn="Undo",
427
- # clear_btn="Clear",
428
- # submit_btn="Send",
429
- # description="Llama-cpp-agent: Chat multi llm selection",
430
- # chatbot=gr.Chatbot(
431
- # scale=1,
432
- # placeholder=PLACEHOLDER,
433
- # likeable=False,
434
- # show_copy_button=True
435
- # )
436
- # )
437
-
438
- # # if __name__ == "__main__":
439
- # demo.launch()
 
85
  def deidentify_doc(pdftext, maxtokens, temperature, top_probability):
86
 
87
  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]. Output the modified text."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
  llm = Llama(
90
  model_path="models/Meta-Llama-3-8B-Instruct.Q8_0.gguf",
 
109
 
110
  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 the modified text."
111
 
 
 
 
112
  output = llm.create_chat_completion(
113
  messages=[
114
  {"role": "assistant", "content": prompt},
 
122
  )
123
  output = output['choices'][0]['message']['content']
124
 
 
 
 
 
125
  return output
126
 
127
  def pdf_to_text(files, maxtokens=2048, temperature=0, top_probability=0.95):
128
+ files=[files]
129
  for file in files:
130
  if not file:
131
  return 'Please provide a valid PDF'
132
  file_name = os.path.basename(file)
133
  file_name_splt = file_name.split('.')
 
134
  if (len(file_name_splt) > 1 and file_name_splt[1] == 'pdf'):
135
  page2content = process_document(file, page_ids=[0])
136
  pdftext = page2content[1]
 
 
137
  if (pdftext): #shift this if block to right later
138
  anonymized_text = deidentify_doc(pdftext, maxtokens, temperature, top_probability)
139
  else:
140
  anonymized_text = 'PDF file appears to be corrupted.'
141
  return anonymized_text
142
 
 
 
 
143
  css = ".gradio-container {background: 'logo.png'}"
144
  temp_slider = gr.Slider(minimum=0, maximum=2, value=0.9, label="Temperature Value")
145
  prob_slider = gr.Slider(minimum=0, maximum=1, value=0.95, label="Max Probability Value")
 
157
  description="This application assists to remove personal information from the uploaded clinical report",
158
  theme=gr.themes.Soft(),
159
  )
160
+ iface.launch()