Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,13 @@
|
|
1 |
# Import libraries
|
2 |
-
|
3 |
import gradio as gr
|
4 |
-
from transformers import
|
5 |
from gtts import gTTS
|
6 |
from io import BytesIO
|
7 |
from PIL import Image
|
8 |
-
from diffusers import DiffusionPipeline
|
9 |
-
|
10 |
-
# Use a pipeline as a high-level helper for text generation
|
11 |
-
vision_alpha_pipe = pipeline("text-generation", model="NousResearch/Nous-Hermes-2-Vision-Alpha")
|
12 |
|
13 |
-
#
|
14 |
-
|
|
|
15 |
|
16 |
# Set the context for the storyteller
|
17 |
messages = [{"role": "system", "content": "You are a magical storyteller, creating wonderful tales for kids. Make them imaginative and full of joy!"}]
|
@@ -20,39 +16,45 @@ messages = [{"role": "system", "content": "You are a magical storyteller, creati
|
|
20 |
current_page = 0
|
21 |
|
22 |
# Define the Storyteller function
|
23 |
-
def
|
24 |
global current_page
|
25 |
-
|
26 |
# Set the characters and lesson based on user choices
|
27 |
character_info = f"Once upon a time, {child_name} met {character}. "
|
28 |
lesson_info = f"Today's lesson is about {lesson_choice}. "
|
29 |
-
|
30 |
messages.append({"role": "user", "content": tell_story})
|
31 |
-
|
32 |
-
# Generate story using
|
33 |
input_text = character_info + lesson_info + tell_story
|
34 |
-
|
|
|
|
|
|
|
|
|
35 |
|
36 |
messages.append({"role": "assistant", "content": story_reply})
|
37 |
-
|
38 |
# Convert text to speech
|
39 |
tts = gTTS(text=story_reply, lang='en', slow=False)
|
40 |
audio_io = BytesIO()
|
41 |
tts.save(audio_io)
|
42 |
audio_io.seek(0)
|
43 |
|
44 |
-
# Convert text to image
|
45 |
-
|
46 |
-
|
|
|
|
|
47 |
# Display the story on separate pages
|
48 |
story_pages = story_reply.split("\n\n") # Split the story into pages
|
49 |
current_page = min(current_page, len(story_pages) - 1) # Ensure the current_page is within bounds
|
50 |
-
|
51 |
-
return story_pages[current_page], Audio(data=audio_io.read(), autoplay=True),
|
52 |
|
53 |
# Create the Gradio Interface with styling
|
54 |
demo = gr.Interface(
|
55 |
-
fn=
|
56 |
inputs=[
|
57 |
gr.Textbox("text", label="Child's Name"),
|
58 |
gr.Dropdown(["unicorn", "dragon", "wizard"], label="Choose a Character"),
|
|
|
1 |
# Import libraries
|
|
|
2 |
import gradio as gr
|
3 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
4 |
from gtts import gTTS
|
5 |
from io import BytesIO
|
6 |
from PIL import Image
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
# Load GPT-2 model and tokenizer
|
9 |
+
model = GPT2LMHeadModel.from_pretrained("gpt2")
|
10 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
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!"}]
|
|
|
16 |
current_page = 0
|
17 |
|
18 |
# Define the Storyteller function
|
19 |
+
def StorytellerGPT(character, child_name, lesson_choice, tell_story, _):
|
20 |
global current_page
|
21 |
+
|
22 |
# Set the characters and lesson based on user choices
|
23 |
character_info = f"Once upon a time, {child_name} met {character}. "
|
24 |
lesson_info = f"Today's lesson is about {lesson_choice}. "
|
25 |
+
|
26 |
messages.append({"role": "user", "content": tell_story})
|
27 |
+
|
28 |
+
# Generate story using Hugging Face's GPT-2
|
29 |
input_text = character_info + lesson_info + tell_story
|
30 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt")
|
31 |
+
story_reply = model.generate(input_ids, max_length=150, num_return_sequences=1, no_repeat_ngram_size=2, top_k=50, top_p=0.95)[0]
|
32 |
+
|
33 |
+
# Decode the generated sequence
|
34 |
+
story_reply = tokenizer.decode(story_reply, skip_special_tokens=True)
|
35 |
|
36 |
messages.append({"role": "assistant", "content": story_reply})
|
37 |
+
|
38 |
# Convert text to speech
|
39 |
tts = gTTS(text=story_reply, lang='en', slow=False)
|
40 |
audio_io = BytesIO()
|
41 |
tts.save(audio_io)
|
42 |
audio_io.seek(0)
|
43 |
|
44 |
+
# Convert text to image
|
45 |
+
image = Image.new("RGB", (300, 300), (255, 255, 255))
|
46 |
+
image_path = "/path/to/output/image.png"
|
47 |
+
image.save(image_path)
|
48 |
+
|
49 |
# Display the story on separate pages
|
50 |
story_pages = story_reply.split("\n\n") # Split the story into pages
|
51 |
current_page = min(current_page, len(story_pages) - 1) # Ensure the current_page is within bounds
|
52 |
+
|
53 |
+
return story_pages[current_page], Audio(data=audio_io.read(), autoplay=True), image
|
54 |
|
55 |
# Create the Gradio Interface with styling
|
56 |
demo = gr.Interface(
|
57 |
+
fn=StorytellerGPT,
|
58 |
inputs=[
|
59 |
gr.Textbox("text", label="Child's Name"),
|
60 |
gr.Dropdown(["unicorn", "dragon", "wizard"], label="Choose a Character"),
|