Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,80 +1,50 @@
|
|
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 |
-
|
10 |
-
|
11 |
from PIL import Image
|
12 |
-
from diffusers import
|
13 |
-
from transformers import pipeline
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
|
18 |
-
#
|
19 |
-
|
|
|
20 |
|
21 |
-
# Initialize
|
22 |
-
|
23 |
|
24 |
-
#
|
25 |
-
|
|
|
|
|
26 |
|
27 |
-
#
|
28 |
-
|
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 Hugging Face's GPT-Neo
|
38 |
-
input_text = character_info + lesson_info + tell_story
|
39 |
-
story_reply = gpt_neo_generator(input_text, max_length=150, num_return_sequences=1, no_repeat_ngram_size=2, top_k=50, top_p=0.95)[0]['generated_text']
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
tts = gTTS(text=story_reply, lang='en', slow=False)
|
45 |
-
audio_io = BytesIO()
|
46 |
-
tts.save("/content/audio_output.mp3")
|
47 |
|
48 |
-
#
|
49 |
-
|
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], "/content/audio_output.mp3", image_reply
|
56 |
|
57 |
-
# Create
|
58 |
-
|
59 |
-
fn=
|
60 |
inputs=[
|
61 |
-
gr.Textbox(
|
62 |
-
gr.
|
63 |
-
|
64 |
-
|
65 |
-
gr.
|
|
|
|
|
66 |
],
|
67 |
-
|
68 |
-
|
69 |
-
|
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
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import whisper
|
4 |
from PIL import Image
|
5 |
+
from diffusers import StableDiffusionPipeline
|
|
|
6 |
|
7 |
+
# Set your OpenAI API key
|
8 |
+
openai.api_key = ""
|
9 |
|
10 |
+
# Initialize text generation and image generation pipelines
|
11 |
+
text_generation_pipe = openai.Completion.create
|
12 |
+
image_generation_pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
|
13 |
|
14 |
+
# Initialize Whisper API key for text-to-speech
|
15 |
+
whisper_api_key = ""
|
16 |
|
17 |
+
# Define function for generating stories and illustrations
|
18 |
+
def generate_story_and_illustration(prompt, character_name):
|
19 |
+
# Generate story
|
20 |
+
story_text = text_generation_pipe(engine="text-davinci-003", prompt=f"Once upon a time, {character_name}... {prompt}", max_tokens=150)["choices"][0]["text"]
|
21 |
|
22 |
+
# Generate image based on story
|
23 |
+
image = image_generation_pipe(story_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
# Convert story text to speech using Whisper API
|
26 |
+
tts_result = whisper.transcribe(audio="content.flac", language="en", api_key=whisper_api_key)
|
27 |
+
audio_data = tts_result["segments"][0]["alternatives"][0]["text"]
|
|
|
|
|
|
|
28 |
|
29 |
+
# Return story text, audio data, and image
|
30 |
+
return story_text, audio_data, image.images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
# Create Gradio interface
|
33 |
+
interface = gr.Interface(
|
34 |
+
fn=generate_story_and_illustration,
|
35 |
inputs=[
|
36 |
+
gr.Textbox(label="Start your story with..."),
|
37 |
+
gr.Textbox(label="Give your character a name:"),
|
38 |
+
],
|
39 |
+
outputs=[
|
40 |
+
gr.Textbox(label="Story"),
|
41 |
+
gr.Audio(label="Listen to the story"),
|
42 |
+
gr.Image(label="See the story come alive"),
|
43 |
],
|
44 |
+
title="Storyteller Playground",
|
45 |
+
description="Create amazing stories with the help of AI!",
|
46 |
+
theme="kids",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
)
|
48 |
|
49 |
+
# Launch the app on Hugging Face Spaces
|
50 |
+
interface.launch()
|