Views
No views yet
1from transformers import AutoConfig, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM, MistralForCausalLM
2from peft import PeftModel, PeftConfig
3import torch
4import gradio as gr
5import random
6from textwrap import wrap
7
8def wrap_text(text, width=90):
9 lines = text.split('\n')
10 wrapped_lines = [textwrap.fill(line, width=width) for line in lines]
11 wrapped_text = '\n'.join(wrapped_lines)
12 return wrapped_text
13
14def multimodal_prompt(user_input, system_prompt="You are an expert medical analyst:"):
15 formatted_input = f"<s>[INST]{system_prompt} {user_input}[/INST]"
16
17 encodeds = tokenizer(formatted_input, return_tensors="pt", add_special_tokens=False)
18 model_inputs = encodeds.to(device)
19
20 output = model.generate(
21 **model_inputs,
22 max_length=max_length,
23 use_cache=True,
24 early_stopping=True,
25 bos_token_id=model.config.bos_token_id,
26 eos_token_id=model.config.eos_token_id,
27 pad_token_id=model.config.eos_token_id,
28 temperature=0.1,
29 do_sample=True
30 )
31
32 response_text = tokenizer.decode(output[0], skip_special_tokens=True)
33
34 return response_text
35
36device = "cuda" if torch.cuda.is_available() else "cpu"
37
38base_model_id = "mistralai/Mistral-7B-v0.1"
39model_directory = "Tonic/mistralmed"
40
41tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1", trust_remote_code=True, padding_side="left")
42tokenizer.pad_token = tokenizer.eos_token
43tokenizer.padding_side = 'left'
44
45peft_config = PeftConfig.from_pretrained("Tonic/mistralmed", token="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")
46peft_model = MistralForCausalLM.from_pretrained("mistralai/Mistral-7B-v0.1", trust_remote_code=True)
47peft_model = PeftModel.from_pretrained(peft_model, "Tonic/mistralmed", token="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")
48
49class ChatBot:
50 def __init__(self):
51 self.history = []
52
53 def predict(self, user_input, system_prompt="You are an expert medical analyst:"):
54 formatted_input = f"<s>[INST]{system_prompt} {user_input}[/INST]"
55
56 user_input_ids = tokenizer.encode(formatted_input, return_tensors="pt")
57
58 response = peft_model.generate(input_ids=user_input_ids, max_length=512, pad_token_id=tokenizer.eos_token_id)
59
60 response_text = tokenizer.decode(response[0], skip_special_tokens=True)
61 return response_text
62
63bot = ChatBot()
64
65title = "👋🏻토닉의 미스트랄메드 채팅에 오신 것을 환영합니다🚀👋🏻Welcome to Tonic's MistralMed Chat🚀"
66description = "이 공간을 사용하여 현재 모델을 테스트할 수 있습니다. [(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)."
67examples = [["[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 complete the answer"]]
68
69iface = gr.Interface(
70 fn=bot.predict,
71 title=title,
72 description=description,
73 examples=examples,
74 inputs=["text", "text"],
75 outputs="text",
76 theme="ParityError/Anime"
77)
78
79iface.launch()1Dataset({
2 features: ['qtype', 'Question', 'Answer'],
3 num_rows: 16407
4})1MistralForCausalLM(
2 (model): MistralModel(
3 (embed_tokens): Embedding(32000, 4096)
4 (layers): ModuleList(
5 (0-31): 32 x MistralDecoderLayer(
6 (self_attn): MistralAttention(
7 (q_proj): Linear4bit(in_features=4096, out_features=4096, bias=False)
8 (k_proj): Linear4bit(in_features=4096, out_features=1024, bias=False)
9 (v_proj): Linear4bit(in_features=4096, out_features=1024, bias=False)
10 (o_proj): Linear4bit(in_features=4096, out_features=4096, bias=False)
11 (rotary_emb): MistralRotaryEmbedding()
12 )
13 (mlp): MistralMLP(
14 (gate_proj): Linear4bit(in_features=4096, out_features=14336, bias=False)
15 (up_proj): Linear4bit(in_features=4096, out_features=14336, bias=False)
16 (down_proj): Linear4bit(in_features=14336, out_features=4096, bias=False)
17 (act_fn): SiLUActivation()
18 )
19 (input_layernorm): MistralRMSNorm()
20 (post_attention_layernorm): MistralRMSNorm()
21 )
22 )
23 (norm): MistralRMSNorm()
24 )
25 (lm_head): Linear(in_features=4096, out_features=32000, bias=False)
26)1config = LoraConfig(
2 r=8,
3 lora_alpha=16,
4 target_modules=[
5 "q_proj",
6 "k_proj",
7 "v_proj",
8 "o_proj",
9 "gate_proj",
10 "up_proj",
11 "down_proj",
12 "lm_head",
13 ],
14 bias="none",
15 lora_dropout=0.05, # Conventional
16 task_type="CAUSAL_LM",
17)| Step | Training Loss |
|---|---|
| 50 | 0.474200 |
| 100 | 0.523300 |
| 150 | 0.484500 |
| 200 | 0.482800 |
| 250 | 0.498800 |
| 300 | 0.451800 |
| 350 | 0.491800 |
| 400 | 0.488000 |
| 450 | 0.472800 |
| 500 | 0.460400 |
| 550 | 0.464700 |
| 600 | 0.484800 |
| 650 | 0.474600 |
| 700 | 0.477900 |
| 750 | 0.445300 |
| 800 | 0.431300 |
| 850 | 0.461500 |
| 900 | 0.451200 |
| 950 | 0.470800 |
| 1000 | 0.454900 |
1PeftModelForCausalLM(
2 (base_model): LoraModel(
3 (model): MistralForCausalLM(
4 (model): MistralModel(
5 (embed_tokens): Embedding(32000, 4096)
6 (layers): ModuleList(
7 (0-31): 32 x MistralDecoderLayer(
8 (self_attn): MistralAttention(
9 (q_proj): Linear4bit(
10 (lora_dropout): ModuleDict(
11 (default): Dropout(p=0.05, inplace=False)
12 )
13 (lora_A): ModuleDict(
14 (default): Linear(in_features=4096, out_features=8, bias=False)
15 )
16 (lora_B): ModuleDict(
17 (default): Linear(in_features=8, out_features=4096, bias=False)
18 )
19 (lora_embedding_A): ParameterDict()
20 (lora_embedding_B): ParameterDict()
21 (base_layer): Linear4bit(in_features=4096, out_features=4096, bias=False)
22 )
23 (k_proj): Linear4bit(
24 (lora_dropout): ModuleDict(
25 (default): Dropout(p=0.05, inplace=False)
26 )
27 (lora_A): ModuleDict(
28 (default): Linear(in_features=4096, out_features=8, bias=False)
29 )
30 (lora_B): ModuleDict(
31 (default): Linear(in_features=8, out_features=1024, bias=False)
32 )
33 (lora_embedding_A): ParameterDict()
34 (lora_embedding_B): ParameterDict()
35 (base_layer): Linear4bit(in_features=4096, out_features=1024, bias=False)
36 )
37 (v_proj): Linear4bit(
38 (lora_dropout): ModuleDict(
39 (default): Dropout(p=0.05, inplace=False)
40 )
41 (lora_A): ModuleDict(
42 (default): Linear(in_features=4096, out_features=8, bias=False)
43 )
44 (lora_B): ModuleDict(
45 (default): Linear(in_features=8, out_features=1024, bias=False)
46 )
47 (lora_embedding_A): ParameterDict()
48 (lora_embedding_B): ParameterDict()
49 (base_layer): Linear4bit(in_features=4096, out_features=1024, bias=False)
50 )
51 (o_proj): Linear4bit(
52 (lora_dropout): ModuleDict(
53 (default): Dropout(p=0.05, inplace=False)
54 )
55 (lora_A): ModuleDict(
56 (default): Linear(in_features=4096, out_features=8, bias=False)
57 )
58 (lora_B): ModuleDict(
59 (default): Linear(in_features=8, out_features=4096, bias=False)
60 )
61 (lora_embedding_A): ParameterDict()
62 (lora_embedding_B): ParameterDict()
63 (base_layer): Linear4bit(in_features=4096, out_features=4096, bias=False)
64 )
65 (rotary_emb): MistralRotaryEmbedding()
66 )
67 (mlp): MistralMLP(
68 (gate_proj): Linear4bit(
69 (lora_dropout): ModuleDict(
70 (default): Dropout(p=0.05, inplace=False)
71 )
72 (lora_A): ModuleDict(
73 (default): Linear(in_features=4096, out_features=8, bias=False)
74 )
75 (lora_B): ModuleDict(
76 (default): Linear(in_features=8, out_features=14336, bias=False)
77 )
78 (lora_embedding_A): ParameterDict()
79 (lora_embedding_B): ParameterDict()
80 (base_layer): Linear4bit(in_features=4096, out_features=14336, bias=False)
81 )
82 (up_proj): Linear4bit(
83 (lora_dropout): ModuleDict(
84 (default): Dropout(p=0.05, inplace=False)
85 )
86 (lora_A): ModuleDict(
87 (default): Linear(in_features=4096, out_features=8, bias=False)
88 )
89 (lora_B): ModuleDict(
90 (default): Linear(in_features=8, out_features=14336, bias=False)
91 )
92 (lora_embedding_A): ParameterDict()
93 (lora_embedding_B): ParameterDict()
94 (base_layer): Linear4bit(in_features=4096, out_features=14336, bias=False)
95 )
96 (down_proj): Linear4bit(
97 (lora_dropout): ModuleDict(
98 (default): Dropout(p=0.05, inplace=False)
99 )
100 (lora_A): ModuleDict(
101 (default): Linear(in_features=14336, out_features=8, bias=False)
102 )
103 (lora_B): ModuleDict(
104 (default): Linear(in_features=8, out_features=4096, bias=False)
105 )
106 (lora_embedding_A): ParameterDict()
107 (lora_embedding_B): ParameterDict()
108 (base_layer): Linear4bit(in_features=14336, out_features=4096, bias=False)
109 )
110 (act_fn): SiLUActivation()
111 )
112 (input_layernorm): MistralRMSNorm()
113 (post_attention_layernorm): MistralRMSNorm()
114 )
115 )
116 (norm): MistralRMSNorm()
117 )
118 (lm_head): Linear(
119 in_features=4096, out_features=32000, bias=False
120 (lora_dropout): ModuleDict(
121 (default): Dropout(p=0.05, inplace=False)
122 )
123 (lora_A): ModuleDict(
124 (default): Linear(in_features=4096, out_features=8, bias=False)
125 )
126 (lora_B): ModuleDict(
127 (default): Linear(in_features=8, out_features=32000, bias=False)
128 )
129 (lora_embedding_A): ParameterDict()
130 (lora_embedding_B): ParameterDict()
131 )
132 )
133 )
134)bitsandbytes quantization config was used during training: