| 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() |
| 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 |
|
|