SarahXia0405 commited on
Commit
f53558e
·
verified ·
1 Parent(s): aeec0a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -6
app.py CHANGED
@@ -349,6 +349,72 @@ def generate_quiz_from_history(
349
  return quiz_text
350
 
351
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
352
  # ---------- Gradio UI ----------
353
  with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant") as demo:
354
  gr.Markdown(
@@ -424,14 +490,19 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant") as demo:
424
  clear_btn = gr.Button("Reset conversation")
425
  export_btn = gr.Button("Export conversation")
426
  quiz_btn = gr.Button("Generate 3 quiz questions")
 
427
 
428
  export_box = gr.Textbox(
429
  label="Conversation export (Markdown)",
430
- lines=10,
431
  )
432
  quiz_box = gr.Textbox(
433
  label="Generated quiz (with answer key)",
434
- lines=10,
 
 
 
 
435
  )
436
 
437
  # 主对话逻辑:更新弱项 + 调用 Clare
@@ -474,14 +545,14 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant") as demo:
474
  [user_input, chatbot, weakness_state],
475
  )
476
 
477
- # 清空对话 & 弱项 & 导出/quiz
478
  def clear_all():
479
- return [], [], "", ""
480
 
481
  clear_btn.click(
482
  clear_all,
483
  None,
484
- [chatbot, weakness_state, export_box, quiz_box],
485
  queue=False,
486
  )
487
 
@@ -511,5 +582,21 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant") as demo:
511
  [quiz_box],
512
  )
513
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
514
  if __name__ == "__main__":
515
- demo.launch()
 
349
  return quiz_text
350
 
351
 
352
+ # ---------- 概念总结(知识点摘要) ----------
353
+ def summarize_conversation(
354
+ history: List[Tuple[str, str]],
355
+ course_outline: List[str],
356
+ weaknesses: List[str],
357
+ model_name: str,
358
+ language_preference: str,
359
+ ) -> str:
360
+ conversation_text = ""
361
+ for user, assistant in history[-10:]: # 最近 10 轮足够
362
+ conversation_text += f"Student: {user}\nClare: {assistant}\n"
363
+
364
+ topics_text = "; ".join(course_outline[:8])
365
+ weakness_text = "; ".join(weaknesses[-5:]) if weaknesses else "N/A"
366
+
367
+ messages = [
368
+ {"role": "system", "content": CLARE_SYSTEM_PROMPT},
369
+ {
370
+ "role": "system",
371
+ "content": (
372
+ "Your task now is to produce a **concept-only summary** of this tutoring "
373
+ "session. Only include knowledge points, definitions, key formulas, "
374
+ "examples, and main takeaways. Do **not** include any personal remarks, "
375
+ "jokes, or off-topic chat. Write in clear bullet points. This summary "
376
+ "should be suitable for the student to paste into their study notes."
377
+ ),
378
+ },
379
+ {
380
+ "role": "system",
381
+ "content": f"Course topics context: {topics_text}",
382
+ },
383
+ {
384
+ "role": "system",
385
+ "content": f"Student known difficulties: {weakness_text}",
386
+ },
387
+ {
388
+ "role": "user",
389
+ "content": (
390
+ "Here is the recent conversation between you and the student:\n\n"
391
+ + conversation_text
392
+ + "\n\nPlease summarize only the concepts and key ideas learned."
393
+ ),
394
+ },
395
+ ]
396
+
397
+ if language_preference == "中文":
398
+ messages.append(
399
+ {
400
+ "role": "system",
401
+ "content": "请用中文给出要点总结,只保留知识点和结论,使用条目符号。"
402
+ }
403
+ )
404
+
405
+ try:
406
+ response = client.chat.completions.create(
407
+ model=model_name or DEFAULT_MODEL,
408
+ messages=messages,
409
+ temperature=0.4,
410
+ )
411
+ summary_text = response.choices[0].message.content
412
+ except Exception as e:
413
+ summary_text = f"⚠️ Error generating summary: {e}"
414
+
415
+ return summary_text
416
+
417
+
418
  # ---------- Gradio UI ----------
419
  with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant") as demo:
420
  gr.Markdown(
 
490
  clear_btn = gr.Button("Reset conversation")
491
  export_btn = gr.Button("Export conversation")
492
  quiz_btn = gr.Button("Generate 3 quiz questions")
493
+ summary_btn = gr.Button("Summarize concepts")
494
 
495
  export_box = gr.Textbox(
496
  label="Conversation export (Markdown)",
497
+ lines=8,
498
  )
499
  quiz_box = gr.Textbox(
500
  label="Generated quiz (with answer key)",
501
+ lines=8,
502
+ )
503
+ summary_box = gr.Textbox(
504
+ label="Concept summary (for study notes)",
505
+ lines=8,
506
  )
507
 
508
  # 主对话逻辑:更新弱项 + 调用 Clare
 
545
  [user_input, chatbot, weakness_state],
546
  )
547
 
548
+ # 清空对话 & 弱项 & 导出/quiz/summary
549
  def clear_all():
550
+ return [], [], "", "", ""
551
 
552
  clear_btn.click(
553
  clear_all,
554
  None,
555
+ [chatbot, weakness_state, export_box, quiz_box, summary_box],
556
  queue=False,
557
  )
558
 
 
582
  [quiz_box],
583
  )
584
 
585
+ # 概念总结按钮
586
+ def on_summary(chat_history, course_outline, weaknesses, model_name_val, language_pref_val):
587
+ return summarize_conversation(
588
+ chat_history,
589
+ course_outline,
590
+ weaknesses or [],
591
+ model_name_val,
592
+ language_pref_val,
593
+ )
594
+
595
+ summary_btn.click(
596
+ on_summary,
597
+ [chatbot, course_outline_state, weakness_state, model_name, language_preference],
598
+ [summary_box],
599
+ )
600
+
601
  if __name__ == "__main__":
602
+ demo.launch()