Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -112,7 +112,7 @@ DEFAULT_PROMPTS = {
|
|
| 112 |
}
|
| 113 |
}
|
| 114 |
|
| 115 |
-
# =====
|
| 116 |
def safe_generate_caption_direct(image, system_prompt, user_prompt, max_chars=1200):
|
| 117 |
try:
|
| 118 |
if image is None:
|
|
@@ -150,7 +150,6 @@ def safe_generate_caption_direct(image, system_prompt, user_prompt, max_chars=12
|
|
| 150 |
gc.collect()
|
| 151 |
return f"β Error: {str(e)[:200]}"
|
| 152 |
|
| 153 |
-
# ===== INDIVIDUAL CAPTION WRAPPERS =====
|
| 154 |
@spaces.GPU(duration=60)
|
| 155 |
@torch.no_grad()
|
| 156 |
def generate_caption(image, system, user):
|
|
@@ -239,9 +238,10 @@ def export_joycaption_data(keywords, custom_instructions, avoid, question, c1, c
|
|
| 239 |
|
| 240 |
# ===== UI =====
|
| 241 |
with gr.Blocks(title="JoyCaption Advanced Prompting System", theme=gr.themes.Soft()) as demo:
|
|
|
|
| 242 |
gr.HTML("""
|
| 243 |
<style>
|
| 244 |
-
textarea
|
| 245 |
</style>
|
| 246 |
""")
|
| 247 |
gr.HTML("<h1 style='text-align:center;margin-top:10px;'>π¨ JoyCaption Advanced Prompting System (v6.0)</h1><hr>")
|
|
@@ -250,7 +250,6 @@ with gr.Blocks(title="JoyCaption Advanced Prompting System", theme=gr.themes.Sof
|
|
| 250 |
active_tab = gr.State("casual")
|
| 251 |
|
| 252 |
with gr.Row():
|
| 253 |
-
# LEFT
|
| 254 |
with gr.Column(scale=1):
|
| 255 |
image_input = gr.Image(type="pil", label="πΈ Image", height=400)
|
| 256 |
keywords_input = gr.Textbox(label="π·οΈ Keywords", lines=2, placeholder="e.g. beach, sunset")
|
|
@@ -260,9 +259,7 @@ with gr.Blocks(title="JoyCaption Advanced Prompting System", theme=gr.themes.Sof
|
|
| 260 |
ask_btn = gr.Button("Ask", variant="secondary")
|
| 261 |
qa_output = gr.Textbox(label="Answer", lines=3, show_copy_button=True)
|
| 262 |
|
| 263 |
-
# RIGHT
|
| 264 |
with gr.Column(scale=1):
|
| 265 |
-
# template buttons top
|
| 266 |
gr.Markdown("**Insert Template**")
|
| 267 |
with gr.Row():
|
| 268 |
key_btn = gr.Button("key", size="sm")
|
|
@@ -270,26 +267,27 @@ with gr.Blocks(title="JoyCaption Advanced Prompting System", theme=gr.themes.Sof
|
|
| 270 |
use_btn = gr.Button("use", size="sm")
|
| 271 |
not_btn = gr.Button("not", size="sm")
|
| 272 |
|
| 273 |
-
# Tabs
|
| 274 |
with gr.Tab("π Casual") as tab1:
|
| 275 |
gr.Markdown("**System Prompt**")
|
| 276 |
-
system1 = gr.Textbox(show_label=False, value=DEFAULT_PROMPTS["casual"]["system"]
|
| 277 |
gr.Markdown("**User Prompt**")
|
| 278 |
-
user1 = gr.Textbox(show_label=False, value=DEFAULT_PROMPTS["casual"]["user"]
|
| 279 |
gen1_btn = gr.Button("Generate Casual", variant="primary")
|
| 280 |
out1 = gr.Textbox(lines=5, show_copy_button=True)
|
|
|
|
| 281 |
with gr.Tab("π€ Friendly") as tab2:
|
| 282 |
gr.Markdown("**System Prompt**")
|
| 283 |
-
system2 = gr.Textbox(show_label=False, value=DEFAULT_PROMPTS["friendly"]["system"]
|
| 284 |
gr.Markdown("**User Prompt**")
|
| 285 |
-
user2 = gr.Textbox(show_label=False, value=DEFAULT_PROMPTS["friendly"]["user"]
|
| 286 |
gen2_btn = gr.Button("Generate Friendly", variant="primary")
|
| 287 |
out2 = gr.Textbox(lines=5, show_copy_button=True)
|
|
|
|
| 288 |
with gr.Tab("π₯ Erotic") as tab3:
|
| 289 |
gr.Markdown("**System Prompt**")
|
| 290 |
-
system3 = gr.Textbox(show_label=False, value=DEFAULT_PROMPTS["erotic"]["system"]
|
| 291 |
gr.Markdown("**User Prompt**")
|
| 292 |
-
user3 = gr.Textbox(show_label=False, value=DEFAULT_PROMPTS["erotic"]["user"]
|
| 293 |
gen3_btn = gr.Button("Generate Erotic", variant="primary")
|
| 294 |
out3 = gr.Textbox(lines=5, show_copy_button=True)
|
| 295 |
|
|
@@ -298,23 +296,21 @@ with gr.Blocks(title="JoyCaption Advanced Prompting System", theme=gr.themes.Sof
|
|
| 298 |
export_out = gr.Textbox(visible=False)
|
| 299 |
export_file = gr.File(visible=False)
|
| 300 |
|
| 301 |
-
# ===
|
| 302 |
tab1.select(lambda: "casual", None, active_tab)
|
| 303 |
tab2.select(lambda: "friendly", None, active_tab)
|
| 304 |
tab3.select(lambda: "erotic", None, active_tab)
|
| 305 |
|
| 306 |
-
# ===
|
| 307 |
gen1_btn.click(generate_caption, [image_input, system1, user1], out1)
|
| 308 |
gen2_btn.click(generate_caption, [image_input, system2, user2], out2)
|
| 309 |
gen3_btn.click(generate_caption, [image_input, system3, user3], out3)
|
| 310 |
ask_btn.click(answer_question, [image_input, question_input], qa_output)
|
| 311 |
|
| 312 |
-
# Template logic
|
| 313 |
def handle_template(btn_type, tab, s1, u1, s2, u2, s3, u3, k, c, q, a):
|
| 314 |
key_f, que_f, use_f, not_f = create_template_functions()
|
| 315 |
-
mapping = {
|
| 316 |
-
"key": key_f, "que": que_f, "use": use_f, "not": not_f
|
| 317 |
-
}
|
| 318 |
fn = mapping.get(btn_type)
|
| 319 |
if not fn:
|
| 320 |
return s1, u1, s2, u2, s3, u3
|
|
@@ -351,5 +347,21 @@ with gr.Blocks(title="JoyCaption Advanced Prompting System", theme=gr.themes.Sof
|
|
| 351 |
[export_out, export_file]
|
| 352 |
)
|
| 353 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
if __name__ == "__main__":
|
| 355 |
demo.launch()
|
|
|
|
| 112 |
}
|
| 113 |
}
|
| 114 |
|
| 115 |
+
# ===== CAPTION GENERATION =====
|
| 116 |
def safe_generate_caption_direct(image, system_prompt, user_prompt, max_chars=1200):
|
| 117 |
try:
|
| 118 |
if image is None:
|
|
|
|
| 150 |
gc.collect()
|
| 151 |
return f"β Error: {str(e)[:200]}"
|
| 152 |
|
|
|
|
| 153 |
@spaces.GPU(duration=60)
|
| 154 |
@torch.no_grad()
|
| 155 |
def generate_caption(image, system, user):
|
|
|
|
| 238 |
|
| 239 |
# ===== UI =====
|
| 240 |
with gr.Blocks(title="JoyCaption Advanced Prompting System", theme=gr.themes.Soft()) as demo:
|
| 241 |
+
# CSS for autoresize
|
| 242 |
gr.HTML("""
|
| 243 |
<style>
|
| 244 |
+
textarea {overflow-y:hidden !important;}
|
| 245 |
</style>
|
| 246 |
""")
|
| 247 |
gr.HTML("<h1 style='text-align:center;margin-top:10px;'>π¨ JoyCaption Advanced Prompting System (v6.0)</h1><hr>")
|
|
|
|
| 250 |
active_tab = gr.State("casual")
|
| 251 |
|
| 252 |
with gr.Row():
|
|
|
|
| 253 |
with gr.Column(scale=1):
|
| 254 |
image_input = gr.Image(type="pil", label="πΈ Image", height=400)
|
| 255 |
keywords_input = gr.Textbox(label="π·οΈ Keywords", lines=2, placeholder="e.g. beach, sunset")
|
|
|
|
| 259 |
ask_btn = gr.Button("Ask", variant="secondary")
|
| 260 |
qa_output = gr.Textbox(label="Answer", lines=3, show_copy_button=True)
|
| 261 |
|
|
|
|
| 262 |
with gr.Column(scale=1):
|
|
|
|
| 263 |
gr.Markdown("**Insert Template**")
|
| 264 |
with gr.Row():
|
| 265 |
key_btn = gr.Button("key", size="sm")
|
|
|
|
| 267 |
use_btn = gr.Button("use", size="sm")
|
| 268 |
not_btn = gr.Button("not", size="sm")
|
| 269 |
|
|
|
|
| 270 |
with gr.Tab("π Casual") as tab1:
|
| 271 |
gr.Markdown("**System Prompt**")
|
| 272 |
+
system1 = gr.Textbox(show_label=False, value=DEFAULT_PROMPTS["casual"]["system"])
|
| 273 |
gr.Markdown("**User Prompt**")
|
| 274 |
+
user1 = gr.Textbox(show_label=False, value=DEFAULT_PROMPTS["casual"]["user"])
|
| 275 |
gen1_btn = gr.Button("Generate Casual", variant="primary")
|
| 276 |
out1 = gr.Textbox(lines=5, show_copy_button=True)
|
| 277 |
+
|
| 278 |
with gr.Tab("π€ Friendly") as tab2:
|
| 279 |
gr.Markdown("**System Prompt**")
|
| 280 |
+
system2 = gr.Textbox(show_label=False, value=DEFAULT_PROMPTS["friendly"]["system"])
|
| 281 |
gr.Markdown("**User Prompt**")
|
| 282 |
+
user2 = gr.Textbox(show_label=False, value=DEFAULT_PROMPTS["friendly"]["user"])
|
| 283 |
gen2_btn = gr.Button("Generate Friendly", variant="primary")
|
| 284 |
out2 = gr.Textbox(lines=5, show_copy_button=True)
|
| 285 |
+
|
| 286 |
with gr.Tab("π₯ Erotic") as tab3:
|
| 287 |
gr.Markdown("**System Prompt**")
|
| 288 |
+
system3 = gr.Textbox(show_label=False, value=DEFAULT_PROMPTS["erotic"]["system"])
|
| 289 |
gr.Markdown("**User Prompt**")
|
| 290 |
+
user3 = gr.Textbox(show_label=False, value=DEFAULT_PROMPTS["erotic"]["user"])
|
| 291 |
gen3_btn = gr.Button("Generate Erotic", variant="primary")
|
| 292 |
out3 = gr.Textbox(lines=5, show_copy_button=True)
|
| 293 |
|
|
|
|
| 296 |
export_out = gr.Textbox(visible=False)
|
| 297 |
export_file = gr.File(visible=False)
|
| 298 |
|
| 299 |
+
# === Tab tracking
|
| 300 |
tab1.select(lambda: "casual", None, active_tab)
|
| 301 |
tab2.select(lambda: "friendly", None, active_tab)
|
| 302 |
tab3.select(lambda: "erotic", None, active_tab)
|
| 303 |
|
| 304 |
+
# === Button actions
|
| 305 |
gen1_btn.click(generate_caption, [image_input, system1, user1], out1)
|
| 306 |
gen2_btn.click(generate_caption, [image_input, system2, user2], out2)
|
| 307 |
gen3_btn.click(generate_caption, [image_input, system3, user3], out3)
|
| 308 |
ask_btn.click(answer_question, [image_input, question_input], qa_output)
|
| 309 |
|
| 310 |
+
# === Template logic
|
| 311 |
def handle_template(btn_type, tab, s1, u1, s2, u2, s3, u3, k, c, q, a):
|
| 312 |
key_f, que_f, use_f, not_f = create_template_functions()
|
| 313 |
+
mapping = {"key": key_f, "que": que_f, "use": use_f, "not": not_f}
|
|
|
|
|
|
|
| 314 |
fn = mapping.get(btn_type)
|
| 315 |
if not fn:
|
| 316 |
return s1, u1, s2, u2, s3, u3
|
|
|
|
| 347 |
[export_out, export_file]
|
| 348 |
)
|
| 349 |
|
| 350 |
+
# === JS autoresize for all tabs
|
| 351 |
+
demo.load(None, None, None, _js="""
|
| 352 |
+
() => {
|
| 353 |
+
function resizeAll() {
|
| 354 |
+
document.querySelectorAll('textarea').forEach(t=>{
|
| 355 |
+
t.style.height='auto';
|
| 356 |
+
t.style.height=(t.scrollHeight+5)+'px';
|
| 357 |
+
});
|
| 358 |
+
}
|
| 359 |
+
resizeAll();
|
| 360 |
+
document.querySelectorAll('[role="tab"]').forEach(tab=>{
|
| 361 |
+
tab.addEventListener('click', ()=>setTimeout(resizeAll,300));
|
| 362 |
+
});
|
| 363 |
+
}
|
| 364 |
+
""")
|
| 365 |
+
|
| 366 |
if __name__ == "__main__":
|
| 367 |
demo.launch()
|