Spaces:
Running on Zero
Running on Zero
Download tests/test_remote_scenes.py from ritianyu/SplatAnything: direct link, hf CLI and curl.
- Browser
- Download file 5.07 kB
-
https://huggingface.co/spaces/ritianyu/SplatAnything/resolve/main/tests/test_remote_scenes.py
- Command line
-
hf download hf://spaces/ritianyu/SplatAnything/tests/test_remote_scenes.py
-
curl -L -o test_remote_scenes.py https://huggingface.co/spaces/ritianyu/SplatAnything/resolve/main/tests/test_remote_scenes.py
5.07 kB
| """Remote examples must remain image-only until explicitly opened.""" | |
| import hashlib | |
| import io | |
| import json | |
| from pathlib import Path | |
| import tempfile | |
| import unittest | |
| from unittest.mock import patch | |
| from fastapi.testclient import TestClient | |
| from PIL import Image | |
| from demo.remote_scenes import RemoteScenes | |
| from demo.runtime import Pipeline | |
| from demo.ui import create_app | |
| from demo.design import EMPTY_VIEWER | |
| class RemoteSceneTests(unittest.TestCase): | |
| def setUp(self): | |
| self.temporary = tempfile.TemporaryDirectory() | |
| self.addCleanup(self.temporary.cleanup) | |
| self.root = Path(self.temporary.name) | |
| self.html = b'<!doctype html><html><title>Gaussian scene</title><canvas></canvas></html>' | |
| Image.new('RGB', (144, 90), (70, 100, 140)).save(self.root / 'object-1.webp') | |
| self.catalog = self.root / 'catalog.json' | |
| self.catalog.write_text(json.dumps(dict(dataset='owner/demo', revision='pinned-commit', scenes={ | |
| 'object-1': dict(path='object/1/scene with spaces.html', bytes=len(self.html), | |
| sha256=hashlib.sha256(self.html).hexdigest(), poster='object-1.webp')}))) | |
| self.library = RemoteScenes(self.catalog, self.root / 'cache') | |
| def make_app(self, examples=None): | |
| return create_app(Pipeline(device='cpu'), self.root / 'results', examples or self.root / 'no-examples', | |
| remote_scenes=self.library) | |
| def test_preview_does_not_download_and_explicit_open_downloads_only_once(self): | |
| app = self.make_app() | |
| with patch('demo.remote_scenes.urlopen', return_value=io.BytesIO(self.html)) as fetch: | |
| with TestClient(app) as client: | |
| shell = client.get('/example-scenes/object-1/') | |
| self.assertEqual(shell.status_code, 200) | |
| self.assertIn('Explore 3D', shell.text) | |
| self.assertEqual(client.get('/example-scenes/object-1/poster').status_code, 200) | |
| self.assertEqual(client.get('/example-scenes/object-1/html').status_code, 404) | |
| fetch.assert_not_called() | |
| self.assertEqual(client.post('/example-scenes/object-1/load').json(), {'url': 'html'}) | |
| self.assertEqual(client.get('/example-scenes/object-1/html').content, self.html) | |
| self.assertEqual(client.post('/example-scenes/object-1/load').status_code, 200) | |
| fetch.assert_called_once() | |
| url = fetch.call_args.args[0] | |
| self.assertIn('/pinned-commit/object/1/scene%20with%20spaces.html', url) | |
| self.assertFalse(app.state.pipeline.model) | |
| self.assertEqual(client.post('/example-scenes/not-a-scene/load').status_code, 404) | |
| self.assertEqual(client.get('/example-scenes/not-a-scene/poster').status_code, 404) | |
| self.assertEqual(client.get('/example-scenes/not-a-scene/html').status_code, 404) | |
| def test_failed_or_corrupt_download_does_not_create_a_cached_viewer(self): | |
| app = self.make_app() | |
| bad = b'x' * len(self.html) | |
| with patch('demo.remote_scenes.urlopen', side_effect=[io.BytesIO(bad), io.BytesIO(self.html)]) as fetch: | |
| with TestClient(app) as client: | |
| with self.assertLogs('demo.ui', level='ERROR'): | |
| self.assertEqual(client.post('/example-scenes/object-1/load').status_code, 502) | |
| self.assertIsNone(self.library.cached('object-1')) | |
| self.assertFalse(list((self.root / 'cache').glob('*.download'))) | |
| self.assertEqual(client.post('/example-scenes/object-1/load').status_code, 200) | |
| self.assertEqual(self.library.cached('object-1').read_bytes(), self.html) | |
| self.assertEqual(fetch.call_count, 2) | |
| def test_default_and_example_selection_show_the_matching_poster_without_fetching_html(self): | |
| images = self.root / 'examples/object/1/images' | |
| images.mkdir(parents=True) | |
| for index in range(2): | |
| Image.new('RGB', (28, 28), (index * 30, 70, 90)).save(images / f'{index:03d}.png') | |
| with patch('demo.remote_scenes.urlopen') as fetch: | |
| app = self.make_app(self.root / 'examples') | |
| config = app.state.demo.config | |
| viewer = next(c for c in config['components'] if c['props'].get('elem_id') == 'scene-viewer') | |
| self.assertEqual(viewer['props']['value'], EMPTY_VIEWER) | |
| dependency = next(d for d in config['dependencies'] if d.get('api_name') == 'example_object') | |
| with TestClient(app) as client: | |
| response = client.post('/gradio_api/run/predict', json=dict( | |
| fn_index=dependency['id'], data=[], session_hash='select-scene', | |
| event_data=dict(index=0, value=None, selected=True))) | |
| self.assertEqual(response.status_code, 200, response.text) | |
| self.assertIn('./example-scenes/object-1/', response.json()['data'][8]) | |
| fetch.assert_not_called() | |
| if __name__ == '__main__': | |
| unittest.main() | |