Views
No views yet
1# pip install -q transformers
2
3import torch
4from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
5
6checkpoint = "abdoelsayed/llama-7b-v1-Receipt-Key-Extraction"
7device = "cuda" if torch.cuda.is_available() else "cpu"
8
9tokenizer = AutoTokenizer.from_pretrained(checkpoint, model_max_length=512,
10 padding_side="right",
11 use_fast=False,)
12model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device)
13
14def generate_response(instruction, input_text, max_new_tokens=100, temperature=0.1, num_beams=4 ,top_k=40):
15 prompt = f"Below is an instruction that describes a task, paired with an input that provides further context.\n\n### Instruction:\n{instruction}\n\n### Input:\n{input_text}\n\n### Response:"
16 inputs = tokenizer(prompt, return_tensors="pt")
17 input_ids = inputs["input_ids"].to(device)
18 generation_config = GenerationConfig(
19 temperature=temperature,
20 top_p=top_p,
21 top_k=top_k,
22 num_beams=num_beams,
23 )
24 with torch.no_grad():
25 outputs = model.generate(input_ids,generation_config=generation_config, max_new_tokens=max_new_tokens)
26 outputs = tokenizer.decode(outputs.sequences[0])
27 return output.split("### Response:")[-1].strip().replace("</s>","")
28
29instruction = "Extract the class, Brand, Weight, Number of units, Size of units, Price, T.Price, Pack, Unit from the following sentence"
30input_text = "Americana Okra zero 400 gm"
31
32response = generate_response(instruction, input_text)
33print(response)
341@misc{abdallah2023amurd,
2 title={AMuRD: Annotated Multilingual Receipts Dataset for Cross-lingual Key Information Extraction and Classification},
3 author={Abdelrahman Abdallah and Mahmoud Abdalla and Mohamed Elkasaby and Yasser Elbendary and Adam Jatowt},
4 year={2023},
5 eprint={2309.09800},
6 archivePrefix={arXiv},
7 primaryClass={cs.CL}
8}