Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234562 unmodified lines910111213141525 unmodified lines4142434445464748495051525354555657585960614 unmodified lines6667686970717214 unmodified lines878889909192939436 unmodified lines1311321331341351361371381391401411421431441451467 unmodified lines15415515615715815916028 unmodified lines18919019119219319419519619719819920020120220320420525 unmodified lines2312322332342352362372382392402418 unmodified lines250251252253254255256257258259# -*- coding: utf-8 -*-"""Personalized Story Generator - Main ApplicationA free SaaS for generating personalized audio stories for Toniebox Creative-Tonies."""2 unmodified linesimport timefrom src.story_generator import StoryGeneratorfrom src.tts_engine import TTSEngine, VOICES, LANGUAGES, get_voices_for_language, get_default_voice_for_languagefrom src.rate_limiter import MAX_STORIES_PER_DAYfrom src.prompts import AGE_GROUPS, THEMES25 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 linesif 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 = "".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 linesstory_text = generator.generate_story(child_name=clean_name,age_group=age_group,theme=final_theme,language=language)# Step 2: Generate audio36 unmodified linesreturn 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 linesgr.Markdown("""# Personalized Story GeneratorCreate 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 storywith audio ready to upload to your Creative-Tonie."""28 unmodified linesmax_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 choicevoice = gr.Dropdown(choices=get_all_voice_choices(),label="Voice (select voice matching your language)",value="en_warm_female_us",)generate_btn = gr.Button(25 unmodified lines"""---### How to Use Your Story1. **Download** the MP3 file using the download button on the audio player2. **Open** [my.tonies.com](https://my.tonies.com) or the **mytonies app**3. **Upload** the MP3 to your Creative-Tonie4. **Place** the Creative-Tonie on your Toniebox to sync5. **Enjoy** your personalized story!---*Made with love for little dreamers everywhere*"""8 unmodified linesgenerate_btn.click(fn=generate_story_and_audio,inputs=[child_name, age_group, language, theme, custom_theme, voice],outputs=[story_output, audio_output, status_output],)if __name__ == "__main__":app.launch(share=True)12345672 unmodified lines1011121314151625 unmodified lines42434445464748495051525354555657585960616263646566676869704 unmodified lines7576777879808114 unmodified lines9697989910010110236 unmodified lines1391401411421431447 unmodified lines15215315415515615715815916028 unmodified lines18919019119219319419519619719825 unmodified lines2242252262272282292302312322332342352368 unmodified lines245246247248249250251252253254# -*- coding: utf-8 -*-"""Personalized Story Generator - Main ApplicationA free SaaS for generating personalized audio stories for Toniebox Creative-Tonies."""2 unmodified linesimport timefrom src.story_generator import StoryGeneratorfrom src.tts_engine import TTSEngine, VOICESfrom src.rate_limiter import MAX_STORIES_PER_DAYfrom src.prompts import AGE_GROUPS, THEMES25 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,theme: str,custom_theme: str,voice: str4 unmodified linesif not child_name or not child_name.strip():return "", None, "Please enter your child's name."# 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 linesstory_text = generator.generate_story(child_name=clean_name,age_group=age_group,theme=final_theme)# Step 2: Generate audio36 unmodified linesreturn gr.update(visible=(theme == "Custom"))# Build the Gradio interfacewith gr.Blocks(title="Personalized Story Generator",7 unmodified linesgr.Markdown("""# Personalized Story GeneratorCreate 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 storywith audio ready to upload to your Creative-Tonie."""28 unmodified linesmax_lines=2,)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 Story1. **Download** the MP3 file using the download button on the audio player2. **Open** [my.tonies.com](https://my.tonies.com) or the **mytonies app**3. **Upload** the MP3 to your Creative-Tonie4. **Place** the Creative-Tonie on your Toniebox to sync5. **Enjoy** your personalized story!---*Made with love for little dreamers everywhere*"""8 unmodified linesgenerate_btn.click(fn=generate_story_and_audio,inputs=[child_name, age_group, theme, custom_theme, voice],outputs=[story_output, audio_output, status_output],)if __name__ == "__main__":app.launch()
app.py+23-28