NeelakshSaxena commited on
Commit
3fcb89e
Β·
1 Parent(s): 34ba9e5

Use chat template for tiny model prompts

Browse files
Files changed (1) hide show
  1. app.py +16 -1
app.py CHANGED
@@ -308,6 +308,21 @@ def score_binary_logits(logits: torch.Tensor, tokenizer) -> tuple[float, str]:
308
  return prob_fake, label
309
 
310
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
311
  def infer_image(image: Image.Image):
312
  try:
313
  if image is None:
@@ -316,7 +331,7 @@ def infer_image(image: Image.Image):
316
  model_obj, processor_obj, device = load_model_and_processor()
317
 
318
  prepared_image = preprocess_image(image)
319
- prompt_text = TINY_PROMPT if MODEL_MODE == "tiny" else TMOS_PROMPT
320
  inputs = processor_obj(text=prompt_text, images=prepared_image, return_tensors="pt", padding=True)
321
  inputs = {name: tensor.to(device) for name, tensor in inputs.items()}
322
 
 
308
  return prob_fake, label
309
 
310
 
311
+ def build_prompt(processor_obj, user_text: str) -> str:
312
+ messages = [
313
+ {
314
+ "role": "user",
315
+ "content": [
316
+ {"type": "image"},
317
+ {"type": "text", "text": user_text},
318
+ ],
319
+ }
320
+ ]
321
+ if hasattr(processor_obj, "apply_chat_template"):
322
+ return processor_obj.apply_chat_template(messages, add_generation_prompt=True)
323
+ return f"<image>\n{user_text}"
324
+
325
+
326
  def infer_image(image: Image.Image):
327
  try:
328
  if image is None:
 
331
  model_obj, processor_obj, device = load_model_and_processor()
332
 
333
  prepared_image = preprocess_image(image)
334
+ prompt_text = build_prompt(processor_obj, TINY_PROMPT if MODEL_MODE == "tiny" else TMOS_PROMPT)
335
  inputs = processor_obj(text=prompt_text, images=prepared_image, return_tensors="pt", padding=True)
336
  inputs = {name: tensor.to(device) for name, tensor in inputs.items()}
337