Commit
·
356c40c
1
Parent(s):
5984321
Update README.md
Browse files
README.md
CHANGED
|
@@ -29,6 +29,11 @@ Use the code below to get started with the model.
|
|
| 29 |
import torch
|
| 30 |
from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
checkpoint = "abdoelsayed/llama-7b-v2-Receipt-Key-Extraction"
|
| 33 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 34 |
|
|
@@ -37,7 +42,7 @@ tokenizer = AutoTokenizer.from_pretrained(checkpoint, model_max_length=512,
|
|
| 37 |
use_fast=False,)
|
| 38 |
model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device)
|
| 39 |
|
| 40 |
-
def generate_response(instruction, input_text, max_new_tokens=100, temperature=0.1, num_beams=4 ,top_k=40):
|
| 41 |
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:"
|
| 42 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 43 |
input_ids = inputs["input_ids"].to(device)
|
|
@@ -48,9 +53,9 @@ def generate_response(instruction, input_text, max_new_tokens=100, temperature=0
|
|
| 48 |
num_beams=num_beams,
|
| 49 |
)
|
| 50 |
with torch.no_grad():
|
| 51 |
-
outputs = model.generate(input_ids,generation_config=generation_config, max_new_tokens=max_new_tokens)
|
| 52 |
outputs = tokenizer.decode(outputs.sequences[0])
|
| 53 |
-
return
|
| 54 |
|
| 55 |
instruction = "Extract the class, Brand, Weight, Number of units, Size of units, Price, T.Price, Pack, Unit from the following sentence"
|
| 56 |
input_text = "Americana Okra zero 400 gm"
|
|
@@ -58,6 +63,7 @@ input_text = "Americana Okra zero 400 gm"
|
|
| 58 |
response = generate_response(instruction, input_text)
|
| 59 |
print(response)
|
| 60 |
|
|
|
|
| 61 |
```
|
| 62 |
|
| 63 |
|
|
|
|
| 29 |
import torch
|
| 30 |
from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
|
| 31 |
|
| 32 |
+
try:
|
| 33 |
+
if torch.backends.mps.is_available():
|
| 34 |
+
device = "mps"
|
| 35 |
+
except:
|
| 36 |
+
pass
|
| 37 |
checkpoint = "abdoelsayed/llama-7b-v2-Receipt-Key-Extraction"
|
| 38 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 39 |
|
|
|
|
| 42 |
use_fast=False,)
|
| 43 |
model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device)
|
| 44 |
|
| 45 |
+
def generate_response(instruction, input_text, max_new_tokens=100, temperature=0.1, num_beams=4 , top_p=0.75, top_k=40):
|
| 46 |
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:"
|
| 47 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 48 |
input_ids = inputs["input_ids"].to(device)
|
|
|
|
| 53 |
num_beams=num_beams,
|
| 54 |
)
|
| 55 |
with torch.no_grad():
|
| 56 |
+
outputs = model.generate(input_ids,generation_config=generation_config, max_new_tokens=max_new_tokens,return_dict_in_generate=True,output_scores=True,)
|
| 57 |
outputs = tokenizer.decode(outputs.sequences[0])
|
| 58 |
+
return outputs.split("### Response:")[-1].strip().replace("</s>","")
|
| 59 |
|
| 60 |
instruction = "Extract the class, Brand, Weight, Number of units, Size of units, Price, T.Price, Pack, Unit from the following sentence"
|
| 61 |
input_text = "Americana Okra zero 400 gm"
|
|
|
|
| 63 |
response = generate_response(instruction, input_text)
|
| 64 |
print(response)
|
| 65 |
|
| 66 |
+
|
| 67 |
```
|
| 68 |
|
| 69 |
|