File size: 3,217 Bytes
54a235d
 
7d9b74c
bfa18a9
7d9b74c
81c4f13
7d9b74c
54a235d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfa18a9
 
 
7d9b74c
 
81c4f13
 
7d9b74c
 
81c4f13
 
7d9b74c
 
 
81c4f13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d9b74c
 
54a235d
efbb976
54a235d
7d9b74c
 
 
 
 
 
 
 
 
 
bfa18a9
 
 
7d9b74c
 
bfa18a9
7d9b74c
 
81c4f13
 
 
 
 
 
 
 
 
54a235d
81c4f13
 
54a235d
81c4f13
54a235d
 
 
 
 
 
 
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
import gradio as gr

from src.storage.state import state as State
from src.modules.logger import logger as Logger

from src.scenarios.interrogation import image_describe, image_test
from src.repositories.images import images as ImagesRepo


TITLE = """
    <div style="text-align: center; max-width: 650px; margin: 0 auto;">
        <div
        style="
            display: inline-flex;
            align-items: center;
            gap: 0.8rem;
            font-size: 1.75rem;
        "
        >
        <h1 style="font-weight: 900; margin-bottom: 7px;">
            CLIP Interrogator
        </h1>
        </div>     
    </div>
"""


def select_image(selection: gr.SelectData):
    Logger.info("Image selected")
    State.set_current_image(selection.value['image']['path'])


def selected_image_describe():
    Logger.info("Selected image describe")
    image_path = State.get_current_image()
    if not image_path:
        Logger.tmp_debug(image_path)
        print(image_path)
        gr.Warning("No image selected!")
        return None

    return image_describe(image_path)


def selected_image_test():
    Logger.info("Selected image test")
    image_path = State.get_current_image()
    if not image_path:
        gr.Warning("No image selected!")
        return None

    result = image_test(image_path, [
        "Is it a photo? Answer only \"Yes\" or \"No\".",
        "Is it a human? Answer only \"Yes\" or \"No\".",
        "Is it alone? Answer only \"Yes\" or \"No\".",
        "Is it a woman? Answer only \"Yes\" or \"No\".",
        "Is it happy? Answer only \"Yes\" or \"No\"."
    ])

    return result


def build():
    State.end()  # Reset state on hot reload
    def analyze_tab():
        with gr.Row():
            with gr.Column():
                gallery = gr.Gallery(
                    label="Pickup any image",
                    preview=True,
                    value=ImagesRepo.get_images(),
                    show_label=False,
                    elem_id="gallery",
                    columns=[2], rows=[2],
                    object_fit="contain",
                    height=800,
                    allow_preview=False,
                    selected_index=None,
                )

                gallery.select(select_image, inputs=None, outputs=None)

            with gr.Column():
                output_text = gr.Textbox(label="Description", elem_id="output-txt")
                describe_button = gr.Button("Describe")

                is_photo = gr.Checkbox(label="Is a photo?")
                is_human = gr.Checkbox(label="Is a human?")
                is_alone = gr.Checkbox(label="Alone?")
                is_woman = gr.Checkbox(label="Is it a woman?")
                is_happy = gr.Checkbox(label="Is it happy?")
                test_button = gr.Button("Test")

        describe_button.click(selected_image_describe, inputs=[], outputs=[output_text])
        test_button.click(selected_image_test(), inputs=[], outputs=[is_photo, is_human, is_alone, is_woman, is_happy])

    with gr.Blocks() as block:
        with gr.Column(elem_id="col-container"):
            gr.HTML(TITLE)

            with gr.Tab("Analyze"):
                analyze_tab()

    return block