Spaces:
Runtime error
Runtime error
Update app.py
Browse files12345672 unmodified lines101112121314151625 unmodified lines4243444546474849505152535455565758596061626364656667586869704 unmodified lines757677697879808114 unmodified lines96979890919910010110236 unmodified lines1391401411341351361371381391401411421431421431447 unmodified lines15215315415515615715815916028 unmodified lines18919019119219319419519619719819920020120219219319419519619719825 unmodified lines2242252262272282292302312322332342352368 unmodified lines245246247253248249250251252253259254# -*- coding: utf-8 -*-"""Personalized Story Generator - Main Application
A free SaaS for generating personalized audio stories for Toniebox Creative-Tonies."""
2 unmodified linesimport time
from src.story_generator import StoryGeneratorfrom src.tts_engine import TTSEngine, VOICES, LANGUAGES, get_voices_for_language, get_default_voice_for_languagefrom src.tts_engine import TTSEngine, VOICESfrom src.rate_limiter import MAX_STORIES_PER_DAYfrom src.prompts import AGE_GROUPS, THEMES
25 unmodified linesdef generate_demo_story(child_name: str, theme: str) -> str: """Generate a demo story for UI testing when no API key is set.""" return f"""Once upon a time, in a land filled with wonder and magic, there lived a brave young adventurer named {child_name}.
{child_name} had always dreamed of going on a {theme.lower()} adventure. One sunny morning, {child_name} woke up to find a mysterious golden envelope on their pillow.
"Dear {child_name}," the letter read, "You have been chosen for a special quest!"
With excitement bubbling in their heart, {child_name} packed a small bag with snacks and their favorite toy. The adventure was about to begin!
Along the way, {child_name} met friendly creatures who helped guide the path. There was a wise owl who shared secrets of the forest, and a playful bunny who knew all the shortcuts.
"You're very brave, {child_name}!" said the owl. "Keep going, you're almost there!"
Finally, after climbing over mossy logs and crossing a sparkling stream, {child_name} reached the treasure - a beautiful chest filled with the most precious thing of all: memories of this wonderful adventure.
As the stars began to twinkle in the evening sky, {child_name} headed home, heart full of joy. Snuggling into bed, {child_name} closed their eyes and dreamed of tomorrow's adventures.
The End.
Sweet dreams, {child_name}."""
def generate_story_and_audio( child_name: str, age_group: str, language: str, theme: str, custom_theme: str, voice: str4 unmodified lines if not child_name or not child_name.strip(): return "", None, "Please enter your child's name."
# Clean name (letters, spaces, hyphens only, plus unicode for international names) # Clean name (letters, spaces, hyphens only) clean_name = "".join(c for c in child_name if c.isalpha() or c in " -'") if not clean_name: return "", None, "Please enter a valid name (letters only)."14 unmodified lines story_text = generator.generate_story( child_name=clean_name, age_group=age_group, theme=final_theme, language=language theme=final_theme )
# Step 2: Generate audio36 unmodified lines return gr.update(visible=(theme == "Custom"))
def get_all_voice_choices(): """Get all voices grouped by language for the dropdown.""" choices = [] for lang_code, lang_info in LANGUAGES.items(): voices = get_voices_for_language(lang_code) for voice_key, voice_info in voices.items(): choices.append((voice_info["label"], voice_key)) return choices
# Build the Gradio interfacewith gr.Blocks( title="Personalized Story Generator",7 unmodified lines gr.Markdown( """ # Personalized Story Generator
Create magical bedtime stories for your child's **Toniebox Creative-Tonie**!
Simply enter your child's name, pick a theme, and we'll generate a unique story with audio ready to upload to your Creative-Tonie. """28 unmodified lines max_lines=2, )
language = gr.Dropdown( choices=[(v["label"], k) for k, v in LANGUAGES.items()], label="Story Language", value="en", )
# All voices - user selects voice matching their language choice voice = gr.Dropdown( choices=get_all_voice_choices(), label="Voice (select voice matching your language)", value="en_warm_female_us", voice = gr.Radio( choices=[(v["label"], k) for k, v in VOICES.items()], label="Voice", value="warm_female_us", )
generate_btn = gr.Button(25 unmodified lines """ --- ### How to Use Your Story
1. **Download** the MP3 file using the download button on the audio player 2. **Open** [my.tonies.com](https://my.tonies.com) or the **mytonies app** 3. **Upload** the MP3 to your Creative-Tonie 4. **Place** the Creative-Tonie on your Toniebox to sync 5. **Enjoy** your personalized story!
--- *Made with love for little dreamers everywhere* """8 unmodified lines
generate_btn.click( fn=generate_story_and_audio, inputs=[child_name, age_group, language, theme, custom_theme, voice], inputs=[child_name, age_group, theme, custom_theme, voice], outputs=[story_output, audio_output, status_output], )
if __name__ == "__main__": app.launch(share=True) app.launch()app.py+23-28