Question Answering
PEFT
English
medical
Tonic commited on
Commit
4268279
1 Parent(s): 1b93288

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +70 -92
README.md CHANGED
@@ -58,128 +58,106 @@ This model is not meant as a decision support system in the wild, only for educa
58
 
59
  ## How to Get Started with the Model
60
 
 
 
 
 
61
  Use the code below to get started with the model.
62
 
63
  ```python
64
- from transformers import AutoConfig, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM, MistralForCausalLM
65
- from peft import PeftModel, PeftConfig
66
  import torch
67
  import gradio as gr
68
- import random
69
- from textwrap import wrap
70
-
71
- # Functions to Wrap the Prompt Correctly
72
- def wrap_text(text, width=90):
73
- lines = text.split('\n')
74
- wrapped_lines = [textwrap.fill(line, width=width) for line in lines]
75
- wrapped_text = '\n'.join(wrapped_lines)
76
- return wrapped_text
77
-
78
- def multimodal_prompt(user_input, system_prompt="You are an expert medical analyst:"):
79
- """
80
- Generates text using a large language model, given a user input and a system prompt.
81
- Args:
82
- user_input: The user's input text to generate a response for.
83
- system_prompt: Optional system prompt.
84
- Returns:
85
- A string containing the generated text.
86
- """
87
- # Combine user input and system prompt
88
- formatted_input = f"Question: {system_prompt} {user_input} \n Mini :"
89
-
90
- # Encode the input text
91
- encodeds = tokenizer(formatted_input, return_tensors="pt", add_special_tokens=False)
92
- model_inputs = encodeds.to(device)
93
-
94
- # Generate a response using the model
95
- output = model.generate(
96
- **model_inputs,
97
- max_length=max_length,
98
- use_cache=True,
99
- early_stopping=True,
100
- bos_token_id=model.config.bos_token_id,
101
- eos_token_id=model.config.eos_token_id,
102
- pad_token_id=model.config.eos_token_id,
103
- temperature=0.1,
104
- do_sample=True
105
- )
106
-
107
- # Decode the response
108
- response_text = tokenizer.decode(output[0], skip_special_tokens=True)
109
-
110
- return response_text
111
 
112
  # Define the device
113
  device = "cuda" if torch.cuda.is_available() else "cpu"
114
 
115
- # Use the base model's ID
116
  base_model_id = "mistralai/Mistral-7B-v0.1"
117
  model_directory = "Tonic/GaiaMiniMed"
118
 
119
  # Instantiate the Tokenizer
120
- tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1", trust_remote_code=True, padding_side="left")
121
- # tokenizer = AutoTokenizer.from_pretrained("Tonic/mistralmed", trust_remote_code=True, padding_side="left")
122
  tokenizer.pad_token = tokenizer.eos_token
123
  tokenizer.padding_side = 'left'
124
 
125
- # Load the GaiaMiniMed model with the specified configuration
126
-
127
- peft_config = PeftConfig.from_pretrained("Tonic/GaiaMiniMed")
128
- peft_model = AutoModelForCausalLM.from_pretrained("tiiuae/falcon-7b-instruct")
129
- peft_model = PeftModel.from_pretrained(model, "Tonic/GaiaMiniMed")
130
-
131
- # Specify the configuration class for the model
132
- #model_config = AutoConfig.from_pretrained(base_model_id)
133
-
134
- # Load the PEFT model with the specified configuration
135
- #peft_model = AutoModelForCausalLM.from_pretrained(base_model_id, config=model_config)
136
-
137
- # Load the PEFT model
138
- # peft_config = PeftConfig.from_pretrained("Tonic/mistralmed", token="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")
139
- # peft_model = MistralForCausalLM.from_pretrained("mistralai/Mistral-7B-v0.1", trust_remote_code=True)
140
- # peft_model = PeftModel.from_pretrained(peft_model, "Tonic/mistralmed", token="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")
141
-
142
- class ChatBot:
143
- def __init__(self):
144
- self.history = []
145
-
146
- class ChatBot:
147
- def __init__(self):
148
- # Initialize the ChatBot class with an empty history
149
- self.history = []
150
-
151
- def predict(self, user_input, system_prompt="You are an expert medical analyst:"):
152
- # Combine the user's input with the system prompt
153
- formatted_input = f"Question: {system_prompt} {user_input} Mini:"
154
-
155
- # Encode the formatted input using the tokenizer
156
- user_input_ids = tokenizer.encode(formatted_input, return_tensors="pt")
157
-
158
- # Generate a response using the PEFT model
159
- response = peft_model.generate(input_ids=user_input_ids, max_length=512, pad_token_id=tokenizer.eos_token_id)
 
 
 
 
160
 
161
  # Decode the generated response to text
162
  response_text = tokenizer.decode(response[0], skip_special_tokens=True)
163
-
164
- return response_text # Return the generated response
165
 
166
- bot = ChatBot()
167
-
168
- title = "馃憢馃徎Welcome to Tonic's GaiaMiniMed Chat馃殌"
169
- description = "You can use this Space to test out the current 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)."
170
- examples = [["What is the proper treatment for buccal herpes?", "You are a medicine and public health expert, you will receive a question, answer the question, and provide a complete answer"]]
 
 
 
 
 
 
 
 
 
 
 
 
 
171
 
172
  iface = gr.Interface(
173
- fn=bot.predict,
174
  title=title,
175
  description=description,
176
  examples=examples,
177
- inputs=["text", "text"], # Take user input and system prompt separately
 
 
178
  outputs="text",
179
  theme="ParityError/Anime"
180
  )
181
 
 
182
  iface.launch()
 
183
  ```
184
 
185
  ## Training Details
 
58
 
59
  ## How to Get Started with the Model
60
 
61
+ Try it here : [Pseudolab/GaiaMiniMed](https://huggingface.co/spaces/pseudolab/GaiaMiniMed)
62
+
63
+ See the [author's demo](https://huggingface.co/spaces/tonic/gaiaminimed)
64
+
65
  Use the code below to get started with the model.
66
 
67
  ```python
68
+ from transformers import AutoTokenizer, AutoModelForCausalLM
 
69
  import torch
70
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
  # Define the device
73
  device = "cuda" if torch.cuda.is_available() else "cpu"
74
 
75
+ # Use model IDs as variables
76
  base_model_id = "mistralai/Mistral-7B-v0.1"
77
  model_directory = "Tonic/GaiaMiniMed"
78
 
79
  # Instantiate the Tokenizer
80
+ tokenizer = AutoTokenizer.from_pretrained(base_model_id, trust_remote_code=True, padding_side="left")
 
81
  tokenizer.pad_token = tokenizer.eos_token
82
  tokenizer.padding_side = 'left'
83
 
84
+ # Load the Falcon model with the specified configuration
85
+ falcon_model = AutoModelForCausalLM.from_pretrained("tiiuae/falcon-7b-instruct")
86
+
87
+ # Class to encapsulate the Falcon chatbot
88
+ class FalconChatBot:
89
+ def __init__(self, system_prompt="You are an expert medical analyst:"):
90
+ self.system_prompt = system_prompt
91
+
92
+ def process_history(self, history):
93
+ # Filter out special commands from the history
94
+ filtered_history = []
95
+ for message in history:
96
+ user_message = message["user"]
97
+ assistant_message = message["assistant"]
98
+ # Check if the user_message is not a special command
99
+ if not user_message.startswith("Falcon:"):
100
+ filtered_history.append({"user": user_message, "assistant": assistant_message})
101
+ return filtered_history
102
+
103
+ def predict(self, input_data, max_length=500):
104
+ # Extract messages from the input data
105
+ preprompt = input_data["preprompt"]
106
+ history = input_data["history"]
107
+
108
+ # Process the history to remove special commands
109
+ processed_history = self.process_history(history)
110
+
111
+ # Generate the formatted conversation in Falcon message format
112
+ conversation = f"{preprompt}\n"
113
+ for message in processed_history:
114
+ user_message = message["user"]
115
+ assistant_message = message["assistant"]
116
+ conversation += f"User: {user_message}\nFalcon:{' ' + assistant_message if assistant_message else ''}\n"
117
+
118
+ # Encode the formatted conversation using the tokenizer
119
+ input_ids = tokenizer.encode(conversation, return_tensors="pt", add_special_tokens=False)
120
+
121
+ # Generate a response using the Falcon model
122
+ response = falcon_model.generate(input_ids, max_length=max_length, use_cache=True, early_stopping=True, bos_token_id=falcon_model.config.bos_token_id, eos_token_id=falcon_model.config.eos_token_id, pad_token_id=falcon_model.config.eos_token_id, temperature=0.1, do_sample=True)
123
 
124
  # Decode the generated response to text
125
  response_text = tokenizer.decode(response[0], skip_special_tokens=True)
 
 
126
 
127
+ return response_text
128
+
129
+ # Create the Falcon chatbot instance
130
+ falcon_bot = FalconChatBot()
131
+
132
+ # Define the Gradio interface
133
+ title = "馃憢馃徎Welcome to Falcon's Medical Expert Chat馃殌"
134
+ description = "You can use this Space to test out the Falcon model [(tiiuae/falcon-7b-instruct)](https://huggingface.co/tiiuae/falcon-7b-instruct) or duplicate this Space and use it locally or on 馃HuggingFace. [Join me on Discord to build together](https://discord.gg/VqTxc76K3u)."
135
+ examples = [{
136
+ "preprompt": "system message",
137
+ "history": [{
138
+ "user": "user message 1",
139
+ "assistant": "assistant message 1"
140
+ }, {
141
+ "user": "user message 1",
142
+ "assistant": None
143
+ }]
144
+ }]
145
 
146
  iface = gr.Interface(
147
+ fn=falcon_bot.predict,
148
  title=title,
149
  description=description,
150
  examples=examples,
151
+ inputs=[
152
+ gr.inputs.Textbox(label="Input Data", type="json),
153
+ ],
154
  outputs="text",
155
  theme="ParityError/Anime"
156
  )
157
 
158
+ # Launch the Gradio interface for the Falcon model
159
  iface.launch()
160
+
161
  ```
162
 
163
  ## Training Details