SarahXia0405 commited on
Commit
6e97a5c
·
verified ·
1 Parent(s): ce485af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -96
app.py CHANGED
@@ -26,31 +26,45 @@ from rag_engine import (
26
  )
27
  from syllabus_utils import extract_course_topics_from_file
28
 
29
- # ================== Assets ==================
30
  HANBRIDGE_LOGO_PATH = "hanbridge_logo.png"
31
  CLARE_LOGO_PATH = "clare_mascot.png"
32
- # CLARE_RUN_PATH = "Clare_Run.png"
33
 
34
- # ================== CSS ==================
35
- # 增加了一些针对布局微调的 CSS
36
  CUSTOM_CSS = """
37
- #user-guide-panel h2 { font-size: 1rem; margin-bottom: 0.35rem; }
38
- #user-guide-panel h3 { font-size: 0.9rem; margin-bottom: 0.25rem; }
39
- #user-guide-panel p, #user-guide-panel li { font-size: 0.8rem; }
 
 
 
 
 
 
 
 
 
40
  #user-guide-links button {
41
- background: transparent !important; border: none !important; box-shadow: none !important;
42
- padding: 2px 0 !important; margin: 1px 0 !important; text-align: left !important;
43
- color: #004a99 !important; text-decoration: underline; font-size: 0.8rem;
44
  }
45
  #user-guide-links button:hover { color: #002b66 !important; }
46
 
47
- /* 调整左侧边栏紧凑度 */
48
- .left-sidebar-group { border: 1px solid #e5e7eb; padding: 10px; border-radius: 8px; }
 
 
 
 
 
 
49
  """
50
 
51
  # ========= Clare User Guide Content (保持原样) =========
52
  USER_GUIDE_SECTIONS = {
53
- "getting_started": "## Getting started\n\nWelcome to **Clare**...", # (为了节省篇幅,此处省略具体文本,保持你原代码内容即可)
54
  "mode_definition": "## Mode Definition\n\nClare offers different teaching modes...",
55
  "how_clare_works": "## How Clare Works\n\nClare combines course context...",
56
  "memory_line": "## What is Memory Line?\n\nMemory Line is a visualization...",
@@ -75,16 +89,43 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
75
  cognitive_state_state = gr.State({"confusion": 0, "mastery": 0})
76
  rag_chunks_state = gr.State([])
77
 
78
- # ============ 整体布局:左(1) - 中(3) - 右(1) ============
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  with gr.Row():
80
 
81
  # ============ [左侧] 控制面板 Sidebar ============
82
  with gr.Column(scale=1, min_width=200):
83
- # 1. Reset 按钮 (深色/红色强调)
84
  clear_btn = gr.Button("Reset Conversation", variant="stop")
85
 
86
  gr.Markdown("### Model Settings")
87
- # 2. 模型与语言
88
  model_name = gr.Textbox(label="Model", value=DEFAULT_MODEL, lines=1)
89
  language_preference = gr.Radio(
90
  choices=["Auto", "English", "简体中文"],
@@ -92,7 +133,6 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
92
  label="Language"
93
  )
94
 
95
- # 3. 学习模式 (Learning Mode)
96
  learning_mode = gr.Radio(
97
  choices=LEARNING_MODES,
98
  value="Concept Explainer",
@@ -100,16 +140,13 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
100
  info="Select style"
101
  )
102
 
103
- # 4. User Guide (折叠菜单)
104
  with gr.Accordion("User Guide", open=False):
105
  with gr.Column(elem_id="user-guide-links"):
106
  ug_getting_started = gr.Button("Getting started")
107
  ug_mode_def = gr.Button("Mode Definition")
108
  ug_how_works = gr.Button("How Clare Works")
109
  ug_memory_line = gr.Button("What is Memory Line")
110
- # ... 可以添加更多 User Guide 按钮
111
 
112
- # 5. 系统设置/退出 (底部)
113
  with gr.Group():
114
  gr.Button("System Settings", variant="secondary")
115
  gr.Button("Log Out", variant="secondary")
@@ -117,55 +154,55 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
117
  # ============ [中间] 主内容区 Center Main ============
118
  with gr.Column(scale=3):
119
 
120
- # 1. Header (Clare & Hanbridge Logos)
121
- gr.Markdown(
122
- f"""
123
- <div style="display:flex;align-items:center;justify-content:space-between; padding-bottom:10px; border-bottom: 2px solid #eee;">
124
- <div style="display:flex;align-items:center;gap:15px;">
125
- <img src="file/{CLARE_LOGO_PATH}" style="height:60px;">
126
- <div>
127
- <h1 style="margin:0;font-size:24px;">Clare</h1>
128
- <p style="margin:0;font-size:14px;color:#666;">Your Personalized AI Tutor</p>
129
- <i style="font-size:12px;color:#888;">Personalized guidance, review, and intelligent reinforcement</i>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  </div>
131
- </div>
132
- <img src="file/{HANBRIDGE_LOGO_PATH}" style="height:50px;">
133
- </div>
134
- """
135
- )
136
-
137
- # 2. 上传区与 Memory Line 并排
138
- with gr.Group():
139
- with gr.Row():
140
- # 左半部分:上传文件
141
- with gr.Column(scale=1):
142
- syllabus_file = gr.File(
143
- label="Upload file (.docx/.pdf/.pptx)",
144
- file_types=[".docx", ".pdf", ".pptx"],
145
- file_count="single",
146
- height=100
147
- )
148
- doc_type = gr.Dropdown(
149
- choices=DOC_TYPES,
150
- value="Syllabus",
151
- label="File type",
152
- container=False
153
- )
154
 
155
- # 右半部分:Memory Line 面板
156
- with gr.Column(scale=1):
157
- gr.Markdown("**Memory Line** 🧠")
158
- # 这里可以用 HTML 模拟一个进度条或 Review 状态
159
- session_status = gr.Markdown(
160
- value=render_session_status("Concept Explainer", [], {"confusion": 0, "mastery": 0}),
161
- elem_id="memory-line-status"
162
- )
163
- with gr.Row():
164
- review_btn = gr.Button("Review Now", size="sm", variant="primary")
165
- report_btn = gr.Button("Learning Progress Report ⬇️", size="sm")
166
-
167
- # 3. Chat Interface
168
- # 为了模仿截图中的 Instructional Text,可以在 Chatbot 上方放提示
169
  gr.Markdown(
170
  """
171
  <div style="background-color:#f9fafb; padding:10px; border-radius:5px; margin-top:10px; font-size:0.9em; color:#555;">
@@ -178,7 +215,7 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
178
  chatbot = gr.Chatbot(
179
  label="",
180
  height=450,
181
- avatar_images=(None, CLARE_LOGO_PATH),
182
  show_label=False,
183
  bubble_full_width=False
184
  )
@@ -195,25 +232,17 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
195
  with gr.Column(scale=1, min_width=180):
196
  gr.Markdown("### Actions")
197
 
198
- # 这里的按钮设计为全宽
199
  export_btn = gr.Button("Export Conversation", size="sm")
200
  quiz_btn = gr.Button("Let's Try (Micro-Quiz)", size="sm")
201
  summary_btn = gr.Button("Summarization", size="sm")
202
 
203
  gr.Markdown("### Results")
204
- # 统一结果展示区(模仿图中右下角的空白区域)
205
- # 用一个 Tab 或者一个通用的 Box 来展示 Export/Quiz/Summary 的结果
206
  result_display = gr.Textbox(
207
  label="Generated Content",
208
  lines=15,
209
  placeholder="Results from Export, Quiz, or Summary will appear here...",
210
  show_label=False
211
  )
212
-
213
- # User Guide 的内容显示框 (隐藏或作为 Modal 更好,但在现有结构下可以放下面或者用 Modal 替代)
214
- # 为了符合截图设计,User Guide 点击后可能是在中间对话框显示,或者弹出。
215
- # 这里暂时保留一个隐藏的 Markdown 区域用于逻辑兼容,或者让结果显示在 result_display 中。
216
- user_guide_display = gr.Markdown(visible=False)
217
 
218
  # ================== Logic Bindings ==================
219
 
@@ -221,7 +250,6 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
221
  def update_course_and_rag(file, doc_type_val):
222
  topics = extract_course_topics_from_file(file, doc_type_val)
223
  rag_chunks = build_rag_chunks_from_file(file, doc_type_val)
224
- # 更新 Memory Line 的状态显示 (示例)
225
  status_md = f"✅ **File Loaded:** {doc_type_val}\n\nAnalyzing content for Memory Line..."
226
  return topics, rag_chunks, status_md
227
 
@@ -231,25 +259,17 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
231
  outputs=[course_outline_state, rag_chunks_state, session_status],
232
  )
233
 
234
- # 2. Chat Logic (Standard)
235
  def respond(message, chat_history, course_outline, weaknesses, cognitive_state, rag_chunks, model, lang, mode, doc_type_val):
236
- # ... (此处逻辑复用你原有的代码逻辑) ...
237
- # 为了简洁,这里直接调用原有的 chat_with_clare 等函数
238
- # 注意:你需要确保 import 了这些函数
239
-
240
  resolved_lang = detect_language(message or "", lang)
241
 
242
  if not message or not message.strip():
243
  return "", chat_history, weaknesses, cognitive_state, session_status.value
244
 
245
- # 更新状态
246
  weaknesses = update_weaknesses_from_message(message, weaknesses or [])
247
  cognitive_state = update_cognitive_state_from_message(message, cognitive_state)
248
-
249
- # RAG Check
250
  rag_context = retrieve_relevant_chunks(message, rag_chunks or [])
251
 
252
- # Chat
253
  answer, new_history = chat_with_clare(
254
  message=message,
255
  history=chat_history,
@@ -263,9 +283,7 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
263
  rag_context=rag_context
264
  )
265
 
266
- # 更新 Memory Line 状态文本 (模拟)
267
  new_status = render_session_status(mode, weaknesses, cognitive_state)
268
-
269
  return "", new_history, weaknesses, cognitive_state, new_status
270
 
271
  user_input.submit(
@@ -274,9 +292,7 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
274
  [user_input, chatbot, weakness_state, cognitive_state_state, session_status]
275
  )
276
 
277
- # 3. Action Buttons (Right Sidebar) -> Output to `result_display`
278
-
279
- # Export
280
  def on_export(chat_history, course_outline, mode, weaknesses, cognitive_state):
281
  return export_conversation(chat_history, course_outline, mode, weaknesses, cognitive_state)
282
 
@@ -286,7 +302,6 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
286
  [result_display]
287
  )
288
 
289
- # Quiz
290
  def on_quiz(chat_history, course_outline, weaknesses, cognitive_state, model, lang):
291
  return generate_quiz_from_history(chat_history, course_outline, weaknesses, cognitive_state, model, lang)
292
 
@@ -296,7 +311,6 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
296
  [result_display]
297
  )
298
 
299
- # Summary
300
  def on_summary(chat_history, course_outline, weaknesses, cognitive_state, model, lang):
301
  return summarize_conversation(chat_history, course_outline, weaknesses, cognitive_state, model, lang)
302
 
@@ -319,9 +333,7 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
319
  queue=False
320
  )
321
 
322
- # 5. User Guide Links (Logic to show text in Result box or popup)
323
- # 为了简化交互,点击 User Guide 按钮时,将内容显示在右侧 Result 框或者中间 Chat
324
- # 这里示例显示在右侧 Result 框
325
  def show_guide(section):
326
  return get_user_guide_section(section)
327
 
 
26
  )
27
  from syllabus_utils import extract_course_topics_from_file
28
 
29
+ # ================== Assets (图片路径配置) ==================
30
  HANBRIDGE_LOGO_PATH = "hanbridge_logo.png"
31
  CLARE_LOGO_PATH = "clare_mascot.png"
32
+ CLARE_RUN_PATH = "Clare_Run.png" # 已取消注释
33
 
34
+ # ================== CSS (样式调整) ==================
 
35
  CUSTOM_CSS = """
36
+ /* 顶部 Header 容器样式 */
37
+ .header-container {
38
+ display: flex;
39
+ align-items: center;
40
+ justify-content: space-between;
41
+ padding: 10px 20px;
42
+ background-color: #ffffff;
43
+ border-bottom: 2px solid #f3f4f6;
44
+ margin-bottom: 15px;
45
+ }
46
+
47
+ /* User Guide 链接样式 */
48
  #user-guide-links button {
49
+ background: transparent !important; border: none !important; box-shadow: none !important;
50
+ padding: 2px 0 !important; margin: 1px 0 !important; text-align: left !important;
51
+ color: #004a99 !important; text-decoration: underline; font-size: 0.8rem;
52
  }
53
  #user-guide-links button:hover { color: #002b66 !important; }
54
 
55
+ /* Memory Line 区域样式 */
56
+ .memory-line-box {
57
+ border: 1px solid #e5e7eb;
58
+ padding: 12px;
59
+ border-radius: 8px;
60
+ background-color: #f9fafb;
61
+ height: 100%; /* 填满高度 */
62
+ }
63
  """
64
 
65
  # ========= Clare User Guide Content (保持原样) =========
66
  USER_GUIDE_SECTIONS = {
67
+ "getting_started": "## Getting started\n\nWelcome to **Clare**...",
68
  "mode_definition": "## Mode Definition\n\nClare offers different teaching modes...",
69
  "how_clare_works": "## How Clare Works\n\nClare combines course context...",
70
  "memory_line": "## What is Memory Line?\n\nMemory Line is a visualization...",
 
89
  cognitive_state_state = gr.State({"confusion": 0, "mastery": 0})
90
  rag_chunks_state = gr.State([])
91
 
92
+ # ============ 1. 顶部 Header (全宽布局) ============
93
+ # 包含 CLARE_LOGO_PATH (左) 和 HANBRIDGE_LOGO_PATH (右)
94
+ gr.HTML(
95
+ f"""
96
+ <div class="header-container">
97
+ <div style="display:flex; align-items:center; gap: 20px;">
98
+ <img src="file/{CLARE_LOGO_PATH}" style="height: 75px; object-fit: contain;">
99
+ <div style="display:flex; flex-direction:column;">
100
+ <div style="font-size: 32px; font-weight: 800; line-height: 1.1; color: #000;">
101
+ Clare
102
+ <span style="font-size: 18px; font-weight: 600; margin-left: 10px;">Your Personalized AI Tutor</span>
103
+ </div>
104
+ <div style="font-size: 14px; font-style: italic; color: #333; margin-top: 4px;">
105
+ Personalized guidance, review, and intelligent reinforcement - designed for how you learn
106
+ </div>
107
+ </div>
108
+ </div>
109
+
110
+ <div style="text-align: right;">
111
+ <img src="file/{HANBRIDGE_LOGO_PATH}" style="height: 55px; object-fit: contain; margin-bottom: 5px;">
112
+ <div style="font-size: 12px; color: #666;">
113
+ 🎓 (Student Name) (Student Email/ID)
114
+ </div>
115
+ </div>
116
+ </div>
117
+ """
118
+ )
119
+
120
+ # ============ 2. 主体分栏布局:左(1) - 中(3) - 右(1) ============
121
  with gr.Row():
122
 
123
  # ============ [左侧] 控制面板 Sidebar ============
124
  with gr.Column(scale=1, min_width=200):
125
+ # Reset 按钮
126
  clear_btn = gr.Button("Reset Conversation", variant="stop")
127
 
128
  gr.Markdown("### Model Settings")
 
129
  model_name = gr.Textbox(label="Model", value=DEFAULT_MODEL, lines=1)
130
  language_preference = gr.Radio(
131
  choices=["Auto", "English", "简体中文"],
 
133
  label="Language"
134
  )
135
 
 
136
  learning_mode = gr.Radio(
137
  choices=LEARNING_MODES,
138
  value="Concept Explainer",
 
140
  info="Select style"
141
  )
142
 
 
143
  with gr.Accordion("User Guide", open=False):
144
  with gr.Column(elem_id="user-guide-links"):
145
  ug_getting_started = gr.Button("Getting started")
146
  ug_mode_def = gr.Button("Mode Definition")
147
  ug_how_works = gr.Button("How Clare Works")
148
  ug_memory_line = gr.Button("What is Memory Line")
 
149
 
 
150
  with gr.Group():
151
  gr.Button("System Settings", variant="secondary")
152
  gr.Button("Log Out", variant="secondary")
 
154
  # ============ [中间] 主内容区 Center Main ============
155
  with gr.Column(scale=3):
156
 
157
+ # 上半部分:上传区(左) + Memory Line(右) 并排
158
+ with gr.Row():
159
+ # --- Upload File ---
160
+ with gr.Column(scale=1):
161
+ syllabus_file = gr.File(
162
+ label="Upload file (.docx/.pdf/.pptx)",
163
+ file_types=[".docx", ".pdf", ".pptx"],
164
+ file_count="single",
165
+ height=100
166
+ )
167
+ doc_type = gr.Dropdown(
168
+ choices=DOC_TYPES,
169
+ value="Syllabus",
170
+ label="File type",
171
+ container=False
172
+ )
173
+
174
+ # --- Memory Line (包含 CLARE_RUN_PATH) ---
175
+ with gr.Column(scale=1):
176
+ # 使用 HTML 渲染带图片的进度条
177
+ gr.HTML(
178
+ f"""
179
+ <div class="memory-line-box">
180
+ <div style="display:flex; justify-content:space-between; font-size:14px; margin-bottom:5px; font-weight:bold;">
181
+ <span>Memory Line <span style="font-weight:normal; color:#999; cursor:help;" title="Visualizes your spaced repetition progress">?</span></span>
182
+ </div>
183
+
184
+ <div style="position: relative; height: 35px; margin-top: 15px; margin-bottom: 5px;">
185
+ <div style="position: absolute; bottom: 5px; left: 0; width: 100%; height: 8px; background-color: #e5e7eb; border-radius: 4px;"></div>
186
+ <div style="position: absolute; bottom: 5px; left: 0; width: 40%; height: 8px; background-color: #8B1A1A; border-radius: 4px 0 0 4px;"></div>
187
+ <img src="file/{CLARE_RUN_PATH}" style="position: absolute; left: 36%; bottom: 8px; height: 35px; z-index: 10;">
188
+ </div>
189
+
190
+ <div style="display:flex; justify-content:space-between; align-items:center; margin-top:8px;">
191
+ <div style="font-size: 12px; color: #666;">Next Review: T+7</div>
192
+ <div style="font-size: 12px; color: #004a99; text-decoration:underline; cursor:pointer;">
193
+ Learning Progress Report ⬇️
194
+ </div>
195
+ </div>
196
  </div>
197
+ """
198
+ )
199
+ # 真实的 Review 按钮放在 HTML 下方
200
+ review_btn = gr.Button("Review Now", size="sm", variant="primary")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
 
202
+ # 隐藏的 Session Status 用于逻辑传递
203
+ session_status = gr.Markdown(visible=False)
204
+
205
+ # --- Chat Interface ---
 
 
 
 
 
 
 
 
 
 
206
  gr.Markdown(
207
  """
208
  <div style="background-color:#f9fafb; padding:10px; border-radius:5px; margin-top:10px; font-size:0.9em; color:#555;">
 
215
  chatbot = gr.Chatbot(
216
  label="",
217
  height=450,
218
+ avatar_images=(None, CLARE_LOGO_PATH), # 聊天框内的头像
219
  show_label=False,
220
  bubble_full_width=False
221
  )
 
232
  with gr.Column(scale=1, min_width=180):
233
  gr.Markdown("### Actions")
234
 
 
235
  export_btn = gr.Button("Export Conversation", size="sm")
236
  quiz_btn = gr.Button("Let's Try (Micro-Quiz)", size="sm")
237
  summary_btn = gr.Button("Summarization", size="sm")
238
 
239
  gr.Markdown("### Results")
 
 
240
  result_display = gr.Textbox(
241
  label="Generated Content",
242
  lines=15,
243
  placeholder="Results from Export, Quiz, or Summary will appear here...",
244
  show_label=False
245
  )
 
 
 
 
 
246
 
247
  # ================== Logic Bindings ==================
248
 
 
250
  def update_course_and_rag(file, doc_type_val):
251
  topics = extract_course_topics_from_file(file, doc_type_val)
252
  rag_chunks = build_rag_chunks_from_file(file, doc_type_val)
 
253
  status_md = f"✅ **File Loaded:** {doc_type_val}\n\nAnalyzing content for Memory Line..."
254
  return topics, rag_chunks, status_md
255
 
 
259
  outputs=[course_outline_state, rag_chunks_state, session_status],
260
  )
261
 
262
+ # 2. Chat Logic
263
  def respond(message, chat_history, course_outline, weaknesses, cognitive_state, rag_chunks, model, lang, mode, doc_type_val):
 
 
 
 
264
  resolved_lang = detect_language(message or "", lang)
265
 
266
  if not message or not message.strip():
267
  return "", chat_history, weaknesses, cognitive_state, session_status.value
268
 
 
269
  weaknesses = update_weaknesses_from_message(message, weaknesses or [])
270
  cognitive_state = update_cognitive_state_from_message(message, cognitive_state)
 
 
271
  rag_context = retrieve_relevant_chunks(message, rag_chunks or [])
272
 
 
273
  answer, new_history = chat_with_clare(
274
  message=message,
275
  history=chat_history,
 
283
  rag_context=rag_context
284
  )
285
 
 
286
  new_status = render_session_status(mode, weaknesses, cognitive_state)
 
287
  return "", new_history, weaknesses, cognitive_state, new_status
288
 
289
  user_input.submit(
 
292
  [user_input, chatbot, weakness_state, cognitive_state_state, session_status]
293
  )
294
 
295
+ # 3. Action Buttons Logic
 
 
296
  def on_export(chat_history, course_outline, mode, weaknesses, cognitive_state):
297
  return export_conversation(chat_history, course_outline, mode, weaknesses, cognitive_state)
298
 
 
302
  [result_display]
303
  )
304
 
 
305
  def on_quiz(chat_history, course_outline, weaknesses, cognitive_state, model, lang):
306
  return generate_quiz_from_history(chat_history, course_outline, weaknesses, cognitive_state, model, lang)
307
 
 
311
  [result_display]
312
  )
313
 
 
314
  def on_summary(chat_history, course_outline, weaknesses, cognitive_state, model, lang):
315
  return summarize_conversation(chat_history, course_outline, weaknesses, cognitive_state, model, lang)
316
 
 
333
  queue=False
334
  )
335
 
336
+ # 5. User Guide Logic
 
 
337
  def show_guide(section):
338
  return get_user_guide_section(section)
339