Views
No views yet
1import torch
2from peft import AutoPeftModelForCausalLM
3from transformers import AutoTokenizer, pipeline
4
5peft_model_id = "Andyrasika/Gemma-ChatML"
6
7# Load Model with PEFT adapter
8tokenizer = AutoTokenizer.from_pretrained(peft_model_id)
9model = AutoPeftModelForCausalLM.from_pretrained(peft_model_id, device_map="auto", torch_dtype=torch.float16)
10pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
11# get token id for end of conversation
12eos_token = tokenizer("<|im_end|>",add_special_tokens=False)["input_ids"][0]1prompts = [
2 "What is the capital of Germany? Explain why thats the case and if it was different in the past?",
3 "Write a Python function to calculate the factorial of a number.",
4 "A rectangular garden has a length of 25 feet and a width of 15 feet. If you want to build a fence around the entire garden, how many feet of fencing will you need?",
5 "What is the difference between a fruit and a vegetable? Give examples of each.",
6]
7
8def test_inference(prompt):
9 prompt = pipe.tokenizer.apply_chat_template([{"role": "user", "content": prompt}], tokenize=False, add_generation_prompt=True)
10 outputs = pipe(prompt, max_new_tokens=1024, do_sample=True, temperature=0.7, top_k=50, top_p=0.95, eos_token_id=eos_token)
11 return outputs[0]['generated_text'][len(prompt):].strip()
12
13
14for prompt in prompts:
15 print(f" prompt:\n{prompt}")
16 print(f" response:\n{test_inference(prompt)}")
17 print("-"*50) prompt:
What is the capital of Germany? Explain why thats the case and if it was different in the past?
/opt/conda/envs/pytorch/lib/python3.10/site-packages/transformers/pipelines/base.py:1157: UserWarning: You seem to be using the pipelines sequentially on GPU. In order to maximize efficiency please use a dataset
warnings.warn(
response:
The capital of Germany is Berlin. Berlin is the capital of Germany because it is the largest city in Germany, and it is the political center of the country. Berlin is also home to the German parliament, the Bundestag, and the federal government. In the past, the capital of Germany was Berlin, but it was divided into two parts during the Cold War. The western part of Berlin was controlled by the United States, France, and the United Kingdom, while the eastern part was controlled by the Soviet Union. Berlin was also the site of the Berlin Wall, which separated the two sides of the city from 1961 to 1989. The fall of the Berlin Wall marked the end of the Cold War, and Germany was reunified in 1990. Berlin has since become the capital of a reunified Germany.
--------------------------------------------------
prompt:
Write a Python function to calculate the factorial of a number.
response:
def factorial(n):
fact = 1
for i in range(1, n+1):
fact = fact * i
return fact
--------------------------------------------------
prompt:
A rectangular garden has a length of 25 feet and a width of 15 feet. If you want to build a fence around the entire garden, how many feet of fencing will you need?
response:
The total amount of fencing needed is 70 feet. This is determined by multiplying the perimeter of the garden (which is 70 feet) by the cost per foot of the fencing material.
--------------------------------------------------
prompt:
What is the difference between a fruit and a vegetable? Give examples of each.
response:
A fruit is the part of a plant that contains the seeds. Examples of fruits are apples, oranges, strawberries, and grapes. A vegetable is the part of a plant that is used for food but does not contain seeds. Examples of vegetables are carrots, potatoes, broccoli, and lettuce.
--------------------------------------------------