Spaces:
Sleeping
Sleeping
import gradio as gr | |
import openai | |
import whisper | |
from PIL import Image | |
from diffusers import StableDiffusionPipeline | |
# Set your OpenAI API key | |
openai.api_key = "" | |
# Initialize text generation and image generation pipelines | |
text_generation_pipe = openai.Completion.create | |
image_generation_pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4") | |
# Initialize Whisper API key for text-to-speech | |
whisper_api_key = "" | |
# Define function for generating stories and illustrations | |
def generate_story_and_illustration(prompt, character_name): | |
# Generate story | |
story_text = text_generation_pipe(engine="text-davinci-003", prompt=f"Once upon a time, {character_name}... {prompt}", max_tokens=150)["choices"][0]["text"] | |
# Generate image based on story | |
image = image_generation_pipe(story_text) | |
# Convert story text to speech using Whisper API | |
tts_result = whisper.transcribe(audio="content.flac", language="en", api_key=whisper_api_key) | |
audio_data = tts_result["segments"][0]["alternatives"][0]["text"] | |
# Return story text, audio data, and image | |
return story_text, audio_data, image.images[0] | |
# Create Gradio interface | |
interface = gr.Interface( | |
fn=generate_story_and_illustration, | |
inputs=[ | |
gr.Textbox(label="Start your story with..."), | |
gr.Textbox(label="Give your character a name:"), | |
], | |
outputs=[ | |
gr.Textbox(label="Story"), | |
gr.Audio(label="Listen to the story"), | |
gr.Image(label="See the story come alive"), | |
], | |
title="Storyteller Playground", | |
description="Create amazing stories with the help of AI!", | |
theme="kids", | |
) | |
# Launch the app on Hugging Face Spaces | |
interface.launch() | |