Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
# Install necessary libraries
|
4 |
+
!pip install -q gradio
|
5 |
+
!pip install -q openai
|
6 |
+
!pip install -q gTTS
|
7 |
+
|
8 |
+
# Import libraries
|
9 |
+
import gradio as gr
|
10 |
+
import openai
|
11 |
+
from gtts import gTTS
|
12 |
+
from io import BytesIO
|
13 |
+
from IPython.display import Audio
|
14 |
+
|
15 |
+
# Set up OpenAI API key
|
16 |
+
openai.api_key = "sk-pZv0gFbHlaKc5o3ejPgYT3BlbkFJ7DPw0d1FqJApeZTBjIqic" # Replace with your actual API key
|
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 |
+
# Define the Storyteller function
|
22 |
+
def StorytellerGPT(tell_story):
|
23 |
+
messages.append({"role": "user", "content": tell_story})
|
24 |
+
response = openai.ChatCompletion.create(
|
25 |
+
model="gpt-3.5-turbo",
|
26 |
+
messages=messages,
|
27 |
+
temperature=.1
|
28 |
+
)
|
29 |
+
story_reply = response["choices"][0]["message"]["content"]
|
30 |
+
messages.append({"role": "assistant", "content": story_reply})
|
31 |
+
|
32 |
+
# Convert text to speech
|
33 |
+
tts = gTTS(text=story_reply, lang='en', slow=False)
|
34 |
+
audio_io = BytesIO()
|
35 |
+
tts.save(audio_io)
|
36 |
+
audio_io.seek(0)
|
37 |
+
|
38 |
+
return story_reply, Audio(data=audio_io.read(), autoplay=True)
|
39 |
+
|
40 |
+
# Create the Gradio Interface
|
41 |
+
demo = gr.Interface(
|
42 |
+
fn=StorytellerGPT,
|
43 |
+
inputs="text",
|
44 |
+
outputs=["text", "audio"],
|
45 |
+
title="📖 Storytelling Magic",
|
46 |
+
description="A magical storyteller app for kids! Type a sentence, and let the app create an enchanting story for you."
|
47 |
+
)
|
48 |
+
|
49 |
+
# Launch the Gradio
|