GH111 commited on
Commit
82e33f9
·
1 Parent(s): a546c80

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -41
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
- # Initialize transformers for each task
8
- text_generation_pipe = pipeline("text-generation", model="gpt2")
9
- text_to_speech_pipe = pipeline("text-to-speech", model="google")
10
- image_generation_pipe = pipeline("image-generation", model="stable-diffusion")
11
-
12
- # Function for generating stories, audio, and illustrations
13
- def generate_story_and_illustration(prompt, character_name):
14
- # Generate story text
15
- story_text = text_generation_pipe(prompt=f"Once upon a time, {character_name}... {prompt}", max_length=150)[0]["generated_text"]
16
-
17
- # Generate audio from story text
18
- tts_audio = gTTS(text=story_text, lang="en")
19
- audio_buffer = BytesIO()
20
- tts_audio.save(audio_buffer)
21
- audio_buffer.seek(0)
22
-
23
- # Generate image based on story
24
- image = image_generation_pipe(prompt=story_text)
25
-
26
- # Return generated story, audio, and image
27
- return story_text, audio_buffer.read(), image
28
-
29
- # Gradio interface for user interaction
30
- interface = gr.Interface(
31
- fn=generate_story_and_illustration,
32
- inputs=[
33
- gr.Textbox(label="Start your story with..."),
34
- gr.Textbox(label="Give your character a name:"),
35
- ],
36
- outputs=[
37
- gr.Textbox(label="Story"),
38
- gr.Audio(label="Listen to the story"),
39
- gr.Image(label="See the story come alive"),
40
- ],
41
- title="Storyteller Playground",
42
- description="Create amazing stories with the help of AI!",
43
- theme="kids",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  )
45
 
46
- # Launch the app
47
- interface.launch()
 
 
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
+