Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
# app.py
|
|
|
|
| 2 |
from typing import List, Dict, Tuple, Optional
|
| 3 |
|
| 4 |
import gradio as gr
|
|
@@ -28,18 +29,198 @@ from rag_engine import (
|
|
| 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
|
| 39 |
with gr.Row():
|
| 40 |
with gr.Column(scale=1):
|
| 41 |
gr.Image(
|
| 42 |
-
value=
|
| 43 |
show_label=False,
|
| 44 |
interactive=False,
|
| 45 |
height=80,
|
|
@@ -48,17 +229,17 @@ with gr.Blocks(
|
|
| 48 |
gr.Markdown(
|
| 49 |
"""
|
| 50 |
<div style="display:flex;flex-direction:column;gap:4px;">
|
| 51 |
-
<div style="font-size:2.
|
| 52 |
-
<div style="font-size:1.
|
| 53 |
<div style="font-size:0.95rem;color:#555;">
|
| 54 |
-
|
| 55 |
</div>
|
| 56 |
</div>
|
| 57 |
""",
|
| 58 |
)
|
| 59 |
with gr.Column(scale=1, min_width=80):
|
| 60 |
gr.Image(
|
| 61 |
-
value=
|
| 62 |
show_label=False,
|
| 63 |
interactive=False,
|
| 64 |
height=80,
|
|
@@ -67,10 +248,10 @@ with gr.Blocks(
|
|
| 67 |
# 简短说明
|
| 68 |
gr.Markdown(
|
| 69 |
"""
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
"""
|
| 75 |
)
|
| 76 |
|
|
@@ -146,7 +327,7 @@ with gr.Blocks(
|
|
| 146 |
|
| 147 |
user_input = gr.Textbox(
|
| 148 |
label="Your question",
|
| 149 |
-
placeholder="Ask
|
| 150 |
)
|
| 151 |
|
| 152 |
with gr.Row():
|
|
@@ -168,6 +349,30 @@ with gr.Blocks(
|
|
| 168 |
lines=8,
|
| 169 |
)
|
| 170 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
# 主对话逻辑
|
| 172 |
def respond(
|
| 173 |
message,
|
|
@@ -393,6 +598,49 @@ with gr.Blocks(
|
|
| 393 |
[summary_box],
|
| 394 |
)
|
| 395 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
|
| 397 |
if __name__ == "__main__":
|
| 398 |
-
demo.launch()
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
|
| 3 |
from typing import List, Dict, Tuple, Optional
|
| 4 |
|
| 5 |
import gradio as gr
|
|
|
|
| 29 |
from syllabus_utils import extract_course_topics_from_file
|
| 30 |
|
| 31 |
|
| 32 |
+
# ================== Assets ==================
|
| 33 |
+
|
| 34 |
HANBRIDGE_LOGO_PATH = "hanbridge_logo.png"
|
| 35 |
CLARE_LOGO_PATH = "clare_mascot.png"
|
| 36 |
+
CLARE_RUN_PATH = "Clare_Run.png" # 预留:可用于 Memory Line 上的小 Clare 动画
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# ========= Clare User Guide Content =========
|
| 40 |
+
|
| 41 |
+
USER_GUIDE_SECTIONS = {
|
| 42 |
+
"getting_started": """
|
| 43 |
+
## Getting started
|
| 44 |
+
|
| 45 |
+
Welcome to **Clare — Your Personalized AI Tutor**.
|
| 46 |
+
|
| 47 |
+
Clare is designed to support your learning through:
|
| 48 |
+
- Clear explanations of course concepts
|
| 49 |
+
- Socratic-style reasoning guidance
|
| 50 |
+
- Personalized reinforcement based on learning science
|
| 51 |
+
- Course-aligned answers using your uploaded materials
|
| 52 |
+
- Micro-quizzes & summaries to strengthen understanding
|
| 53 |
+
|
| 54 |
+
**To begin:**
|
| 55 |
+
1. Select your **Learning Mode** on the left
|
| 56 |
+
2. (Optional) Upload your **syllabus / slides / notes** at the top
|
| 57 |
+
3. Ask Clare any question about your course, assignment, or study plan.
|
| 58 |
+
""",
|
| 59 |
+
"mode_definition": """
|
| 60 |
+
## Mode Definition
|
| 61 |
+
|
| 62 |
+
Clare offers different teaching modes to match how you prefer to learn.
|
| 63 |
+
|
| 64 |
+
### Concept Explainer
|
| 65 |
+
Clear, structured explanations with examples — ideal for learning new topics.
|
| 66 |
+
|
| 67 |
+
### Socratic Tutor
|
| 68 |
+
Clare asks guiding questions instead of giving direct answers.
|
| 69 |
+
Helps you build reasoning and problem-solving skills.
|
| 70 |
+
|
| 71 |
+
### Exam Prep / Quiz
|
| 72 |
+
Generates short practice questions aligned with your course week.
|
| 73 |
+
Useful for self-testing and preparing for exams.
|
| 74 |
+
|
| 75 |
+
### Assignment Helper
|
| 76 |
+
Helps you interpret assignment prompts, plan structure, and understand requirements.
|
| 77 |
+
❗ Clare does **not** produce full assignment answers (academic integrity).
|
| 78 |
+
|
| 79 |
+
### Quick Summary
|
| 80 |
+
Gives brief summaries of slides, reading materials, or long questions.
|
| 81 |
+
""",
|
| 82 |
+
"how_clare_works": """
|
| 83 |
+
## How Clare Works
|
| 84 |
+
|
| 85 |
+
Clare combines **course context + learning science + AI reasoning** to generate answers.
|
| 86 |
+
|
| 87 |
+
Clare uses:
|
| 88 |
+
- **Your selected Learning Mode**
|
| 89 |
+
Determines tone, depth, and interaction style.
|
| 90 |
+
|
| 91 |
+
- **Your uploaded course files**
|
| 92 |
+
Syllabus, slides, or papers help align answers with your specific course.
|
| 93 |
+
|
| 94 |
+
- **Reinforcement learning cycle**
|
| 95 |
+
Answers may prioritize key concepts you’re likely to forget (based on the forgetting curve).
|
| 96 |
+
|
| 97 |
+
- **Adaptive explanation depth**
|
| 98 |
+
Clare can adjust complexity depending on your previous interactions.
|
| 99 |
+
|
| 100 |
+
- **Responsible AI principles**
|
| 101 |
+
Avoids harmful output and preserves academic integrity.
|
| 102 |
+
""",
|
| 103 |
+
"memory_line": """
|
| 104 |
+
## What is Memory Line?
|
| 105 |
+
|
| 106 |
+
**Memory Line** is a visualization of your *learning reinforcement cycle*.
|
| 107 |
+
|
| 108 |
+
Based on the **forgetting-curve model**, Clare organizes your review topics into:
|
| 109 |
+
- **T+0 (Current Week)** – new concepts
|
| 110 |
+
- **T+7** – first spaced review
|
| 111 |
+
- **T+14** – reinforcement review
|
| 112 |
+
- **T+30** – long-term consolidation
|
| 113 |
+
|
| 114 |
+
Clare uses these cycles to decide:
|
| 115 |
+
- Which concepts to reinforce today
|
| 116 |
+
- Which explanations to prioritize
|
| 117 |
+
- When to introduce quick review questions
|
| 118 |
+
|
| 119 |
+
**Review Now** will generate a small set of concepts that are due for spaced repetition.
|
| 120 |
+
""",
|
| 121 |
+
"learning_progress": """
|
| 122 |
+
## Learning Progress Report
|
| 123 |
+
|
| 124 |
+
The Learning Progress Report highlights:
|
| 125 |
+
- **Concepts mastered**
|
| 126 |
+
- **Concepts in progress**
|
| 127 |
+
- **Concepts due for review**
|
| 128 |
+
- Your recent **micro-quiz results**
|
| 129 |
+
- Suggested **next-step topics**
|
| 130 |
+
|
| 131 |
+
You can download a **summary** at any time to use as study notes.
|
| 132 |
+
""",
|
| 133 |
+
"how_files": """
|
| 134 |
+
## How Clare Uses Your Files
|
| 135 |
+
|
| 136 |
+
Your uploaded syllabus / slides / notes help Clare:
|
| 137 |
+
|
| 138 |
+
- Align explanations with your exact course
|
| 139 |
+
- Use terminology consistent with your professor
|
| 140 |
+
- Improve factual accuracy
|
| 141 |
+
- Generate personalized reinforcement content
|
| 142 |
+
|
| 143 |
+
🔒 **Privacy**
|
| 144 |
+
- Files are used only within your session
|
| 145 |
+
- They are not kept as permanent training data
|
| 146 |
+
|
| 147 |
+
Accepted formats: **.docx / .pdf / .pptx**
|
| 148 |
+
""",
|
| 149 |
+
"micro_quiz": """
|
| 150 |
+
## Micro-Quiz
|
| 151 |
+
|
| 152 |
+
The **Micro-Quiz** function provides a:
|
| 153 |
+
|
| 154 |
+
- 1-minute self-check
|
| 155 |
+
- 1–3 questions based on your recent topics
|
| 156 |
+
- Instant feedback
|
| 157 |
+
|
| 158 |
+
Micro-quizzes strengthen your memory and are part of Clare’s reinforcement system.
|
| 159 |
+
""",
|
| 160 |
+
"summarization": """
|
| 161 |
+
## Summarization
|
| 162 |
|
| 163 |
+
Clare can summarize:
|
| 164 |
+
|
| 165 |
+
- Lecture notes
|
| 166 |
+
- Uploaded PDFs
|
| 167 |
+
- Long conversation threads
|
| 168 |
+
- Complex concepts
|
| 169 |
+
|
| 170 |
+
Summary styles can include:
|
| 171 |
+
- Bullet points
|
| 172 |
+
- Key ideas
|
| 173 |
+
- Study notes
|
| 174 |
+
- Comparison-style highlights.
|
| 175 |
+
""",
|
| 176 |
+
"export_conversation": """
|
| 177 |
+
## Export Conversation
|
| 178 |
+
|
| 179 |
+
You can export your chat session for:
|
| 180 |
+
|
| 181 |
+
- Study review
|
| 182 |
+
- Exam preparation
|
| 183 |
+
- Documentation for tutoring help
|
| 184 |
+
- Saving important explanations
|
| 185 |
+
|
| 186 |
+
Export format: **Markdown / plain text** (depending on this Space configuration).
|
| 187 |
+
""",
|
| 188 |
+
"faq": """
|
| 189 |
+
## FAQ
|
| 190 |
+
|
| 191 |
+
**Q: Does Clare give assignment answers?**
|
| 192 |
+
No. Clare assists with understanding and planning but does **not** generate full solutions.
|
| 193 |
+
|
| 194 |
+
**Q: Does Clare replace lectures or TA office hours?**
|
| 195 |
+
No. Clare supplements your learning by providing on-demand guidance.
|
| 196 |
+
|
| 197 |
+
**Q: Can Clare explain my professor’s slides?**
|
| 198 |
+
Yes — upload your slides so Clare can align explanations with your course.
|
| 199 |
+
|
| 200 |
+
**Q: What languages does Clare support?**
|
| 201 |
+
Currently: English & 简体中文.
|
| 202 |
+
|
| 203 |
+
**Q: Why does Clare sometimes ask me questions instead of answering directly?**
|
| 204 |
+
You may be in **Socratic Tutor** mode — Clare is guiding your reasoning.
|
| 205 |
+
"""
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
def get_user_guide_section(section_key: str) -> str:
|
| 210 |
+
"""Return markdown text for a User Guide section."""
|
| 211 |
+
return USER_GUIDE_SECTIONS.get(section_key, "Section not found.")
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
# ================== Gradio App ==================
|
| 215 |
|
| 216 |
with gr.Blocks(
|
| 217 |
title="Clare – Hanbridge AI Teaching Assistant",
|
| 218 |
) as demo:
|
| 219 |
+
# 顶部品牌区:左 Hanbridge logo,中间标题,右 Clare 小火苗
|
| 220 |
with gr.Row():
|
| 221 |
with gr.Column(scale=1):
|
| 222 |
gr.Image(
|
| 223 |
+
value=CLARE_LOGO_PATH,
|
| 224 |
show_label=False,
|
| 225 |
interactive=False,
|
| 226 |
height=80,
|
|
|
|
| 229 |
gr.Markdown(
|
| 230 |
"""
|
| 231 |
<div style="display:flex;flex-direction:column;gap:4px;">
|
| 232 |
+
<div style="font-size:2.2rem;font-weight:700;">Clare</div>
|
| 233 |
+
<div style="font-size:1.2rem;font-weight:500;">Your Personalized AI Tutor</div>
|
| 234 |
<div style="font-size:0.95rem;color:#555;">
|
| 235 |
+
Personalized guidance, review, and intelligent reinforcement – designed for how you learn.
|
| 236 |
</div>
|
| 237 |
</div>
|
| 238 |
""",
|
| 239 |
)
|
| 240 |
with gr.Column(scale=1, min_width=80):
|
| 241 |
gr.Image(
|
| 242 |
+
value=HANBRIDGE_LOGO_PATH,
|
| 243 |
show_label=False,
|
| 244 |
interactive=False,
|
| 245 |
height=80,
|
|
|
|
| 248 |
# 简短说明
|
| 249 |
gr.Markdown(
|
| 250 |
"""
|
| 251 |
+
- Ask in **English** → Clare answers in English.
|
| 252 |
+
- Ask in **Chinese** → Clare can answer in Chinese.
|
| 253 |
+
- Use different **Learning Modes** to change Clare's teaching style.
|
| 254 |
+
- Optionally upload your **course syllabus / slides / literature (.docx / .pdf / .pptx)** so Clare stays aligned with your course.
|
| 255 |
"""
|
| 256 |
)
|
| 257 |
|
|
|
|
| 327 |
|
| 328 |
user_input = gr.Textbox(
|
| 329 |
label="Your question",
|
| 330 |
+
placeholder="Ask about a concept, your assignment, or let Clare test you with a quick question...",
|
| 331 |
)
|
| 332 |
|
| 333 |
with gr.Row():
|
|
|
|
| 349 |
lines=8,
|
| 350 |
)
|
| 351 |
|
| 352 |
+
# ========== User Guide ==========
|
| 353 |
+
|
| 354 |
+
with gr.Accordion("User Guide", open=False):
|
| 355 |
+
with gr.Row():
|
| 356 |
+
# 左侧“蓝色链接列表”(之后可以通过 CSS 把 Button 改成蓝色文字样式)
|
| 357 |
+
with gr.Column(scale=1, min_width=180):
|
| 358 |
+
ug_getting_started = gr.Button("Getting started", variant="secondary")
|
| 359 |
+
ug_mode_def = gr.Button("Mode Definition", variant="secondary")
|
| 360 |
+
ug_how_works = gr.Button("How Clare Works", variant="secondary")
|
| 361 |
+
ug_memory_line = gr.Button("What is Memory Line", variant="secondary")
|
| 362 |
+
ug_learning_progress = gr.Button("Learning Progress Report", variant="secondary")
|
| 363 |
+
ug_how_files = gr.Button("How Clare Uses Your Files", variant="secondary")
|
| 364 |
+
ug_micro_quiz = gr.Button("Micro-Quiz", variant="secondary")
|
| 365 |
+
ug_summarization = gr.Button("Summarization", variant="secondary")
|
| 366 |
+
ug_export_conv = gr.Button("Export Conversation", variant="secondary")
|
| 367 |
+
ug_faq = gr.Button("FAQ", variant="secondary")
|
| 368 |
+
|
| 369 |
+
# 右侧具体内容框
|
| 370 |
+
with gr.Column(scale=3):
|
| 371 |
+
user_guide_md = gr.Markdown(
|
| 372 |
+
value=USER_GUIDE_SECTIONS["getting_started"],
|
| 373 |
+
elem_id="user-guide-panel",
|
| 374 |
+
)
|
| 375 |
+
|
| 376 |
# 主对话逻辑
|
| 377 |
def respond(
|
| 378 |
message,
|
|
|
|
| 598 |
[summary_box],
|
| 599 |
)
|
| 600 |
|
| 601 |
+
# ========== User Guide button bindings ==========
|
| 602 |
+
|
| 603 |
+
ug_getting_started.click(
|
| 604 |
+
lambda: get_user_guide_section("getting_started"),
|
| 605 |
+
outputs=user_guide_md,
|
| 606 |
+
)
|
| 607 |
+
ug_mode_def.click(
|
| 608 |
+
lambda: get_user_guide_section("mode_definition"),
|
| 609 |
+
outputs=user_guide_md,
|
| 610 |
+
)
|
| 611 |
+
ug_how_works.click(
|
| 612 |
+
lambda: get_user_guide_section("how_clare_works"),
|
| 613 |
+
outputs=user_guide_md,
|
| 614 |
+
)
|
| 615 |
+
ug_memory_line.click(
|
| 616 |
+
lambda: get_user_guide_section("memory_line"),
|
| 617 |
+
outputs=user_guide_md,
|
| 618 |
+
)
|
| 619 |
+
ug_learning_progress.click(
|
| 620 |
+
lambda: get_user_guide_section("learning_progress"),
|
| 621 |
+
outputs=user_guide_md,
|
| 622 |
+
)
|
| 623 |
+
ug_how_files.click(
|
| 624 |
+
lambda: get_user_guide_section("how_files"),
|
| 625 |
+
outputs=user_guide_md,
|
| 626 |
+
)
|
| 627 |
+
ug_micro_quiz.click(
|
| 628 |
+
lambda: get_user_guide_section("micro_quiz"),
|
| 629 |
+
outputs=user_guide_md,
|
| 630 |
+
)
|
| 631 |
+
ug_summarization.click(
|
| 632 |
+
lambda: get_user_guide_section("summarization"),
|
| 633 |
+
outputs=user_guide_md,
|
| 634 |
+
)
|
| 635 |
+
ug_export_conv.click(
|
| 636 |
+
lambda: get_user_guide_section("export_conversation"),
|
| 637 |
+
outputs=user_guide_md,
|
| 638 |
+
)
|
| 639 |
+
ug_faq.click(
|
| 640 |
+
lambda: get_user_guide_section("faq"),
|
| 641 |
+
outputs=user_guide_md,
|
| 642 |
+
)
|
| 643 |
+
|
| 644 |
|
| 645 |
if __name__ == "__main__":
|
| 646 |
+
demo.launch()
|