forked from sarthakchittawar/Medmini
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedmini.py
156 lines (109 loc) · 5.66 KB
/
medmini.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from transformers import pipeline
import torch
from langchain.vectorstores import Chroma
from langchain.embeddings import HuggingFaceEmbeddings
def formatPrompt(prompt,context):
# prompts
# fp=f'Use the context to answer the question.Incorporate the context in your answer.\nContext: {context}\nQuestion: {prompt}\nAnswer: '
# fp = f'Summarize the following:\n {context}'
# fp = f'Summarize the following:\n {context}'
fp=f'{context}{prompt}'
return fp
def systemInit():
# initializing the vector db
model_name = "sentence-transformers/all-mpnet-base-v2"
model_kwargs = {'device': 'cpu'}
encode_kwargs = {'normalize_embeddings': False}
hf = HuggingFaceEmbeddings(
model_name=model_name,
model_kwargs=model_kwargs,
encode_kwargs=encode_kwargs
)
vectordb=Chroma(persist_directory='./med_db',embedding_function=hf)
# initializing the model
summarizer = pipeline("summarization", model="Falconsai/medical_summarization")
return summarizer,vectordb
def infer(prompt,summarizer,vectordb):
# prompt = f'What are some ways to deal with bipolar disorder?'
### RAG
# print(vectordb._collection.count())
docs = vectordb.similarity_search(prompt,k=4)
# print(len(docs))
# print(docs)
context=' '.join(d.page_content for d in docs)
# print(context)
def useModel(model_name):
if model_name=='summarizer':
#### Summarization
output = summarizer(formatPrompt(prompt,context), max_length=512, min_length=32, do_sample=False)
# elif model_name=='gpt2':
# #### Base GPT2 unquantized
# low_cpu=True
# # args={"low_cpu_mem_usage":low_cpu,"device":'cpu',"load_in_8bit":quantized,"load_in_4bit":extreme_quantization,"torch_dtype":torch.float32}
# args={"low_cpu_mem_usage":low_cpu,"device_map":'cpu',"torch_dtype":torch.float32}
# pipe = pipeline('text-generation', model='gpt2',model_kwargs=args)
# output=pipe(formatPrompt(prompt, context), max_new_tokens=60)
# elif model_name=='gpt2_quantized':
# #### Base GPT2 quantized
# tokenizer = AutoTokenizer.from_pretrained("gpt2", trust_remote_code=True)
# tokenizer.pad_token = tokenizer.eos_token
# bnb_config = BitsAndBytesConfig(
# load_in_4bit=True,
# bnb_4bit_use_double_quant=True,
# bnb_4bit_quant_type="nf4",
# bnb_4bit_compute_dtype=torch.float16
# )
# model = AutoModelForCausalLM.from_pretrained(
# "gpt2",
# quantization_config=bnb_config,
# trust_remote_code=True
# )
# model.config.use_cache = False
# pipe=pipeline('text-generation',model=model,tokenizer=tokenizer)
# output=pipe(formatPrompt(prompt,context))
# elif model_name=='phi1.5_quantized':
# #### Phi 1.5 quantized/unquantized
# tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-1_5", trust_remote_code=True)
# # tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small", trust_remote_code=True)
# tokenizer.pad_token = tokenizer.eos_token
# bnb_config = BitsAndBytesConfig(
# load_in_4bit=True,
# bnb_4bit_use_double_quant=True,
# bnb_4bit_quant_type="nf4",
# bnb_4bit_compute_dtype=torch.float16
# )
# model = AutoModelForCausalLM.from_pretrained(
# "microsoft/phi-1_5",
# # "google/flan-t5-small",
# # quantization_config=bnb_config,
# trust_remote_code=True
# )
# pipe=pipeline('text-generation',model=model,tokenizer=tokenizer)
# output=pipe(formatPrompt(prompt,context), max_new_tokens=60)
# elif model_name=='phi1.5':
# ### Phi 1.5 official
# torch.set_default_device("cpu")
# model = AutoModelForCausalLM.from_pretrained("microsoft/phi-1_5", trust_remote_code=True,device_map='cuda')
# tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-1_5", trust_remote_code=True)
# inputs = tokenizer(formatPrompt(prompt,context), return_tensors="pt", return_attention_mask=False)
# outputs = model.generate(**inputs)
# output = tokenizer.batch_decode(outputs)[0]
# elif model_name=='flant5':
# from transformers import T5Tokenizer, T5ForConditionalGeneration
# tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-large")
# model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-large")
# input_text = formatPrompt(prompt, context)
# input_ids = tokenizer(input_text, return_tensors="pt").input_ids
# outputs= model.generate(input_ids, max_new_tokens=400)
# output = tokenizer.decode(outputs[0])
# elif model_name=='gpt2_finetuned':
# #### Finetuned GPT2 model
# model = AutoModelForCausalLM.from_pretrained("SidhiPanda/gpt2-finetuned-megathon", trust_remote_code=True, torch_dtype=torch.float32)
# tokenizer = AutoTokenizer.from_pretrained("gpt2", trust_remote_code=True)
# inputs = tokenizer(formatPrompt(prompt,context), return_tensors="pt", return_attention_mask=False)
# outputs = model.generate(**inputs, max_new_tokens=45)
# output = tokenizer.batch_decode(outputs)[0]
# output
# print(output)
return output[0]['summary_text']
return useModel('summarizer')