Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -497,15 +497,159 @@ async def root():
|
|
| 497 |
"oci_bucket_api": OCI_API_BASE_URL
|
| 498 |
}
|
| 499 |
|
| 500 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 501 |
def get_app():
|
| 502 |
return app
|
| 503 |
|
| 504 |
if __name__ == "__main__":
|
| 505 |
import uvicorn
|
| 506 |
-
print("π Starting Storybook Generator API...")
|
| 507 |
print("π Features: Multi-image generation + Text saving + OCI bucket storage")
|
| 508 |
print("π OCI API:", OCI_API_BASE_URL)
|
| 509 |
-
print("π
|
|
|
|
| 510 |
|
| 511 |
-
|
|
|
|
|
|
| 497 |
"oci_bucket_api": OCI_API_BASE_URL
|
| 498 |
}
|
| 499 |
|
| 500 |
+
# GRADIO INTERFACE FOR TESTING
|
| 501 |
+
def create_gradio_interface():
|
| 502 |
+
"""Create Gradio interface for testing image generation"""
|
| 503 |
+
|
| 504 |
+
def generate_test_image(prompt, model_choice, style_choice):
|
| 505 |
+
"""Generate a single image for testing"""
|
| 506 |
+
try:
|
| 507 |
+
if not prompt.strip():
|
| 508 |
+
return None, "β Please enter a prompt"
|
| 509 |
+
|
| 510 |
+
print(f"π¨ Generating test image with prompt: {prompt}")
|
| 511 |
+
|
| 512 |
+
# Enhance the prompt
|
| 513 |
+
enhanced_prompt, negative_prompt = enhance_prompt(prompt, style_choice)
|
| 514 |
+
|
| 515 |
+
# Generate the image
|
| 516 |
+
image = generate_high_quality_image(
|
| 517 |
+
enhanced_prompt,
|
| 518 |
+
model_choice,
|
| 519 |
+
style_choice,
|
| 520 |
+
negative_prompt
|
| 521 |
+
)
|
| 522 |
+
|
| 523 |
+
status_msg = f"""β
Success! Generated: {prompt}
|
| 524 |
+
|
| 525 |
+
π¨ Enhanced prompt: {enhanced_prompt}
|
| 526 |
+
|
| 527 |
+
π‘ This is a test image. For complete storybook generation, use the API endpoint:
|
| 528 |
+
POST /api/generate-storybook"""
|
| 529 |
+
|
| 530 |
+
return image, status_msg
|
| 531 |
+
|
| 532 |
+
except Exception as e:
|
| 533 |
+
error_msg = f"β Generation failed: {str(e)}"
|
| 534 |
+
print(error_msg)
|
| 535 |
+
return None, error_msg
|
| 536 |
+
|
| 537 |
+
with gr.Blocks(title="Storybook Generator - Test Interface", theme="soft") as demo:
|
| 538 |
+
gr.Markdown("# π¨ Storybook Generator - Test Interface")
|
| 539 |
+
gr.Markdown("**Test image generation β’ Full storybooks via API**")
|
| 540 |
+
|
| 541 |
+
with gr.Row():
|
| 542 |
+
with gr.Column(scale=1):
|
| 543 |
+
gr.Markdown("### βοΈ Test Settings")
|
| 544 |
+
|
| 545 |
+
model_dropdown = gr.Dropdown(
|
| 546 |
+
label="AI Model",
|
| 547 |
+
choices=list(MODEL_CHOICES.keys()),
|
| 548 |
+
value="dreamshaper-8"
|
| 549 |
+
)
|
| 550 |
+
|
| 551 |
+
style_dropdown = gr.Dropdown(
|
| 552 |
+
label="Art Style",
|
| 553 |
+
choices=["childrens_book", "realistic", "fantasy", "anime"],
|
| 554 |
+
value="childrens_book"
|
| 555 |
+
)
|
| 556 |
+
|
| 557 |
+
prompt_input = gr.Textbox(
|
| 558 |
+
label="Test Prompt",
|
| 559 |
+
placeholder="Enter a prompt to test image generation...",
|
| 560 |
+
lines=3
|
| 561 |
+
)
|
| 562 |
+
|
| 563 |
+
generate_btn = gr.Button("β¨ Generate Test Image", variant="primary")
|
| 564 |
+
|
| 565 |
+
gr.Markdown("### π API Usage")
|
| 566 |
+
gr.Markdown("""
|
| 567 |
+
**For complete storybooks with multiple images:**
|
| 568 |
+
- Use: `POST /api/generate-storybook`
|
| 569 |
+
- Send: story_title, scenes[], characters[]
|
| 570 |
+
- Returns: job_id for status tracking
|
| 571 |
+
""")
|
| 572 |
+
|
| 573 |
+
with gr.Column(scale=2):
|
| 574 |
+
image_output = gr.Image(
|
| 575 |
+
label="Generated Image",
|
| 576 |
+
height=400,
|
| 577 |
+
show_download_button=True
|
| 578 |
+
)
|
| 579 |
+
status_output = gr.Textbox(
|
| 580 |
+
label="Status",
|
| 581 |
+
lines=4,
|
| 582 |
+
show_copy_button=True
|
| 583 |
+
)
|
| 584 |
+
|
| 585 |
+
# Quick examples
|
| 586 |
+
with gr.Row():
|
| 587 |
+
gr.Button("π Dragon Example").click(
|
| 588 |
+
lambda: "A friendly dragon reading a giant book under a magical tree",
|
| 589 |
+
outputs=prompt_input
|
| 590 |
+
)
|
| 591 |
+
gr.Button("π± Kitten Example").click(
|
| 592 |
+
lambda: "Cute kitten playing with yarn ball in a sunny living room",
|
| 593 |
+
outputs=prompt_input
|
| 594 |
+
)
|
| 595 |
+
gr.Button("π Space Example").click(
|
| 596 |
+
lambda: "Space astronaut exploring a colorful alien planet",
|
| 597 |
+
outputs=prompt_input
|
| 598 |
+
)
|
| 599 |
+
|
| 600 |
+
# API info section
|
| 601 |
+
with gr.Accordion("π§ API Documentation", open=False):
|
| 602 |
+
gr.Markdown("""
|
| 603 |
+
### API Endpoints
|
| 604 |
+
|
| 605 |
+
**Generate Storybook**
|
| 606 |
+
```bash
|
| 607 |
+
POST /api/generate-storybook
|
| 608 |
+
{
|
| 609 |
+
"story_title": "Your Story",
|
| 610 |
+
"scenes": [
|
| 611 |
+
{
|
| 612 |
+
"visual": "Scene description",
|
| 613 |
+
"text": "Story text"
|
| 614 |
+
}
|
| 615 |
+
],
|
| 616 |
+
"callback_url": "your-webhook-url"
|
| 617 |
+
}
|
| 618 |
+
```
|
| 619 |
+
|
| 620 |
+
**Check Status**
|
| 621 |
+
```bash
|
| 622 |
+
GET /api/job-status/{job_id}
|
| 623 |
+
```
|
| 624 |
+
|
| 625 |
+
**Health Check**
|
| 626 |
+
```bash
|
| 627 |
+
GET /api/health
|
| 628 |
+
```
|
| 629 |
+
""")
|
| 630 |
+
|
| 631 |
+
generate_btn.click(
|
| 632 |
+
fn=generate_test_image,
|
| 633 |
+
inputs=[prompt_input, model_dropdown, style_dropdown],
|
| 634 |
+
outputs=[image_output, status_output]
|
| 635 |
+
)
|
| 636 |
+
|
| 637 |
+
return demo
|
| 638 |
+
|
| 639 |
+
# Create Gradio app
|
| 640 |
+
gradio_app = create_gradio_interface()
|
| 641 |
+
|
| 642 |
+
# For Hugging Face Spaces deployment
|
| 643 |
def get_app():
|
| 644 |
return app
|
| 645 |
|
| 646 |
if __name__ == "__main__":
|
| 647 |
import uvicorn
|
| 648 |
+
print("π Starting Storybook Generator API + Gradio Interface...")
|
| 649 |
print("π Features: Multi-image generation + Text saving + OCI bucket storage")
|
| 650 |
print("π OCI API:", OCI_API_BASE_URL)
|
| 651 |
+
print("π Web Interface: http://localhost:7860")
|
| 652 |
+
print("π API Endpoints: http://localhost:7860/api/")
|
| 653 |
|
| 654 |
+
# Launch both FastAPI and Gradio
|
| 655 |
+
gradio_app.launch(server_name="0.0.0.0", server_port=7860)
|