Skip to content

Commit

Permalink
feat: add assertions to the Application component
Browse files Browse the repository at this point in the history
  • Loading branch information
Col0ring committed Dec 19, 2024
1 parent 5e69b1c commit 35de7b8
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from gradio.data_classes import GradioModel
from gradio.events import EventListener

from ....utils.dev import ModelScopeDataLayoutComponent, resolve_frontend_dir
from ....utils.dev import (AppContext, ModelScopeDataLayoutComponent,
resolve_frontend_dir)


class ApplicationPageScreenData(GradioModel):
Expand Down Expand Up @@ -65,6 +66,7 @@ def __init__(
key: int | str | None = None,
render: bool = True,
**kwargs):
AppContext.set_app(self)
super().__init__(value=value,
visible=visible,
render=render,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from gradio_client import utils as client_utils
from gradio_client.documentation import document, set_documentation_group

from ....utils.dev import (CustomComponentDict, process_links,
from ....utils.dev import (AppContext, CustomComponentDict, process_links,
resolve_frontend_dir)
from ..MultimodalInput import MultimodalInputData

Expand Down Expand Up @@ -147,6 +147,7 @@ def __init__(
preview: If True (default), will enable image preview.
custom_components: Define custom tags for markdown rendering.
"""
AppContext.assert_app()
self.likeable = likeable
self.height = height
self.rtl = rtl
Expand Down
3 changes: 2 additions & 1 deletion backend/modelscope_studio/components/legacy/Flow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from gradio.events import Events
from gradio_client.documentation import document

from ....utils.dev import CustomComponentDict, resolve_frontend_dir
from ....utils.dev import AppContext, CustomComponentDict, resolve_frontend_dir
from .edge import *
from .edge import Edge
from .node import *
Expand Down Expand Up @@ -88,6 +88,7 @@ def __init__(
max_zoom: The maximum zoom level.
custom_components: Define the custom node types for the flow schema.
"""
AppContext.assert_app()
self.height = height
self.custom_components = custom_components
self.show_sidebar = show_sidebar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from gradio.events import EventListener
from gradio_client.documentation import document, set_documentation_group

from ....utils.dev import resolve_frontend_dir
from ....utils.dev import AppContext, resolve_frontend_dir

set_documentation_group("component")

Expand Down Expand Up @@ -67,6 +67,7 @@ def __init__(
Parameters:
every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
"""
AppContext.assert_app()
self._bind_mount_event = _bind_mount_event
self._bind_resize_event = _bind_resize_event
self._bind_unmount_event = _bind_unmount_event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from gradio.events import Events
from gradio_client.documentation import document, set_documentation_group

from ....utils.dev import (CustomComponentDict, process_links,
from ....utils.dev import (AppContext, CustomComponentDict, process_links,
resolve_frontend_dir)

set_documentation_group("component")
Expand Down Expand Up @@ -78,6 +78,7 @@ def __init__(
header_links: If True, will automatically create anchors for headings, displaying a link icon on hover.
custom_components: Define custom tags for markdown rendering.
"""
AppContext.assert_app()
self.rtl = rtl
self.enable_latex = enable_latex
self.latex_single_dollar_delimiter = latex_single_dollar_delimiter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from __future__ import annotations

from pathlib import Path
from typing import Any, Callable, List, Literal, Optional, Union
from typing import Any, Callable, List, Literal, Union

from gradio.components.base import FormComponent
from gradio.data_classes import FileData, GradioModel
from gradio.events import Events
from gradio_client import utils as client_utils
from gradio_client.documentation import document, set_documentation_group

from ....utils.dev import resolve_frontend_dir
from ....utils.dev import AppContext, resolve_frontend_dir

set_documentation_group("component")

Expand Down Expand Up @@ -113,6 +113,7 @@ def __init__(self,
file_preview_props: FilePreview will render if `value.files` is not empty, accepting the following props: height(int).
webcam_props: Webcam will render if `sources` contains "webcam", accepting the following props: mirror_webcam(bool), include_audio(bool).
"""
AppContext.assert_app()
if type not in ["text", "password", "email"]:
raise ValueError(
'`type` must be one of "text", "password", or "email".')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from gradio_client.utils import is_http_url_like
from PIL import Image as _Image # using _ to minimize namespace pollution

from ....utils.dev import resolve_frontend_dir
from ....utils.dev import AppContext, resolve_frontend_dir

set_documentation_group("component")

Expand Down Expand Up @@ -119,6 +119,7 @@ def __init__(self,
likeable: Whether the gallery image display a like or dislike button. Set automatically by the .like method but has to be present in the signature for it to show up in the config.
clickable: Whether the gallery image display an action button. Set automatically by the .click method but has to be present in the signature for it to show up in the config.
"""
AppContext.assert_app()
self.columns = columns
self.height = height
self.gap = gap
Expand Down
1 change: 1 addition & 0 deletions backend/modelscope_studio/utils/dev/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import List, Optional, TypedDict

from .app_context import *
from .component import *
from .process_links import process_links
from .resolve_frontend_dir import *
Expand Down
21 changes: 21 additions & 0 deletions backend/modelscope_studio/utils/dev/app_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class AppContext:
_app = None

@classmethod
def set_app(cls, app):
cls._app = app

@classmethod
def has_app(cls):
return cls._app is not None

@classmethod
def assert_app(cls):
if cls._app is None:
raise ImportError(
"""<modelscope-studio>: Cannot find the `Application` component, did you forget to import it from `modelscope_studio.components.base`."""
)

@classmethod
def get_app(cls):
return cls._app
7 changes: 7 additions & 0 deletions backend/modelscope_studio/utils/dev/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from gradio.component_meta import ComponentMeta
from gradio.components.base import BlockContext, Component

from .app_context import AppContext


class ModelScopeLayoutComponent(BlockContext, metaclass=ComponentMeta):
"""
Expand Down Expand Up @@ -32,6 +34,7 @@ def __init__(
elem_id=elem_id,
elem_classes=elem_classes,
render=render)
AppContext.assert_app()
self.as_item = as_item
if self.parent:
self._internal = dict(index=len(self.parent.children) - 1)
Expand Down Expand Up @@ -75,6 +78,8 @@ def __init__(
inputs=inputs,
load_fn=load_fn,
render=render)
AppContext.assert_app()

if self.parent:
self._internal = dict(index=len(self.parent.children) - 1)
else:
Expand Down Expand Up @@ -142,6 +147,8 @@ def __init__(
elem_id=elem_id,
elem_classes=elem_classes,
render=render)
AppContext.assert_app()

if self.parent:
self._internal = dict(index=len(self.parent.children) - 1)
else:
Expand Down

0 comments on commit 35de7b8

Please sign in to comment.