Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
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
|
7 |
-
from
|
8 |
|
9 |
-
#
|
10 |
-
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
messages.append({"role": "user", "content": tell_story})
|
18 |
-
|
19 |
-
# Generate story using
|
20 |
-
|
|
|
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 |
-
|
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 |
-
#
|
42 |
-
|
|
|
43 |
|
44 |
-
|
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=
|
56 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
outputs=["text", "audio", "image"],
|
58 |
title="π Storytelling Magic",
|
59 |
-
description="A magical storyteller app for kids!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
)
|
61 |
|
62 |
# Launch the Gradio Interface
|
63 |
demo.launch()
|
64 |
-
|
|
|
1 |
+
# Import libraries
|
2 |
+
!pip install gradio transformers
|
3 |
+
!pip install -q gradio
|
4 |
+
!pip install -q openai
|
5 |
+
!pip install -q gTTS
|
6 |
|
7 |
import gradio as gr
|
8 |
from transformers import pipeline
|
9 |
from gtts import gTTS
|
10 |
from io import BytesIO
|
11 |
+
from PIL import Image
|
12 |
+
from diffusers import DiffusionPipeline
|
13 |
|
14 |
+
# Use a pipeline as a high-level helper for text generation
|
15 |
+
vision_alpha_pipe = pipeline("text-generation", model="NousResearch/Nous-Hermes-2-Vision-Alpha")
|
16 |
+
|
17 |
+
# Initialize DiffusionPipeline for text-to-image
|
18 |
+
image_generation_pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-video-diffusion-img2vid-xt")
|
19 |
|
20 |
# Set the context for the storyteller
|
21 |
messages = [{"role": "system", "content": "You are a magical storyteller, creating wonderful tales for kids. Make them imaginative and full of joy!"}]
|
22 |
|
23 |
+
# Initialize page number
|
24 |
+
current_page = 0
|
25 |
+
|
26 |
# Define the Storyteller function
|
27 |
+
def StorytellerNous(character, child_name, lesson_choice, tell_story, _):
|
28 |
+
global current_page
|
29 |
+
|
30 |
+
# Set the characters and lesson based on user choices
|
31 |
+
character_info = f"Once upon a time, {child_name} met {character}. "
|
32 |
+
lesson_info = f"Today's lesson is about {lesson_choice}. "
|
33 |
+
|
34 |
messages.append({"role": "user", "content": tell_story})
|
35 |
+
|
36 |
+
# Generate story using Nous-Hermes-2-Vision-Alpha
|
37 |
+
input_text = character_info + lesson_info + tell_story
|
38 |
+
story_reply = vision_alpha_pipe(input_text, max_length=150, num_return_sequences=1, no_repeat_ngram_size=2, top_k=50, top_p=0.95)[0]['generated_text']
|
39 |
|
40 |
messages.append({"role": "assistant", "content": story_reply})
|
41 |
+
|
42 |
# Convert text to speech
|
43 |
tts = gTTS(text=story_reply, lang='en', slow=False)
|
44 |
audio_io = BytesIO()
|
45 |
tts.save(audio_io)
|
46 |
audio_io.seek(0)
|
47 |
|
48 |
+
# Convert text to image using DiffusionPipeline
|
49 |
+
image_reply = image_generation_pipe(story_reply)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
+
# Display the story on separate pages
|
52 |
+
story_pages = story_reply.split("\n\n") # Split the story into pages
|
53 |
+
current_page = min(current_page, len(story_pages) - 1) # Ensure the current_page is within bounds
|
54 |
|
55 |
+
return story_pages[current_page], Audio(data=audio_io.read(), autoplay=True), image_reply
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
+
# Create the Gradio Interface with styling
|
58 |
demo = gr.Interface(
|
59 |
+
fn=StorytellerNous,
|
60 |
+
inputs=[
|
61 |
+
gr.Textbox("text", label="Child's Name"),
|
62 |
+
gr.Dropdown(["unicorn", "dragon", "wizard"], label="Choose a Character"),
|
63 |
+
gr.Dropdown(["kindness", "creativity", "bravery"], label="Choose a Lesson"),
|
64 |
+
gr.Textbox("text", label="Start the Story with"),
|
65 |
+
gr.Button("Next Page"),
|
66 |
+
],
|
67 |
outputs=["text", "audio", "image"],
|
68 |
title="π Storytelling Magic",
|
69 |
+
description="A magical storyteller app for kids! Choose characters, add your name, and select the lesson you want to learn.",
|
70 |
+
live=True, # Enable live updates for CSS changes
|
71 |
+
css=f"""body {{
|
72 |
+
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');
|
73 |
+
background-size: cover;
|
74 |
+
background-position: center;
|
75 |
+
font-family: 'Comic Sans MS', cursive, sans-serif; /* Optional: Change the font */
|
76 |
+
}}""",
|
77 |
)
|
78 |
|
79 |
# Launch the Gradio Interface
|
80 |
demo.launch()
|
|