Spaces:
Sleeping
Sleeping
# Install necessary libraries | |
!pip install -q gradio | |
!pip install -q openai | |
!pip install -q gTTS | |
# Import libraries | |
import gradio as gr | |
import openai | |
from gtts import gTTS | |
from io import BytesIO | |
from IPython.display import Audio | |
# Set up OpenAI API key | |
openai.api_key = "sk-pZv0gFbHlaKc5o3ejPgYT3BlbkFJ7DPw0d1FqJApeZTBjIqic" # Replace with your actual API key | |
# Set the context for the storyteller | |
messages = [{"role": "system", "content": "You are a magical storyteller, creating wonderful tales for kids. Make them imaginative and full of joy!"}] | |
# Define the Storyteller function | |
def StorytellerGPT(tell_story): | |
messages.append({"role": "user", "content": tell_story}) | |
response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", | |
messages=messages, | |
temperature=.1 | |
) | |
story_reply = response["choices"][0]["message"]["content"] | |
messages.append({"role": "assistant", "content": story_reply}) | |
# Convert text to speech | |
tts = gTTS(text=story_reply, lang='en', slow=False) | |
audio_io = BytesIO() | |
tts.save(audio_io) | |
audio_io.seek(0) | |
return story_reply, Audio(data=audio_io.read(), autoplay=True) | |
# Create the Gradio Interface | |
demo = gr.Interface( | |
fn=StorytellerGPT, | |
inputs="text", | |
outputs=["text", "audio"], | |
title="π Storytelling Magic", | |
description="A magical storyteller app for kids! Type a sentence, and let the app create an enchanting story for you." | |
) | |
# Launch the Gradio |