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

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +121 -1
README.md CHANGED
@@ -60,7 +60,127 @@ This model is not meant as a decision support system in the wild, only for educa
60
 
61
  Use the code below to get started with the model.
62
 
63
- {{ get_started_code | default("[More Information Needed]", true)}}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
  ## Training Details
66
 
 
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
186