Question Answering
PEFT
English
medical
Tonic commited on
Commit
46d1540
1 Parent(s): 41e3995

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +168 -1
README.md CHANGED
@@ -14,6 +14,7 @@ pipeline_tag: question-answering
14
 
15
  This is a medical fine tuned model from the [Falcon-7b-Instruction](https://huggingface.co/tiiuae/falcon-7b-instruct) Base using 500 steps & 6 epochs with [MedAware](https://huggingface.co/datasets/keivalya/MedQuad-MedicalQnADataset) Dataset from [keivalya](https://huggingface.co/datasets/keivalya)
16
 
 
17
 
18
  ## Model Details
19
 
@@ -29,7 +30,7 @@ This is a medical fine tuned model from the [Falcon-7b-Instruction](https://hugg
29
  ### Model Sources
30
 
31
  - **Repository:** [Github](https://github.com/Josephrp/AI-challenge-hackathon/blob/master/falcon_7b_instruct_GaiaMiniMed_dataset.ipynb)
32
- - **Demo :** [pseudolab/gaiaminimed](https://huggingface.co/spaces/pseudolab/gaiaminimed) & [tonic/gaiaminimed](https://huggingface.com/spaces/tonic/gaiaminimed)
33
 
34
  ## Uses
35
 
@@ -67,6 +68,8 @@ Use the code below to get started with the model.
67
 
68
  ```python
69
 
 
 
70
  from transformers import AutoConfig, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM, MistralForCausalLM
71
  from peft import PeftModel, PeftConfig
72
  import torch
@@ -142,6 +145,170 @@ iface.launch()
142
 
143
  ```
144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  ## Training Details
146
 
147
  ### Results
 
14
 
15
  This is a medical fine tuned model from the [Falcon-7b-Instruction](https://huggingface.co/tiiuae/falcon-7b-instruct) Base using 500 steps & 6 epochs with [MedAware](https://huggingface.co/datasets/keivalya/MedQuad-MedicalQnADataset) Dataset from [keivalya](https://huggingface.co/datasets/keivalya)
16
 
17
+ Check out a cool demo with chat memory here : [pseudolab/GaiaFalconChat](https://huggingface.co/spaces/pseudolab/GaiaMiniMed_ChatWithFalcon)
18
 
19
  ## Model Details
20
 
 
30
  ### Model Sources
31
 
32
  - **Repository:** [Github](https://github.com/Josephrp/AI-challenge-hackathon/blob/master/falcon_7b_instruct_GaiaMiniMed_dataset.ipynb)
33
+ - **Demo :** [pseudolab/gaiafalconchat](https://huggingface.co/spaces/pseudolab/GaiaMiniMed_ChatWithFalcon)[pseudolab/gaiaminimed](https://huggingface.co/spaces/pseudolab/gaiaminimed) & [tonic/gaiaminimed](https://huggingface.com/spaces/tonic/gaiaminimed)
34
 
35
  ## Uses
36
 
 
68
 
69
  ```python
70
 
71
+ # Gaia Mini Med Quick Start
72
+
73
  from transformers import AutoConfig, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM, MistralForCausalLM
74
  from peft import PeftModel, PeftConfig
75
  import torch
 
145
 
146
  ```
147
 
148
+ See the code below for more advanced deployment , including a naive memory store and user controllable parameters:
149
+
150
+ ```Python
151
+
152
+ # Welcome to a more advanced implementation of Gaia Mini Med - Falcon Chat
153
+
154
+ from transformers import AutoConfig, AutoTokenizer, AutoModelForCausalLM
155
+ from peft import PeftModel, PeftConfig
156
+ import torch
157
+ import gradio as gr
158
+ import json
159
+ import os
160
+ import shutil
161
+ import requests
162
+
163
+ # Define the device
164
+ device = "cuda" if torch.cuda.is_available() else "cpu"
165
+ #Define variables
166
+ temperature=0.4
167
+ max_new_tokens=240
168
+ top_p=0.92
169
+ repetition_penalty=1.7
170
+ max_length=2048
171
+
172
+ # Use model IDs as variables
173
+ base_model_id = "tiiuae/falcon-7b-instruct"
174
+ model_directory = "Tonic/GaiaMiniMed"
175
+
176
+ # Instantiate the Tokenizer
177
+ tokenizer = AutoTokenizer.from_pretrained(base_model_id, trust_remote_code=True, padding_side="left")
178
+ tokenizer.pad_token = tokenizer.eos_token
179
+ tokenizer.padding_side = 'left'
180
+
181
+
182
+ # Load the GaiaMiniMed model with the specified configuration
183
+ # Load the Peft model with a specific configuration
184
+ # Specify the configuration class for the model
185
+ model_config = AutoConfig.from_pretrained(base_model_id)
186
+ # Load the PEFT model with the specified configuration
187
+ peft_model = AutoModelForCausalLM.from_pretrained(model_directory, config=model_config)
188
+ peft_model = PeftModel.from_pretrained(peft_model, model_directory)
189
+
190
+
191
+
192
+ # Class to encapsulate the Falcon chatbot
193
+ class FalconChatBot:
194
+ def __init__(self, system_prompt="You are an expert medical analyst:"):
195
+ self.system_prompt = system_prompt
196
+
197
+ def process_history(self, history):
198
+ if history is None:
199
+ return []
200
+
201
+ # Ensure that history is a list of dictionaries
202
+ if not isinstance(history, list):
203
+ return []
204
+
205
+ # Filter out special commands from the history
206
+ filtered_history = []
207
+ for message in history:
208
+ if isinstance(message, dict):
209
+ user_message = message.get("user", "")
210
+ assistant_message = message.get("assistant", "")
211
+ # Check if the user_message is not a special command
212
+ if not user_message.startswith("Falcon:"):
213
+ filtered_history.append({"user": user_message, "assistant": assistant_message})
214
+ return filtered_history
215
+
216
+ def predict(self, user_message, assistant_message, history, temperature=0.4, max_new_tokens=700, top_p=0.99, repetition_penalty=1.9):
217
+
218
+ # Process the history to remove special commands
219
+ processed_history = self.process_history(history)
220
+ # Combine the user and assistant messages into a conversation
221
+ conversation = f"{self.system_prompt}\nFalcon: {assistant_message if assistant_message else ''} User: {user_message}\nFalcon:\n"
222
+ # Encode the conversation using the tokenizer
223
+ input_ids = tokenizer.encode(conversation, return_tensors="pt", add_special_tokens=False)
224
+ # Generate a response using the Falcon model
225
+ response = peft_model.generate(input_ids=input_ids, max_length=max_length, use_cache=False, early_stopping=False, bos_token_id=peft_model.config.bos_token_id, eos_token_id=peft_model.config.eos_token_id, pad_token_id=peft_model.config.eos_token_id, temperature=0.4, do_sample=True)
226
+ # Decode the generated response to text
227
+ response_text = tokenizer.decode(response[0], skip_special_tokens=True)
228
+ # Append the Falcon-like conversation to the history
229
+ self.history.append(conversation)
230
+ self.history.append(response_text)
231
+
232
+ return response_text
233
+
234
+
235
+ # Create the Falcon chatbot instance
236
+ falcon_bot = FalconChatBot()
237
+
238
+ # Define the Gradio interface
239
+ title = "👋🏻Welcome to Tonic's 🦅Falcon's Medical👨🏻‍⚕️Expert Chat🚀"
240
+ description = "You can use this Space to test out the GaiaMiniMed model [(Tonic/GaiaMiniMed)](https://huggingface.co/Tonic/GaiaMiniMed) or duplicate this Space and use it locally or on 🤗HuggingFace. [Join me on Discord to build together](https://discord.gg/VqTxc76K3u)."
241
+
242
+ examples = [
243
+ [
244
+ "Assistant is a public health and medical expert named Gaia ready to help the user.",
245
+ "Hi there, I have a question!",
246
+ "My name is Gaia, I'm a health and sanitation expert ready to answer your medical questions.",
247
+ "Assistant is a medical and sanitation question expert trained to answer medical questions", 0.4, 700, 0.90, 1.9
248
+ ]
249
+ ]
250
+
251
+
252
+ additional_inputs=[
253
+ gr.Textbox("", label="Optional system prompt"),
254
+ gr.Slider(
255
+ label="Temperature",
256
+ value=0.9,
257
+ minimum=0.0,
258
+ maximum=1.0,
259
+ step=0.05,
260
+ interactive=True,
261
+ info="Higher values produce more diverse outputs",
262
+ ),
263
+ gr.Slider(
264
+ label="Max new tokens",
265
+ value=256,
266
+ minimum=0,
267
+ maximum=3000,
268
+ step=64,
269
+ interactive=True,
270
+ info="The maximum numbers of new tokens",
271
+ ),
272
+ gr.Slider(
273
+ label="Top-p (nucleus sampling)",
274
+ value=0.90,
275
+ minimum=0.01,
276
+ maximum=0.99,
277
+ step=0.05,
278
+ interactive=True,
279
+ info="Higher values sample more low-probability tokens",
280
+ ),
281
+ gr.Slider(
282
+ label="Repetition penalty",
283
+ value=1.2,
284
+ minimum=1.0,
285
+ maximum=2.0,
286
+ step=0.05,
287
+ interactive=True,
288
+ info="Penalize repeated tokens",
289
+ )
290
+ ]
291
+
292
+ iface = gr.Interface(
293
+ fn=falcon_bot.predict,
294
+ title=title,
295
+ description=description,
296
+ examples=examples,
297
+ inputs=[
298
+ gr.inputs.Textbox(label="System Prompt", type="text", lines=2),
299
+ gr.inputs.Textbox(label="User Message", type="text", lines=3),
300
+ gr.inputs.Textbox(label="Assistant Message", type="text", lines=2),
301
+ ] + additional_inputs,
302
+ outputs="text",
303
+ theme="ParityError/Anime"
304
+ )
305
+
306
+ # Launch the Gradio interface for the Falcon model
307
+ iface.launch()
308
+
309
+ ```
310
+
311
+
312
  ## Training Details
313
 
314
  ### Results