ServeGradio

class lightning.app.components.serve.gradio_server.ServeGradio(*args, theme=None, **kwargs)[source]

Bases: LightningWork, ABC

The ServeGradio Class enables to quickly create a gradio based UI for your LightningApp.

In the example below, the ServeGradio is subclassed to deploy AnimeGANv2.

from functools import partial

import gradio as gr
import requests
import torch
from lightning.app import LightningApp, LightningFlow
from lightning.app.components import ServeGradio
from PIL import Image


# Credit to @akhaliq for his inspiring work.
# Find his original code there: https://huggingface.co/spaces/akhaliq/AnimeGANv2/blob/main/app.py
class AnimeGANv2UI(ServeGradio):
    inputs = gr.inputs.Image(type="pil")
    outputs = gr.outputs.Image(type="pil")
    elon = "https://upload.wikimedia.org/wikipedia/commons/thumb/3/34/Elon_Musk_Royal_Society_%28crop2%29.jpg/330px-Elon_Musk_Royal_Society_%28crop2%29.jpg"
    img = Image.open(requests.get(elon, stream=True).raw)
    img.save("elon.jpg")
    examples = [["elon.jpg"]]

    def __init__(self):
        super().__init__()
        self.ready = False

    def predict(self, img):
        return self.model(img=img)

    def build_model(self):
        repo = "AK391/animegan2-pytorch:main"
        model = torch.hub.load(repo, "generator", device="cpu")
        face2paint = torch.hub.load(repo, "face2paint", size=512, device="cpu")
        self.ready = True
        return partial(face2paint, model=model)


class RootFlow(LightningFlow):
    def __init__(self):
        super().__init__()
        self.demo = AnimeGANv2UI()

    def run(self):
        self.demo.run()

    def configure_layout(self):
        tabs = []
        if self.demo.ready:
            tabs.append({"name": "Home", "content": self.demo})
        return tabs


app = LightningApp(RootFlow())

The result would be the following:

Animation showing how to AnimeGANv2 UI would looks like.

LightningWork, or Work in short, is a building block for long-running jobs.

The LightningApp runs its LightningFlow component within an infinite loop and track the LightningWork status update.

Use LightningWork for third-party services or for launching heavy jobs such as downloading data, training or serving a model.

Each LightningWork is running in its own independent process. Works are self-isolated from the rest, e.g any state changes happening within the work will be reflected within the flow but not the other way around.

Parameters:
  • parallel – Whether to run in parallel mode or not. When False, the flow waits for the work to finish.

  • cache_calls – Whether the run method should cache its input arguments and not run again when provided with the same arguments in subsequent calls.

  • raise_exception – Whether to re-raise an exception in the flow when raised from within the work run method.

  • host – Bind socket to this host

  • port – Bind socket to this port. Be default, this is None and should be called within your run method.

  • local_build_config – The local BuildConfig isn’t used until Lightning supports DockerRuntime.

  • cloud_build_config – The cloud BuildConfig enables user to easily configure machine before running this work.

  • run_once – Deprecated in favor of cache_calls. This will be removed soon.

  • start_with_flow – Whether the work should be started at the same time as the root flow. Only applies to works defined in __init__.

Learn More About Lightning Work Inner Workings


abstract build_model()[source]

Override to instantiate and return your model.

The model would be accessible under self.model

Return type:

Any

configure_layout()[source]

Configure the UI of this LightningWork.

You can either :rtype: str

  1. Return a single Frontend object to serve a user interface for this Work.

  2. Return a string containing a URL to act as the user interface for this Work.

  3. Return None to indicate that this Work doesn’t currently have a user interface.

Example: Serve a static directory (with at least a file index.html inside).

from lightning.app.frontend import StaticWebFrontend


class Work(LightningWork):
    def configure_layout(self):
        return StaticWebFrontend("path/to/folder/to/serve")

Example: Arrange the UI of my children in tabs (default UI by Lightning).

class Work(LightningWork):
    def configure_layout(self):
        return [
            dict(name="First Tab", content=self.child0),
            dict(name="Second Tab", content=self.child1),
            dict(name="Lightning", content="https://lightning.ai"),
        ]

If you don’t implement configure_layout, Lightning will use self.url.

Note

This hook gets called at the time of app creation and then again as part of the loop. If desired, a returned URL can depend on the state. This is not the case if the work returns a Frontend. These need to be provided at the time of app creation in order for the runtime to start the server.

abstract predict(*args, **kwargs)[source]

Override with your logic to make a prediction.

run(*args, **kwargs)[source]

Override to add your own logic.

Raises:

LightningPlatformException – If resource exceeds platform quotas or other constraints.