SarahXia0405 commited on
Commit
b3996b4
·
verified ·
1 Parent(s): 73ad6fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -19
app.py CHANGED
@@ -1,6 +1,6 @@
1
- import gradio as gr
2
- import os
3
  from typing import List, Dict, Tuple, Optional
 
4
 
5
  from config import (
6
  DEFAULT_MODEL,
@@ -27,10 +27,9 @@ from rag_engine import (
27
  from syllabus_utils import extract_course_topics_from_file
28
 
29
  # ================== Assets (图片路径配置) ==================
30
- # 根据你的截图,文件名大小写必须严格一致
31
  HANBRIDGE_LOGO_PATH = "hanbridge_logo.png"
32
- CLARE_LOGO_PATH = "clare_mascot.png"
33
- CLARE_RUN_PATH = "Clare_Run.png" # 注意:你的截图里这个文件首字母是大写的
34
 
35
  # ================== CSS (样式调整) ==================
36
  CUSTOM_CSS = """
@@ -44,7 +43,6 @@ CUSTOM_CSS = """
44
  border-bottom: 2px solid #f3f4f6;
45
  margin-bottom: 15px;
46
  }
47
-
48
  /* User Guide 链接样式 */
49
  #user-guide-links button {
50
  background: transparent !important; border: none !important; box-shadow: none !important;
@@ -52,14 +50,13 @@ CUSTOM_CSS = """
52
  color: #004a99 !important; text-decoration: underline; font-size: 0.8rem;
53
  }
54
  #user-guide-links button:hover { color: #002b66 !important; }
55
-
56
  /* Memory Line 区域样式 */
57
  .memory-line-box {
58
  border: 1px solid #e5e7eb;
59
  padding: 12px;
60
  border-radius: 8px;
61
  background-color: #f9fafb;
62
- height: 100%;
63
  }
64
  """
65
 
@@ -82,7 +79,6 @@ def get_user_guide_section(section_key: str) -> str:
82
 
83
  # ================== Gradio App ==================
84
 
85
- # 【重要修正】:这里不要放 allowed_paths,否则会报错
86
  with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS) as demo:
87
 
88
  # 状态变量
@@ -92,11 +88,12 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
92
  rag_chunks_state = gr.State([])
93
 
94
  # ============ 1. 顶部 Header (全宽布局) ============
 
95
  gr.HTML(
96
  f"""
97
  <div class="header-container">
98
  <div style="display:flex; align-items:center; gap: 20px;">
99
- <img src="/file/{CLARE_LOGO_PATH}" style="height: 75px; object-fit: contain;">
100
  <div style="display:flex; flex-direction:column;">
101
  <div style="font-size: 32px; font-weight: 800; line-height: 1.1; color: #000;">
102
  Clare
@@ -107,9 +104,8 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
107
  </div>
108
  </div>
109
  </div>
110
-
111
  <div style="text-align: right;">
112
- <img src="/file/{HANBRIDGE_LOGO_PATH}" style="height: 55px; object-fit: contain; margin-bottom: 5px;">
113
  <div style="font-size: 12px; color: #666;">
114
  🎓 (Student Name) (Student Email/ID)
115
  </div>
@@ -123,6 +119,7 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
123
 
124
  # ============ [左侧] 控制面板 Sidebar ============
125
  with gr.Column(scale=1, min_width=200):
 
126
  clear_btn = gr.Button("Reset Conversation", variant="stop")
127
 
128
  gr.Markdown("### Model Settings")
@@ -173,7 +170,7 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
173
 
174
  # --- Memory Line (包含 CLARE_RUN_PATH) ---
175
  with gr.Column(scale=1):
176
- # 使用 HTML 渲染带图片的进度条,src="/file/..." 是关键
177
  gr.HTML(
178
  f"""
179
  <div class="memory-line-box">
@@ -184,9 +181,8 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
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;">
@@ -196,8 +192,10 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
196
  </div>
197
  """
198
  )
199
- # Review 按钮放在 HTML 下方
200
  review_btn = gr.Button("Review Now", size="sm", variant="primary")
 
 
201
  session_status = gr.Markdown(visible=False)
202
 
203
  # --- Chat Interface ---
@@ -213,7 +211,7 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
213
  chatbot = gr.Chatbot(
214
  label="",
215
  height=450,
216
- avatar_images=(None, CLARE_LOGO_PATH),
217
  show_label=False,
218
  bubble_full_width=False
219
  )
@@ -229,6 +227,7 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
229
  # ============ [右侧] 功能栏 Right Sidebar ============
230
  with gr.Column(scale=1, min_width=180):
231
  gr.Markdown("### Actions")
 
232
  export_btn = gr.Button("Export Conversation", size="sm")
233
  quiz_btn = gr.Button("Let's Try (Micro-Quiz)", size="sm")
234
  summary_btn = gr.Button("Summarization", size="sm")
@@ -243,6 +242,7 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
243
 
244
  # ================== Logic Bindings ==================
245
 
 
246
  def update_course_and_rag(file, doc_type_val):
247
  topics = extract_course_topics_from_file(file, doc_type_val)
248
  rag_chunks = build_rag_chunks_from_file(file, doc_type_val)
@@ -255,6 +255,7 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
255
  outputs=[course_outline_state, rag_chunks_state, session_status],
256
  )
257
 
 
258
  def respond(message, chat_history, course_outline, weaknesses, cognitive_state, rag_chunks, model, lang, mode, doc_type_val):
259
  resolved_lang = detect_language(message or "", lang)
260
 
@@ -287,6 +288,7 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
287
  [user_input, chatbot, weakness_state, cognitive_state_state, session_status]
288
  )
289
 
 
290
  def on_export(chat_history, course_outline, mode, weaknesses, cognitive_state):
291
  return export_conversation(chat_history, course_outline, mode, weaknesses, cognitive_state)
292
 
@@ -314,6 +316,7 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
314
  [result_display]
315
  )
316
 
 
317
  def clear_all():
318
  empty_state = {"confusion": 0, "mastery": 0}
319
  default_status = render_session_status("Concept Explainer", [], empty_state)
@@ -326,6 +329,7 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
326
  queue=False
327
  )
328
 
 
329
  def show_guide(section):
330
  return get_user_guide_section(section)
331
 
@@ -335,5 +339,4 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
335
  ug_memory_line.click(lambda: show_guide("memory_line"), outputs=result_display)
336
 
337
  if __name__ == "__main__":
338
- # 【修正完成】:allowed_paths 必须放在这里
339
- demo.launch(allowed_paths=["."])
 
1
+ # app.py
 
2
  from typing import List, Dict, Tuple, Optional
3
+ import gradio as gr
4
 
5
  from config import (
6
  DEFAULT_MODEL,
 
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 = """
 
43
  border-bottom: 2px solid #f3f4f6;
44
  margin-bottom: 15px;
45
  }
 
46
  /* User Guide 链接样式 */
47
  #user-guide-links button {
48
  background: transparent !important; border: none !important; box-shadow: none !important;
 
50
  color: #004a99 !important; text-decoration: underline; font-size: 0.8rem;
51
  }
52
  #user-guide-links button:hover { color: #002b66 !important; }
 
53
  /* Memory Line 区域样式 */
54
  .memory-line-box {
55
  border: 1px solid #e5e7eb;
56
  padding: 12px;
57
  border-radius: 8px;
58
  background-color: #f9fafb;
59
+ height: 100%; /* 填满高度 */
60
  }
61
  """
62
 
 
79
 
80
  # ================== Gradio App ==================
81
 
 
82
  with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS) as demo:
83
 
84
  # 状态变量
 
88
  rag_chunks_state = gr.State([])
89
 
90
  # ============ 1. 顶部 Header (全宽布局) ============
91
+ # 包含 CLARE_LOGO_PATH (左) 和 HANBRIDGE_LOGO_PATH (右)
92
  gr.HTML(
93
  f"""
94
  <div class="header-container">
95
  <div style="display:flex; align-items:center; gap: 20px;">
96
+ <img src="file={CLARE_LOGO_PATH}" style="height: 75px; object-fit: contain;">
97
  <div style="display:flex; flex-direction:column;">
98
  <div style="font-size: 32px; font-weight: 800; line-height: 1.1; color: #000;">
99
  Clare
 
104
  </div>
105
  </div>
106
  </div>
 
107
  <div style="text-align: right;">
108
+ <img src="file/{HANBRIDGE_LOGO_PATH}" style="height: 55px; object-fit: contain; margin-bottom: 5px;">
109
  <div style="font-size: 12px; color: #666;">
110
  🎓 (Student Name) (Student Email/ID)
111
  </div>
 
119
 
120
  # ============ [左侧] 控制面板 Sidebar ============
121
  with gr.Column(scale=1, min_width=200):
122
+ # Reset 按钮
123
  clear_btn = gr.Button("Reset Conversation", variant="stop")
124
 
125
  gr.Markdown("### Model Settings")
 
170
 
171
  # --- Memory Line (包含 CLARE_RUN_PATH) ---
172
  with gr.Column(scale=1):
173
+ # 使用 HTML 渲染带图片的进度条
174
  gr.HTML(
175
  f"""
176
  <div class="memory-line-box">
 
181
  <div style="position: relative; height: 35px; margin-top: 15px; margin-bottom: 5px;">
182
  <div style="position: absolute; bottom: 5px; left: 0; width: 100%; height: 8px; background-color: #e5e7eb; border-radius: 4px;"></div>
183
  <div style="position: absolute; bottom: 5px; left: 0; width: 40%; height: 8px; background-color: #8B1A1A; border-radius: 4px 0 0 4px;"></div>
184
+ <img src="file/{CLARE_RUN_PATH}" style="position: absolute; left: 36%; bottom: 8px; height: 35px; z-index: 10;">
185
  </div>
 
186
  <div style="display:flex; justify-content:space-between; align-items:center; margin-top:8px;">
187
  <div style="font-size: 12px; color: #666;">Next Review: T+7</div>
188
  <div style="font-size: 12px; color: #004a99; text-decoration:underline; cursor:pointer;">
 
192
  </div>
193
  """
194
  )
195
+ # 真实的 Review 按钮放在 HTML 下方
196
  review_btn = gr.Button("Review Now", size="sm", variant="primary")
197
+
198
+ # 隐藏的 Session Status 用于逻辑传递
199
  session_status = gr.Markdown(visible=False)
200
 
201
  # --- Chat Interface ---
 
211
  chatbot = gr.Chatbot(
212
  label="",
213
  height=450,
214
+ avatar_images=(None, CLARE_LOGO_PATH), # 聊天框内的头像
215
  show_label=False,
216
  bubble_full_width=False
217
  )
 
227
  # ============ [右侧] 功能栏 Right Sidebar ============
228
  with gr.Column(scale=1, min_width=180):
229
  gr.Markdown("### Actions")
230
+
231
  export_btn = gr.Button("Export Conversation", size="sm")
232
  quiz_btn = gr.Button("Let's Try (Micro-Quiz)", size="sm")
233
  summary_btn = gr.Button("Summarization", size="sm")
 
242
 
243
  # ================== Logic Bindings ==================
244
 
245
+ # 1. File Upload Logic
246
  def update_course_and_rag(file, doc_type_val):
247
  topics = extract_course_topics_from_file(file, doc_type_val)
248
  rag_chunks = build_rag_chunks_from_file(file, doc_type_val)
 
255
  outputs=[course_outline_state, rag_chunks_state, session_status],
256
  )
257
 
258
+ # 2. Chat Logic
259
  def respond(message, chat_history, course_outline, weaknesses, cognitive_state, rag_chunks, model, lang, mode, doc_type_val):
260
  resolved_lang = detect_language(message or "", lang)
261
 
 
288
  [user_input, chatbot, weakness_state, cognitive_state_state, session_status]
289
  )
290
 
291
+ # 3. Action Buttons Logic
292
  def on_export(chat_history, course_outline, mode, weaknesses, cognitive_state):
293
  return export_conversation(chat_history, course_outline, mode, weaknesses, cognitive_state)
294
 
 
316
  [result_display]
317
  )
318
 
319
+ # 4. Reset Logic
320
  def clear_all():
321
  empty_state = {"confusion": 0, "mastery": 0}
322
  default_status = render_session_status("Concept Explainer", [], empty_state)
 
329
  queue=False
330
  )
331
 
332
+ # 5. User Guide Logic
333
  def show_guide(section):
334
  return get_user_guide_section(section)
335
 
 
339
  ug_memory_line.click(lambda: show_guide("memory_line"), outputs=result_display)
340
 
341
  if __name__ == "__main__":
342
+ demo.launch()