File size: 4,924 Bytes
d35f312
0216c0d
d35f312
0216c0d
 
 
 
8221e77
 
0216c0d
d35f312
0216c0d
 
d35f312
a0f2ea5
 
 
0216c0d
a0f2ea5
d4f1c9d
 
a0f2ea5
d4f1c9d
d35f312
d4f1c9d
d35f312
d4f1c9d
 
0216c0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d35f312
0216c0d
d35f312
0216c0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6860fc5
0216c0d
 
fca694c
0216c0d
 
 
db0d363
0216c0d
 
 
 
 
 
 
f56212a
0216c0d
 
 
 
 
f56212a
0216c0d
 
 
 
f56212a
0216c0d
 
 
8221e77
 
 
 
 
 
 
 
 
 
0216c0d
 
 
 
 
 
 
 
 
 
 
d35f312
 
0216c0d
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import uuid

import gradio as gr
import pandas as pd
from PIL import Image
from transformers import CLIPModel, CLIPProcessor

from comet import get_experiment, get_experiment_status, start_experiment

CLIP_MODEL_PATH = "openai/clip-vit-base-patch32"

clip_model = CLIPModel.from_pretrained(CLIP_MODEL_PATH)
clip_processor = CLIPProcessor.from_pretrained(CLIP_MODEL_PATH)

DESCRIPTION = """Glad to see you here 😄.
You can use this Space to log predictions to [Comet](https://www.comet.ml/site) from Spaces that use Text to Image Diffusion Models.

Keep track of all your prompts and generated images so that you remember the good ones!

Set your Comet credentials in the Comet Settings tab and create an Experiment for logging data. If you don't have credentials yet,
you can [sign up for Comet here](https://www.comet.ml/signup)

If you want to continue logging to the same Experiment over multiple sessions, simply provide the experiment name.

Set a path to a Space using that uses a Diffusion model and submit your prompt in the Diffusion Model tab

** Note: ** This Space will still run even if you don't set credentials
"""


def predict(
    model,
    prompt,
    experiment_state,
):
    io = gr.Interface.load(model)
    image = io(prompt)
    pil_image = Image.open(image)

    inputs = clip_processor(
        text=[prompt],
        images=pil_image,
        return_tensors="pt",
        padding=True,
    )
    outputs = clip_model(**inputs)
    clip_score = outputs.logits_per_image.item() / 100.0

    experiment = get_experiment(experiment_state)
    if experiment is not None:
        image_id = uuid.uuid4().hex
        experiment.log_image(image, image_id)

        asset = pd.DataFrame.from_records(
            [
                {
                    "prompt": prompt,
                    "model": model,
                    "clip_model": CLIP_MODEL_PATH,
                    "clip_score": round(clip_score, 3),
                }
            ]
        )
        experiment.log_table(f"{image_id}.json", asset, orient="records")

    return image, experiment_state


def start_interface():
    demo = gr.Blocks()
    with demo:
        description = gr.Markdown(DESCRIPTION)
        with gr.Tabs():
            with gr.TabItem(label="Comet Settings"):
                # credentials
                comet_api_key = gr.Textbox(
                    label="Comet API Key",
                    placeholder="This is required if you'd like to create an Experiment",
                )
                comet_workspace = gr.Textbox(label="Comet Workspace")
                comet_project_name = gr.Textbox(label="Comet Project Name")
                comet_experiment_name = gr.Textbox(
                    label="Comet Experiment Name",
                    placeholder=(
                        "Set this if you'd like"
                        "to continue logging to an existing Experiment",
                    ),
                )

                with gr.Row():
                    start = gr.Button("Start Experiment", variant="primary")
                    status = gr.Button("Experiment Status")

                status_output = gr.Textbox(label="Status")
                experiment_state = gr.Variable(label="Experiment State")

                start.click(
                    start_experiment,
                    inputs=[
                        comet_api_key,
                        comet_workspace,
                        comet_project_name,
                        comet_experiment_name,
                        experiment_state,
                    ],
                    outputs=[experiment_state, status_output],
                )

                status.click(
                    get_experiment_status,
                    inputs=[experiment_state],
                    outputs=[experiment_state, status_output],
                )

            with gr.TabItem(label="Diffusion Model"):
                diff_description = gr.Markdown(
                    """The Model must be a path to any Space that accepts
                    only text as input and produces an image as an output
                    """
                )
                model = gr.Textbox(
                    label="Model",
                    value="spaces/valhalla/glide-text2im",
                    placeholder="Enter a path to a Space",
                )
                prompt = gr.Textbox(
                    label="Prompt",
                    value="an oil painting of a corgi",
                    placeholder="Enter your text prompt here",
                )

                outputs = gr.Image(label="Image")

                submit = gr.Button("Submit", variant="primary")
                submit.click(
                    predict,
                    inputs=[model, prompt, experiment_state],
                    outputs=[outputs, experiment_state],
                )

    demo.launch()


start_interface()