Upload folder using huggingface_hub
Browse files- .gitattributes +1 -0
- .gitignore +4 -0
- README.md +78 -3
- adapter_config.json +34 -0
- adapter_model.safetensors +3 -0
- apply.py +50 -0
- requirements.txt +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
*.psd filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
.idea/
|
| 3 |
+
|
| 4 |
+
|
README.md
CHANGED
|
@@ -1,3 +1,78 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: mit
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
base_model:
|
| 6 |
+
- meta-llama/Meta-Llama-3-8B-Instruct
|
| 7 |
+
pipeline_tag: text-generation
|
| 8 |
+
tags:
|
| 9 |
+
- Food
|
| 10 |
+
- nutrient prediction
|
| 11 |
+
- healthy diet
|
| 12 |
+
- Food NEL
|
| 13 |
+
- Food NER
|
| 14 |
+
---
|
| 15 |
+
# 'FoodyLLM: A FAIR-aligned specialized large language model for food and nutrition analysis'
|
| 16 |
+
|
| 17 |
+
## The model is based on Meta-Llama-3-8B-Instruct, which was fine-tuned (using LoRA) for food and nutrition analysis.
|
| 18 |
+
|
| 19 |
+
More specifically, it can conduct the following tasks:
|
| 20 |
+
- Assessing recipe nutritional profiles
|
| 21 |
+
- Classifying recipes by traffic light nutrition labels (see https://www.food.gov.uk/safety-hygiene/check-the-label for details on the labeling)
|
| 22 |
+
- Extract food named entities from text (Food NER)
|
| 23 |
+
- Link the food entities to three distinct ontologies, Hansard taxonomy, FoodOn and SNOMED-CT (Food NEL)
|
| 24 |
+
|
| 25 |
+
## How to use it: ##
|
| 26 |
+
|
| 27 |
+
```python
|
| 28 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 29 |
+
import torch
|
| 30 |
+
|
| 31 |
+
if __name__ == '__main__':
|
| 32 |
+
base_model = "meta-llama/Meta-Llama-3-8B-Instruct"
|
| 33 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 34 |
+
|
| 35 |
+
bnb_config = BitsAndBytesConfig(
|
| 36 |
+
load_in_4bit=True,
|
| 37 |
+
bnb_4bit_quant_type="nf4",
|
| 38 |
+
bnb_4bit_compute_dtype=torch.float16,
|
| 39 |
+
bnb_4bit_use_double_quant=True,
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 43 |
+
base_model,
|
| 44 |
+
quantization_config=bnb_config,
|
| 45 |
+
device_map={"": 0},
|
| 46 |
+
attn_implementation="eager"
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model, trust_remote_code=True)
|
| 50 |
+
tokenizer.pad_token = '<|pad|>'
|
| 51 |
+
tokenizer.pad_token_id = 128255
|
| 52 |
+
|
| 53 |
+
#Load LORA weights
|
| 54 |
+
model.load_adapter("Matej/FoodyLLM")
|
| 55 |
+
model.config.use_cache = True
|
| 56 |
+
model.eval()
|
| 57 |
+
|
| 58 |
+
system_prompt = ""
|
| 59 |
+
user_prompt = "Calculate the nutrient values per 100 grams in a recipe with these ingredients: 10 ounce candies, marshmallows, 1/4 cup butter, without salt, 6 cup cocoa, dry powder, unsweetened"
|
| 60 |
+
|
| 61 |
+
messages = [
|
| 62 |
+
{
|
| 63 |
+
"role": "user",
|
| 64 |
+
"content": f"{system_prompt} {user_prompt}".strip()
|
| 65 |
+
}
|
| 66 |
+
]
|
| 67 |
+
|
| 68 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 69 |
+
|
| 70 |
+
#Here we have a batch of one
|
| 71 |
+
tokenizer_input = [prompt]
|
| 72 |
+
|
| 73 |
+
inputs = tokenizer(tokenizer_input, return_tensors="pt", padding=True, truncation=True, max_length=1024).to(device)
|
| 74 |
+
generated_ids = model.generate(**inputs, max_new_tokens=1024, do_sample=True)
|
| 75 |
+
answers = tokenizer.batch_decode(generated_ids[:, inputs['input_ids'].shape[1]:])
|
| 76 |
+
answers = [x.split('<|eot_id|>')[0].strip() for x in answers]
|
| 77 |
+
print(answers)
|
| 78 |
+
```
|
adapter_config.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"alpha_pattern": {},
|
| 3 |
+
"auto_mapping": null,
|
| 4 |
+
"base_model_name_or_path": "meta-llama/Meta-Llama-3-8B-Instruct",
|
| 5 |
+
"bias": "none",
|
| 6 |
+
"fan_in_fan_out": false,
|
| 7 |
+
"inference_mode": true,
|
| 8 |
+
"init_lora_weights": true,
|
| 9 |
+
"layer_replication": null,
|
| 10 |
+
"layers_pattern": null,
|
| 11 |
+
"layers_to_transform": null,
|
| 12 |
+
"loftq_config": {},
|
| 13 |
+
"lora_alpha": 16,
|
| 14 |
+
"lora_dropout": 0.05,
|
| 15 |
+
"megatron_config": null,
|
| 16 |
+
"megatron_core": "megatron.core",
|
| 17 |
+
"modules_to_save": null,
|
| 18 |
+
"peft_type": "LORA",
|
| 19 |
+
"r": 16,
|
| 20 |
+
"rank_pattern": {},
|
| 21 |
+
"revision": null,
|
| 22 |
+
"target_modules": [
|
| 23 |
+
"up_proj",
|
| 24 |
+
"down_proj",
|
| 25 |
+
"q_proj",
|
| 26 |
+
"o_proj",
|
| 27 |
+
"v_proj",
|
| 28 |
+
"gate_proj",
|
| 29 |
+
"k_proj"
|
| 30 |
+
],
|
| 31 |
+
"task_type": "CAUSAL_LM",
|
| 32 |
+
"use_dora": false,
|
| 33 |
+
"use_rslora": false
|
| 34 |
+
}
|
adapter_model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:cee014ffbf0e4595d8773716cdc3693d5e3ed05033d64b52687d09cf4e93b4af
|
| 3 |
+
size 167832240
|
apply.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
if __name__ == '__main__':
|
| 5 |
+
base_model = "meta-llama/Meta-Llama-3-8B-Instruct"
|
| 6 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 7 |
+
|
| 8 |
+
bnb_config = BitsAndBytesConfig(
|
| 9 |
+
load_in_4bit=True,
|
| 10 |
+
bnb_4bit_quant_type="nf4",
|
| 11 |
+
bnb_4bit_compute_dtype=torch.float16,
|
| 12 |
+
bnb_4bit_use_double_quant=True,
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 16 |
+
base_model,
|
| 17 |
+
quantization_config=bnb_config,
|
| 18 |
+
device_map={"": 0},
|
| 19 |
+
attn_implementation="eager"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model, trust_remote_code=True)
|
| 23 |
+
tokenizer.pad_token = '<|pad|>'
|
| 24 |
+
tokenizer.pad_token_id = 128255
|
| 25 |
+
|
| 26 |
+
#Load LORA weights
|
| 27 |
+
model.load_adapter("Matej/FoodyLLM")
|
| 28 |
+
model.config.use_cache = True
|
| 29 |
+
model.eval()
|
| 30 |
+
|
| 31 |
+
system_prompt = ""
|
| 32 |
+
user_prompt = "Calculate the nutrient values per 100 grams in a recipe with these ingredients: 10 ounce candies, marshmallows, 1/4 cup butter, without salt, 6 cup cocoa, dry powder, unsweetened"
|
| 33 |
+
|
| 34 |
+
messages = [
|
| 35 |
+
{
|
| 36 |
+
"role": "user",
|
| 37 |
+
"content": f"{system_prompt} {user_prompt}".strip()
|
| 38 |
+
}
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 42 |
+
|
| 43 |
+
#Here we have a batch of one
|
| 44 |
+
tokenizer_input = [prompt]
|
| 45 |
+
|
| 46 |
+
inputs = tokenizer(tokenizer_input, return_tensors="pt", padding=True, truncation=True, max_length=1024).to(device)
|
| 47 |
+
generated_ids = model.generate(**inputs, max_new_tokens=1024, do_sample=True)
|
| 48 |
+
answers = tokenizer.batch_decode(generated_ids[:, inputs['input_ids'].shape[1]:])
|
| 49 |
+
answers = [x.split('<|eot_id|>')[0].strip() for x in answers]
|
| 50 |
+
print(answers)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
peft
|