gradio_integrations.py 968 B

1234567891011121314151617181920212223242526272829303132
  1. import logging
  2. from typing import Callable
  3. from ray import serve
  4. from ray.serve._private.constants import SERVE_LOGGER_NAME
  5. from ray.serve._private.http_util import ASGIAppReplicaWrapper
  6. from ray.util.annotations import PublicAPI
  7. try:
  8. from gradio import Blocks, routes
  9. except ModuleNotFoundError:
  10. print("Gradio isn't installed. Run `pip install gradio` to install Gradio.")
  11. raise
  12. logger = logging.getLogger(SERVE_LOGGER_NAME)
  13. @PublicAPI(stability="alpha")
  14. class GradioIngress(ASGIAppReplicaWrapper):
  15. """User-facing class that wraps a Gradio App in a Serve Deployment."""
  16. def __init__(self, builder: Callable[[], Blocks]):
  17. """Builds and wraps an ASGI app from the provided builder.
  18. The builder should take no arguments and return a Gradio App (of type Interface
  19. or Blocks).
  20. """
  21. io: Blocks = builder()
  22. super().__init__(routes.App.create_app(io))
  23. GradioServer = serve.deployment(GradioIngress)