GH111 commited on
Commit
98ab077
·
1 Parent(s): 55c0f00

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -66
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
- from gtts import gTTS
10
- from io import BytesIO
11
  from PIL import Image
12
- from diffusers import DiffusionPipeline
13
- from transformers import pipeline
14
 
15
- # Use a DiffusionPipeline for text-to-image
16
- image_generation_pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-video-diffusion-img2vid-xt")
17
 
18
- # Set the context for the storyteller
19
- messages = [{"role": "system", "content": "You are a magical storyteller, creating wonderful tales for kids. Make them imaginative and full of joy!"}]
 
20
 
21
- # Initialize page number
22
- current_page = 0
23
 
24
- # Initialize Hugging Face text generation pipeline
25
- gpt_neo_generator = pipeline("text-generation", model="EleutherAI/gpt-neo-1.3B", device=0) # You can adjust the device parameter based on your setup
 
 
26
 
27
- # Define the Storyteller function
28
- def StorytellerHuggingFace(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 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
- messages.append({"role": "assistant", "content": story_reply})
42
-
43
- # Convert text to speech using gTTS
44
- tts = gTTS(text=story_reply, lang='en', slow=False)
45
- audio_io = BytesIO()
46
- tts.save("/content/audio_output.mp3")
47
 
48
- # Convert text to image using DiffusionPipeline
49
- image_reply = image_generation_pipe(story_reply)
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 the Gradio Interface with styling
58
- demo = gr.Interface(
59
- fn=StorytellerHuggingFace,
60
  inputs=[
61
- gr.Textbox("text", label="Child's Name"),
62
- gr.Dropdown(["unicorn", "dragon", "wizard"], label="Choose a Character"),
63
- gr.Dropdown(["kindness", "creativity", "bravery"], label="Choose a Lesson"),
64
- gr.Textbox("text", label="Start the Story with"),
65
- gr.Button("Next Page"),
 
 
66
  ],
67
- outputs=["text", "audio", "image"],
68
- title="📖 Storytelling Magic",
69
- description="A magical storyteller app for kids! Choose characters, add your name, and select the lesson you want to learn.",
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 Gradio Interface
80
- demo.launch()
 
 
 
 
 
 
 
 
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()