srijaydeshpande commited on
Commit
127c875
1 Parent(s): 72540c8
Files changed (1) hide show
  1. app.py +50 -50
app.py CHANGED
@@ -4,13 +4,13 @@ from tqdm import tqdm
4
  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
 
@@ -73,49 +73,49 @@ def deidentify_doc(pdftext=""):
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
- # print('Input prompt is ',prompt)
77
- # print('Input pdf text is ',pdftext)
78
-
79
- # output = model.create_chat_completion(
80
- # messages = [
81
- # {"role": "assistant", "content": prompt},
82
- # {
83
- # "role": "user",
84
- # "content": pdftext
85
- # }
86
- # ],
87
- # max_tokens=600,
88
- # temperature=0
89
- # )
90
- # output = output['choices'][0]['message']['content']
91
 
92
  # if (pdftext):
93
  # prompt = prompt + ': ' + pdftext
94
  # output = model.generate(prompt=prompt, max_tokens=1024, n_batch=128)
95
 
96
- messages = [
97
- {"role": "assistant",
98
- "content": prompt},
99
- {"role": "user",
100
- "content": pdftext}, ]
101
- prompt = model.tokenizer.apply_chat_template(
102
- messages,
103
- tokenize=False,
104
- add_generation_prompt=True
105
- )
106
- terminators = [
107
- model.tokenizer.eos_token_id,
108
- model.tokenizer.convert_tokens_to_ids("<|eot_id|>")
109
- ]
110
- outputs = model(
111
- prompt,
112
- max_new_tokens=1024,
113
- eos_token_id=terminators,
114
- do_sample=True,
115
- temperature=0.3,
116
- top_p=0.95,
117
- )
118
- output = outputs[0]["generated_text"][len(prompt):]
119
 
120
  return output
121
 
@@ -130,19 +130,19 @@ def pdf_to_text(file):
130
  file.write(html)
131
  return html
132
 
133
- # model_id = "Meta-Llama-3-8B-Instruct.Q5_K_M.gguf"
134
- # model = Llama(model_path=model_id, n_ctx=2048, n_threads=8, n_gpu_layers=32, n_batch=64)
135
 
136
  # model = GPT4All("Meta-Llama-3-8B-Instruct.Q4_0.gguf", n_threads=8, device='gpu')
137
  # model.chat_session()
138
 
139
- model_id = "Meta-Llama-3-8B-Instruct"
140
- model = transformers.pipeline(
141
- "text-generation",
142
- model=model_id,
143
- model_kwargs={"torch_dtype": torch.bfloat16},
144
- device="cuda",
145
- )
146
 
147
  css = ".gradio-container {background: 'logo.png'}"
148
 
 
4
  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
 
 
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
+ print('Input prompt is ',prompt)
77
+ print('Input pdf text is ',pdftext)
78
+
79
+ output = model.create_chat_completion(
80
+ messages = [
81
+ {"role": "assistant", "content": prompt},
82
+ {
83
+ "role": "user",
84
+ "content": pdftext
85
+ }
86
+ ],
87
+ max_tokens=600,
88
+ temperature=0
89
+ )
90
+ output = output['choices'][0]['message']['content']
91
 
92
  # if (pdftext):
93
  # prompt = prompt + ': ' + pdftext
94
  # output = model.generate(prompt=prompt, max_tokens=1024, n_batch=128)
95
 
96
+ # messages = [
97
+ # {"role": "assistant",
98
+ # "content": prompt},
99
+ # {"role": "user",
100
+ # "content": pdftext}, ]
101
+ # prompt = model.tokenizer.apply_chat_template(
102
+ # messages,
103
+ # tokenize=False,
104
+ # add_generation_prompt=True
105
+ # )
106
+ # terminators = [
107
+ # model.tokenizer.eos_token_id,
108
+ # model.tokenizer.convert_tokens_to_ids("<|eot_id|>")
109
+ # ]
110
+ # outputs = model(
111
+ # prompt,
112
+ # max_new_tokens=1024,
113
+ # eos_token_id=terminators,
114
+ # do_sample=True,
115
+ # temperature=0.3,
116
+ # top_p=0.95,
117
+ # )
118
+ # output = outputs[0]["generated_text"][len(prompt):]
119
 
120
  return output
121
 
 
130
  file.write(html)
131
  return html
132
 
133
+ model_id = "Meta-Llama-3-8B-Instruct.Q5_K_M.gguf"
134
+ model = Llama(model_path=model_id, n_ctx=2048, n_threads=8, n_gpu_layers=-1, n_batch=64)
135
 
136
  # model = GPT4All("Meta-Llama-3-8B-Instruct.Q4_0.gguf", n_threads=8, device='gpu')
137
  # model.chat_session()
138
 
139
+ # model_id = "D:/llama/meta-llama/Meta-Llama-3-8B-Instruct"
140
+ # model = transformers.pipeline(
141
+ # "text-generation",
142
+ # model=model_id,
143
+ # model_kwargs={"torch_dtype": torch.bfloat16},
144
+ # device="cpu",
145
+ # )
146
 
147
  css = ".gradio-container {background: 'logo.png'}"
148