JoyStroy / app1.py
Rathapoom's picture
Rename app.py to app1.py
a34715a verified
raw
history blame
2.57 kB
import streamlit as st
import openai
from openai import OpenAI
# การตั้งค่า API Key สำหรับ 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 # บันทึกระดับที่เลือกไว้ใน session
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"):
# การเรียกใช้ API ตามโครงสร้างใหม่
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 # Increased to accommodate answers and explanations
)
ai_text = response.choices[0].message["content"].strip()
# อัปเดตเรื่องราวที่กำลังดำเนินอยู่
st.session_state["story_text"] += " " + user_input + " " + ai_text
st.write(st.session_state["story_text"])