SarahXia0405 commited on
Commit
b17e745
·
verified ·
1 Parent(s): 66068c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -14
app.py CHANGED
@@ -1,6 +1,48 @@
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 +69,9 @@ from rag_engine import (
27
  from syllabus_utils import extract_course_topics_from_file
28
 
29
  # ================== Assets (图片路径配置) ==================
30
- # 确保文件名与你上传到 Hugging Face Space 的文件名完全一致(区分大小写)
31
  HANBRIDGE_LOGO_PATH = "hanbridge_logo.png"
32
- CLARE_LOGO_PATH = "clare_mascot.png" # 注意看截图里是 clare_mascot.png (小写c)
33
- CLARE_RUN_PATH = "Clare_Run.png" # 截图里是 Clare_Run.png (大写C)
34
 
35
  # ================== CSS (样式调整) ==================
36
  CUSTOM_CSS = """
@@ -44,7 +85,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 +92,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,8 +121,7 @@ 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, allowed_paths=["."]) as demo:
87
 
88
  # 状态变量
89
  course_outline_state = gr.State(DEFAULT_COURSE_TOPICS)
@@ -92,6 +130,7 @@ 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">
@@ -107,7 +146,6 @@ 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;">
@@ -123,6 +161,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")
@@ -186,7 +225,6 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
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 +234,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 ---
@@ -229,6 +269,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 +284,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 +297,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 +330,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 +358,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 +371,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,4 +381,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
- demo.launch()
 
1
+ Hugging Face's logo
2
+ Hugging Face
3
+ Models
4
+ Datasets
5
+ Spaces
6
+ Community
7
+ Docs
8
+ Pricing
9
+
10
+
11
+ Spaces:
12
+ SarahXia0405
13
+ /
14
+ hanbridge-clare-assistant-v2
15
+
16
+
17
+ like
18
+ 0
19
+
20
+ App
21
+ Files
22
+ Community
23
+ Settings
24
+ hanbridge-clare-assistant-v2
25
+ /
26
+ app.py
27
+
28
+ SarahXia0405's picture
29
+ SarahXia0405
30
+ Update app.py
31
+ 6e97a5c
32
+ verified
33
+ 7 minutes ago
34
+ raw
35
+
36
+ Copy download link
37
+ history
38
+ blame
39
+ edit
40
+ delete
41
+
42
+ 14.8 kB
43
+ # app.py
44
  from typing import List, Dict, Tuple, Optional
45
+ import gradio as gr
46
 
47
  from config import (
48
  DEFAULT_MODEL,
 
69
  from syllabus_utils import extract_course_topics_from_file
70
 
71
  # ================== Assets (图片路径配置) ==================
 
72
  HANBRIDGE_LOGO_PATH = "hanbridge_logo.png"
73
+ CLARE_LOGO_PATH = "clare_mascot.png"
74
+ CLARE_RUN_PATH = "Clare_Run.png" # 已取消注释
75
 
76
  # ================== CSS (样式调整) ==================
77
  CUSTOM_CSS = """
 
85
  border-bottom: 2px solid #f3f4f6;
86
  margin-bottom: 15px;
87
  }
 
88
  /* User Guide 链接样式 */
89
  #user-guide-links button {
90
  background: transparent !important; border: none !important; box-shadow: none !important;
 
92
  color: #004a99 !important; text-decoration: underline; font-size: 0.8rem;
93
  }
94
  #user-guide-links button:hover { color: #002b66 !important; }
 
95
  /* Memory Line 区域样式 */
96
  .memory-line-box {
97
  border: 1px solid #e5e7eb;
98
  padding: 12px;
99
  border-radius: 8px;
100
  background-color: #f9fafb;
101
+ height: 100%; /* 填满高度 */
102
  }
103
  """
104
 
 
121
 
122
  # ================== Gradio App ==================
123
 
124
+ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS) as demo:
 
125
 
126
  # 状态变量
127
  course_outline_state = gr.State(DEFAULT_COURSE_TOPICS)
 
130
  rag_chunks_state = gr.State([])
131
 
132
  # ============ 1. 顶部 Header (全宽布局) ============
133
+ # 包含 CLARE_LOGO_PATH (左) 和 HANBRIDGE_LOGO_PATH (右)
134
  gr.HTML(
135
  f"""
136
  <div class="header-container">
 
146
  </div>
147
  </div>
148
  </div>
 
149
  <div style="text-align: right;">
150
  <img src="file/{HANBRIDGE_LOGO_PATH}" style="height: 55px; object-fit: contain; margin-bottom: 5px;">
151
  <div style="font-size: 12px; color: #666;">
 
161
 
162
  # ============ [左侧] 控制面板 Sidebar ============
163
  with gr.Column(scale=1, min_width=200):
164
+ # Reset 按钮
165
  clear_btn = gr.Button("Reset Conversation", variant="stop")
166
 
167
  gr.Markdown("### Model Settings")
 
225
  <div style="position: absolute; bottom: 5px; left: 0; width: 40%; height: 8px; background-color: #8B1A1A; border-radius: 4px 0 0 4px;"></div>
226
  <img src="file/{CLARE_RUN_PATH}" style="position: absolute; left: 36%; bottom: 8px; height: 35px; z-index: 10;">
227
  </div>
 
228
  <div style="display:flex; justify-content:space-between; align-items:center; margin-top:8px;">
229
  <div style="font-size: 12px; color: #666;">Next Review: T+7</div>
230
  <div style="font-size: 12px; color: #004a99; text-decoration:underline; cursor:pointer;">
 
234
  </div>
235
  """
236
  )
237
+ # 真实的 Review 按钮放在 HTML 下方
238
  review_btn = gr.Button("Review Now", size="sm", variant="primary")
239
+
240
+ # 隐藏的 Session Status 用于逻辑传递
241
  session_status = gr.Markdown(visible=False)
242
 
243
  # --- Chat Interface ---
 
269
  # ============ [右侧] 功能栏 Right Sidebar ============
270
  with gr.Column(scale=1, min_width=180):
271
  gr.Markdown("### Actions")
272
+
273
  export_btn = gr.Button("Export Conversation", size="sm")
274
  quiz_btn = gr.Button("Let's Try (Micro-Quiz)", size="sm")
275
  summary_btn = gr.Button("Summarization", size="sm")
 
284
 
285
  # ================== Logic Bindings ==================
286
 
287
+ # 1. File Upload Logic
288
  def update_course_and_rag(file, doc_type_val):
289
  topics = extract_course_topics_from_file(file, doc_type_val)
290
  rag_chunks = build_rag_chunks_from_file(file, doc_type_val)
 
297
  outputs=[course_outline_state, rag_chunks_state, session_status],
298
  )
299
 
300
+ # 2. Chat Logic
301
  def respond(message, chat_history, course_outline, weaknesses, cognitive_state, rag_chunks, model, lang, mode, doc_type_val):
302
  resolved_lang = detect_language(message or "", lang)
303
 
 
330
  [user_input, chatbot, weakness_state, cognitive_state_state, session_status]
331
  )
332
 
333
+ # 3. Action Buttons Logic
334
  def on_export(chat_history, course_outline, mode, weaknesses, cognitive_state):
335
  return export_conversation(chat_history, course_outline, mode, weaknesses, cognitive_state)
336
 
 
358
  [result_display]
359
  )
360
 
361
+ # 4. Reset Logic
362
  def clear_all():
363
  empty_state = {"confusion": 0, "mastery": 0}
364
  default_status = render_session_status("Concept Explainer", [], empty_state)
 
371
  queue=False
372
  )
373
 
374
+ # 5. User Guide Logic
375
  def show_guide(section):
376
  return get_user_guide_section(section)
377
 
 
381
  ug_memory_line.click(lambda: show_guide("memory_line"), outputs=result_display)
382
 
383
  if __name__ == "__main__":
384
+ demo.launch()