Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,103 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
from
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
st.session_state.pipe = pipeline('text-generation', model='mistralai/Mixtral-8x7B-Instruct-v0.1')
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you?"}]
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import transformers
|
| 3 |
+
from datasets import load_dataset
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, Trainer, TrainingArguments
|
| 5 |
+
from peft import prepare_model_for_kbit_training, LoraConfig, get_peft_model, PeftModel
|
| 6 |
|
| 7 |
+
model_name = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 8 |
+
new_model = "sedataskan/mistral8x7B-finetuned"
|
| 9 |
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
|
| 11 |
|
| 12 |
+
model = AutoModelForCausalLM.from_pretrained(model_name,
|
| 13 |
+
load_in_4bit=True,
|
| 14 |
+
torch_dtype=torch.float16,
|
| 15 |
+
device_map="auto")
|
| 16 |
+
tokenizer.pad_token = "!"
|
| 17 |
|
| 18 |
+
LORA_R = 8
|
| 19 |
+
LORA_ALPHA = 2 * LORA_R
|
| 20 |
+
LORA_DROPOUT = 0.1
|
|
|
|
| 21 |
|
| 22 |
+
config = LoraConfig(
|
| 23 |
+
r=LORA_R,
|
| 24 |
+
lora_alpha=LORA_ALPHA,
|
| 25 |
+
target_modules=[ "w1", "w2", "w3"], # Only Training the "expert" layers
|
| 26 |
+
lora_dropout=LORA_DROPOUT,
|
| 27 |
+
bias="none",
|
| 28 |
+
task_type="CAUSAL_LM"
|
| 29 |
+
)
|
| 30 |
|
| 31 |
+
model = get_peft_model(model, config)
|
| 32 |
+
|
| 33 |
+
def print_trainable_parameters(m):
|
| 34 |
+
trainable_params = sum(p.numel() for p in m.parameters() if p.requires_grad)
|
| 35 |
+
all_params = sum(p.numel() for p in m.parameters())
|
| 36 |
+
print(f"trainable params: {trainable_params} || all params: {all_params} || trainable%: {100 * trainable_params / all_params}")
|
| 37 |
+
|
| 38 |
+
print_trainable_parameters(model)
|
| 39 |
+
|
| 40 |
+
train_data = load_dataset("oscar-corpus/OSCAR-2201")
|
| 41 |
+
print("Dataset", train_data)
|
| 42 |
+
|
| 43 |
+
def generate_prompt(user_query, sep="\n\n### "): #The prompt format is taken from the official Mixtral huggingface page
|
| 44 |
+
sys_msg= "Take a look at the following instructions and try to follow them."
|
| 45 |
+
p = "<s> [INST]" + sys_msg +"\n"+ user_query["instruction"] + "[/INST]" + user_query["output"] + "</s>"
|
| 46 |
+
return p
|
| 47 |
+
|
| 48 |
+
max_len = 1024
|
| 49 |
+
|
| 50 |
+
def tokenize(prompt):
|
| 51 |
+
return tokenizer(
|
| 52 |
+
prompt + tokenizer.eos_token,
|
| 53 |
+
truncation=True,
|
| 54 |
+
max_length=max_len,
|
| 55 |
+
padding="max_length"
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
train_data = train_data.shuffle().map(lambda x: tokenize(generate_prompt(x)), remove_columns=["instruction" , "output"])
|
| 59 |
+
|
| 60 |
+
trainer = Trainer(
|
| 61 |
+
model=model,
|
| 62 |
+
train_dataset=train_data,
|
| 63 |
+
args=TrainingArguments(
|
| 64 |
+
per_device_train_batch_size=1,
|
| 65 |
+
gradient_accumulation_steps=4,
|
| 66 |
+
num_train_epochs=6,
|
| 67 |
+
learning_rate=1e-4,
|
| 68 |
+
logging_steps=2,
|
| 69 |
+
optim="adamw_torch",
|
| 70 |
+
save_strategy="epoch"
|
| 71 |
+
),
|
| 72 |
+
data_collator=transformers.DataCollatorForLanguageModeling(tokenizer, mlm=False)
|
| 73 |
+
)
|
| 74 |
+
model.config.use_cache = False
|
| 75 |
+
|
| 76 |
+
# Train model
|
| 77 |
+
trainer.train()
|
| 78 |
+
# Save trained model
|
| 79 |
+
trainer.model.save_pretrained(new_model)
|
| 80 |
+
tokenizer.save_pretrained(new_model)
|
| 81 |
+
|
| 82 |
+
# Push them to the HF Hub
|
| 83 |
+
trainer.model.push_to_hub(new_model, use_temp_dir=False, token="")
|
| 84 |
+
tokenizer.push_to_hub(new_model, use_temp_dir=False, token="")
|
| 85 |
+
|
| 86 |
+
# Format prompt
|
| 87 |
+
message = [
|
| 88 |
+
"Türkiye'nin başkenti neresidir?"
|
| 89 |
+
]
|
| 90 |
+
tokenizer = AutoTokenizer.from_pretrained(new_model)
|
| 91 |
+
prompt = tokenizer(message, return_tensors="pt", padding=True)
|
| 92 |
+
# Generate output
|
| 93 |
+
output = trainer.model.generate(
|
| 94 |
+
input_ids=prompt.input_ids,
|
| 95 |
+
attention_mask=prompt.attention_mask,
|
| 96 |
+
max_length=128,
|
| 97 |
+
do_sample=True,
|
| 98 |
+
top_p=0.95,
|
| 99 |
+
top_k=60,
|
| 100 |
+
num_return_sequences=1,
|
| 101 |
+
)
|
| 102 |
+
# Print output
|
| 103 |
+
print(tokenizer.batch_decode(output, skip_special_tokens=True))
|