akhaliq HF Staff commited on
Commit
3aa909c
·
1 Parent(s): fd5bc24

Fix: pass image through chat template content so pixel_values are produced

Browse files
Files changed (1) hide show
  1. app.py +8 -20
app.py CHANGED
@@ -19,21 +19,6 @@ model = AutoModelForImageTextToText.from_pretrained(
19
  print("Model loaded!")
20
 
21
 
22
- def _load_image(image):
23
- """Accept a workflow image value (dict with 'path', or a path/URL string)
24
- and return a PIL.Image."""
25
- from PIL import Image
26
-
27
- if isinstance(image, dict):
28
- image = image.get("path") or image.get("url")
29
- if isinstance(image, str) and image.startswith(("http://", "https://")):
30
- import requests
31
- from io import BytesIO
32
-
33
- return Image.open(BytesIO(requests.get(image, timeout=30).content)).convert("RGB")
34
- return Image.open(image).convert("RGB")
35
-
36
-
37
  def _estimate_duration(image, prompt, max_new_tokens, temperature, top_p, top_k) -> int:
38
  """Rough wall-clock estimate (seconds) for one VLM call. Requesting less
39
  than the 60s default raises queue priority and frees the GPU slot sooner
@@ -70,13 +55,16 @@ def _run_vlm_gpu(image, prompt, max_new_tokens, temperature, top_p, top_k):
70
  if image is None:
71
  raise gr.Error("Please provide an image.")
72
 
73
- pil_image = _load_image(image)
 
 
 
74
 
75
  messages = [
76
  {
77
  "role": "user",
78
  "content": [
79
- {"type": "image"},
80
  {"type": "text", "text": prompt},
81
  ],
82
  }
@@ -88,9 +76,9 @@ def _run_vlm_gpu(image, prompt, max_new_tokens, temperature, top_p, top_k):
88
  add_generation_prompt=True,
89
  return_tensors="pt",
90
  return_dict=True,
91
- )
92
- inputs = {k: v.to(model.device) if hasattr(v, "to") else v for k, v in inputs.items()}
93
- inputs["pixel_values"] = inputs["pixel_values"].to(torch.bfloat16)
94
 
95
  do_sample = float(temperature) > 0
96
  gen_kwargs = dict(max_new_tokens=int(max_new_tokens), do_sample=do_sample)
 
19
  print("Model loaded!")
20
 
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  def _estimate_duration(image, prompt, max_new_tokens, temperature, top_p, top_k) -> int:
23
  """Rough wall-clock estimate (seconds) for one VLM call. Requesting less
24
  than the 60s default raises queue priority and frees the GPU slot sooner
 
55
  if image is None:
56
  raise gr.Error("Please provide an image.")
57
 
58
+ if isinstance(image, dict):
59
+ image_ref = image.get("path") or image.get("url")
60
+ else:
61
+ image_ref = image
62
 
63
  messages = [
64
  {
65
  "role": "user",
66
  "content": [
67
+ {"type": "image", "url": image_ref},
68
  {"type": "text", "text": prompt},
69
  ],
70
  }
 
76
  add_generation_prompt=True,
77
  return_tensors="pt",
78
  return_dict=True,
79
+ ).to(model.device)
80
+ if "pixel_values" in inputs:
81
+ inputs["pixel_values"] = inputs["pixel_values"].to(torch.bfloat16)
82
 
83
  do_sample = float(temperature) > 0
84
  gen_kwargs = dict(max_new_tokens=int(max_new_tokens), do_sample=do_sample)