balthou's picture
interactive-pipe template
b5322cd
raw
history blame
1.61 kB
from interactive_pipe import interactive_pipeline, interactive, Image
from typing import Tuple
import numpy as np
# Processing blocks
# -----------------
def processing_block(
inp: np.ndarray,
amplify: float = 1.,
context: dict = {}
) -> np.ndarray:
context["amplify"] = amplify
return amplify*inp
# Pipeline definition
# -------------------
def pipe(inp: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
out = processing_block(inp)
return inp, out
# Add interactivity to the processing blocks
# ------------------------------------------
def add_controls() -> None:
interactive(
amplify=(0.5, [0., 1.], "Amplify")
)(processing_block)
# Add interactivity to the processing blocks
# ------------------------------------------
def launch(img: np.ndarray, backend: str = "gradio"):
add_controls()
pipe_interactive = interactive_pipeline(
gui=backend,
markdown_description=markdown_description
)(pipe)
pipe_interactive(img)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-b", "--backend", default="gradio",
choices=["gradio", "qt", "mpl"], type=str)
parser.add_argument(
"-d", "--debug", action="store_true",
help="Debug mode (to tune difficulty and tolerance)"
)
args = parser.parse_args()
markdown_description = "# Code to build this app on gradio \n\n"
markdown_description += "```python\n"+open(__file__, 'r').read()+"```"
img = Image.load_image("sample.jpg")
launch(img, backend=args.backend)