File size: 3,156 Bytes
7da30cc
a546c80
128c600
a546c80
 
7da30cc
 
128c600
 
 
82e33f9
 
 
 
7da30cc
 
 
82e33f9
128c600
7da30cc
128c600
7da30cc
 
 
128c600
82e33f9
128c600
 
7da30cc
128c600
 
 
 
 
82e33f9
 
128c600
82e33f9
 
 
 
 
 
128c600
 
 
 
 
7da30cc
 
 
128c600
 
82e33f9
7da30cc
82e33f9
128c600
7da30cc
 
 
 
 
 
 
82e33f9
 
7da30cc
 
 
 
 
 
 
 
a546c80
 
82e33f9
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Import libraries
import gradio as gr
from transformers import GPT2LMHeadModel, GPT2Tokenizer
from gtts import gTTS
from io import BytesIO
from PIL import Image

# Load GPT-2 model and tokenizer
model = GPT2LMHeadModel.from_pretrained("gpt2")
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")

# 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!"}]

# Initialize page number
current_page = 0

# Define the Storyteller function
def StorytellerGPT(character, child_name, lesson_choice, tell_story, _):
    global current_page

    # 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
    input_text = character_info + lesson_info + tell_story
    input_ids = tokenizer.encode(input_text, return_tensors="pt")
    story_reply = model.generate(input_ids, max_length=150, num_return_sequences=1, no_repeat_ngram_size=2, top_k=50, top_p=0.95)[0]

    # Decode the generated sequence
    story_reply = tokenizer.decode(story_reply, skip_special_tokens=True)

    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)

    # Display the story on separate pages
    story_pages = story_reply.split("\n\n")  # Split the story into pages
    current_page = min(current_page, len(story_pages) - 1)  # Ensure the current_page is within bounds

    return story_pages[current_page], Audio(data=audio_io.read(), autoplay=True), image

# Create the Gradio Interface with styling
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"),
        gr.Button("Next Page"),
    ],
    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.",
    live=True,  # Enable live updates for CSS changes
    css=f"""body {{
        background-image: url('https://www.bing.com/images/create/a-castle-ai-metaverse-style/1-657576205c7146f2b7f2f8d1c552810f?id=dZs6kpD2HfmH4eojx%2bHjdA%3d%3d&view=detailv2&idpp=genimg&FORM=GCRIDP&mode=overlay');
        background-size: cover;
        background-position: center;
        font-family: 'Comic Sans MS', cursive, sans-serif; /* Optional: Change the font */
    }}""",
)

# Launch the Gradio Interface
demo.launch()