Cuentos / App.py
celeni's picture
Create App.py
2af79a2 verified
Raw History Blame Contribute Delete
2.29 kB
import gradio as gr
import random
# Sample stories for all ages
stories = [
("The Friendly Dragon", "Once upon a time, in a friendly village, a dragon and children became best friends."),
("A Day at the Beach", "The sun was shining brightly as the family enjoyed a fun day at the sandy beach."),
("The Lost Puppy", "A small puppy got lost but found its way home thanks to the kindness of strangers."),
("The Magical Forest", "In the heart of the magical forest, creatures of all kinds lived in harmony."),
("The Little Star", "A little star in the night sky wished to shine brighter than all the others."),
("The Brave Knight", "A brave knight set out on a quest to save his kingdom from a terrible fate."),
("The Rainbow Adventure", "Two kids followed a rainbow and discovered a pot of gold and new friends."),
]
def greet_and_tell_story(name, story_button):
if not name.strip():
greeting = "Hello! Please enter your name."
story_text = ""
else:
greeting = f"Hello, {name}! Here's a story for you:"
if story_button:
story_title, story_body = random.choice(stories)
story_text = f"### {story_title}\n\n{story_body}"
else:
story_text = ""
return greeting, story_text
with gr.Blocks() as demo:
gr.Markdown("# Welcome to the Storytelling App!")
name_input = gr.Textbox(label="Enter your name")
greet_output = gr.Textbox(label="Greeting", interactive=False)
story_output = gr.Markdown()
story_button = gr.Button("Tell me a story!")
def on_name_change(name):
greeting = f"Hello, {name}!" if name.strip() else "Hello! Please enter your name."
return greeting
name_input.change(fn=on_name_change, inputs=name_input, outputs=greet_output)
def on_story_button_click(name):
if not name.strip():
return "Hello! Please enter your name.", ""
greeting = f"Hello, {name}! Here's a story for you:"
story_title, story_body = random.choice(stories)
story_text = f"### {story_title}\n\n{story_body}"
return greeting, story_text
story_button.click(fn=on_story_button_click, inputs=name_input, outputs=[greet_output, story_output])
if __name__ == "__main__":
demo.launch()