Spaces:
Running
on
Zero
Running
on
Zero
File size: 26,500 Bytes
2fb728d 89e9fac 2fb728d 4c7e9d0 2fb728d 4c7e9d0 6c8c209 4c7e9d0 40e61c6 4c7e9d0 40e61c6 ccb684e 40e61c6 2fb728d 40e61c6 301ae18 40e61c6 ccb684e 40e61c6 ccb684e 40e61c6 1435089 40e61c6 1435089 2388e75 40e61c6 1435089 40e61c6 1435089 40e61c6 301ae18 40e61c6 3b7cfa9 6c8c209 3b7cfa9 2fb728d 89e9fac 40e61c6 89e9fac 40e61c6 89e9fac 40e61c6 27c9477 40e61c6 89e9fac 40e61c6 2388e75 40e61c6 89e9fac ccb684e 2fb728d 89e9fac 2fb728d 89e9fac 4c7e9d0 2fb728d 89e9fac 2fb728d 89e9fac 2fb728d 89e9fac 2fb728d ccb684e 2fb728d 89e9fac 2fb728d 1435089 2a3d557 89e9fac 2fb728d 89e9fac 6c8c209 2fb728d 89e9fac 2fb728d 89e9fac 2fb728d 89e9fac 2fb728d 89e9fac 5a5956e 2fb728d 89e9fac 2fb728d 89e9fac 2fb728d d0a1b8c 2fb728d d0a1b8c 2fb728d 89e9fac 40e61c6 89e9fac 301ae18 ccb684e 301ae18 89e9fac ccb684e 89e9fac ccb684e 89e9fac 27c9477 3b7cfa9 2a3d557 2fb728d |
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
import spaces
import gradio as gr
from transformers import LlavaForConditionalGeneration, TextIteratorStreamer, AutoProcessor
import torch
from PIL import Image
from threading import Thread
from typing import Generator
from liger_kernel.transformers import apply_liger_kernel_to_llama
LOGO_SRC = """data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+CjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+Cjxzdmcgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDUzOCA1MzUiIHZlcnNpb249IjEuMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSIgeG1sbnM6c2VyaWY9Imh0dHA6Ly93d3cuc2VyaWYuY29tLyIgc3R5bGU9ImZpbGwtcnVsZTpldmVub2RkO2NsaXAtcn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IDQyMy40OCwzOTEuMTIgNDE5LjI5LDM5MS42M0w0MTkuMywzOTEuNjNaIiBzdHlsZT0iZmlsbDpyZ2IoMjUwLDEzOSwxKTtmaWxsLXJ1bGU6bm9uemVybzsiLz4KICAgICAgICA8cGF0aCBkPSJNNDYyLjcxLDI0MC4xOUM0NjIuOCwyMTYuOTEgNDgwLjI0LDE5OS43OSA1MDQuMDEsMTk5LjY3QzUyNi41NywxOTkuNTUgNTQ0Ljg5LDIxOC4wNyA1NDQuNTEsMjQxLjM0QzU0NC4xOCwyNjEuODUgNTMwLjA5LDI4MS45NiA1MDEuOTEsMjgxLjIzQzQ4MC42OCwyODAuNjggNDYyLjE1LDI2My44IDQ2Mi43MSwyNDAuMkw0NjIuNzEsMjQwLjE5WiIgc3R5bGU9ImZpbGw6cmdiKDI1MCwxMzksMSk7ZmlsbC1ydWxlOm5vbnplcm87Ii8+CiAgICAgICAgPHBhdGggZD0iTTM3MC45OSwyNDAuMDhDMzcxLDI2Mi43OSAzNTIuNTMsMjgxLjM1IDMyOS44OSwyODEuMzdDMzA3LjA1LDI4MS40IDI4OC45NiwyNjMuNDIgMjg4Ljk2LDI0MC42OEMyODguOTYsMjE4LjE0IDMwNi43MywyMDAgMzI5LjE2LDE5OS42MkMzNTIuMDIsMTk5LjI0IDM3MC45OCwyMTcuNTcgMzcwLjk5LDI0MC4wOFoiIHN0eWxlPSJmaWxsOnJnYigyNTAsMTM5LDEpO2ZpbGwtcnVsZTpub256ZXJvOyIvPgogICAgPC9nPgo8L3N2Zz4K"""
MODEL_PATH = "fancyfeast/llama-joycaption-beta-one-hf-llava"
TITLE = f"""<style>
.joy-header {{display:flex; align-items:center; justify-content:center;
gap:16px; margin:4px 0 12px;}}
.joy-header h1{{margin:0; font-size:1.9rem; line-height:1.2;}}
.joy-header p {{margin:2px 0 0; font-size:0.9rem; color:#666;}}
.joy-header img{{height:56px;}}
</style>
<div class="joy-header">
<img src="{LOGO_SRC}" alt="JoyCaption logo">
<div>
<h1>JoyCaption <span style="font-weight:400">Beta One</span></h1>
<p>Image-captioning model | build mb3500zp</p>
</div>
</div>
<hr>"""
DESCRIPTION = """
<div>
<h2>Quick-start</h2>
<ol>
<li><strong>Upload or drop</strong> an image in the left-hand panel.</li>
<li>Pick a <strong>Caption Type</strong> and, if you wish, adjust the <strong>Caption Length</strong>.</li>
<li>(Optional) <em>expand the "Extra Options" accordion</em> and tick any boxes that should influence the caption.</li>
<li>(Optional) open <em>Generation settings</em> to adjust <code>temperature</code>, <code>top-p</code>, or <code>max tokens</code>.</li>
<li>Press <kbd>Caption</kbd>.
The prompt sent to the model appears in the <em>Prompt</em> box (editable),
and the resulting caption streams into the <em>Caption</em> box.</li>
</ol>
<!-- ───────────────────── Caption-type reference ──────────────────── -->
<h2>Caption Types</h2>
<table>
<tr><th>Mode</th><th>What it does</th></tr>
<tr><td><strong>Descriptive</strong></td>
<td>Formal, detailed prose description.</td></tr>
<tr><td><strong>Descriptive (Casual)</strong></td>
<td>Similar to Descriptive but with a friendlier, conversational tone.</td></tr>
<tr><td><strong>Straightforward</strong></td>
<td>Objective, no fluff, and more succinct than Descriptive.</td></tr>
<tr><td><strong>Stable Diffusion Prompt</strong></td>
<td>Reverse-engineers a prompt that could have produced the image in a SD/T2I model.<br><em>⚠︎ Experimental – can glitch ≈ 3% of the time.</em></td></tr>
<tr><td><strong>MidJourney</strong></td>
<td>Same idea as above but tuned to MidJourney’s prompt style.<br><em>⚠︎ Experimental – can glitch ≈ 3% of the time.</em></td></tr>
<tr><td><strong>Danbooru tag list</strong></td>
<td>Comma-separated tags strictly following Danbooru conventions
(artist:, copyright:, etc.). Lower-case underscores only.<br><em>⚠︎ Experimental – can glitch ≈ 3% of the time.</em></td></tr>
<tr><td><strong>e621 tag list</strong></td>
<td>Alphabetical, namespaced tags in e621 style – includes species/meta
tags when relevant.<br><em>⚠︎ Experimental – can glitch ≈ 3% of the time.</em></td></tr>
<tr><td><strong>Rule34 tag list</strong></td>
<td>Rule34 style alphabetical tag dump; artist/copyright/character
prefixes first.<br><em>⚠︎ Experimental – can glitch ≈ 3% of the time.</em></td></tr>
<tr><td><strong>Booru-like tag list</strong></td>
<td>Looser tag list when you want labels but not a specific Booru format.<br><em>⚠︎ Experimental – can glitch ≈ 3% of the time.</em></td></tr>
<tr><td><strong>Art Critic</strong></td>
<td>Paragraph of art-historical commentary: composition, symbolism, style,
lighting, movement, etc.</td></tr>
<tr><td><strong>Product Listing</strong></td>
<td>Short marketing copy as if selling the depicted object.</td></tr>
<tr><td><strong>Social Media Post</strong></td>
<td>Catchy caption aimed at platforms like Instagram or BlueSky.</td></tr>
</table>
<p style="margin-top:0.6em">
<strong>Note on Booru modes:</strong> They’re tuned for
anime-style / illustration imagery; accuracy drops on real-world photographs
or highly abstract artwork.
</p>
<!-- ───────────────────── Extras + generation notes ───────────────── -->
<h3>Extra Options</h3>
<p>These check-boxes fine-tune what the model should or should not mention:
lighting, camera angle, aesthetic rating, profanity, etc.
Toggle them before hitting <kbd>Caption</kbd>; the prompt box will update
instantly.</p>
<h3>Generation settings</h3>
<ul>
<li><strong>Temperature</strong> – randomness.
0 = deterministic; higher = more variety.</li>
<li><strong>Top-p</strong> – nucleus sampling cutoff. Lower = safer,
higher = freer.</li>
<li><strong>Max New Tokens</strong> – hard stop for the model’s output length.</li>
</ul>
<p>Enjoy experimenting, and feel free to open an issue if you
spot any bugs or have feature ideas!</p>
<hr>
<p>🚨🚨🚨 If the "Help improve JoyCaption" box is checked, the _text_ query you write will be logged and I _might_ use it to help improve JoyCaption.
It does not log images, user data, etc; only the text query. I cannot see what images you send, and frankly, I don't want to. But knowing what kinds of instructions
and queries users want JoyCaption to handle will help guide me in building JoyCaption's dataset. This dataset will be made public. As always, the model itself is completely
public and free to use outside of this space. And, of course, I have no control nor access to what HuggingFace, which are graciously hosting this space, collects.</p>
</div>
"""
CAPTION_TYPE_MAP = {
"Descriptive": [
"Write a detailed description for this image.",
"Write a detailed description for this image in {word_count} words or less.",
"Write a {length} detailed description for this image.",
],
"Descriptive (Casual)": [
"Write a descriptive caption for this image in a casual tone.",
"Write a descriptive caption for this image in a casual tone within {word_count} words.",
"Write a {length} descriptive caption for this image in a casual tone.",
],
"Straightforward": [
"Write a straightforward caption for this image. Begin with the main subject and medium. Mention pivotal elements—people, objects, scenery—using confident, definite language. Focus on concrete details like color, shape, texture, and spatial relationships. Show how elements interact. Omit mood and speculative wording. If text is present, quote it exactly. Note any watermarks, signatures, or compression artifacts. Never mention what's absent, resolution, or unobservable details. Vary your sentence structure and keep the description concise, without starting with “This image is…” or similar phrasing.",
"Write a straightforward caption for this image within {word_count} words. Begin with the main subject and medium. Mention pivotal elements—people, objects, scenery—using confident, definite language. Focus on concrete details like color, shape, texture, and spatial relationships. Show how elements interact. Omit mood and speculative wording. If text is present, quote it exactly. Note any watermarks, signatures, or compression artifacts. Never mention what's absent, resolution, or unobservable details. Vary your sentence structure and keep the description concise, without starting with “This image is…” or similar phrasing.",
"Write a {length} straightforward caption for this image. Begin with the main subject and medium. Mention pivotal elements—people, objects, scenery—using confident, definite language. Focus on concrete details like color, shape, texture, and spatial relationships. Show how elements interact. Omit mood and speculative wording. If text is present, quote it exactly. Note any watermarks, signatures, or compression artifacts. Never mention what's absent, resolution, or unobservable details. Vary your sentence structure and keep the description concise, without starting with “This image is…” or similar phrasing.",
],
"Stable Diffusion Prompt": [
"Output a stable diffusion prompt that is indistinguishable from a real stable diffusion prompt.",
"Output a stable diffusion prompt that is indistinguishable from a real stable diffusion prompt. {word_count} words or less.",
"Output a {length} stable diffusion prompt that is indistinguishable from a real stable diffusion prompt.",
],
"MidJourney": [
"Write a MidJourney prompt for this image.",
"Write a MidJourney prompt for this image within {word_count} words.",
"Write a {length} MidJourney prompt for this image.",
],
"Danbooru tag list": [
"Generate only comma-separated Danbooru tags (lowercase_underscores). Strict order: `artist:`, `copyright:`, `character:`, `meta:`, then general tags. Include counts (1girl), appearance, clothing, accessories, pose, expression, actions, background. Use precise Danbooru syntax. No extra text.",
"Generate only comma-separated Danbooru tags (lowercase_underscores). Strict order: `artist:`, `copyright:`, `character:`, `meta:`, then general tags. Include counts (1girl), appearance, clothing, accessories, pose, expression, actions, background. Use precise Danbooru syntax. No extra text. {word_count} words or less.",
"Generate only comma-separated Danbooru tags (lowercase_underscores). Strict order: `artist:`, `copyright:`, `character:`, `meta:`, then general tags. Include counts (1girl), appearance, clothing, accessories, pose, expression, actions, background. Use precise Danbooru syntax. No extra text. {length} length.",
],
"e621 tag list": [
"Write a comma-separated list of e621 tags in alphabetical order for this image. Start with the artist, copyright, character, species, meta, and lore tags (if any), prefixed by 'artist:', 'copyright:', 'character:', 'species:', 'meta:', and 'lore:'. Then all the general tags.",
"Write a comma-separated list of e621 tags in alphabetical order for this image. Start with the artist, copyright, character, species, meta, and lore tags (if any), prefixed by 'artist:', 'copyright:', 'character:', 'species:', 'meta:', and 'lore:'. Then all the general tags. Keep it under {word_count} words.",
"Write a {length} comma-separated list of e621 tags in alphabetical order for this image. Start with the artist, copyright, character, species, meta, and lore tags (if any), prefixed by 'artist:', 'copyright:', 'character:', 'species:', 'meta:', and 'lore:'. Then all the general tags.",
],
"Rule34 tag list": [
"Write a comma-separated list of rule34 tags in alphabetical order for this image. Start with the artist, copyright, character, and meta tags (if any), prefixed by 'artist:', 'copyright:', 'character:', and 'meta:'. Then all the general tags.",
"Write a comma-separated list of rule34 tags in alphabetical order for this image. Start with the artist, copyright, character, and meta tags (if any), prefixed by 'artist:', 'copyright:', 'character:', and 'meta:'. Then all the general tags. Keep it under {word_count} words.",
"Write a {length} comma-separated list of rule34 tags in alphabetical order for this image. Start with the artist, copyright, character, and meta tags (if any), prefixed by 'artist:', 'copyright:', 'character:', and 'meta:'. Then all the general tags.",
],
"Booru-like tag list": [
"Write a list of Booru-like tags for this image.",
"Write a list of Booru-like tags for this image within {word_count} words.",
"Write a {length} list of Booru-like tags for this image.",
],
"Art Critic": [
"Analyze this image like an art critic would with information about its composition, style, symbolism, the use of color, light, any artistic movement it might belong to, etc.",
"Analyze this image like an art critic would with information about its composition, style, symbolism, the use of color, light, any artistic movement it might belong to, etc. Keep it within {word_count} words.",
"Analyze this image like an art critic would with information about its composition, style, symbolism, the use of color, light, any artistic movement it might belong to, etc. Keep it {length}.",
],
"Product Listing": [
"Write a caption for this image as though it were a product listing.",
"Write a caption for this image as though it were a product listing. Keep it under {word_count} words.",
"Write a {length} caption for this image as though it were a product listing.",
],
"Social Media Post": [
"Write a caption for this image as if it were being used for a social media post.",
"Write a caption for this image as if it were being used for a social media post. Limit the caption to {word_count} words.",
"Write a {length} caption for this image as if it were being used for a social media post.",
],
}
NAME_OPTION = "If there is a person/character in the image you must refer to them as {name}."
# Load model
processor = AutoProcessor.from_pretrained(MODEL_PATH)
model = LlavaForConditionalGeneration.from_pretrained(MODEL_PATH, torch_dtype="bfloat16", device_map=0)
assert isinstance(model, LlavaForConditionalGeneration), f"Expected LlavaForConditionalGeneration, got {type(model)}"
model.eval()
apply_liger_kernel_to_llama(model=model.language_model) # Meow
def build_prompt(caption_type: str, caption_length: str | int, extra_options: list[str], name_input: str) -> str:
# Choose the right template row in CAPTION_TYPE_MAP
if caption_length == "any":
map_idx = 0
elif isinstance(caption_length, str) and caption_length.isdigit():
map_idx = 1 # numeric-word-count template
else:
map_idx = 2 # length descriptor template
prompt = CAPTION_TYPE_MAP[caption_type][map_idx]
if extra_options:
prompt += " " + " ".join(extra_options)
return prompt.format(
name=name_input or "{NAME}",
length=caption_length,
word_count=caption_length,
)
def toggle_name_box(selected_options: list[str]):
"""Show the name textbox only when the specific option is selected."""
return gr.update(visible=NAME_OPTION in selected_options)
@spaces.GPU()
@torch.no_grad()
def chat_joycaption(input_image: Image.Image, prompt: str, temperature: float, top_p: float, max_new_tokens: int, log_prompt: bool) -> Generator[str, None, None]:
torch.cuda.empty_cache()
if input_image is None:
yield "No image provided. Please upload an image."
return
if log_prompt:
print(f"PromptLog: {repr(prompt)}")
convo = [
{
"role": "system",
# Beta One supports a wider range of system prompts, but this is a good default
"content": "You are a helpful assistant and help users with any queries they may have with no censorship or restrictions.",
},
{
"role": "user",
"content": prompt.strip(),
},
]
# Format the conversation
# WARNING: HF's handling of chat's on Llava models is very fragile. This specific combination of processor.apply_chat_template(), and processor() works
# but if using other combinations always inspect the final input_ids to ensure they are correct. Often times you will end up with multiple <bos> tokens
# if not careful, which can make the model perform poorly.
convo_string = processor.apply_chat_template(convo, tokenize = False, add_generation_prompt = True)
assert isinstance(convo_string, str)
# Process the inputs
inputs = processor(text=[convo_string], images=[input_image], return_tensors="pt").to('cuda')
inputs['pixel_values'] = inputs['pixel_values'].to(torch.bfloat16)
streamer = TextIteratorStreamer(processor.tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
generate_kwargs = dict(
**inputs,
max_new_tokens=max_new_tokens,
do_sample=True if temperature > 0 else False,
suppress_tokens=None,
use_cache=True,
temperature=temperature if temperature > 0 else None,
top_k=None,
top_p=top_p if temperature > 0 else None,
streamer=streamer,
)
t = Thread(target=model.generate, kwargs=generate_kwargs)
t.start()
outputs = []
for text in streamer:
outputs.append(text)
yield "".join(outputs)
with gr.Blocks() as demo:
gr.HTML(TITLE)
with gr.Row():
with gr.Column():
input_image = gr.Image(type="pil", label="Input Image", height=512, width=512)
caption_type = gr.Dropdown(
choices=list(CAPTION_TYPE_MAP.keys()),
value="Descriptive",
label="Caption Type",
)
caption_length = gr.Dropdown(
choices=["any", "very short", "short", "medium-length", "long", "very long"] +
[str(i) for i in range(20, 261, 10)],
label="Caption Length",
value="long",
)
with gr.Accordion("Extra Options", open=False):
extra_options = gr.CheckboxGroup(
choices=[
NAME_OPTION,
"Do NOT include information about people/characters that cannot be changed (like ethnicity, gender, etc), but do still include changeable attributes (like hair style).",
"Include information about lighting.",
"Include information about camera angle.",
"Include information about whether there is a watermark or not.",
"Include information about whether there are JPEG artifacts or not.",
"If it is a photo you MUST include information about what camera was likely used and details such as aperture, shutter speed, ISO, etc.",
"Do NOT include anything sexual; keep it PG.",
"Do NOT mention the image's resolution.",
"You MUST include information about the subjective aesthetic quality of the image from low to very high.",
"Include information on the image's composition style, such as leading lines, rule of thirds, or symmetry.",
"Do NOT mention any text that is in the image.",
"Specify the depth of field and whether the background is in focus or blurred.",
"If applicable, mention the likely use of artificial or natural lighting sources.",
"Do NOT use any ambiguous language.",
"Include whether the image is sfw, suggestive, or nsfw.",
"ONLY describe the most important elements of the image.",
"If it is a work of art, do not include the artist's name or the title of the work.",
"Identify the image orientation (portrait, landscape, or square) and aspect ratio if obvious.",
"""Use vulgar slang and profanity, such as (but not limited to) "fucking," "slut," "cock," etc.""",
"Do NOT use polite euphemisms—lean into blunt, casual phrasing.",
"Include information about the ages of any people/characters when applicable.",
"Mention whether the image depicts an extreme close-up, close-up, medium close-up, medium shot, cowboy shot, medium wide shot, wide shot, or extreme wide shot.",
"Do not mention the mood/feeling/etc of the image.",
"Explicitly specify the vantage height (eye-level, low-angle worm’s-eye, bird’s-eye, drone, rooftop, etc.).",
"If there is a watermark, you must mention it.",
"""Your response will be used by a text-to-image model, so avoid useless meta phrases like “This image shows…”, "You are looking at...", etc.""",
],
label="Select one or more",
)
name_input = gr.Textbox(label="Person / Character Name", visible=False)
with gr.Accordion("Generation settings", open=False):
temperature_slider = gr.Slider(
minimum=0.0, maximum=2.0, value=0.6, step=0.05,
label="Temperature",
info="Higher values make the output more random, lower values make it more deterministic."
)
top_p_slider = gr.Slider(
minimum=0.0, maximum=1.0, value=0.9, step=0.01,
label="Top-p"
)
max_tokens_slider = gr.Slider(
minimum=1, maximum=2048, value=512, step=1,
label="Max New Tokens",
info="Maximum number of tokens to generate. The model will stop generating if it reaches this limit."
)
log_prompt = gr.Checkbox(value=True, label="Help improve JoyCaption by logging your text query")
with gr.Column():
prompt_box = gr.Textbox(lines=4, label="Prompt", interactive=True)
# Show the name input box only when the specific option is selected
extra_options.change(
toggle_name_box,
inputs=extra_options,
outputs=name_input,
)
# Auto-update prompt box whenever any of the inputs change
for ctrl in (caption_type, caption_length, extra_options, name_input):
ctrl.change(
build_prompt,
inputs=[caption_type, caption_length, extra_options, name_input],
outputs=prompt_box,
)
run_button = gr.Button("Caption")
output_caption = gr.Textbox(label="Caption")
run_button.click(
chat_joycaption,
inputs=[input_image, prompt_box, temperature_slider, top_p_slider, max_tokens_slider, log_prompt],
outputs=output_caption,
)
# Initial prompt
prompt_box.value = build_prompt(caption_type.value, caption_length.value, extra_options.value, name_input.value)
gr.Markdown(DESCRIPTION)
if __name__ == "__main__":
demo.launch() |