Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,47 +1,64 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
from gtts import gTTS
|
4 |
from io import BytesIO
|
5 |
-
from PIL import Image
|
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 |
-
# Launch the
|
47 |
-
|
|
|
|
1 |
+
|
2 |
import gradio as gr
|
3 |
from transformers import pipeline
|
4 |
from gtts import gTTS
|
5 |
from io import BytesIO
|
6 |
+
from PIL import Image, ImageDraw, ImageFont
|
7 |
+
from IPython.display import Audio
|
8 |
+
|
9 |
+
# Create a text generation pipeline with GPT-2
|
10 |
+
story_generator = pipeline("text-generation", model="EleutherAI/gpt-neo-1.3B")
|
11 |
+
|
12 |
+
# Set the context for the storyteller
|
13 |
+
messages = [{"role": "system", "content": "You are a magical storyteller, creating wonderful tales for kids. Make them imaginative and full of joy!"}]
|
14 |
+
|
15 |
+
# Define the Storyteller function
|
16 |
+
def StorytellerGPT(tell_story):
|
17 |
+
messages.append({"role": "user", "content": tell_story})
|
18 |
+
|
19 |
+
# Generate story using Hugging Face's GPT-2
|
20 |
+
story_reply = story_generator(tell_story, max_length=100, num_return_sequences=1)[0]['generated_text']
|
21 |
+
|
22 |
+
messages.append({"role": "assistant", "content": story_reply})
|
23 |
+
|
24 |
+
# Convert text to speech
|
25 |
+
tts = gTTS(text=story_reply, lang='en', slow=False)
|
26 |
+
audio_io = BytesIO()
|
27 |
+
tts.save(audio_io)
|
28 |
+
audio_io.seek(0)
|
29 |
+
|
30 |
+
# Convert text to image
|
31 |
+
image = generate_dynamic_image(story_reply)
|
32 |
+
|
33 |
+
return story_reply, Audio(data=audio_io.read(), autoplay=True), image
|
34 |
+
|
35 |
+
# Function to generate a dynamic image based on the story text
|
36 |
+
def generate_dynamic_image(story_text):
|
37 |
+
# Create a blank image
|
38 |
+
image = Image.new("RGB", (500, 300), (255, 255, 255))
|
39 |
+
draw = ImageDraw.Draw(image)
|
40 |
+
|
41 |
+
# Use a truetype font file, replace "arial.ttf" with the path to your font file
|
42 |
+
font = ImageFont.truetype("arial.ttf", 20)
|
43 |
+
|
44 |
+
# Write the story text on the image
|
45 |
+
lines = [story_text[i:i+40] for i in range(0, len(story_text), 40)]
|
46 |
+
y_position = 10
|
47 |
+
for line in lines:
|
48 |
+
draw.text((10, y_position), line, font=font, fill=(0, 0, 0))
|
49 |
+
y_position += 30
|
50 |
+
|
51 |
+
return image
|
52 |
+
|
53 |
+
# Create the Gradio Interface
|
54 |
+
demo = gr.Interface(
|
55 |
+
fn=StorytellerGPT,
|
56 |
+
inputs="text",
|
57 |
+
outputs=["text", "audio", "image"],
|
58 |
+
title="📖 Storytelling Magic",
|
59 |
+
description="A magical storyteller app for kids! Type a sentence, and let the app create an enchanting story for you."
|
60 |
)
|
61 |
|
62 |
+
# Launch the Gradio Interface
|
63 |
+
demo.launch()
|
64 |
+
|