Views
No views yet
1from peft import PeftModel, PeftConfig
2from transformers import AutoModelForCausalLM
3import torch
4from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
5
6config = PeftConfig.from_pretrained("shujatoor/phi3nedtuned-ner-json")
7model = AutoModelForCausalLM.from_pretrained(
8 "microsoft/Phi-3-mini-4k-instruct",
9 device_map="cuda",
10 torch_dtype="auto",
11 trust_remote_code=True,
12)
13model = PeftModel.from_pretrained(model, "shujatoor/phi3nedtuned-ner-json")
14model.config.to_json_file('adapter_config.json')
15
16
17torch.random.manual_seed(0)
18tokenizer = AutoTokenizer.from_pretrained("shujatoor/phi3nedtuned-ner-json")
19
20
21text = "Tehzeeb Bakers STRN3277876134234 Block A. Police Foundation,PwD Islamabad 051-5170713-4.051-5170501 STRN#3277876134234 NTN#7261076-2 Sales Receipt 05/04/202405:56:40PM CashierM J Payment:Cash Rate Qty. Total # Descriptlon 80.512.000 190.00 1.VEGETABLESAMOSA Sub Total 161.02 Total Tax: 28.98 POS Service Fee 1.00 Total 191.00 Cash 200.00 Change Due 9.00 SR#th007-220240405175640730 Goods Once Sold Can Not Be Taken Back or Replaced All Prices Are Inclusive Sales Tax 134084240405175640553"
22q_json = "extracted_data': {'store_name': '', 'address': '', 'receipt_number': '', 'drug_license_number': '', 'gst_number': '', 'vat_number': '', 'date': '', 'time': '', 'items': [], 'total_items': '', 'gst_tax': '', 'vat_tax': '', 'gross_total': '', 'discount': '', 'net_total': '', 'contact': ''}"
23qs = f'{text}. {q_json}'
24print('Question:',qs, '\n')
25messages = [
26 #{"role": "system", "content": ""},
27 {"role": "user", "content": qs},
28
29]
30
31pipe = pipeline(
32 "text-generation",
33 model=model,
34 tokenizer=tokenizer,
35)
36
37generation_args = {
38 "max_new_tokens": 512,
39 "return_full_text": False,
40 #"temperature": 0.0,
41 "do_sample": False,
42}
43
44output = pipe(messages, **generation_args)
45
46print('Answer:', output[0]['generated_text'], '\n')
47| Training Loss | Epoch | Step | Validation Loss |
|---|---|---|---|
| 1.1904 | 0.5618 | 500 | 1.0617 |
| 0.765 | 1.1236 | 1000 | 0.9442 |
| 0.782 | 1.6854 | 1500 | 0.8690 |
| 0.5591 | 2.2472 | 2000 | 0.8647 |
| 0.5669 | 2.8090 | 2500 | 0.8296 |
| 0.4205 | 3.3708 | 3000 | 0.8820 |
| 0.3812 | 3.9326 | 3500 | 0.8859 |
| 0.3323 | 4.4944 | 4000 | 0.9360 |