import jsonfrom transformers import AutoModelForCausalLM, AutoTokenizer model_id = "yugbirla/ToxSense-json-ultimate"tokenizer = AutoTokenizer.from_pretrained(model_id)model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto") # 1. Prepare the JSON Payloadinput_data = { "ocr_text": "Look at this completely normal picture.", "image_caption": "A controversial political figure.", "toxicity_scores": {"safe": 0.9, "hate": 0.1}} sys_msg = ( "You are ToxSense, a highly intelligent safety moderator. " "You will receive input as a JSON object containing 'ocr_text', 'image_caption', and 'toxicity_scores'. " "Think step-by-step. Analyze the contrast between the text and the image. " "Classify the input into exactly ONE of these categories: " "[safe, racism, sexism, threat, harassment, insult]. " "Output JSON ONLY in this format: {\"reasoning\": \"your short analysis\", \"category\": \"<category_name>\"}") messages = [ {"role": "system", "content": sys_msg}, {"role": "user", "content": json.dumps(input_data, indent=2)}] # 2. Generatetext = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)inputs = tokenizer([text], return_tensors="pt").to("cuda")out = model.generate(**inputs, max_new_tokens=150) print(tokenizer.decode(out[0][inputs.input_ids.shape[1]:], skip_special_tokens=True))