File size: 4,203 Bytes
d234d72
f9726b2
22f05c3
f9726b2
 
 
8d3461d
d234d72
bf2da25
d234d72
cc700fc
f9726b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc700fc
 
edb8e55
d234d72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9726b2
 
69fb274
75bd3a8
edb8e55
cc700fc
 
f9726b2
cc700fc
 
 
 
 
 
 
 
 
 
 
 
7ba470b
cc700fc
f9726b2
 
cc700fc
 
f9726b2
cc700fc
f9726b2
cc700fc
75bd3a8
22442a6
f9726b2
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import json
import numpy as np
import spaces
import gradio as gr
from gradio.themes.base import Base
from gradio.themes.utils import colors, fonts, sizes
from typing import Iterable
from tempfile import NamedTemporaryFile
from PIL import Image
from nudenet import NudeDetector
from parallax import generate_parallax_images, generate_animation_images


class Theme(Base):
    def __init__(
        self,
        *,
        primary_hue: colors.Color | str = colors.neutral,
        secondary_hue: colors.Color | str = colors.neutral,
        neutral_hue: colors.Color | str = colors.neutral,
        spacing_size: sizes.Size | str = sizes.spacing_md,
        radius_size: sizes.Size | str = sizes.radius_md,
        text_size: sizes.Size | str = sizes.text_md,
        font: fonts.Font | str | Iterable[fonts.Font | str] = (fonts.GoogleFont('Barlow'), 'ui-sans-serif', 'sans-serif'),
        font_mono: fonts.Font| str | Iterable[fonts.Font | str] = (fonts.GoogleFont('IBM Plex Mono'), 'ui-monospace', 'monospace',),
    ):
        super().__init__(
            primary_hue=primary_hue,
            secondary_hue=secondary_hue,
            neutral_hue=neutral_hue,
            spacing_size=spacing_size,
            radius_size=radius_size,
            text_size=text_size,
            font=font,
            font_mono=font_mono,
        )
        super().set(
            color_accent='rgb(0 231 255 / 1)',
            slider_color='rgb(0 231 255 / 1)',
            slider_color_dark='rgb(0 231 255 / 1)',
            button_primary_background_fill='rgb(0 231 255 / 1)',
            button_primary_background_fill_hover='rgb(0 231 255 / .75)',
            button_primary_text_color='#ffffff',
            button_primary_background_fill_dark='rgb(0 231 255 / 1)',
            button_primary_background_fill_hover_dark='rgb(0 231 255 / .75)',
            button_primary_text_color_dark='#ffffff',
            loader_color='rgb(255 199 229 / 1)',
            loader_color_dark='rgb(255 199 229 / 1)'
        )


@spaces.GPU
def generate_parallax(image: np.ndarray, n: int):
    input_image = Image.fromarray(image).convert('RGBA')
    detector = NudeDetector()
    layers = []
    indexes = []
    nsfw = False

    with NamedTemporaryFile(delete=False, suffix='.webp') as file:
        input_image.save(file, format='WEBP')
        file.flush()
        
        for detection in detector.detect(file.name):
            if detection['score'] >= 0.75:
                nsfw = True

                break
    
    for index, layer in enumerate(generate_parallax_images(input_image, n)):
        if layer is not None:
            layers.append(layer)
            indexes.append(index)

    return [layers, json.dumps(indexes), nsfw]


@spaces.GPU
def generate_animation(image: np.ndarray):
    return generate_animation_images(Image.fromarray(image).convert('RGBA'))


with gr.Blocks(theme=Theme()) as demo:
    gr.Markdown('**Parallax**')

    with gr.Row():
        with gr.Column():
            parallax_input_image = gr.Image(image_mode='RGBA', label='Input')
            parallax_input_layers = gr.Number(value=5, precision=0, minimum=2, maximum=10, step=1, label='Layers')
            parallax_generate_button = gr.Button('Generate')
        with gr.Column():
            parallax_output_gallery = gr.Gallery(label='Outputs', columns=5)
            parallax_output_json = gr.JSON(label='Indexes')
            parallax_output_nsfw = gr.Checkbox(label='NSFW')

    gr.Markdown('**Animation**')

    with gr.Row():
        with gr.Column():
            animation_input_image = gr.Image(image_mode='RGBA', label='Input')
            animation_generate_button = gr.Button('Generate')
        with gr.Column():
            animation_output_gallery = gr.Gallery(label='Outputs', columns=5)

    parallax_generate_button.click(fn=generate_parallax, inputs=[parallax_input_image, parallax_input_layers], outputs=[parallax_output_gallery, parallax_output_json, parallax_output_nsfw], api_name='generate_parallax')
    animation_generate_button.click(fn=generate_animation, inputs=animation_input_image, outputs=animation_output_gallery, api_name='generate_animation')


if __name__ == '__main__':
    demo.launch()