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