Spaces:
Running on Zero
Running on Zero
Bobby Claude Opus 5.5 commited on
Commit ·
df79a87
1
Parent(s): 7df6873
Validate the image and texture before requesting a grant
Browse filesRegenerate with no image or an invalid image set or texture size used to
request a grant the webapp counted, then exit before the access check,
stranding the use and leaving a settled entry for the next run. The
grant step now receives the generation image and texture size and
requests no grant for inputs the generation would reject.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Files changed (2)
- app.py+27-6
- tests/test_grant_requests.pyAdded+73
1171 unmodified lines1172117311741175117611777 unmodified lines11851186118711881189119011911192119393 unmodified lines12871288128912901291129212931294486 unmodified lines17811782178317841785178617871171 unmodified linesreturn int(value)def generate_and_extract(image: Optional[Image.Image],seed: int,7 unmodified lines):if image is None:return None, Noneif isinstance(image, (list, tuple)):image = validate_multi_images(image)texture_size = validate_texture_size(texture_size)args = (image, seed, ss_guidance_strength, ss_sampling_steps,slat_guidance_strength, slat_sampling_steps, texture_size,93 unmodified linesraise gr.Error(ALLOWANCE_ERROR) from causedef issue_grant_request(req: gr.Request):return ACCESS.grants.request(session_hash(req)) if grants_required(req) else Nonedef accept_generation_grant(ack, receipt, req: gr.Request):486 unmodified linestrigger_mode="always_last",).then(reset_fallback_state, outputs=[fallback_receipt, fallback_status], show_progress="hidden").then(fn=issue_grant_request, outputs=[grant_receipt], show_progress="hidden").then(fn=accept_generation_grant,inputs=[grant_ack, grant_receipt],1171 unmodified lines11721173117411751176117711781179118011811182118311841185118611871188118911901191119211937 unmodified lines120112021203120412051206120793 unmodified lines1301130213031304130513061307130813091310486 unmodified lines1797179817991800180118021803180418051806180718081171 unmodified linesreturn int(value)def validated_generation_input(image: Any, texture_size: Any):if isinstance(image, (list, tuple)):image = validate_multi_images(image)return image, validate_texture_size(texture_size)def generation_ready(image: Any, texture_size: Any) -> bool:if image is None:return Falsetry:validated_generation_input(image, texture_size)except gr.Error:return Falsereturn Truedef generate_and_extract(image: Optional[Image.Image],seed: int,7 unmodified lines):if image is None:return None, Noneimage, texture_size = validated_generation_input(image, texture_size)args = (image, seed, ss_guidance_strength, ss_sampling_steps,slat_guidance_strength, slat_sampling_steps, texture_size,93 unmodified linesraise gr.Error(ALLOWANCE_ERROR) from causedef issue_grant_request(image, texture_size, req: gr.Request):if not generation_ready(image, texture_size) or not grants_required(req):return Nonereturn ACCESS.grants.request(session_hash(req))def accept_generation_grant(ack, receipt, req: gr.Request):486 unmodified linestrigger_mode="always_last",).then(reset_fallback_state, outputs=[fallback_receipt, fallback_status], show_progress="hidden").then(fn=issue_grant_request,inputs=[generation_inputs[0], generation_inputs[6]],outputs=[grant_receipt],show_progress="hidden",).then(fn=accept_generation_grant,inputs=[grant_ack, grant_receipt],
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273import osimport unittestfrom unittest import mock
from PIL import Image
from tests.test_glb_cache import app, gr
SPACE_HOST = "broyang-3dai.hf.space"EMBED = {"host": SPACE_HOST, "referer": f"https://{SPACE_HOST}/?__theme=light&__labs_parent=http://localhost:8080"}
def embed_request(session): request = gr.Request(headers=EMBED) request.session_hash = session return request
def picture(): return Image.new("RGB", (8, 8), "red")
class GrantRequestValidation(unittest.TestCase): def setUp(self): self.book = app.GrantBook() patcher = mock.patch.object(app.ACCESS, "grants", self.book) patcher.start() self.addCleanup(patcher.stop) env = mock.patch.dict(os.environ, {"LABS_EMBED_GRANTS": "1"}) env.start() self.addCleanup(env.stop)
def entries(self, session): return self.book._session_entries(session)
def test_invalid_inputs_request_no_grant(self): invalid = [ (None, 1024), ([picture()], 1024), ([picture()] * (app.MULTI_IMAGE_MAX_VIEWS + 1), 1024), (picture(), 999), ] for image, texture_size in invalid: self.assertIsNone(app.issue_grant_request(image, texture_size, embed_request("invalid"))) self.assertEqual(self.entries("invalid"), [])
def test_valid_inputs_request_one_grant(self): for image in (picture(), [picture(), picture()]): self.assertIn("ticket", app.issue_grant_request(image, 1024, embed_request("valid"))) self.assertEqual(len(self.entries("valid")), 2)
def test_regenerate_without_an_image_leaves_nothing_for_the_next_run(self): self.assertIsNone(app.issue_grant_request(None, 1024, embed_request("s"))) self.assertEqual(app.generate_and_extract(None, 0, 7.5, 12, 3.0, 12, 1024, req=embed_request("s")), (None, None)) with self.assertRaises(gr.Error): app.generate_and_extract(picture(), 0, 7.5, 12, 3.0, 12, 999, req=embed_request("s")) self.assertEqual(self.entries("s"), []) self.assertEqual(self.book.take("s").mode, "denied")
class GrantRequestWiring(unittest.TestCase): def test_every_grant_request_validates_the_generation_image_and_texture(self): fns = list(app.demo.fns.values()) requests = [fn for fn in fns if fn.fn is app.issue_grant_request] generations = [fn for fn in fns if fn.fn is app.generate_and_extract] self.assertEqual(len(requests), len(generations)) expected = {(fn.inputs[0]._id, fn.inputs[6]._id) for fn in generations} self.assertEqual({tuple(component._id for component in fn.inputs) for fn in requests}, expected) self.assertTrue(all(len(fn.inputs) == 7 for fn in generations))
if __name__ == "__main__": unittest.main()app.py+27-6
tests/test_grant_requests.pyAdded+73