from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
from peft import PeftModel
import torch, json
ADAPTER = "techwithrayoma/barqpulse-v1-qwen2.5-1.5b-lora"
SYSTEM = (
"You are a strict text classification system.\n"
"Classify the comment into exactly one of these labels:\n"
"Question, Complaint, Statement, Praise, Suggestion.\n"
"Return ONLY valid JSON in this exact format:\n"
'{"predicted_intent": "<label>"}\n'
"Do not output anything else."
)
tok = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-1.5B-Instruct")
base = AutoModelForCausalLM.from_pretrained(
"Qwen/Qwen2.5-1.5B-Instruct", torch_dtype=torch.float16, device_map="auto"
)
pipe = pipeline("text-generation", model=PeftModel.from_pretrained(base, ADAPTER),
tokenizer=tok, max_new_tokens=32, do_sample=False)
def classify(comment):
prompt = tok.apply_chat_template(
[{"role": "system", "content": SYSTEM},
{"role": "user", "content": f"Comment:\n{comment}"}],
tokenize=False, add_generation_prompt=True)
out = pipe(prompt)[0]["generated_text"][len(prompt):]
return json.loads(out[out.find("{"):out.rfind("}") + 1])["predicted_intent"]
classify("كم المده بعد الطلب لحتى توصل البطاقه")