Spaces:
Sleeping
Sleeping
import gradio as gr | |
from transformers import pipeline | |
from gtts import gTTS | |
from io import BytesIO | |
from PIL import Image, ImageDraw, ImageFont | |
from IPython.display import Audio | |
# 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(tell_story): | |
messages.append({"role": "user", "content": tell_story}) | |
# Generate story using Hugging Face's GPT-2 | |
story_reply = story_generator(tell_story, max_length=100, 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 = generate_dynamic_image(story_reply) | |
return story_reply, Audio(data=audio_io.read(), autoplay=True), image | |
# Function to generate a dynamic image based on the story text | |
def generate_dynamic_image(story_text): | |
# Create a blank image | |
image = Image.new("RGB", (500, 300), (255, 255, 255)) | |
draw = ImageDraw.Draw(image) | |
# Use a truetype font file, replace "arial.ttf" with the path to your font file | |
font = ImageFont.truetype("arial.ttf", 20) | |
# Write the story text on the image | |
lines = [story_text[i:i+40] for i in range(0, len(story_text), 40)] | |
y_position = 10 | |
for line in lines: | |
draw.text((10, y_position), line, font=font, fill=(0, 0, 0)) | |
y_position += 30 | |
return image | |
# Create the Gradio Interface | |
demo = gr.Interface( | |
fn=StorytellerGPT, | |
inputs="text", | |
outputs=["text", "audio", "image"], | |
title="π Storytelling Magic", | |
description="A magical storyteller app for kids! Type a sentence, and let the app create an enchanting story for you." | |
) | |
# Launch the Gradio Interface | |
demo.launch() | |