Update README.md
Browse files
README.md
CHANGED
@@ -58,7 +58,124 @@ Further fine tunes and uses would include :
|
|
58 |
|
59 |
Use the code below to get started with the model.
|
60 |
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
## Training Details
|
64 |
|
|
|
58 |
|
59 |
Use the code below to get started with the model.
|
60 |
|
61 |
+
|
62 |
+
```Python
|
63 |
+
|
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"<s>[INST]{system_prompt} {user_input}[/INST]"
|
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 = "HuggingFaceH4/zephyr-7b-beta"
|
117 |
+
model_directory = "pseudolab/K23_MiniMed"
|
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 |
+
# Specify the configuration class for the model
|
126 |
+
#model_config = AutoConfig.from_pretrained(base_model_id)
|
127 |
+
|
128 |
+
# Load the PEFT model with the specified configuration
|
129 |
+
#peft_model = AutoModelForCausalLM.from_pretrained(base_model_id, config=model_config)
|
130 |
+
|
131 |
+
# Load the PEFT model
|
132 |
+
peft_config = PeftConfig.from_pretrained("pseudolab/K23_MiniMed")
|
133 |
+
peft_model = MistralForCausalLM.from_pretrained("https://huggingface.co/HuggingFaceH4/zephyr-7b-beta", trust_remote_code=True)
|
134 |
+
peft_model = PeftModel.from_pretrained(peft_model, "pseudolab/K23_MiniMed)
|
135 |
+
|
136 |
+
class ChatBot:
|
137 |
+
def __init__(self):
|
138 |
+
self.history = []
|
139 |
+
|
140 |
+
class ChatBot:
|
141 |
+
def __init__(self):
|
142 |
+
# Initialize the ChatBot class with an empty history
|
143 |
+
self.history = []
|
144 |
+
|
145 |
+
def predict(self, user_input, system_prompt="You are an expert medical analyst:"):
|
146 |
+
# Combine the user's input with the system prompt
|
147 |
+
formatted_input = f"<s>[INST]{system_prompt} {user_input}[/INST]"
|
148 |
+
|
149 |
+
# Encode the formatted input using the tokenizer
|
150 |
+
user_input_ids = tokenizer.encode(formatted_input, return_tensors="pt")
|
151 |
+
|
152 |
+
# Generate a response using the PEFT model
|
153 |
+
response = peft_model.generate(input_ids=user_input_ids, max_length=512, pad_token_id=tokenizer.eos_token_id)
|
154 |
+
|
155 |
+
# Decode the generated response to text
|
156 |
+
response_text = tokenizer.decode(response[0], skip_special_tokens=True)
|
157 |
+
|
158 |
+
return response_text # Return the generated response
|
159 |
+
|
160 |
+
bot = ChatBot()
|
161 |
+
|
162 |
+
title = "๐๐ปํ ๋์ ๋ฏธ์คํธ๋๋ฉ๋ ์ฑํ
์ ์ค์ ๊ฒ์ ํ์ํฉ๋๋ค๐๐๐ปWelcome to Tonic's MistralMed Chat๐"
|
163 |
+
description = "์ด ๊ณต๊ฐ์ ์ฌ์ฉํ์ฌ ํ์ฌ ๋ชจ๋ธ์ ํ
์คํธํ ์ ์์ต๋๋ค. [(Tonic/MistralMed)](https://huggingface.co/Tonic/MistralMed) ๋๋ ์ด ๊ณต๊ฐ์ ๋ณต์ ํ๊ณ ๋ก์ปฌ ๋๋ ๐คHuggingFace์์ ์ฌ์ฉํ ์ ์์ต๋๋ค. [Discord์์ ํจ๊ป ๋ง๋ค๊ธฐ ์ํด Discord์ ๊ฐ์
ํ์ญ์์ค](https://discord.gg/VqTxc76K3u). You can use this Space to test out the current model [(Tonic/MistralMed)](https://huggingface.co/Tonic/MistralMed) or duplicate this Space and use it locally or on ๐คHuggingFace. [Join me on Discord to build together](https://discord.gg/VqTxc76K3u)."
|
164 |
+
examples = [["[Question:] 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"]]
|
165 |
+
|
166 |
+
iface = gr.Interface(
|
167 |
+
fn=bot.predict,
|
168 |
+
title=title,
|
169 |
+
description=description,
|
170 |
+
examples=examples,
|
171 |
+
inputs=["text", "text"], # Take user input and system prompt separately
|
172 |
+
outputs="text",
|
173 |
+
theme="ParityError/Anime"
|
174 |
+
)
|
175 |
+
|
176 |
+
iface.launch()
|
177 |
+
|
178 |
+
```
|
179 |
|
180 |
## Training Details
|
181 |
|