Views
No views yet
transformers library installed. You can install it via pip:pip install transformerstorch with CUDA support:pip install torch1from transformers import AutoTokenizer, AutoModelForCausalLM
2
3# Replace with the name of this repository
4model_name = "XiWangEric/literary-classicist-llama3-qlora"
5
6# Load the tokenizer
7tokenizer = AutoTokenizer.from_pretrained(model_name)
8
9# Load the model
10model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype="auto", device_map="auto")
11
12# Set the model to evaluation mode
13model.eval()1# Define your input prompt
2input_text = "Once upon a time in a faraway land,"
3
4# Tokenize the input and prepare for inference
5inputs = tokenizer(input_text, return_tensors="pt").to("cuda") # Move tensors to the GPU
6
7# Generate text
8outputs = model.generate(**inputs, max_length=50, do_sample=True, top_k=50, top_p=0.95)
9
10# Decode the output
11generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
12print("Generated Text:", generated_text)max_length: The maximum length of the generated sequence.do_sample: Whether to sample the next token or pick the most probable one.top_k: The number of highest probability vocabulary tokens to keep for sampling.top_p: The cumulative probability threshold for nucleus sampling.1outputs = model.generate(
2 **inputs,
3 max_length=100,
4 do_sample=True,
5 top_k=40,
6 top_p=0.9
7)Once upon a time in a faraway land,Once upon a time in a faraway land, there was a beautiful castle surrounded by an enchanted forest. The villagers spoke of a hidden treasure deep within the woods, guarded by a magical creature of legend.