maomao88/anime-waifu-personality-chat-with-questions
Viewer • Updated • 744 • 109 • 8
How to use maomao88/anime-waifu-mistral-lora with PEFT:
from peft import PeftModel
from transformers import AutoModelForCausalLM
base_model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1")
model = PeftModel.from_pretrained(base_model, "maomao88/anime-waifu-mistral-lora")This repository contains a LoRA adapter trained with the maomao88/anime-waifu-personality-chat-with-questions dataset via Supervised Fine-Tuning (SFT) to add anime-style personalities to the Mistral-7B-Instruct model.
The personalities supported are:
You can load this adapter with the following code:
from transformers import AutoModelForCausalLM
from peft import PeftModel
from transformers import AutoTokenizer
import torch
base_model = AutoModelForCausalLM.from_pretrained(
"mistralai/Mistral-7B-Instruct-v0.1"
)
tokenizer = AutoTokenizer.from_pretrained(
"mistralai/Mistral-7B-Instruct-v0.1"
)
model = PeftModel.from_pretrained(
base_model,
"maomao88/anime-waifu-mistral-lora"
)
def chat_with_personality(trait, user_input, max_new_tokens=100, temperature=0.8):
"""
Generate a response from the fine-tuned model using a given personality trait.
"""
messages = [
{
"role": "system",
"content": f"You are an anime character with the following personality: {trait}."
},
{
"role": "user",
"content": user_input
}
]
# Apply Mistral's chat template
prompt = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=max_new_tokens,
do_sample=True,
temperature=temperature,
top_p=0.9,
pad_token_id=tokenizer.eos_token_id,
repetition_penalty=1.1
)
# Decode only the generated text portion (skip <s> etc.)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Remove the prompt portion to keep only the assistant reply
if "[/INST]" in response:
response = response.split("[/INST]")[-1].strip()
return response
chat_with_personality("tsundere", "What do you think of the moon?")
# Response Example: It’s not like I care about the moon or anything… but it sure is pretty tonight!
The anime characters supported are as follows:
Base model
mistralai/Mistral-7B-v0.1