Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
2import torch
3
4base_model = "meta-llama/Meta-Llama-3-8B-Instruct"
5device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
6
7bnb_config = BitsAndBytesConfig(
8 load_in_4bit=True,
9 bnb_4bit_quant_type="nf4",
10 bnb_4bit_compute_dtype=torch.float16,
11 bnb_4bit_use_double_quant=True,
12)
13
14model = AutoModelForCausalLM.from_pretrained(
15 base_model,
16 quantization_config=bnb_config,
17 device_map={"": 0},
18 attn_implementation="eager"
19)
20
21tokenizer = AutoTokenizer.from_pretrained(base_model, trust_remote_code=True)
22tokenizer.pad_token = '<|pad|>'
23tokenizer.pad_token_id = 128255
24
25#Load LORA weights
26model.load_adapter("Matej/FoodyLLM")
27model.config.use_cache = True
28model.eval()1system_prompt = ""
2user_prompt = "Compute the nutrient values per 100 grams in a recipe with the following ingredients: 250 g cream, whipped, cream topping, pressurized, 250 g yogurt, greek, plain, nonfat, 50 g sugars, powdered"
3
4messages = [
5 {
6 "role": "user",
7 "content": f"{system_prompt} {user_prompt}".strip()
8 }
9]
10
11prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
12
13#Here we have a batch of one
14tokenizer_input = [prompt]
15
16inputs = tokenizer(tokenizer_input, return_tensors="pt", padding=True, truncation=True, max_length=1024).to(device)
17generated_ids = model.generate(**inputs, max_new_tokens=1024, do_sample=True)
18answers = tokenizer.batch_decode(generated_ids[:, inputs['input_ids'].shape[1]:])
19answers = [x.split('<|eot_id|>')[0].strip() for x in answers]
20print(answers[0])1user_prompt = "Review the fsa traffic lights per 100 grams in a recipe using the following ingredients: 1/2 cup soup, swanson chicken broth 99% fat free, 1 pinch salt, table"
2
3messages = [
4 {
5 "role": "user",
6 "content": f"{system_prompt} {user_prompt}".strip()
7 }
8]
9
10prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
11
12# Here we have a batch of one
13tokenizer_input = [prompt]
14
15inputs = tokenizer(tokenizer_input, return_tensors="pt", padding=True, truncation=True, max_length=1024).to(device)
16generated_ids = model.generate(**inputs, max_new_tokens=1024, do_sample=True)
17answers = tokenizer.batch_decode(generated_ids[:, inputs['input_ids'].shape[1]:])
18answers = [x.split('<|eot_id|>')[0].strip() for x in answers]
19print(answers[0])1user_prompt = "Retrieve all food entities referenced in the text: Line a large colander with a cheesecloth. Stir salt into the yogurt, and pour the yogurt into the cheesecloth. Set the colander in the sink or bowl to catch the liquid that drains off. Leave to drain for 24 hours. After draining for the 24 hours, transfer the resulting cheese to a bowl. Stir in the olive oil. Store in a covered container in the refrigerator."
2messages = [
3 {
4 "role": "user",
5 "content": f"{system_prompt} {user_prompt}".strip()
6 }
7]
8
9prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
10
11# Here we have a batch of one
12tokenizer_input = [prompt]
13
14inputs = tokenizer(tokenizer_input, return_tensors="pt", padding=True, truncation=True, max_length=1024).to(device)
15generated_ids = model.generate(**inputs, max_new_tokens=1024, do_sample=True)
16answers = tokenizer.batch_decode(generated_ids[:, inputs['input_ids'].shape[1]:])
17answers = [x.split('<|eot_id|>')[0].strip() for x in answers]
18print(answers[0])1user_prompt = "Link the following food entities to a SNOMEDCT ontology: cream cheese, meat"
2messages = [
3 {
4 "role": "user",
5 "content": f"{system_prompt} {user_prompt}".strip()
6 }
7]
8
9prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
10
11# Here we have a batch of one
12tokenizer_input = [prompt]
13
14inputs = tokenizer(tokenizer_input, return_tensors="pt", padding=True, truncation=True, max_length=1024).to(device)
15generated_ids = model.generate(**inputs, max_new_tokens=1024, do_sample=True)
16answers = tokenizer.batch_decode(generated_ids[:, inputs['input_ids'].shape[1]:])
17answers = [x.split('<|eot_id|>')[0].strip() for x in answers]
18print(answers[0])