mirror of
https://github.com/gradio-app/gradio.git
synced 2024-11-21 01:01:05 +08:00
add tests
This commit is contained in:
parent
ad8018c1d6
commit
c03f7b2f3a
@ -1637,11 +1637,9 @@ class Model3d(InputComponent):
|
||||
):
|
||||
"""
|
||||
Parameters:
|
||||
type (str): Type of value to be returned by component. "file" returns a temporary file object whose path can be retrieved by file_obj.name, "binary" returns an bytes object.
|
||||
label (str): component name in interface.
|
||||
optional (bool): If True, the interface can be submitted with no uploaded image, in which case the input value is None.
|
||||
"""
|
||||
self.type = type
|
||||
self.test_input = None
|
||||
super().__init__(label, optional=optional)
|
||||
|
||||
@ -1684,6 +1682,9 @@ class Model3d(InputComponent):
|
||||
file_name = file.name
|
||||
return file_name
|
||||
|
||||
def serialize(self, x, called_directly):
|
||||
raise NotImplementedError()
|
||||
|
||||
def save_flagged(self, dir, label, data, encryption_key):
|
||||
"""
|
||||
Returns: (str) path to 3D model file
|
||||
@ -1691,6 +1692,9 @@ class Model3d(InputComponent):
|
||||
return self.save_flagged_file(
|
||||
dir, label, None if data is None else data["data"], encryption_key
|
||||
)
|
||||
|
||||
def generate_sample(self):
|
||||
return test_data.BASE64_MODEL3D
|
||||
|
||||
def get_input_instance(iface: Interface):
|
||||
if isinstance(iface, str):
|
||||
|
File diff suppressed because one or more lines are too long
1777
test/test_files/Fox.gltf
Normal file
1777
test/test_files/Fox.gltf
Normal file
File diff suppressed because one or more lines are too long
@ -781,6 +781,43 @@ class TestTimeseries(unittest.TestCase):
|
||||
],
|
||||
)
|
||||
|
||||
class TestModel3d(unittest.TestCase):
|
||||
def test_as_component(self):
|
||||
model3d = gr.test_data.BASE64_MODEL3D
|
||||
model3d_input = gr.inputs.Model3d()
|
||||
output = model3d_input.preprocess(model3d)
|
||||
self.assertIsInstance(output, str)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||
to_save = model3d_input.save_flagged(tmpdirname, "model3d_input", model3d, None)
|
||||
self.assertEqual("model3d_input/0", to_save)
|
||||
to_save = model3d_input.save_flagged(tmpdirname, "model3d_input", model3d, None)
|
||||
self.assertEqual("model3d_input/1", to_save)
|
||||
restored = model3d_input.restore_flagged(tmpdirname, to_save, None)
|
||||
self.assertEqual(restored, "model3d_input/1")
|
||||
|
||||
self.assertIsInstance(model3d_input.generate_sample(), dict)
|
||||
model3d_input = gr.inputs.Model3d(label="Upload Your 3D Model")
|
||||
self.assertEqual(
|
||||
model3d_input.get_template_context(),
|
||||
{
|
||||
"optional": False,
|
||||
"name": "model3d",
|
||||
"label": "Upload Your 3D Model",
|
||||
},
|
||||
)
|
||||
|
||||
self.assertIsNone(model3d_input.preprocess(None))
|
||||
model3d["is_example"] = True
|
||||
self.assertIsNotNone(model3d_input.preprocess(model3d))
|
||||
model3d_input = gr.inputs.Model3d()
|
||||
with self.assertRaises(NotImplementedError):
|
||||
model3d_input.serialize(model3d, True)
|
||||
|
||||
def test_in_interface(self):
|
||||
model3d = gr.test_data.BASE64_MODEL3D
|
||||
iface = gr.Interface(lambda x: x, "model3d", "model3d")
|
||||
self.assertEqual(iface.process([model3d])[0][0]["data"], model3d["data"].replace("@file/gltf", ""))
|
||||
|
||||
class TestNames(unittest.TestCase):
|
||||
# this ensures that `inputs.get_input_instance()` works correctly when instantiating from components
|
||||
|
@ -540,6 +540,23 @@ class TestTimeseries(unittest.TestCase):
|
||||
},
|
||||
)
|
||||
|
||||
class TestModel3d(unittest.TestCase):
|
||||
def test_as_component(self):
|
||||
model3d = "test/test_files/fox.gltf"
|
||||
model3d_output = gr.outputs.Model3d()
|
||||
self.assertTrue(
|
||||
model3d_output.postprocess(model3d)["data"].startswith("data:;base64,")
|
||||
)
|
||||
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||
to_save = model3d_output.save_flagged(
|
||||
tmpdirname, "model3d_output", gr.test_data.BASE64_MODEL3D, None
|
||||
)
|
||||
self.assertEqual("model3d_output/0", to_save)
|
||||
to_save = model3d_output.save_flagged(
|
||||
tmpdirname, "model3d_output", gr.test_data.BASE64_MODEL3D, None
|
||||
)
|
||||
self.assertEqual("model3d_output/1", to_save)
|
||||
|
||||
|
||||
class TestNames(unittest.TestCase):
|
||||
def test_no_duplicate_uncased_names(
|
||||
|
@ -11,7 +11,7 @@ import InputTextbox from "./input/Textbox/config.js";
|
||||
import InputVideo from "./input/Video/config.js";
|
||||
import InputDataFrame from "./input/DataFrame/config.js";
|
||||
import InputTimeSeries from "./input/TimeSeries/config.js";
|
||||
import InputModel3D from "./input/Model3D/config.js";
|
||||
import InputModel3d from "./input/Model3d/config.js";
|
||||
|
||||
import OutputAudio from "./output/Audio/config.js";
|
||||
import OutputCarousel from "./output/Carousel/config.js";
|
||||
@ -26,7 +26,7 @@ import OutputTextbox from "./output/Textbox/config.js";
|
||||
import OutputVideo from "./output/Video/config.js";
|
||||
import OutputTimeSeries from "./output/TimeSeries/config.js";
|
||||
import OutputChatbot from "./output/Chatbot/config.js";
|
||||
import OutputModel3D from "./output/Model3D/config.js";
|
||||
import OutputModel3d from "./output/Model3d/config.js";
|
||||
|
||||
import StaticButton from "./static/Button/config.js";
|
||||
import StaticMarkdown from "./static/Markdown/config.js";
|
||||
@ -45,7 +45,7 @@ export const input_component_map = {
|
||||
textbox: InputTextbox,
|
||||
timeseries: InputTimeSeries,
|
||||
video: InputVideo,
|
||||
model3d: InputModel3D
|
||||
model3d: InputModel3d
|
||||
};
|
||||
|
||||
export const output_component_map = {
|
||||
@ -62,7 +62,7 @@ export const output_component_map = {
|
||||
timeseries: OutputTimeSeries,
|
||||
video: OutputVideo,
|
||||
chatbot: OutputChatbot,
|
||||
model3d: OutputModel3D
|
||||
model3d: OutputModel3d
|
||||
};
|
||||
|
||||
export const static_component_map = {
|
||||
|
@ -61,13 +61,19 @@
|
||||
</script>
|
||||
|
||||
{#if value === null}
|
||||
<Upload load={handle_load} {theme}>
|
||||
<Upload load={handle_load} {theme}
|
||||
filetype=".obj, .gltf, .glb">
|
||||
{$_("interface.drop_file")}
|
||||
<br />- {$_("interface.or")} -<br />
|
||||
{$_("interface.click_to_upload")}
|
||||
</Upload>
|
||||
{:else}
|
||||
<canvas class="w-full h-full object-fit" bind:this={canvas}></canvas>
|
||||
<div
|
||||
class="input-model w-full h-60 flex justify-center items-center bg-gray-200 dark:bg-gray-600 relative"
|
||||
{theme}
|
||||
>
|
||||
<canvas class="w-full h-full object-contain" bind:this={canvas}></canvas>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<style lang="postcss">
|
||||
|
@ -1,4 +1,4 @@
|
||||
import Component from "./Model3D.svelte";
|
||||
import Component from "./Model3d.svelte";
|
||||
import ExampleComponent from "./Example.svelte";
|
||||
import { loadAsData } from "../../utils/example_processors";
|
||||
|
||||
|
@ -45,10 +45,10 @@
|
||||
|
||||
|
||||
<div
|
||||
class="output-model w-full h-60 flex justify-center items-center bg-gray-200 dark:bg-gray-600 relative"
|
||||
{theme}
|
||||
class="output-model w-full h-60 flex justify-center items-center bg-gray-200 dark:bg-gray-600 relative"
|
||||
{theme}
|
||||
>
|
||||
<canvas class="w-full h-full object-fit" bind:this={canvas}></canvas>
|
||||
<canvas class="w-full h-full object-contain" bind:this={canvas}></canvas>
|
||||
</div>
|
||||
|
||||
<style lang="postcss">
|
||||
|
@ -1,4 +1,4 @@
|
||||
import Component from "./Model3D.svelte";
|
||||
import Component from "./Model3d.svelte";
|
||||
|
||||
export default {
|
||||
component: Component
|
||||
|
Loading…
Reference in New Issue
Block a user