Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -587,9 +587,112 @@ async def delete_local_image_api(filename: str):
|
|
| 587 |
except Exception as e:
|
| 588 |
return {"status": "error", "message": str(e)}
|
| 589 |
|
| 590 |
-
#
|
| 591 |
-
|
| 592 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 593 |
|
| 594 |
def generate_test_image(prompt, model_choice, style_choice):
|
| 595 |
"""Generate a single image for testing and save locally"""
|
|
@@ -640,7 +743,6 @@ def create_gradio_interface():
|
|
| 640 |
|
| 641 |
success, message = delete_local_image(filepath)
|
| 642 |
updated_files = refresh_local_images()
|
| 643 |
-
storage_info = get_local_storage_info()
|
| 644 |
|
| 645 |
if success:
|
| 646 |
status_msg = f"✅ {message}"
|
|
@@ -665,151 +767,62 @@ def create_gradio_interface():
|
|
| 665 |
except Exception as e:
|
| 666 |
return f"❌ Error: {str(e)}", refresh_local_images()
|
| 667 |
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
model_dropdown = gr.Dropdown(
|
| 690 |
-
label="AI Model",
|
| 691 |
-
choices=list(MODEL_CHOICES.keys()),
|
| 692 |
-
value="dreamshaper-8"
|
| 693 |
-
)
|
| 694 |
-
|
| 695 |
-
style_dropdown = gr.Dropdown(
|
| 696 |
-
label="Art Style",
|
| 697 |
-
choices=["childrens_book", "realistic", "fantasy", "anime"],
|
| 698 |
-
value="childrens_book"
|
| 699 |
-
)
|
| 700 |
-
|
| 701 |
-
prompt_input = gr.Textbox(
|
| 702 |
-
label="Test Prompt",
|
| 703 |
-
placeholder="Enter a prompt to test image generation...",
|
| 704 |
-
lines=3
|
| 705 |
-
)
|
| 706 |
-
|
| 707 |
-
generate_btn = gr.Button("✨ Generate Test Image", variant="primary")
|
| 708 |
-
|
| 709 |
-
# Current image management
|
| 710 |
-
current_file_path = gr.State()
|
| 711 |
-
delete_btn = gr.Button("🗑️ Delete This Image", variant="stop")
|
| 712 |
-
delete_status = gr.Textbox(label="Delete Status", interactive=False, lines=2)
|
| 713 |
-
|
| 714 |
-
gr.Markdown("### 📚 API Usage for n8n")
|
| 715 |
-
gr.Markdown("""
|
| 716 |
-
**For complete storybooks (OCI bucket):**
|
| 717 |
-
- Endpoint: `POST /api/generate-storybook`
|
| 718 |
-
- Input: `story_title`, `scenes[]`, `characters[]`
|
| 719 |
-
- Output: Saves to OCI bucket automatically
|
| 720 |
-
""")
|
| 721 |
-
|
| 722 |
-
with gr.Column(scale=2):
|
| 723 |
-
image_output = gr.Image(
|
| 724 |
-
label="Generated Image",
|
| 725 |
-
height=400,
|
| 726 |
-
show_download_button=True
|
| 727 |
-
)
|
| 728 |
-
status_output = gr.Textbox(
|
| 729 |
-
label="Status",
|
| 730 |
-
lines=4,
|
| 731 |
-
show_copy_button=True
|
| 732 |
-
)
|
| 733 |
-
|
| 734 |
-
# Quick examples
|
| 735 |
-
with gr.Row():
|
| 736 |
-
gr.Button("🐉 Dragon Example").click(
|
| 737 |
-
lambda: "A friendly dragon reading a giant book under a magical tree",
|
| 738 |
-
outputs=prompt_input
|
| 739 |
-
)
|
| 740 |
-
gr.Button("🐱 Kitten Example").click(
|
| 741 |
-
lambda: "Cute kitten playing with yarn ball in a sunny living room",
|
| 742 |
-
outputs=prompt_input
|
| 743 |
-
)
|
| 744 |
-
gr.Button("🚀 Space Example").click(
|
| 745 |
-
lambda: "Space astronaut exploring a colorful alien planet",
|
| 746 |
-
outputs=prompt_input
|
| 747 |
-
)
|
| 748 |
-
|
| 749 |
-
# Local file management section
|
| 750 |
-
with gr.Accordion("📁 Manage Local Test Images", open=True):
|
| 751 |
-
gr.Markdown("### Locally Saved Images")
|
| 752 |
-
|
| 753 |
-
with gr.Row():
|
| 754 |
-
refresh_btn = gr.Button("🔄 Refresh List")
|
| 755 |
-
clear_all_btn = gr.Button("🗑️ Clear All Images", variant="stop")
|
| 756 |
-
|
| 757 |
-
file_gallery = gr.Gallery(
|
| 758 |
-
label="Local Images",
|
| 759 |
-
show_label=True,
|
| 760 |
-
elem_id="gallery",
|
| 761 |
-
columns=4,
|
| 762 |
-
height="auto"
|
| 763 |
-
)
|
| 764 |
-
|
| 765 |
-
clear_status = gr.Textbox(label="Clear Status", interactive=False)
|
| 766 |
-
|
| 767 |
-
# Connect events
|
| 768 |
-
generate_btn.click(
|
| 769 |
-
fn=generate_test_image,
|
| 770 |
-
inputs=[prompt_input, model_dropdown, style_dropdown],
|
| 771 |
-
outputs=[image_output, status_output, current_file_path]
|
| 772 |
-
).then(
|
| 773 |
-
fn=refresh_local_images,
|
| 774 |
-
outputs=file_gallery
|
| 775 |
-
).then(
|
| 776 |
-
fn=update_storage_info,
|
| 777 |
-
outputs=storage_info
|
| 778 |
-
)
|
| 779 |
-
|
| 780 |
-
delete_btn.click(
|
| 781 |
-
fn=delete_current_image,
|
| 782 |
-
inputs=current_file_path,
|
| 783 |
-
outputs=[delete_status, image_output, status_output, file_gallery]
|
| 784 |
-
).then(
|
| 785 |
-
fn=update_storage_info,
|
| 786 |
-
outputs=storage_info
|
| 787 |
-
)
|
| 788 |
-
|
| 789 |
-
refresh_btn.click(
|
| 790 |
-
fn=refresh_local_images,
|
| 791 |
-
outputs=file_gallery
|
| 792 |
-
).then(
|
| 793 |
-
fn=update_storage_info,
|
| 794 |
-
outputs=storage_info
|
| 795 |
-
)
|
| 796 |
-
|
| 797 |
-
clear_all_btn.click(
|
| 798 |
-
fn=clear_all_images,
|
| 799 |
-
outputs=[clear_status, file_gallery]
|
| 800 |
-
).then(
|
| 801 |
-
fn=update_storage_info,
|
| 802 |
-
outputs=storage_info
|
| 803 |
-
)
|
| 804 |
-
|
| 805 |
-
# Initialize on load
|
| 806 |
-
demo.load(fn=refresh_local_images, outputs=file_gallery)
|
| 807 |
-
demo.load(fn=update_storage_info, outputs=storage_info)
|
| 808 |
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 813 |
|
| 814 |
# Enhanced root endpoint that explains the API structure
|
| 815 |
@app.get("/")
|
|
@@ -853,7 +866,7 @@ if __name__ == "__main__":
|
|
| 853 |
print("🔌 Both API and UI running on same port")
|
| 854 |
|
| 855 |
# FIX: Properly mount Gradio without reassigning app
|
| 856 |
-
gr.mount_gradio_app(app,
|
| 857 |
|
| 858 |
# Run the combined app
|
| 859 |
uvicorn.run(
|
|
@@ -880,7 +893,7 @@ if __name__ == "__main__":
|
|
| 880 |
|
| 881 |
def run_gradio():
|
| 882 |
"""Run Gradio on port 7860 for web interface"""
|
| 883 |
-
|
| 884 |
server_name="0.0.0.0",
|
| 885 |
server_port=7860,
|
| 886 |
share=False,
|
|
|
|
| 587 |
except Exception as e:
|
| 588 |
return {"status": "error", "message": str(e)}
|
| 589 |
|
| 590 |
+
# Create the Gradio interface with QUALITY OPTIONS and LOCAL FILE MANAGEMENT
|
| 591 |
+
with gr.Blocks(title="Premium Children's Book Illustrator", theme="soft") as demo:
|
| 592 |
+
gr.Markdown("# 🎨 Premium Children's Book Illustrator")
|
| 593 |
+
gr.Markdown("Generate **studio-quality** storybook images with professional results")
|
| 594 |
+
|
| 595 |
+
# Storage info display
|
| 596 |
+
storage_info = gr.Textbox(
|
| 597 |
+
label="📊 Local Storage Information",
|
| 598 |
+
interactive=False,
|
| 599 |
+
lines=2
|
| 600 |
+
)
|
| 601 |
+
|
| 602 |
+
def update_storage_info():
|
| 603 |
+
info = get_local_storage_info()
|
| 604 |
+
if "error" not in info:
|
| 605 |
+
return f"📁 Local Storage: {info['total_files']} images, {info['total_size_mb']} MB used"
|
| 606 |
+
return "📁 Local Storage: Unable to calculate"
|
| 607 |
+
|
| 608 |
+
with gr.Row():
|
| 609 |
+
with gr.Column(scale=1):
|
| 610 |
+
gr.Markdown("### 🎯 Quality Settings")
|
| 611 |
+
|
| 612 |
+
model_choice = gr.Dropdown(
|
| 613 |
+
label="AI Model",
|
| 614 |
+
choices=list(MODEL_CHOICES.keys()),
|
| 615 |
+
value="dreamshaper-8",
|
| 616 |
+
info="Choose the best model for your style"
|
| 617 |
+
)
|
| 618 |
+
|
| 619 |
+
style_choice = gr.Dropdown(
|
| 620 |
+
label="Art Style",
|
| 621 |
+
choices=["childrens_book", "realistic", "fantasy", "anime"],
|
| 622 |
+
value="childrens_book",
|
| 623 |
+
info="Select the artistic style"
|
| 624 |
+
)
|
| 625 |
+
|
| 626 |
+
prompt_input = gr.Textbox(
|
| 627 |
+
label="Scene Description",
|
| 628 |
+
placeholder="Describe your scene in detail...\nExample: A friendly dragon reading a giant book under a magical tree with glowing fairies",
|
| 629 |
+
lines=3
|
| 630 |
+
)
|
| 631 |
+
|
| 632 |
+
generate_btn = gr.Button("✨ Generate Premium Image", variant="primary")
|
| 633 |
+
|
| 634 |
+
# Current image management
|
| 635 |
+
current_file_path = gr.State()
|
| 636 |
+
delete_btn = gr.Button("🗑️ Delete This Image", variant="stop")
|
| 637 |
+
delete_status = gr.Textbox(label="Delete Status", interactive=False, lines=2)
|
| 638 |
+
|
| 639 |
+
gr.Markdown("### 📚 API Usage for n8n")
|
| 640 |
+
gr.Markdown("""
|
| 641 |
+
**For complete storybooks (OCI bucket):**
|
| 642 |
+
- Endpoint: `POST /api/generate-storybook`
|
| 643 |
+
- Input: `story_title`, `scenes[]`, `characters[]`
|
| 644 |
+
- Output: Saves to OCI bucket automatically
|
| 645 |
+
""")
|
| 646 |
+
|
| 647 |
+
with gr.Column(scale=2):
|
| 648 |
+
image_output = gr.Image(label="Generated Image", height=500, show_download_button=True)
|
| 649 |
+
status_output = gr.Textbox(label="Status", interactive=False, lines=4)
|
| 650 |
+
|
| 651 |
+
# Examples section
|
| 652 |
+
with gr.Accordion("💡 Prompt Examples & Tips", open=False):
|
| 653 |
+
gr.Markdown("""
|
| 654 |
+
## 🎨 Professional Prompt Examples:
|
| 655 |
+
|
| 656 |
+
**Best Results:**
|
| 657 |
+
- "A cute baby dragon learning to read with an old wise turtle in a magical forest"
|
| 658 |
+
- "Group of animal friends having a picnic under a giant glowing mushroom"
|
| 659 |
+
- "Little girl exploring a castle made of candy with talking animals"
|
| 660 |
+
- "Space adventure with friendly robots and colorful aliens"
|
| 661 |
+
|
| 662 |
+
## 🚫 Avoid:
|
| 663 |
+
- "dragon" → **"friendly cartoon dragon reading book"**
|
| 664 |
+
- "cat" → **"cute kitten playing with yarn ball"**
|
| 665 |
+
- "tree" → **"magical tree with glowing fairies"**
|
| 666 |
+
|
| 667 |
+
## ⚡ Pro Tips:
|
| 668 |
+
1. **Be descriptive** - Add colors, emotions, settings
|
| 669 |
+
2. **Specify style** - "watercolor", "cartoon", "realistic"
|
| 670 |
+
3. **Add details** - "with glowing effects", "in a magical forest"
|
| 671 |
+
4. **Use positive language** - "happy", "friendly", "colorful"
|
| 672 |
+
""")
|
| 673 |
+
|
| 674 |
+
# Local file management section
|
| 675 |
+
with gr.Accordion("📁 Manage Local Test Images", open=True):
|
| 676 |
+
gr.Markdown("### Locally Saved Images")
|
| 677 |
+
|
| 678 |
+
with gr.Row():
|
| 679 |
+
refresh_btn = gr.Button("🔄 Refresh List")
|
| 680 |
+
clear_all_btn = gr.Button("🗑️ Clear All Images", variant="stop")
|
| 681 |
+
|
| 682 |
+
file_gallery = gr.Gallery(
|
| 683 |
+
label="Local Images",
|
| 684 |
+
show_label=True,
|
| 685 |
+
elem_id="gallery",
|
| 686 |
+
columns=4,
|
| 687 |
+
height="auto"
|
| 688 |
+
)
|
| 689 |
+
|
| 690 |
+
clear_status = gr.Textbox(label="Clear Status", interactive=False)
|
| 691 |
+
|
| 692 |
+
# Debug section
|
| 693 |
+
with gr.Accordion("🔧 Advanced Settings", open=False):
|
| 694 |
+
debug_btn = gr.Button("🔄 Check System Status", variant="secondary")
|
| 695 |
+
debug_output = gr.Textbox(label="System Info", interactive=False, lines=4)
|
| 696 |
|
| 697 |
def generate_test_image(prompt, model_choice, style_choice):
|
| 698 |
"""Generate a single image for testing and save locally"""
|
|
|
|
| 743 |
|
| 744 |
success, message = delete_local_image(filepath)
|
| 745 |
updated_files = refresh_local_images()
|
|
|
|
| 746 |
|
| 747 |
if success:
|
| 748 |
status_msg = f"✅ {message}"
|
|
|
|
| 767 |
except Exception as e:
|
| 768 |
return f"❌ Error: {str(e)}", refresh_local_images()
|
| 769 |
|
| 770 |
+
def check_system_status():
|
| 771 |
+
"""Check system status"""
|
| 772 |
+
return f"""**System Status:**
|
| 773 |
+
- Model: {current_model_name}
|
| 774 |
+
- OCI API: {OCI_API_BASE_URL}
|
| 775 |
+
- Local Storage: {get_local_storage_info().get('total_files', 0)} images
|
| 776 |
+
- Active Jobs: {len(job_storage)}
|
| 777 |
+
- Ready for premium image generation!"""
|
| 778 |
+
|
| 779 |
+
# Connect buttons to functions
|
| 780 |
+
generate_btn.click(
|
| 781 |
+
fn=generate_test_image,
|
| 782 |
+
inputs=[prompt_input, model_choice, style_choice],
|
| 783 |
+
outputs=[image_output, status_output, current_file_path]
|
| 784 |
+
).then(
|
| 785 |
+
fn=refresh_local_images,
|
| 786 |
+
outputs=file_gallery
|
| 787 |
+
).then(
|
| 788 |
+
fn=update_storage_info,
|
| 789 |
+
outputs=storage_info
|
| 790 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 791 |
|
| 792 |
+
delete_btn.click(
|
| 793 |
+
fn=delete_current_image,
|
| 794 |
+
inputs=current_file_path,
|
| 795 |
+
outputs=[delete_status, image_output, status_output, file_gallery]
|
| 796 |
+
).then(
|
| 797 |
+
fn=update_storage_info,
|
| 798 |
+
outputs=storage_info
|
| 799 |
+
)
|
| 800 |
+
|
| 801 |
+
refresh_btn.click(
|
| 802 |
+
fn=refresh_local_images,
|
| 803 |
+
outputs=file_gallery
|
| 804 |
+
).then(
|
| 805 |
+
fn=update_storage_info,
|
| 806 |
+
outputs=storage_info
|
| 807 |
+
)
|
| 808 |
+
|
| 809 |
+
clear_all_btn.click(
|
| 810 |
+
fn=clear_all_images,
|
| 811 |
+
outputs=[clear_status, file_gallery]
|
| 812 |
+
).then(
|
| 813 |
+
fn=update_storage_info,
|
| 814 |
+
outputs=storage_info
|
| 815 |
+
)
|
| 816 |
+
|
| 817 |
+
debug_btn.click(
|
| 818 |
+
fn=check_system_status,
|
| 819 |
+
inputs=None,
|
| 820 |
+
outputs=debug_output
|
| 821 |
+
)
|
| 822 |
+
|
| 823 |
+
# Initialize on load
|
| 824 |
+
demo.load(fn=refresh_local_images, outputs=file_gallery)
|
| 825 |
+
demo.load(fn=update_storage_info, outputs=storage_info)
|
| 826 |
|
| 827 |
# Enhanced root endpoint that explains the API structure
|
| 828 |
@app.get("/")
|
|
|
|
| 866 |
print("🔌 Both API and UI running on same port")
|
| 867 |
|
| 868 |
# FIX: Properly mount Gradio without reassigning app
|
| 869 |
+
gr.mount_gradio_app(app, demo, path="/ui")
|
| 870 |
|
| 871 |
# Run the combined app
|
| 872 |
uvicorn.run(
|
|
|
|
| 893 |
|
| 894 |
def run_gradio():
|
| 895 |
"""Run Gradio on port 7860 for web interface"""
|
| 896 |
+
demo.launch(
|
| 897 |
server_name="0.0.0.0",
|
| 898 |
server_port=7860,
|
| 899 |
share=False,
|