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>
- app.py +27 -6
- tests/test_grant_requests.py +73 -0
app.py
CHANGED
|
@@ -1172,6 +1172,22 @@ def validate_texture_size(value):
|
|
| 1172 |
return int(value)
|
| 1173 |
|
| 1174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1175 |
def generate_and_extract(
|
| 1176 |
image: Optional[Image.Image],
|
| 1177 |
seed: int,
|
|
@@ -1185,9 +1201,7 @@ def generate_and_extract(
|
|
| 1185 |
):
|
| 1186 |
if image is None:
|
| 1187 |
return None, None
|
| 1188 |
-
|
| 1189 |
-
image = validate_multi_images(image)
|
| 1190 |
-
texture_size = validate_texture_size(texture_size)
|
| 1191 |
args = (
|
| 1192 |
image, seed, ss_guidance_strength, ss_sampling_steps,
|
| 1193 |
slat_guidance_strength, slat_sampling_steps, texture_size,
|
|
@@ -1287,8 +1301,10 @@ def defer_or_fail(args, req: Optional[gr.Request], nonce: Optional[str], cause:
|
|
| 1287 |
raise gr.Error(ALLOWANCE_ERROR) from cause
|
| 1288 |
|
| 1289 |
|
| 1290 |
-
def issue_grant_request(req: gr.Request):
|
| 1291 |
-
|
|
|
|
|
|
|
| 1292 |
|
| 1293 |
|
| 1294 |
def accept_generation_grant(ack, receipt, req: gr.Request):
|
|
@@ -1781,7 +1797,12 @@ with gr.Blocks(title="Pocket 3D AI | SkyeBrowse") as demo:
|
|
| 1781 |
trigger_mode="always_last",
|
| 1782 |
)
|
| 1783 |
.then(reset_fallback_state, outputs=[fallback_receipt, fallback_status], show_progress="hidden")
|
| 1784 |
-
.then(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1785 |
.then(
|
| 1786 |
fn=accept_generation_grant,
|
| 1787 |
inputs=[grant_ack, grant_receipt],
|
|
|
|
| 1172 |
return int(value)
|
| 1173 |
|
| 1174 |
|
| 1175 |
+
def validated_generation_input(image: Any, texture_size: Any):
|
| 1176 |
+
if isinstance(image, (list, tuple)):
|
| 1177 |
+
image = validate_multi_images(image)
|
| 1178 |
+
return image, validate_texture_size(texture_size)
|
| 1179 |
+
|
| 1180 |
+
|
| 1181 |
+
def generation_ready(image: Any, texture_size: Any) -> bool:
|
| 1182 |
+
if image is None:
|
| 1183 |
+
return False
|
| 1184 |
+
try:
|
| 1185 |
+
validated_generation_input(image, texture_size)
|
| 1186 |
+
except gr.Error:
|
| 1187 |
+
return False
|
| 1188 |
+
return True
|
| 1189 |
+
|
| 1190 |
+
|
| 1191 |
def generate_and_extract(
|
| 1192 |
image: Optional[Image.Image],
|
| 1193 |
seed: int,
|
|
|
|
| 1201 |
):
|
| 1202 |
if image is None:
|
| 1203 |
return None, None
|
| 1204 |
+
image, texture_size = validated_generation_input(image, texture_size)
|
|
|
|
|
|
|
| 1205 |
args = (
|
| 1206 |
image, seed, ss_guidance_strength, ss_sampling_steps,
|
| 1207 |
slat_guidance_strength, slat_sampling_steps, texture_size,
|
|
|
|
| 1301 |
raise gr.Error(ALLOWANCE_ERROR) from cause
|
| 1302 |
|
| 1303 |
|
| 1304 |
+
def issue_grant_request(image, texture_size, req: gr.Request):
|
| 1305 |
+
if not generation_ready(image, texture_size) or not grants_required(req):
|
| 1306 |
+
return None
|
| 1307 |
+
return ACCESS.grants.request(session_hash(req))
|
| 1308 |
|
| 1309 |
|
| 1310 |
def accept_generation_grant(ack, receipt, req: gr.Request):
|
|
|
|
| 1797 |
trigger_mode="always_last",
|
| 1798 |
)
|
| 1799 |
.then(reset_fallback_state, outputs=[fallback_receipt, fallback_status], show_progress="hidden")
|
| 1800 |
+
.then(
|
| 1801 |
+
fn=issue_grant_request,
|
| 1802 |
+
inputs=[generation_inputs[0], generation_inputs[6]],
|
| 1803 |
+
outputs=[grant_receipt],
|
| 1804 |
+
show_progress="hidden",
|
| 1805 |
+
)
|
| 1806 |
.then(
|
| 1807 |
fn=accept_generation_grant,
|
| 1808 |
inputs=[grant_ack, grant_receipt],
|
tests/test_grant_requests.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import unittest
|
| 3 |
+
from unittest import mock
|
| 4 |
+
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
from tests.test_glb_cache import app, gr
|
| 8 |
+
|
| 9 |
+
SPACE_HOST = "broyang-3dai.hf.space"
|
| 10 |
+
EMBED = {"host": SPACE_HOST, "referer": f"https://{SPACE_HOST}/?__theme=light&__labs_parent=http://localhost:8080"}
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def embed_request(session):
|
| 14 |
+
request = gr.Request(headers=EMBED)
|
| 15 |
+
request.session_hash = session
|
| 16 |
+
return request
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def picture():
|
| 20 |
+
return Image.new("RGB", (8, 8), "red")
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class GrantRequestValidation(unittest.TestCase):
|
| 24 |
+
def setUp(self):
|
| 25 |
+
self.book = app.GrantBook()
|
| 26 |
+
patcher = mock.patch.object(app.ACCESS, "grants", self.book)
|
| 27 |
+
patcher.start()
|
| 28 |
+
self.addCleanup(patcher.stop)
|
| 29 |
+
env = mock.patch.dict(os.environ, {"LABS_EMBED_GRANTS": "1"})
|
| 30 |
+
env.start()
|
| 31 |
+
self.addCleanup(env.stop)
|
| 32 |
+
|
| 33 |
+
def entries(self, session):
|
| 34 |
+
return self.book._session_entries(session)
|
| 35 |
+
|
| 36 |
+
def test_invalid_inputs_request_no_grant(self):
|
| 37 |
+
invalid = [
|
| 38 |
+
(None, 1024),
|
| 39 |
+
([picture()], 1024),
|
| 40 |
+
([picture()] * (app.MULTI_IMAGE_MAX_VIEWS + 1), 1024),
|
| 41 |
+
(picture(), 999),
|
| 42 |
+
]
|
| 43 |
+
for image, texture_size in invalid:
|
| 44 |
+
self.assertIsNone(app.issue_grant_request(image, texture_size, embed_request("invalid")))
|
| 45 |
+
self.assertEqual(self.entries("invalid"), [])
|
| 46 |
+
|
| 47 |
+
def test_valid_inputs_request_one_grant(self):
|
| 48 |
+
for image in (picture(), [picture(), picture()]):
|
| 49 |
+
self.assertIn("ticket", app.issue_grant_request(image, 1024, embed_request("valid")))
|
| 50 |
+
self.assertEqual(len(self.entries("valid")), 2)
|
| 51 |
+
|
| 52 |
+
def test_regenerate_without_an_image_leaves_nothing_for_the_next_run(self):
|
| 53 |
+
self.assertIsNone(app.issue_grant_request(None, 1024, embed_request("s")))
|
| 54 |
+
self.assertEqual(app.generate_and_extract(None, 0, 7.5, 12, 3.0, 12, 1024, req=embed_request("s")), (None, None))
|
| 55 |
+
with self.assertRaises(gr.Error):
|
| 56 |
+
app.generate_and_extract(picture(), 0, 7.5, 12, 3.0, 12, 999, req=embed_request("s"))
|
| 57 |
+
self.assertEqual(self.entries("s"), [])
|
| 58 |
+
self.assertEqual(self.book.take("s").mode, "denied")
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
class GrantRequestWiring(unittest.TestCase):
|
| 62 |
+
def test_every_grant_request_validates_the_generation_image_and_texture(self):
|
| 63 |
+
fns = list(app.demo.fns.values())
|
| 64 |
+
requests = [fn for fn in fns if fn.fn is app.issue_grant_request]
|
| 65 |
+
generations = [fn for fn in fns if fn.fn is app.generate_and_extract]
|
| 66 |
+
self.assertEqual(len(requests), len(generations))
|
| 67 |
+
expected = {(fn.inputs[0]._id, fn.inputs[6]._id) for fn in generations}
|
| 68 |
+
self.assertEqual({tuple(component._id for component in fn.inputs) for fn in requests}, expected)
|
| 69 |
+
self.assertTrue(all(len(fn.inputs) == 7 for fn in generations))
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
+
unittest.main()
|