alibicer commited on
Commit
de4d9ed
·
verified ·
1 Parent(s): 754082a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -140
app.py CHANGED
@@ -5,145 +5,35 @@ from openai import OpenAI
5
  from prompts.initial_prompt import INITIAL_PROMPT
6
  from prompts.main_prompt import MAIN_PROMPT
7
 
8
- # .env 파일에서 OPENAI_API_KEY 로드
9
  if os.path.exists(".env"):
10
- load_dotenv(".env")Module 10: Developing Conceptual Understanding through Tables and Proportional Reasoning
11
-
12
- Task Introduction
13
- "Welcome to the final module in this series! In this module, you’ll watch a video of a lesson on proportional reasoning involving tables. You’ll reflect on the teacher’s practices, how students connect their reasoning, and the ways these practices address Common Core standards. Let’s dive in!"
14
-
15
- Video:
16
- "Watch the video provided at this link. Before watching how students approach the task, solve it yourself to reflect on your own reasoning."
17
-
18
- 🚀 **Pre-Video Task Prompt**
19
- Before watching the video, let's start by solving the problem.
20
-
21
- 1️⃣ **How did you approach solving the problem?**
22
- - What strategies did you use?
23
- - Did you recognize proportional relationships within the table?
24
-
25
- 🛠 **Hints if Needed**:
26
- - Think about the relationships both horizontally (within rows) and vertically (between columns) in the table.
27
- - How might unit rate play a role in reasoning proportionally?
28
-
29
- After you solve the problem, **let me know**, and we’ll move to the next step!
30
-
31
- ---
32
-
33
- 📖 **Post-Video Reflection Prompts**
34
- Now that you’ve watched the video and solved the problem, let’s reflect on different aspects of the lesson **one by one**:
35
-
36
- ### **Step 1: Observing Creativity-Directed Practices**
37
- 🔹 What creativity-directed practices did you notice the teacher implementing during the lesson?
38
- 🔹 Reflect on how these practices supported students’ reasoning and collaboration.
39
-
40
- 💡 **Hints if Needed**:
41
- - Consider whether the teacher encouraged mathematical connections, collaborative problem-solving, or extended students’ thinking beyond the unit rate.
42
-
43
- ✅ When you're ready, **share your thoughts**, and we'll move to the next reflection.
44
-
45
- ---
46
-
47
- ### **Step 2: Student Reasoning and Connections**
48
- 🔹 How did students connect the relationship between price and container size?
49
- 🔹 How did their reasoning evolve as they worked through the task?
50
-
51
- 💡 **Hints if Needed**:
52
- - Did students start with the given information (e.g., the 24-ounce container costing $3)?
53
- - How did they use this information to reason proportionally?
54
-
55
- ✅ **Once you respond, we’ll move on!**
56
-
57
- ---
58
-
59
- ### **Step 3: Teacher Actions in Small Groups**
60
- 🔹 How did the teacher’s actions during small group interactions reflect the students’ reasoning?
61
- 🔹 How did the teacher use these interactions to inform whole-class discussions?
62
-
63
- 💡 **Hints if Needed**:
64
- - Think about how the teacher listened to students’ reasoning and used their ideas to guide the next steps.
65
- - What types of questions did the teacher ask?
66
-
67
- ✅ **Once you're ready, let’s move forward!**
68
-
69
- ---
70
-
71
- ### **Step 4: Initial Prompts and Sense-Making**
72
- 🔹 How did the teacher prompt students to initially make sense of the task?
73
- 🔹 What role did these prompts play in guiding students’ reasoning?
74
-
75
- 💡 **Hints if Needed**:
76
- - Did the teacher ask open-ended questions?
77
- - How did these prompts help students engage with the task?
78
-
79
- ✅ **Share your response, and we’ll continue!**
80
-
81
- ---
82
-
83
- ### **Step 5: Common Core Practice Standards**
84
- 🔹 What Common Core practice standards do you think the teacher emphasized during the lesson?
85
- 🔹 Choose four and explain how you observed these practices in action.
86
-
87
- 💡 **Hints if Needed**:
88
- - Consider whether the teacher emphasized reasoning, collaboration, or modeling with mathematics.
89
- - How did the students demonstrate these practices?
90
-
91
- ✅ **When you’re ready, let’s move to the final steps!**
92
-
93
- ---
94
-
95
- ### **Step 6: Problem Posing Activity**
96
- 📝 Based on what you observed, **pose a problem** that encourages students to use visuals and proportional reasoning.
97
- 🔹 What real-world context will you use?
98
- 🔹 How will students use visuals like bar models or tables to represent proportional relationships?
99
- 🔹 Does your problem encourage multiple solution paths?
100
-
101
- 💡 **Hints if Needed**:
102
- - Try to design a problem where students can approach it differently but still apply proportional reasoning.
103
-
104
- ✅ **Once you've created your problem, let me know!**
105
-
106
- ---
107
-
108
- ### **Step 7: Summary and Final Reflection**
109
- 📚 **What’s one change you will make in your own teaching based on this module?**
110
- Reflect on a specific strategy, question type, or approach to representation that you want to implement.
111
-
112
- 💡 **Encouraging Closing Statement**:
113
- "Great work completing all the modules! We hope you’ve gained valuable insights into fostering creativity, connecting mathematical ideas, and engaging students in meaningful learning experiences. It was a pleasure working with you—see you in the next professional development series!" 🎉
114
-
115
 
116
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
117
-
118
  client = OpenAI(api_key=OPENAI_API_KEY)
119
 
120
-
121
-
122
  def gpt_call(history, user_message,
123
  model="gpt-4o-mini",
124
- max_tokens=512,
125
  temperature=0.7,
126
  top_p=0.95):
127
  """
128
- OpenAI ChatCompletion API 통해 답변을 생성하는 함수.
129
  - history: [(user_text, assistant_text), ...]
130
- - user_message: 사용자가 방금 입력한 메시지
131
  """
132
- # 1) 시스템 메시지(=MAIN_PROMPT)를 가장 앞에 추가
133
  messages = [{"role": "system", "content": MAIN_PROMPT}]
134
 
135
- # 2) 기존 대화 기록(history)을 OpenAI 형식으로 변환
136
- # user_text -> 'user' / assistant_text -> 'assistant'
137
  for user_text, assistant_text in history:
138
  if user_text:
139
  messages.append({"role": "user", "content": user_text})
140
  if assistant_text:
141
  messages.append({"role": "assistant", "content": assistant_text})
142
 
143
- # 3) 마지막에 이번 사용자의 입력을 추가
144
  messages.append({"role": "user", "content": user_message})
145
-
146
- # 4) OpenAI API 호출
147
  completion = client.chat.completions.create(
148
  model=model,
149
  messages=messages,
@@ -151,63 +41,65 @@ def gpt_call(history, user_message,
151
  temperature=temperature,
152
  top_p=top_p
153
  )
154
- return completion.choices[0].message.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
 
156
  def respond(user_message, history):
157
  """
158
- Gradio 상에서 submit할 호출되는 함수
159
- - user_message: 사용자가 방금 친 메시지
160
- - history: 기존 (user, assistant) 튜플 리스트
161
  """
162
- # 사용자가 빈 문자열을 보냈다면 아무 일도 하지 않음
163
  if not user_message:
164
  return "", history
165
 
166
- # GPT 모델로부터 응답을 받음
167
  assistant_reply = gpt_call(history, user_message)
168
-
169
- # history에 (user, assistant) 쌍 추가
170
  history.append((user_message, assistant_reply))
171
-
172
- # Gradio에서는 (새로 비워질 입력창, 갱신된 history)를 반환
173
  return "", history
174
 
175
  ##############################
176
  # Gradio Blocks UI
177
  ##############################
178
  with gr.Blocks() as demo:
179
- gr.Markdown("## Simple Chat Interface")
180
 
181
- # Chatbot 초기 상태를 설정
182
- # 첫 번째 메시지는 (user="", assistant=INITIAL_PROMPT) 형태로 넣어
183
- # 화면상에서 'assistant'가 INITIAL_PROMPT를 말한 것처럼 보이게 함
184
  chatbot = gr.Chatbot(
185
- value=[("", INITIAL_PROMPT)], # (user, assistant)
186
- height=500
187
  )
188
 
189
- # (user, assistant) 쌍을 저장할 히스토리 상태
190
- # 여기서도 동일한 초기 상태를 넣어줌
191
  state_history = gr.State([("", INITIAL_PROMPT)])
192
 
193
- # 사용자 입력
194
  user_input = gr.Textbox(
195
  placeholder="Type your message here...",
196
  label="Your Input"
197
  )
198
 
199
- # 입력이 submit되면 respond() 호출 → 출력은 (새 입력창, 갱신된 chatbot)
200
  user_input.submit(
201
  respond,
202
  inputs=[user_input, state_history],
203
  outputs=[user_input, chatbot]
204
  ).then(
205
- # respond 끝난 뒤, 최신 history를 state_history에 반영
206
  fn=lambda _, h: h,
207
  inputs=[user_input, chatbot],
208
  outputs=[state_history]
209
  )
210
 
211
- # 메인 실행
212
  if __name__ == "__main__":
213
  demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
 
5
  from prompts.initial_prompt import INITIAL_PROMPT
6
  from prompts.main_prompt import MAIN_PROMPT
7
 
8
+ # Load OpenAI API Key from .env file
9
  if os.path.exists(".env"):
10
+ load_dotenv(".env")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
 
13
  client = OpenAI(api_key=OPENAI_API_KEY)
14
 
 
 
15
  def gpt_call(history, user_message,
16
  model="gpt-4o-mini",
17
+ max_tokens=1024,
18
  temperature=0.7,
19
  top_p=0.95):
20
  """
21
+ Calls OpenAI Chat API to generate responses.
22
  - history: [(user_text, assistant_text), ...]
23
+ - user_message: latest message from user
24
  """
 
25
  messages = [{"role": "system", "content": MAIN_PROMPT}]
26
 
27
+ # Add conversation history
 
28
  for user_text, assistant_text in history:
29
  if user_text:
30
  messages.append({"role": "user", "content": user_text})
31
  if assistant_text:
32
  messages.append({"role": "assistant", "content": assistant_text})
33
 
 
34
  messages.append({"role": "user", "content": user_message})
35
+
36
+ # OpenAI API Call
37
  completion = client.chat.completions.create(
38
  model=model,
39
  messages=messages,
 
41
  temperature=temperature,
42
  top_p=top_p
43
  )
44
+
45
+ response = completion.choices[0].message.content
46
+
47
+ # Provide step-by-step responses for each section
48
+ if "pre-video" in user_message.lower():
49
+ response = "Great! Before watching the video, solve the problem first. How did you approach solving it?"
50
+
51
+ if "post-video" in user_message.lower():
52
+ response = "Now that you’ve watched the video, let’s start by reflecting on the teacher’s creativity-directed practices. What stood out to you the most?"
53
+
54
+ if "common core" in user_message.lower():
55
+ response = "What Common Core practice standards do you think the teacher emphasized during the lesson? Choose four and explain how you observed these practices in action."
56
+
57
+ if "problem posing" in user_message.lower():
58
+ response = "Based on what you observed, pose a problem that encourages students to use visuals and proportional reasoning. What real-world context will you use?"
59
+
60
+ if "summary" in user_message.lower():
61
+ response = "What’s one change you will make in your own teaching based on this module? Reflect on a specific strategy or approach."
62
+
63
+ return response
64
 
65
  def respond(user_message, history):
66
  """
67
+ Handles user input and chatbot responses.
 
 
68
  """
 
69
  if not user_message:
70
  return "", history
71
 
 
72
  assistant_reply = gpt_call(history, user_message)
 
 
73
  history.append((user_message, assistant_reply))
 
 
74
  return "", history
75
 
76
  ##############################
77
  # Gradio Blocks UI
78
  ##############################
79
  with gr.Blocks() as demo:
80
+ gr.Markdown("## AI-Guided Math PD Chatbot")
81
 
 
 
 
82
  chatbot = gr.Chatbot(
83
+ value=[("", INITIAL_PROMPT)],
84
+ height=600
85
  )
86
 
 
 
87
  state_history = gr.State([("", INITIAL_PROMPT)])
88
 
 
89
  user_input = gr.Textbox(
90
  placeholder="Type your message here...",
91
  label="Your Input"
92
  )
93
 
 
94
  user_input.submit(
95
  respond,
96
  inputs=[user_input, state_history],
97
  outputs=[user_input, chatbot]
98
  ).then(
 
99
  fn=lambda _, h: h,
100
  inputs=[user_input, chatbot],
101
  outputs=[state_history]
102
  )
103
 
 
104
  if __name__ == "__main__":
105
  demo.launch(server_name="0.0.0.0", server_port=7860, share=True)