Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,16 +28,49 @@ from rag_engine import (
|
|
| 28 |
from syllabus_utils import extract_course_topics_from_file
|
| 29 |
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
gr.Markdown(
|
| 33 |
"""
|
| 34 |
-
|
| 35 |
-
**
|
| 36 |
-
|
| 37 |
-
- Ask in English → Clare answers in English.
|
| 38 |
-
- Ask in Chinese → Clare can answer in Chinese.
|
| 39 |
- Use different **learning modes** to change Clare's teaching style.
|
| 40 |
-
- Optionally upload your **course syllabus / slides / literature (.docx)** so Clare stays aligned with your course.
|
| 41 |
"""
|
| 42 |
)
|
| 43 |
|
|
@@ -63,7 +96,7 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant") as demo:
|
|
| 63 |
with gr.Row():
|
| 64 |
syllabus_file = gr.File(
|
| 65 |
label="Upload course file (.docx / .pdf / .pptx)",
|
| 66 |
-
file_types=[".docx", ".pdf", ".pptx"],
|
| 67 |
)
|
| 68 |
doc_type = gr.Dropdown(
|
| 69 |
choices=DOC_TYPES,
|
|
@@ -89,7 +122,6 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant") as demo:
|
|
| 89 |
rag_chunks = build_rag_chunks_from_file(file, doc_type_val)
|
| 90 |
return topics, rag_chunks
|
| 91 |
|
| 92 |
-
|
| 93 |
syllabus_file.change(
|
| 94 |
fn=update_course_and_rag,
|
| 95 |
inputs=[syllabus_file, doc_type],
|
|
@@ -101,9 +133,14 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant") as demo:
|
|
| 101 |
chatbot = gr.Chatbot(
|
| 102 |
label="Clare Chat",
|
| 103 |
height=450,
|
|
|
|
| 104 |
)
|
| 105 |
session_status = gr.Markdown(
|
| 106 |
-
value=render_session_status(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
label="Session status",
|
| 108 |
)
|
| 109 |
|
|
@@ -151,7 +188,11 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant") as demo:
|
|
| 151 |
if not message or not message.strip():
|
| 152 |
empty_msg = get_empty_input_prompt(resolved_lang)
|
| 153 |
new_history = chat_history + [("", empty_msg)]
|
| 154 |
-
status_text = render_session_status(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
return "", new_history, weaknesses, cognitive_state, status_text
|
| 156 |
|
| 157 |
# 3) 更新弱项 & 认知状态
|
|
@@ -181,7 +222,11 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant") as demo:
|
|
| 181 |
answer = prefix_en + past_a
|
| 182 |
|
| 183 |
new_history = chat_history + [(message, answer)]
|
| 184 |
-
status_text = render_session_status(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
return "", new_history, weaknesses, cognitive_state, status_text
|
| 186 |
|
| 187 |
# 5) RAG:基于上传文档做检索
|
|
@@ -208,14 +253,18 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant") as demo:
|
|
| 208 |
prefix = "(本次回答参考了你上传的课程文档内容。)\n\n"
|
| 209 |
else:
|
| 210 |
prefix = "(This answer used excerpts from your uploaded course document.)\n\n"
|
| 211 |
-
|
| 212 |
tagged_answer = prefix + answer
|
| 213 |
# 更新最后一条记录里的 assistant 内容
|
| 214 |
if new_history:
|
| 215 |
new_history[-1] = (message, tagged_answer)
|
| 216 |
answer = tagged_answer
|
| 217 |
-
|
| 218 |
-
status_text = render_session_status(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
return "", new_history, weaknesses, cognitive_state, status_text
|
| 220 |
|
| 221 |
user_input.submit(
|
|
@@ -345,8 +394,5 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant") as demo:
|
|
| 345 |
)
|
| 346 |
|
| 347 |
|
| 348 |
-
|
| 349 |
if __name__ == "__main__":
|
| 350 |
-
demo.launch()
|
| 351 |
-
|
| 352 |
-
import os
|
|
|
|
| 28 |
from syllabus_utils import extract_course_topics_from_file
|
| 29 |
|
| 30 |
|
| 31 |
+
HANBRIDGE_LOGO_PATH = "hanbridge_logo.png"
|
| 32 |
+
CLARE_LOGO_PATH = "clare_mascot.png"
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
with gr.Blocks(
|
| 36 |
+
title="Clare – Hanbridge AI Teaching Assistant",
|
| 37 |
+
) as demo:
|
| 38 |
+
# 顶部品牌区:左 Hanbridge logo + 文案,中间标题,右 Clare 小火苗
|
| 39 |
+
with gr.Row():
|
| 40 |
+
with gr.Column(scale=1):
|
| 41 |
+
gr.Image(
|
| 42 |
+
value=HANBRIDGE_LOGO_PATH,
|
| 43 |
+
show_label=False,
|
| 44 |
+
interactive=False,
|
| 45 |
+
height=80,
|
| 46 |
+
)
|
| 47 |
+
with gr.Column(scale=6):
|
| 48 |
+
gr.Markdown(
|
| 49 |
+
"""
|
| 50 |
+
<div style="display:flex;flex-direction:column;gap:4px;">
|
| 51 |
+
<div style="font-size:2.0rem;font-weight:700;">Hanbridge University</div>
|
| 52 |
+
<div style="font-size:1.3rem;font-weight:600;">Clare – AI Teaching Assistant</div>
|
| 53 |
+
<div style="font-size:0.95rem;color:#555;">
|
| 54 |
+
English UI · Supports both English & Chinese questions
|
| 55 |
+
</div>
|
| 56 |
+
</div>
|
| 57 |
+
""",
|
| 58 |
+
)
|
| 59 |
+
with gr.Column(scale=1, min_width=80):
|
| 60 |
+
gr.Image(
|
| 61 |
+
value=CLARE_LOGO_PATH,
|
| 62 |
+
show_label=False,
|
| 63 |
+
interactive=False,
|
| 64 |
+
height=80,
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# 简短说明
|
| 68 |
gr.Markdown(
|
| 69 |
"""
|
| 70 |
+
- Ask in **English** → Clare answers in English.
|
| 71 |
+
- Ask in **Chinese** → Clare can answer in Chinese.
|
|
|
|
|
|
|
|
|
|
| 72 |
- Use different **learning modes** to change Clare's teaching style.
|
| 73 |
+
- Optionally upload your **course syllabus / slides / literature (.docx / .pdf / .pptx)** so Clare stays aligned with your course.
|
| 74 |
"""
|
| 75 |
)
|
| 76 |
|
|
|
|
| 96 |
with gr.Row():
|
| 97 |
syllabus_file = gr.File(
|
| 98 |
label="Upload course file (.docx / .pdf / .pptx)",
|
| 99 |
+
file_types=[".docx", ".pdf", ".pptx"],
|
| 100 |
)
|
| 101 |
doc_type = gr.Dropdown(
|
| 102 |
choices=DOC_TYPES,
|
|
|
|
| 122 |
rag_chunks = build_rag_chunks_from_file(file, doc_type_val)
|
| 123 |
return topics, rag_chunks
|
| 124 |
|
|
|
|
| 125 |
syllabus_file.change(
|
| 126 |
fn=update_course_and_rag,
|
| 127 |
inputs=[syllabus_file, doc_type],
|
|
|
|
| 133 |
chatbot = gr.Chatbot(
|
| 134 |
label="Clare Chat",
|
| 135 |
height=450,
|
| 136 |
+
avatar_images=(None, CLARE_LOGO_PATH), # 用户默认头像,助教用小火苗
|
| 137 |
)
|
| 138 |
session_status = gr.Markdown(
|
| 139 |
+
value=render_session_status(
|
| 140 |
+
"Concept Explainer",
|
| 141 |
+
[],
|
| 142 |
+
{"confusion": 0, "mastery": 0},
|
| 143 |
+
),
|
| 144 |
label="Session status",
|
| 145 |
)
|
| 146 |
|
|
|
|
| 188 |
if not message or not message.strip():
|
| 189 |
empty_msg = get_empty_input_prompt(resolved_lang)
|
| 190 |
new_history = chat_history + [("", empty_msg)]
|
| 191 |
+
status_text = render_session_status(
|
| 192 |
+
learning_mode_val,
|
| 193 |
+
weaknesses or [],
|
| 194 |
+
cognitive_state,
|
| 195 |
+
)
|
| 196 |
return "", new_history, weaknesses, cognitive_state, status_text
|
| 197 |
|
| 198 |
# 3) 更新弱项 & 认知状态
|
|
|
|
| 222 |
answer = prefix_en + past_a
|
| 223 |
|
| 224 |
new_history = chat_history + [(message, answer)]
|
| 225 |
+
status_text = render_session_status(
|
| 226 |
+
learning_mode_val,
|
| 227 |
+
weaknesses,
|
| 228 |
+
cognitive_state,
|
| 229 |
+
)
|
| 230 |
return "", new_history, weaknesses, cognitive_state, status_text
|
| 231 |
|
| 232 |
# 5) RAG:基于上传文档做检索
|
|
|
|
| 253 |
prefix = "(本次回答参考了你上传的课程文档内容。)\n\n"
|
| 254 |
else:
|
| 255 |
prefix = "(This answer used excerpts from your uploaded course document.)\n\n"
|
| 256 |
+
|
| 257 |
tagged_answer = prefix + answer
|
| 258 |
# 更新最后一条记录里的 assistant 内容
|
| 259 |
if new_history:
|
| 260 |
new_history[-1] = (message, tagged_answer)
|
| 261 |
answer = tagged_answer
|
| 262 |
+
|
| 263 |
+
status_text = render_session_status(
|
| 264 |
+
learning_mode_val,
|
| 265 |
+
weaknesses,
|
| 266 |
+
cognitive_state,
|
| 267 |
+
)
|
| 268 |
return "", new_history, weaknesses, cognitive_state, status_text
|
| 269 |
|
| 270 |
user_input.submit(
|
|
|
|
| 394 |
)
|
| 395 |
|
| 396 |
|
|
|
|
| 397 |
if __name__ == "__main__":
|
| 398 |
+
demo.launch()
|
|
|
|
|
|