magical-tales / ui /events.py
agharsallah
adding Progress bar on chapter creation
c323310
"""
Event handlers for the Magic Story Creator
"""
import gradio as gr
from controllers.app_controller import (
process_story_generation,
clear_fields,
handle_chapter_processing,
)
def setup_event_handlers(
generate_button,
clear_button,
process_chapters_button,
story_type,
tone,
kid_age,
kid_language,
kid_interests,
subject,
reading_time,
pdf_upload,
model_selector,
story_title,
story_text,
chapters_state,
chapter_loading_container=None,
):
"""Set up all the event handlers for the application"""
# Story generation event handler
generate_button.click(
process_story_generation,
inputs=[
story_type,
tone,
kid_interests,
subject,
kid_age,
kid_language,
reading_time,
pdf_upload,
model_selector,
],
outputs=[story_title, story_text, process_chapters_button],
)
# Clear fields event handler
clear_button.click(
clear_fields,
inputs=[],
outputs=[subject, story_title, story_text, process_chapters_button],
)
# Function to show loading container and hide placeholder when processing starts
def start_chapter_processing(story_text, story_title):
# Return initial data structure with processing flag
return {"title": story_title, "processing": True, "chapters": []}
# If we have a loading container reference, show it when processing starts
if chapter_loading_container:
process_chapters_button.click(
lambda: gr.update(visible=True),
inputs=[],
outputs=[chapter_loading_container],
)
# Show the loading indicator before processing starts and hide placeholder
process_chapters_button.click(
start_chapter_processing,
inputs=[story_text, story_title],
outputs=[chapters_state],
js="function() { document.querySelectorAll('.empty-placeholder').forEach(el => el.style.display = 'none');window.scrollTo({ top: 0, behavior: 'smooth' }); return []; }",
)
# Chapter processing event handler - happens after loading container is shown
process_chapters_button.click(
handle_chapter_processing,
inputs=[story_text, story_title],
outputs=[chapters_state],
show_progress=True, # Show progress bar
)
return generate_button, clear_button, process_chapters_button