Welcome to WordPress. This is your first post. Edit or delete it, then start writing!
NREW
tert ertwertwert ertwe r
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
# Set the random seed for reproducibility
torch.manual_seed(0)
# Check if CUDA is available; use GPU if available, otherwise use CPU
device = "cuda" if torch.cuda.is_available() else "cpu"
# Load the model with the appropriate device map and other settings
model = AutoModelForCausalLM.from_pretrained(
"microsoft/Phi-3-mini-4k-instruct",
# Use "auto" for CUDA, otherwise None
device_map="auto" if device == "cuda" else None,
torch_dtype="auto",
trust_remote_code=True,
# Specify attention implementation
attn_implementation="eager"
)
# Load the tokenizer
tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct")
# Define the messages for the model
messages = [
{"role": "system", "content": "You are a Python developer."},
{"role": "user", "content": "Help me generate a bubble sort algorithm."},
]
# Initialize the pipeline for text generation
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
)
# Define generation arguments
generation_args = {
"max_new_tokens": 600,
"return_full_text": False,
"temperature": 0.3,
"do_sample": True,
}
# Generate text using the pipeline
output = pipe(messages, **generation_args)
# Print the generated text
print(output[0]['generated_text'])
Python