Views
No views yet
1from transformers import AutoTokenizer,AutoModelForCausalLM,pipeline
2import torch
3
4model_id = "kingabzpro/Phi-3.5-mini-instruct-Ecommerce-Text-Classification"
5
6tokenizer = AutoTokenizer.from_pretrained(model_id)
7
8model = AutoModelForCausalLM.from_pretrained(
9 model_id,
10 return_dict=True,
11 low_cpu_mem_usage=True,
12 torch_dtype=torch.float16,
13 device_map="auto",
14 trust_remote_code=True,
15)
16
17text = "Inalsa Dazzle Glass Top, 3 Burner Gas Stove with Rust Proof Powder Coated Body, Black Toughened Glass Top, 2 Medium and 1 Small High Efficiency Brass Burners, Aluminum Mixing Tubes, Powder Coated Body, Inbuilt Stainless Steel Drip Trays, 360 degree Swivel Nozzle,Bigger Legs to Facilitate Cleaning Under Cooktop"
18prompt = f"""Classify the E-commerce text into Electronics, Household, Books and Clothing.
19text: {text}
20label: """.strip()
21
22pipe = pipeline(
23 "text-generation",
24 model=model,
25 tokenizer=tokenizer,
26 torch_dtype=torch.float16,
27 device_map="auto",
28)
29
30outputs = pipe(prompt, max_new_tokens=4, do_sample=True, temperature=0.1)
31
32print(outputs[0]["generated_text"].split("label: ")[-1].strip())
33
34# Household1Accuracy: 0.860
2Accuracy for label Electronics: 0.825
3Accuracy for label Household: 0.926
4Accuracy for label Books: 0.683
5Accuracy for label Clothing: 0.9471 precision recall f1-score support
2
3 Electronics 0.97 0.82 0.89 40
4 Household 0.88 0.93 0.90 81
5 Books 0.90 0.68 0.78 41
6 Clothing 0.88 0.95 0.91 38
7
8 micro avg 0.90 0.86 0.88 200
9 macro avg 0.91 0.85 0.87 200
10weighted avg 0.90 0.86 0.88 2001[[33 6 1 0]
2 [ 1 75 2 3]
3 [ 0 3 28 2]
4 [ 0 1 0 36]]