|
import streamlit as st |
|
import openai |
|
from openai import OpenAI |
|
|
|
|
|
openai.api_key = st.secrets["OPENAI_API_KEY"] |
|
client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"]) |
|
|
|
|
|
st.title("Welcome to JoyStory! 🎉") |
|
st.subheader("Let's create fun and imaginative stories together!") |
|
|
|
|
|
level = st.selectbox("Choose your level:", ["Beginner", "Intermediate", "Advanced"]) |
|
|
|
|
|
if "story_started" not in st.session_state: |
|
st.session_state["story_started"] = False |
|
|
|
start_button = st.button("Start Creating Your Story!") |
|
|
|
|
|
if start_button: |
|
st.session_state["level"] = level |
|
st.session_state["story_started"] = True |
|
|
|
|
|
if st.session_state["story_started"]: |
|
st.header("JoyStory - Let's Create!") |
|
|
|
|
|
if "story_text" not in st.session_state: |
|
st.session_state["story_text"] = "Once upon a time, in a magical forest..." |
|
|
|
st.write(st.session_state["story_text"]) |
|
|
|
|
|
user_input = st.text_input("Add your sentence:") |
|
if st.button("Submit"): |
|
|
|
|
|
response = client.chat.completions.create( |
|
model="gpt-4o-mini", |
|
messages=[ |
|
{"role": "system", "content": "You are a storytelling assistant who helps children write fun and imaginative stories."}, |
|
{"role": "user", "content": st.session_state["story_text"] + " " + user_input} |
|
], |
|
temperature=0.7, |
|
max_tokens=50 |
|
) |
|
|
|
ai_text = response.choices[0].message["content"].strip() |
|
|
|
|
|
st.session_state["story_text"] += " " + user_input + " " + ai_text |
|
st.write(st.session_state["story_text"]) |
|
|