Views
No views yet
1from transformers import AutoTokenizer, AutoModelForCausalLM
2
3tokenizer = AutoTokenizer.from_pretrained("ishitas2365/llama-3.2-3b-instruct-finetunedToPersona")
4model = AutoModelForCausalLM.from_pretrained("ishitas2365/llama-3.2-3b-instruct-finetunedToPersona")
5
6tokenizer.pad_token_id = tokenizer.eos_token_id
7
8# Enter the characteristics of persona in system prompt and the initial dialogue of the user in user prompt
9messages = [
10 {
11 "role": "system",
12 "content": "Persona B's characteristics: My name is David, and I'm a 35-year-old math teacher. "
13 "I like to hike and spend time in nature. I'm married with two kids."
14 },
15 {
16 "role": "user",
17 "content": "Morning! I think I saw you at the parent meeting, what's your name?"
18 }
19]
20
21prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
22inputs = tokenizer(prompt, return_tensors='pt', padding=True, truncation=True).to("cuda")
23with torch.no_grad():
24 outputs = model.generate(
25 input_ids=inputs.input_ids,
26 attention_mask=inputs.attention_mask,
27 max_length=200,
28 num_return_sequences=1,
29 temperature=0.8,
30 top_p=0.9
31 )
32decoded_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
33if "assistant" in decoded_text:
34 response = decoded_text.split("assistant", 1)[1].strip()
35else:
36 response = decoded_text.strip()
37
38print("Assistant's Reply:", response)