File size: 2,570 Bytes
3a6f270
 
1da9be0
3a6f270
 
70f3ebc
1da9be0
3a6f270
 
 
 
 
 
 
 
 
aaf35e9
 
 
3a6f270
 
 
 
 
aaf35e9
c09b35d
aaf35e9
 
c09b35d
 
aaf35e9
c09b35d
 
 
 
 
aaf35e9
c09b35d
 
a9cc308
1da9be0
 
 
a9cc308
 
 
 
 
1da9be0
 
a9cc308
 
c09b35d
 
 
aaf35e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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"])