GH111 commited on
Commit
0c5599e
Β·
1 Parent(s): 5ce8815

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -7
app.py CHANGED
@@ -1,6 +1,9 @@
1
  # Import libraries
2
  import gradio as gr
3
  from transformers import pipeline
 
 
 
4
 
5
  # Create a text generation pipeline with GPT-2
6
  story_generator = pipeline("text-generation", model="EleutherAI/gpt-neo-1.3B")
@@ -9,24 +12,44 @@ story_generator = pipeline("text-generation", model="EleutherAI/gpt-neo-1.3B")
9
  messages = [{"role": "system", "content": "You are a magical storyteller, creating wonderful tales for kids. Make them imaginative and full of joy!"}]
10
 
11
  # Define the Storyteller function
12
- def StorytellerGPT(tell_story):
 
 
 
 
13
  messages.append({"role": "user", "content": tell_story})
14
 
15
  # Generate story using Hugging Face's GPT-2
16
- story_reply = story_generator(tell_story, max_length=100, num_return_sequences=1)[0]['generated_text']
17
 
18
  messages.append({"role": "assistant", "content": story_reply})
19
 
20
- return story_reply
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  # Create the Gradio Interface
23
  demo = gr.Interface(
24
  fn=StorytellerGPT,
25
- inputs="text",
26
- outputs="text",
 
 
 
 
 
27
  title="πŸ“– Storytelling Magic",
28
- description="A magical storyteller app for kids! Type a sentence, and let the app create an enchanting story for you."
29
  )
30
 
31
  # Launch the Gradio Interface
32
- demo.launch()
 
1
  # Import libraries
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
 
8
  # Create a text generation pipeline with GPT-2
9
  story_generator = pipeline("text-generation", model="EleutherAI/gpt-neo-1.3B")
 
12
  messages = [{"role": "system", "content": "You are a magical storyteller, creating wonderful tales for kids. Make them imaginative and full of joy!"}]
13
 
14
  # Define the Storyteller function
15
+ def StorytellerGPT(character, child_name, lesson_choice, tell_story):
16
+ # Set the characters and lesson based on user choices
17
+ character_info = f"Once upon a time, {child_name} met {character}. "
18
+ lesson_info = f"Today's lesson is about {lesson_choice}. "
19
+
20
  messages.append({"role": "user", "content": tell_story})
21
 
22
  # Generate story using Hugging Face's GPT-2
23
+ story_reply = story_generator(character_info + lesson_info + tell_story, max_length=150, num_return_sequences=1)[0]['generated_text']
24
 
25
  messages.append({"role": "assistant", "content": story_reply})
26
 
27
+ # Convert text to speech
28
+ tts = gTTS(text=story_reply, lang='en', slow=False)
29
+ audio_io = BytesIO()
30
+ tts.save(audio_io)
31
+ audio_io.seek(0)
32
+
33
+ # Convert text to image
34
+ image = Image.new("RGB", (300, 300), (255, 255, 255))
35
+ image_path = "/path/to/output/image.png"
36
+ image.save(image_path)
37
+
38
+ return story_reply, Audio(data=audio_io.read(), autoplay=True), image
39
 
40
  # Create the Gradio Interface
41
  demo = gr.Interface(
42
  fn=StorytellerGPT,
43
+ inputs=[
44
+ gr.Textbox("text", label="Child's Name"),
45
+ gr.Dropdown(["unicorn", "dragon", "wizard"], label="Choose a Character"),
46
+ gr.Dropdown(["kindness", "creativity", "bravery"], label="Choose a Lesson"),
47
+ gr.Textbox("text", label="Start the Story with"),
48
+ ],
49
+ outputs=["text", "audio", "image"],
50
  title="πŸ“– Storytelling Magic",
51
+ description="A magical storyteller app for kids! Choose characters, add your name, and select the lesson you want to learn.",
52
  )
53
 
54
  # Launch the Gradio Interface
55
+ demo.launch(