Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -49,41 +49,43 @@ def generate_response(image, prompt):
|
|
| 49 |
|
| 50 |
# Handle optional image input
|
| 51 |
pil_img = None
|
| 52 |
-
image_token_str = ""
|
| 53 |
if image is not None:
|
| 54 |
if isinstance(image, str):
|
| 55 |
pil_img = Image.open(image).convert("RGB")
|
| 56 |
else:
|
| 57 |
pil_img = Image.fromarray(image).convert("RGB")
|
| 58 |
-
|
| 59 |
-
image_token = getattr(proc, "image_token", "<|image|>")
|
| 60 |
-
image_token_str = f"{image_token}\n"
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
| 68 |
return_tensors="pt",
|
| 69 |
-
|
| 70 |
).to(mod.device)
|
| 71 |
|
| 72 |
-
input_len = inputs["input_ids"].shape[1]
|
| 73 |
|
| 74 |
with torch.no_grad():
|
| 75 |
outputs = mod.generate(
|
| 76 |
**inputs,
|
| 77 |
-
max_new_tokens=
|
| 78 |
-
do_sample=False, # Greedy decoding
|
| 79 |
eos_token_id=proc.tokenizer.eos_token_id,
|
| 80 |
pad_token_id=proc.tokenizer.pad_token_id
|
| 81 |
)
|
| 82 |
|
| 83 |
-
# Slice
|
| 84 |
-
|
| 85 |
-
response
|
| 86 |
-
return response.strip()
|
| 87 |
|
| 88 |
except Exception as e:
|
| 89 |
error_msg = str(e).lower()
|
|
|
|
| 49 |
|
| 50 |
# Handle optional image input
|
| 51 |
pil_img = None
|
|
|
|
| 52 |
if image is not None:
|
| 53 |
if isinstance(image, str):
|
| 54 |
pil_img = Image.open(image).convert("RGB")
|
| 55 |
else:
|
| 56 |
pil_img = Image.fromarray(image).convert("RGB")
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
# Build official chat message structure
|
| 59 |
+
content = []
|
| 60 |
+
if pil_img is not None:
|
| 61 |
+
content.append({"type": "image", "image": pil_img})
|
| 62 |
+
content.append({"type": "text", "text": prompt})
|
| 63 |
+
|
| 64 |
+
messages = [{"role": "user", "content": content}]
|
| 65 |
|
| 66 |
+
# Use official Gemma 4 processor chat template pipeline
|
| 67 |
+
inputs = proc.apply_chat_template(
|
| 68 |
+
messages,
|
| 69 |
+
tokenize=True,
|
| 70 |
+
return_dict=True,
|
| 71 |
return_tensors="pt",
|
| 72 |
+
add_generation_prompt=True
|
| 73 |
).to(mod.device)
|
| 74 |
|
| 75 |
+
input_len = inputs["input_ids"].shape[-1]
|
| 76 |
|
| 77 |
with torch.no_grad():
|
| 78 |
outputs = mod.generate(
|
| 79 |
**inputs,
|
| 80 |
+
max_new_tokens=512,
|
| 81 |
+
do_sample=False, # Greedy decoding for clean, deterministic responses
|
| 82 |
eos_token_id=proc.tokenizer.eos_token_id,
|
| 83 |
pad_token_id=proc.tokenizer.pad_token_id
|
| 84 |
)
|
| 85 |
|
| 86 |
+
# Slice precisely using the official input length
|
| 87 |
+
response = proc.decode(outputs[0][input_len:], skip_special_tokens=True)
|
| 88 |
+
return response.strip() if response.strip() else "⚠️ Model generated an empty response. Please try another prompt."
|
|
|
|
| 89 |
|
| 90 |
except Exception as e:
|
| 91 |
error_msg = str(e).lower()
|