dtm95's picture
Update app.py
560fa5d verified
import streamlit as st
import openai
import os
# Set up the Streamlit app
st.title("πŸ§šβ€β™€οΈ Magic Story Buddy πŸ“š")
st.markdown("Let's create a magical story just for you!")
# Set up OpenAI API key
openai.api_key = os.getenv("sk-proj-tWGLTJozNuRBlh6RGqFAT3BlbkFJQy7dSGpukXfwuwueZGIt")
# User input
child_name = st.text_input("What's your name, young storyteller?")
story_theme = st.selectbox("What would you like your story to be about?",
["Space Adventure", "Magical Forest", "Underwater World", "Dinosaur Discovery"])
# Additional options
story_length = st.slider("How long should the story be?", 50, 200, 100)
include_moral = st.checkbox("Include a moral lesson?")
def generate_story(prompt, max_tokens=500):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a friendly storyteller for children."},
{"role": "user", "content": prompt}
],
max_tokens=max_tokens,
n=1,
stop=None,
temperature=0.7,
)
return response.choices[0].message['content'].strip()
if st.button("Create My Story!"):
if child_name and story_theme:
# Construct the prompt
prompt = f"""Create a short children's story with the following details:
- Main character: {child_name}
- Theme: {story_theme}
- Length: About {story_length} words
- Audience: Children aged 5-10
- Tone: Friendly, educational, and imaginative
Story:
Once upon a time, in a {story_theme.lower()}, there was a brave child named {child_name}. """
if include_moral:
prompt += "This story teaches us that "
# Generate the story
story = generate_story(prompt, max_tokens=story_length * 2) # Multiply by 2 as tokens != words
# Display the story
st.markdown("## Your Magical Story")
st.write(story)
# Add a fun element
st.balloons()
else:
st.warning("Please tell me your name and choose a story theme.")
# Add some child-friendly decorations
st.markdown("---")
st.markdown("🌟 Remember, you're the star of every story! 🌟")