Spaces:
Sleeping
Sleeping
File size: 5,872 Bytes
0216c0d d35f312 0216c0d d35f312 0216c0d d35f312 0216c0d d35f312 a0f2ea5 0216c0d a0f2ea5 0216c0d a0f2ea5 0216c0d a0f2ea5 d35f312 0216c0d d35f312 f0e9ec4 d35f312 0216c0d d35f312 0216c0d a0f2ea5 0216c0d d35f312 0216c0d d35f312 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
import json
import uuid
import comet_ml
import gradio as gr
import pandas as pd
from PIL import Image
from transformers import CLIPModel, CLIPProcessor
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 want to continue logging to the same Experiment over multiple sessions, add in the
Then use the path to a Space to generate from in the Diffusion Model tab
"""
def create_experiment(
comet_api_key,
comet_workspace,
comet_project_name,
comet_experiment_name,
experiment,
):
if not comet_api_key:
experiment = None
return (
experiment,
"""
Please add your API key in order to log your predictions to a Comet Experiment.
If you don't have a Comet account yet, you can sign up using the link below:
https://www.comet.ml/signup
""",
)
try:
api_experiment = comet_ml.APIExperiment(
api_key=comet_api_key,
workspace=comet_workspace,
project_name=comet_project_name,
experiment_name=comet_experiment_name,
)
experiment = {
"api_key": comet_api_key,
"workspace": comet_workspace,
"project_name": comet_project_name,
"previous_experiment": api_experiment.id,
}
return experiment, f"Started {api_experiment.name}. Happy logging!π"
except Exception as e:
return None, e
def get_experiment(kwargs) -> comet_ml.APIExperiment:
try:
return comet_ml.APIExperiment(**kwargs)
except Exception as e:
return None
def get_experiment_status(experiment_state):
experiment = get_experiment(experiment_state)
if experiment is not None:
name = experiment.name
return experiment_state, f"Currently logging to: {name}"
return experiment_state, f"No Experiments found"
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("Create Experiment", variant="primary")
status = gr.Button("Experiment Status")
output = gr.Markdown(label="Status")
experiment_state = gr.Variable(label="Experiment State")
start.click(
create_experiment,
inputs=[
comet_api_key,
comet_workspace,
comet_project_name,
comet_experiment_name,
experiment_state,
],
outputs=[experiment_state, output],
)
status.click(
get_experiment_status,
inputs=[experiment_state],
outputs=[experiment_state, 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")
prompt = gr.Textbox(label="Prompt")
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()
|