srijaydeshpande commited on
Commit
ca3e828
·
verified ·
1 Parent(s): e852fd8
Files changed (1) hide show
  1. app.py +50 -46
app.py CHANGED
@@ -5,12 +5,16 @@ import re
5
  import gradio as gr
6
  import os
7
  from llama_cpp import Llama
8
- from gpt4all import GPT4All
9
  import transformers
10
  # from transformers import GemmaTokenizer, AutoModelForCausalLM
11
  # from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
12
  import accelerate
13
  import torch
 
 
 
 
14
 
15
  # HF_TOKEN = os.environ.get("HF_TOKEN", None)
16
 
@@ -71,48 +75,48 @@ def txt_to_html(text):
71
 
72
  def deidentify_doc(pdftext="", prompt="", maxtokens=600, temperature=1.2, top_probability=0.95):
73
 
74
- # prompt = "Please anonymize the following clinical note. Replace all the following information with the term '[redacted]': Redact any strings that might be a name or initials, patients’ names, doctors’ names, the names Dr., redact any medical staff names, redact any strings that might be a location or address, such as '3970 Longview Drive', redact any strings that look like 'age 37', redact any dates and registration numbers, redact professions such as 'manager', redact any contact information."
75
 
76
- # output = model.create_chat_completion(
77
- # messages = [
78
- # {"role": "assistant", "content": prompt},
79
- # {
80
- # "role": "user",
81
- # "content": pdftext
82
- # }
83
- # ],
84
- # max_tokens=800,
85
- # temperature=0
86
- # )
87
- # output = output['choices'][0]['message']['content']
88
 
89
  # if (pdftext):
90
  # prompt = prompt + ': ' + pdftext
91
  # output = model.generate(prompt=prompt, max_tokens=1024, n_batch=128)
92
 
93
- messages = [
94
- {"role": "assistant",
95
- "content": prompt},
96
- {"role": "user",
97
- "content": pdftext}, ]
98
- prompt = model.tokenizer.apply_chat_template(
99
- messages,
100
- tokenize=False,
101
- add_generation_prompt=True
102
- )
103
- terminators = [
104
- model.tokenizer.eos_token_id,
105
- model.tokenizer.convert_tokens_to_ids("<|eot_id|>")
106
- ]
107
- outputs = model(
108
- prompt,
109
- max_new_tokens=maxtokens,
110
- eos_token_id=terminators,
111
- do_sample=True,
112
- temperature=temperature,
113
- top_p=top_probability,
114
- )
115
- output = outputs[0]["generated_text"][len(prompt):]
116
 
117
  return output
118
 
@@ -141,19 +145,19 @@ def pdf_to_text(files, output_folder, prompt, maxtokens=600, temperature=1.2, to
141
  display_text = "All selected reports are anonymized and results are saved in " + output_folder
142
  return anonymized_text
143
 
144
- # model_id = "D:/llama/meta-llama/Meta-Llama-3-8B-Instruct.Q5_K_M.gguf"
145
- # model = Llama(model_path=model_id, n_ctx=2048, n_threads=8, n_gpu_layers=32, n_batch=64)
146
 
147
  # model = GPT4All("Meta-Llama-3-8B-Instruct.Q4_0.gguf", n_threads=8, device='gpu')
148
  # model.chat_session()
149
 
150
- model_id = "Meta-Llama-3-8B-Instruct"
151
- model = transformers.pipeline(
152
- "text-generation",
153
- model=model_id,
154
- model_kwargs={"torch_dtype": torch.bfloat16},
155
- device="cuda",
156
- )
157
 
158
  css = ".gradio-container {background: 'logo.png'}"
159
  temp_slider = gr.Slider(minimum=0, maximum=2, value=0.2, label="Temperature Value")
 
5
  import gradio as gr
6
  import os
7
  from llama_cpp import Llama
8
+ # from gpt4all import GPT4All
9
  import transformers
10
  # from transformers import GemmaTokenizer, AutoModelForCausalLM
11
  # from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
12
  import accelerate
13
  import torch
14
+ import subprocess
15
+
16
+ subprocess.run('pip install llama-cpp-python==0.2.75 --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cu124', shell=True)
17
+ subprocess.run('pip install llama-cpp-agent==0.2.10', shell=True)
18
 
19
  # HF_TOKEN = os.environ.get("HF_TOKEN", None)
20
 
 
75
 
76
  def deidentify_doc(pdftext="", prompt="", maxtokens=600, temperature=1.2, top_probability=0.95):
77
 
78
+ prompt = "Please anonymize the following clinical note. Replace all the following information with the term '[redacted]': Redact any strings that might be a name or initials, patients’ names, doctors’ names, the names Dr., redact any medical staff names, redact any strings that might be a location or address, such as '3970 Longview Drive', redact any strings that look like 'age 37', redact any dates and registration numbers, redact professions such as 'manager', redact any contact information."
79
 
80
+ output = model.create_chat_completion(
81
+ messages = [
82
+ {"role": "assistant", "content": prompt},
83
+ {
84
+ "role": "user",
85
+ "content": pdftext
86
+ }
87
+ ],
88
+ max_tokens=800,
89
+ temperature=0
90
+ )
91
+ output = output['choices'][0]['message']['content']
92
 
93
  # if (pdftext):
94
  # prompt = prompt + ': ' + pdftext
95
  # output = model.generate(prompt=prompt, max_tokens=1024, n_batch=128)
96
 
97
+ # messages = [
98
+ # {"role": "assistant",
99
+ # "content": prompt},
100
+ # {"role": "user",
101
+ # "content": pdftext}, ]
102
+ # prompt = model.tokenizer.apply_chat_template(
103
+ # messages,
104
+ # tokenize=False,
105
+ # add_generation_prompt=True
106
+ # )
107
+ # terminators = [
108
+ # model.tokenizer.eos_token_id,
109
+ # model.tokenizer.convert_tokens_to_ids("<|eot_id|>")
110
+ # ]
111
+ # outputs = model(
112
+ # prompt,
113
+ # max_new_tokens=maxtokens,
114
+ # eos_token_id=terminators,
115
+ # do_sample=True,
116
+ # temperature=temperature,
117
+ # top_p=top_probability,
118
+ # )
119
+ # output = outputs[0]["generated_text"][len(prompt):]
120
 
121
  return output
122
 
 
145
  display_text = "All selected reports are anonymized and results are saved in " + output_folder
146
  return anonymized_text
147
 
148
+ model_id = "Meta-Llama-3-8B-Instruct.Q5_K_M.gguf"
149
+ model = Llama(model_path=model_id, n_ctx=2048, n_threads=8, n_gpu_layers=81, n_batch=64)
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
+ # "text-generation",
157
+ # model=model_id,
158
+ # model_kwargs={"torch_dtype": torch.bfloat16},
159
+ # device="cuda",
160
+ # )
161
 
162
  css = ".gradio-container {background: 'logo.png'}"
163
  temp_slider = gr.Slider(minimum=0, maximum=2, value=0.2, label="Temperature Value")