Spaces:
Running
Running
Rishi Desai
commited on
Commit
·
0f8d917
1
Parent(s):
69f7712
fixed PO info
Browse files
demo.py
CHANGED
|
@@ -494,6 +494,119 @@ def setup_event_handlers(
|
|
| 494 |
outputs=None
|
| 495 |
)
|
| 496 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 497 |
# ------- Main Application -------
|
| 498 |
|
| 499 |
def build_ui():
|
|
@@ -531,108 +644,25 @@ def build_ui():
|
|
| 531 |
|
| 532 |
with gr.TabItem("Prompt Optimization") as prompt_tab:
|
| 533 |
with gr.Row():
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
file_count="multiple",
|
| 541 |
-
label="Upload caption files",
|
| 542 |
-
file_types=[".txt"],
|
| 543 |
-
type="filepath",
|
| 544 |
-
elem_classes="file-upload-container"
|
| 545 |
-
)
|
| 546 |
-
|
| 547 |
-
manual_captions = gr.Textbox(
|
| 548 |
-
label="Or enter captions manually (one per line)",
|
| 549 |
-
lines=5,
|
| 550 |
-
placeholder="Enter captions here, one per line",
|
| 551 |
-
elem_classes="prompt-box"
|
| 552 |
-
)
|
| 553 |
-
|
| 554 |
-
# Add button to use captions from image captioning tab
|
| 555 |
-
use_generated_captions = gr.Button("Use Captions from Manual Entry", variant="secondary")
|
| 556 |
-
|
| 557 |
-
# Function to update manual captions with shared ones
|
| 558 |
-
def fill_with_shared_captions(captions_list):
|
| 559 |
-
if not captions_list or len(captions_list) == 0:
|
| 560 |
-
return "No captions available. Generate captions in the Image Captioning tab first."
|
| 561 |
-
return "\n".join(captions_list)
|
| 562 |
-
|
| 563 |
-
# Connect button to fill manual captions area
|
| 564 |
-
use_generated_captions.click(
|
| 565 |
-
fill_with_shared_captions,
|
| 566 |
-
inputs=[shared_captions],
|
| 567 |
-
outputs=[manual_captions]
|
| 568 |
-
)
|
| 569 |
-
|
| 570 |
-
with gr.Column(scale=1):
|
| 571 |
-
# Right side for prompt input and output
|
| 572 |
-
gr.Markdown("### Optimize Prompt")
|
| 573 |
-
|
| 574 |
-
user_prompt = gr.Textbox(
|
| 575 |
-
label="Enter your prompt",
|
| 576 |
-
lines=3,
|
| 577 |
-
placeholder="Enter the prompt you want to optimize",
|
| 578 |
-
elem_classes="prompt-box"
|
| 579 |
-
)
|
| 580 |
-
|
| 581 |
-
optimize_btn = gr.Button("Optimize Prompt", variant="primary", elem_classes="optimize-btn")
|
| 582 |
-
|
| 583 |
-
optimized_prompt = gr.Textbox(
|
| 584 |
-
label="Optimized Prompt",
|
| 585 |
-
lines=5,
|
| 586 |
-
placeholder="Optimized prompt will appear here",
|
| 587 |
-
elem_classes="prompt-box"
|
| 588 |
-
)
|
| 589 |
-
|
| 590 |
-
optimization_status = gr.Markdown("Enter a prompt and upload captions to begin", elem_classes="optimization-status")
|
| 591 |
-
|
| 592 |
-
# Function to handle optimization
|
| 593 |
-
def run_optimization(prompt, caption_files, manual_caption_text):
|
| 594 |
-
if not prompt or prompt.strip() == "":
|
| 595 |
-
return "", "Please enter a prompt to optimize"
|
| 596 |
-
|
| 597 |
-
# Handle different input sources for captions
|
| 598 |
-
caption_list = []
|
| 599 |
-
|
| 600 |
-
if manual_caption_text and manual_caption_text.strip():
|
| 601 |
-
# Use manually entered captions
|
| 602 |
-
caption_list = [line.strip() for line in manual_caption_text.split("\n") if line.strip()]
|
| 603 |
-
|
| 604 |
-
elif caption_files and len(caption_files) > 0:
|
| 605 |
-
# Read captions from uploaded files
|
| 606 |
-
for file_path in caption_files:
|
| 607 |
-
if os.path.exists(file_path) and file_path.lower().endswith('.txt'):
|
| 608 |
-
with open(file_path, 'r', encoding='utf-8') as f:
|
| 609 |
-
content = f.read().strip()
|
| 610 |
-
if content:
|
| 611 |
-
caption_list.append(content)
|
| 612 |
-
|
| 613 |
-
if not caption_list:
|
| 614 |
-
return "", "Please upload caption files or enter captions manually"
|
| 615 |
-
|
| 616 |
-
try:
|
| 617 |
-
# Call the optimize_prompt function from prompt.py
|
| 618 |
-
result = optimize_prompt(prompt, captions_list=caption_list)
|
| 619 |
-
return result, "✅ Prompt optimization complete"
|
| 620 |
-
except Exception as e:
|
| 621 |
-
return "", f"❌ Error optimizing prompt: {str(e)}"
|
| 622 |
|
| 623 |
-
#
|
| 624 |
gr.Markdown("""
|
| 625 |
**About Prompt Optimization:**
|
| 626 |
- This feature helps you craft prompts that match the style of your training captions
|
| 627 |
-
- Upload caption files, enter captions manually, or use captions from the Image Captioning tab
|
| 628 |
- Enter a simple prompt and the system will optimize it to match your training style
|
| 629 |
""", elem_classes=["category-info"])
|
| 630 |
|
| 631 |
-
#
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
)
|
| 637 |
|
| 638 |
# Add CSS styling
|
|
|
|
| 494 |
outputs=None
|
| 495 |
)
|
| 496 |
|
| 497 |
+
# ------- Prompt Optimization UI -------
|
| 498 |
+
|
| 499 |
+
def create_prompt_optimization_ui():
|
| 500 |
+
"""Create UI components for prompt optimization tab"""
|
| 501 |
+
with gr.Column(scale=1) as left_column:
|
| 502 |
+
# Left side for caption input
|
| 503 |
+
gr.Markdown("### Upload Captions")
|
| 504 |
+
gr.Markdown("Upload caption files (.txt) or enter captions manually", elem_classes="file-types-info")
|
| 505 |
+
|
| 506 |
+
captions_upload = gr.File(
|
| 507 |
+
file_count="multiple",
|
| 508 |
+
label="Upload caption files",
|
| 509 |
+
file_types=[".txt"],
|
| 510 |
+
type="filepath",
|
| 511 |
+
elem_classes="file-upload-container"
|
| 512 |
+
)
|
| 513 |
+
|
| 514 |
+
manual_captions = gr.Textbox(
|
| 515 |
+
label="Or enter captions manually",
|
| 516 |
+
lines=5,
|
| 517 |
+
placeholder="Enter captions here, one per line",
|
| 518 |
+
elem_classes="prompt-box"
|
| 519 |
+
)
|
| 520 |
+
|
| 521 |
+
# Add button to use captions from image captioning tab
|
| 522 |
+
use_generated_captions = gr.Button("Use Captions from Manual Entry", variant="secondary")
|
| 523 |
+
|
| 524 |
+
with gr.Column(scale=1) as right_column:
|
| 525 |
+
# Right side for prompt input and output
|
| 526 |
+
gr.Markdown("### Optimize Prompt")
|
| 527 |
+
|
| 528 |
+
user_prompt = gr.Textbox(
|
| 529 |
+
label="Enter your prompt",
|
| 530 |
+
lines=3,
|
| 531 |
+
placeholder="Enter the prompt you want to optimize",
|
| 532 |
+
elem_classes="prompt-box"
|
| 533 |
+
)
|
| 534 |
+
|
| 535 |
+
optimize_btn = gr.Button("Optimize Prompt", variant="primary", elem_classes="optimize-btn")
|
| 536 |
+
|
| 537 |
+
optimized_prompt = gr.Textbox(
|
| 538 |
+
label="Optimized Prompt",
|
| 539 |
+
lines=5,
|
| 540 |
+
placeholder="Optimized prompt will appear here",
|
| 541 |
+
elem_classes="prompt-box"
|
| 542 |
+
)
|
| 543 |
+
|
| 544 |
+
optimization_status = gr.Markdown("Enter a prompt and upload captions to begin", elem_classes="optimization-status")
|
| 545 |
+
|
| 546 |
+
# Return components but NOT info_md (will create it separately in build_ui)
|
| 547 |
+
return (
|
| 548 |
+
left_column, right_column, captions_upload, manual_captions,
|
| 549 |
+
use_generated_captions, user_prompt, optimize_btn,
|
| 550 |
+
optimized_prompt, optimization_status
|
| 551 |
+
)
|
| 552 |
+
|
| 553 |
+
def run_optimization(prompt, caption_files, manual_caption_text):
|
| 554 |
+
"""Handle the prompt optimization logic"""
|
| 555 |
+
if not prompt or prompt.strip() == "":
|
| 556 |
+
return "", "Please enter a prompt to optimize"
|
| 557 |
+
|
| 558 |
+
# Handle different input sources for captions
|
| 559 |
+
caption_list = []
|
| 560 |
+
|
| 561 |
+
if manual_caption_text and manual_caption_text.strip():
|
| 562 |
+
# Use manually entered captions
|
| 563 |
+
caption_list = [line.strip() for line in manual_caption_text.split("\n") if line.strip()]
|
| 564 |
+
|
| 565 |
+
elif caption_files and len(caption_files) > 0:
|
| 566 |
+
# Read captions from uploaded files
|
| 567 |
+
for file_path in caption_files:
|
| 568 |
+
if os.path.exists(file_path) and file_path.lower().endswith('.txt'):
|
| 569 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
| 570 |
+
content = f.read().strip()
|
| 571 |
+
if content:
|
| 572 |
+
caption_list.append(content)
|
| 573 |
+
|
| 574 |
+
if not caption_list:
|
| 575 |
+
return "", "Please upload caption files or enter captions manually"
|
| 576 |
+
|
| 577 |
+
try:
|
| 578 |
+
# Call the optimize_prompt function from prompt.py
|
| 579 |
+
result = optimize_prompt(prompt, captions_list=caption_list)
|
| 580 |
+
return result, "✅ Prompt optimization complete"
|
| 581 |
+
except Exception as e:
|
| 582 |
+
return "", f"❌ Error optimizing prompt: {str(e)}"
|
| 583 |
+
|
| 584 |
+
def setup_prompt_optimization_handlers(
|
| 585 |
+
captions_upload, manual_captions, use_generated_captions,
|
| 586 |
+
user_prompt, optimize_btn, optimized_prompt,
|
| 587 |
+
optimization_status, shared_captions
|
| 588 |
+
):
|
| 589 |
+
"""Set up event handlers for prompt optimization tab"""
|
| 590 |
+
# Function to update manual captions with shared ones
|
| 591 |
+
def fill_with_shared_captions(captions_list):
|
| 592 |
+
if not captions_list or len(captions_list) == 0:
|
| 593 |
+
return "No captions available. Generate captions in the Image Captioning tab first."
|
| 594 |
+
return "\n".join(captions_list)
|
| 595 |
+
|
| 596 |
+
# Connect button to fill manual captions area
|
| 597 |
+
use_generated_captions.click(
|
| 598 |
+
fill_with_shared_captions,
|
| 599 |
+
inputs=[shared_captions],
|
| 600 |
+
outputs=[manual_captions]
|
| 601 |
+
)
|
| 602 |
+
|
| 603 |
+
# Connect the optimize button to the optimization function
|
| 604 |
+
optimize_btn.click(
|
| 605 |
+
run_optimization,
|
| 606 |
+
inputs=[user_prompt, captions_upload, manual_captions],
|
| 607 |
+
outputs=[optimized_prompt, optimization_status]
|
| 608 |
+
)
|
| 609 |
+
|
| 610 |
# ------- Main Application -------
|
| 611 |
|
| 612 |
def build_ui():
|
|
|
|
| 644 |
|
| 645 |
with gr.TabItem("Prompt Optimization") as prompt_tab:
|
| 646 |
with gr.Row():
|
| 647 |
+
# Create prompt optimization UI components
|
| 648 |
+
(
|
| 649 |
+
left_column, right_column, captions_upload, manual_captions,
|
| 650 |
+
use_generated_captions, user_prompt, optimize_btn,
|
| 651 |
+
optimized_prompt, optimization_status
|
| 652 |
+
) = create_prompt_optimization_ui()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 653 |
|
| 654 |
+
# Create info markdown directly at the bottom of the tab
|
| 655 |
gr.Markdown("""
|
| 656 |
**About Prompt Optimization:**
|
| 657 |
- This feature helps you craft prompts that match the style of your training captions
|
|
|
|
| 658 |
- Enter a simple prompt and the system will optimize it to match your training style
|
| 659 |
""", elem_classes=["category-info"])
|
| 660 |
|
| 661 |
+
# Set up prompt optimization event handlers
|
| 662 |
+
setup_prompt_optimization_handlers(
|
| 663 |
+
captions_upload, manual_captions, use_generated_captions,
|
| 664 |
+
user_prompt, optimize_btn, optimized_prompt,
|
| 665 |
+
optimization_status, shared_captions
|
| 666 |
)
|
| 667 |
|
| 668 |
# Add CSS styling
|