File size: 1,612 Bytes
b5322cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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)