Rathapoom commited on
Commit
e046fa6
·
verified ·
1 Parent(s): b0be270

Rename app1.py to app.py

Browse files
Files changed (2) hide show
  1. app.py +86 -0
  2. app1.py +0 -56
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import openai
3
+ from datetime import datetime
4
+
5
+ # Custom CSS for layout
6
+ st.markdown("""
7
+ <style>
8
+ .box-container {
9
+ background-color: #ffffff;
10
+ border-radius: 10px;
11
+ padding: 20px;
12
+ margin: 10px 0;
13
+ border: 2px solid #eee;
14
+ box-shadow: 0 2px 5px rgba(0,0,0,0.1);
15
+ }
16
+
17
+ .story-box {
18
+ max-height: 300px;
19
+ overflow-y: auto;
20
+ padding: 15px;
21
+ background-color: #f8f9fa;
22
+ border-radius: 8px;
23
+ }
24
+
25
+ .input-box {
26
+ background-color: #ffffff;
27
+ padding: 15px;
28
+ border-radius: 8px;
29
+ }
30
+ </style>
31
+ """, unsafe_allow_html=True)
32
+
33
+ # ตั้งค่า API Key
34
+ openai.api_key = st.secrets["OPENAI_API_KEY"]
35
+
36
+ # Initialize session state variables
37
+ if 'story' not in st.session_state:
38
+ st.session_state.story = ["Once upon a time, in a magical forest..."]
39
+ if 'story_started' not in st.session_state:
40
+ st.session_state.story_started = False
41
+
42
+ def continue_story(user_input):
43
+ """Get story continuation from OpenAI API"""
44
+ story_context = " ".join(st.session_state.story)
45
+
46
+ response = openai.ChatCompletion.create(
47
+ model="gpt-4o-mini",
48
+ messages=[
49
+ {"role": "system", "content": "You are a creative storyteller helping to continue a story with interesting details. Keep it simple and coherent."},
50
+ {"role": "user", "content": f"Story so far: {story_context}"},
51
+ {"role": "user", "content": f"Continue with: {user_input}"}
52
+ ],
53
+ temperature=0.7,
54
+ max_tokens=50
55
+ )
56
+ return response.choices[0].message.content.strip()
57
+
58
+ # Main application
59
+ st.title("🎨 Interactive Story Adventure")
60
+ st.subheader("Let's create a fun story together!")
61
+
62
+ # Start story button
63
+ if not st.session_state.story_started:
64
+ if st.button("Start Story"):
65
+ st.session_state.story_started = True
66
+ st.success("Story started! Let's add to it.")
67
+
68
+ # Display story so far
69
+ st.write("### 📖 Our Story So Far:")
70
+ for entry in st.session_state.story:
71
+ st.markdown(f"<div class='box-container story-box'>{entry}</div>", unsafe_allow_html=True)
72
+
73
+ # Add user input for the story continuation
74
+ if st.session_state.story_started:
75
+ user_input = st.text_input("Add your sentence:", "")
76
+
77
+ if st.button("Submit"):
78
+ # Add the user's input to the story
79
+ st.session_state.story.append(user_input)
80
+
81
+ # Generate AI continuation
82
+ ai_continuation = continue_story(user_input)
83
+ st.session_state.story.append(ai_continuation)
84
+
85
+ # Rerun to display updated story
86
+ st.experimental_rerun()
app1.py DELETED
@@ -1,56 +0,0 @@
1
- import streamlit as st
2
- import openai
3
- from openai import OpenAI
4
-
5
- # การตั้งค่า API Key สำหรับ OpenAI
6
- openai.api_key = st.secrets["OPENAI_API_KEY"]
7
- client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
8
-
9
- # ชื่อโปรเจกต์
10
- st.title("Welcome to JoyStory! 🎉")
11
- st.subheader("Let's create fun and imaginative stories together!")
12
-
13
- # ตัวเลือกระดับภาษา
14
- level = st.selectbox("Choose your level:", ["Beginner", "Intermediate", "Advanced"])
15
-
16
- # ปุ่มเริ่มต้น
17
- if "story_started" not in st.session_state:
18
- st.session_state["story_started"] = False # กำหนดค่าเริ่มต้น
19
-
20
- start_button = st.button("Start Creating Your Story!")
21
-
22
- # ตรวจสอบเมื่อกดปุ่มเริ่มต้น
23
- if start_button:
24
- st.session_state["level"] = level # บันทึกระดับที่เลือกไว้ใน session
25
- st.session_state["story_started"] = True # เปลี่ยนสถานะการเริ่มเรื่อง
26
-
27
- # เมื่อการแต่งเรื่องเริ่มต้นแล้ว
28
- if st.session_state["story_started"]:
29
- st.header("JoyStory - Let's Create!")
30
-
31
- # เริ่มต้นเนื้อเรื่อง
32
- if "story_text" not in st.session_state:
33
- st.session_state["story_text"] = "Once upon a time, in a magical forest..."
34
-
35
- st.write(st.session_state["story_text"])
36
-
37
- # กล่องให้เด็กแต่งประโยค
38
- user_input = st.text_input("Add your sentence:")
39
- if st.button("Submit"):
40
- # การเรียกใช้ API ตามโครงสร้างใหม่
41
-
42
- response = client.chat.completions.create(
43
- model="gpt-4o-mini",
44
- messages=[
45
- {"role": "system", "content": "You are a storytelling assistant who helps children write fun and imaginative stories."},
46
- {"role": "user", "content": st.session_state["story_text"] + " " + user_input}
47
- ],
48
- temperature=0.7,
49
- max_tokens=50 # Increased to accommodate answers and explanations
50
- )
51
-
52
- ai_text = response.choices[0].message["content"].strip()
53
-
54
- # อัปเดตเรื่องราวที่กำลังดำเนินอยู่
55
- st.session_state["story_text"] += " " + user_input + " " + ai_text
56
- st.write(st.session_state["story_text"])