wonderlore / app.py
GH111's picture
Update app.py
0c5599e
raw
history blame
2.1 kB
# Import libraries
import gradio as gr
from transformers import pipeline
from gtts import gTTS
from io import BytesIO
from PIL import Image
# Create a text generation pipeline with GPT-2
story_generator = pipeline("text-generation", model="EleutherAI/gpt-neo-1.3B")
# Set the context for the storyteller
messages = [{"role": "system", "content": "You are a magical storyteller, creating wonderful tales for kids. Make them imaginative and full of joy!"}]
# Define the Storyteller function
def StorytellerGPT(character, child_name, lesson_choice, tell_story):
# Set the characters and lesson based on user choices
character_info = f"Once upon a time, {child_name} met {character}. "
lesson_info = f"Today's lesson is about {lesson_choice}. "
messages.append({"role": "user", "content": tell_story})
# Generate story using Hugging Face's GPT-2
story_reply = story_generator(character_info + lesson_info + tell_story, max_length=150, num_return_sequences=1)[0]['generated_text']
messages.append({"role": "assistant", "content": story_reply})
# Convert text to speech
tts = gTTS(text=story_reply, lang='en', slow=False)
audio_io = BytesIO()
tts.save(audio_io)
audio_io.seek(0)
# Convert text to image
image = Image.new("RGB", (300, 300), (255, 255, 255))
image_path = "/path/to/output/image.png"
image.save(image_path)
return story_reply, Audio(data=audio_io.read(), autoplay=True), image
# Create the Gradio Interface
demo = gr.Interface(
fn=StorytellerGPT,
inputs=[
gr.Textbox("text", label="Child's Name"),
gr.Dropdown(["unicorn", "dragon", "wizard"], label="Choose a Character"),
gr.Dropdown(["kindness", "creativity", "bravery"], label="Choose a Lesson"),
gr.Textbox("text", label="Start the Story with"),
],
outputs=["text", "audio", "image"],
title="πŸ“– Storytelling Magic",
description="A magical storyteller app for kids! Choose characters, add your name, and select the lesson you want to learn.",
)
# Launch the Gradio Interface
demo.launch(