-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestllama.py
66 lines (52 loc) · 1.96 KB
/
testllama.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
from accelerate import load_checkpoint_and_dispatch, init_empty_weights
model_path = r"/home/ubuntu/marketxmind/Llama-3_2_1B_Instruct/"
# Initialize model with empty weights
with init_empty_weights():
model = AutoModelForCausalLM.from_pretrained(
model_path,
torch_dtype=torch.bfloat16 # Adjust to torch.float16 if needed
)
# Tie the model weights
model.tie_weights()
# Load the model with offloading and device mapping
model = load_checkpoint_and_dispatch(
model,
model_path,
device_map="auto",
offload_folder=r"/home/ubuntu/marketxmind/offload/"
)
# Initialize the tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_path)
# Create the pipeline
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
torch_dtype=torch.bfloat16,
device_map="auto",
pad_token_id=tokenizer.eos_token_id # Set pad_token_id explicitly
)
# Define conversation structure
conversation = [
{"role": "system", "content": "Hi, I am a market-X-mind chatbot who is a business analyst specializing in customer loyalty and engagement strategies."},
]
# Simulate user input
user_input = "hi"
if user_input:
conversation.append({"role": "user", "content": user_input})
try:
# Convert conversation to a single prompt text for the pipeline
prompt_text = "\n".join([f"{msg['role']}: {msg['content']}" for msg in conversation])
# Generate response
response = pipe(prompt_text, max_new_tokens=500)
assistant_response = response[0]['generated_text'].strip()
# Append assistant's response to conversation
conversation.append({"role": "assistant", "content": assistant_response})
# Print assistant response
print(assistant_response)
except Exception as e:
print(f"Error: {e}")
else:
print("Please enter a valid message.")