Deevyankar commited on
Commit
8f9e9ab
·
verified ·
1 Parent(s): 74a7a39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -22
app.py CHANGED
@@ -316,12 +316,7 @@ def answer_question(message, history, mode, language_mode, quiz_count_mode, show
316
  ensure_loaded()
317
  user_text = message.strip()
318
 
319
- # Add user message first
320
- history.append({"role": "user", "content": user_text})
321
-
322
- # -----------------------------------------
323
- # QUIZ EVALUATION STEP
324
- # -----------------------------------------
325
  if quiz_state.get("active", False):
326
  evaluation_prompt = build_quiz_evaluation_prompt(
327
  quiz_state["language_mode"],
@@ -345,7 +340,7 @@ def answer_question(message, history, mode, language_mode, quiz_count_mode, show
345
  lines.append(f"**Feedback:** {item['feedback']}")
346
 
347
  final_answer = "\n".join(lines)
348
- history.append({"role": "assistant", "content": final_answer})
349
 
350
  quiz_state = {
351
  "active": False,
@@ -356,15 +351,11 @@ def answer_question(message, history, mode, language_mode, quiz_count_mode, show
356
 
357
  return history, quiz_state, ""
358
 
359
- # -----------------------------------------
360
- # NORMAL RETRIEVAL
361
- # -----------------------------------------
362
  records = search_hybrid(user_text, shortlist_k=20, final_k=3)
363
  context = build_context(records)
364
 
365
- # -----------------------------------------
366
- # QUIZ GENERATION
367
- # -----------------------------------------
368
  if mode == "Quiz Me":
369
  n_questions = choose_quiz_count(user_text, quiz_count_mode)
370
  prompt = build_quiz_generation_prompt(language_mode, user_text, context, n_questions)
@@ -389,7 +380,7 @@ def answer_question(message, history, mode, language_mode, quiz_count_mode, show
389
  lines.append(make_sources(records))
390
 
391
  assistant_text = "\n".join(lines)
392
- history.append({"role": "assistant", "content": assistant_text})
393
 
394
  quiz_state = {
395
  "active": True,
@@ -400,20 +391,18 @@ def answer_question(message, history, mode, language_mode, quiz_count_mode, show
400
 
401
  return history, quiz_state, ""
402
 
403
- # -----------------------------------------
404
- # OTHER MODES
405
- # -----------------------------------------
406
  prompt = build_tutor_prompt(mode, language_mode, user_text, context)
407
  answer = chat_text(prompt)
408
 
409
  if show_sources:
410
  answer += "\n\n---\n**Sources used:**\n" + make_sources(records)
411
 
412
- history.append({"role": "assistant", "content": answer})
413
  return history, quiz_state, ""
414
 
415
  except Exception as e:
416
- history.append({"role": "assistant", "content": f"Error: {str(e)}"})
417
  quiz_state["active"] = False
418
  return history, quiz_state, ""
419
 
@@ -457,7 +446,7 @@ footer { display: none !important; }
457
  """
458
 
459
 
460
- with gr.Blocks(css=CSS) as demo:
461
  quiz_state = gr.State({
462
  "active": False,
463
  "topic": None,
@@ -495,7 +484,7 @@ with gr.Blocks(css=CSS) as demo:
495
  - The system will ask questions, and your **next message will be evaluated automatically**
496
  """)
497
 
498
- chatbot = gr.Chatbot(height=520, type="messages")
499
  msg = gr.Textbox(
500
  placeholder="Ask a question or type a topic...",
501
  lines=1,
@@ -525,4 +514,4 @@ with gr.Blocks(css=CSS) as demo:
525
  )
526
 
527
  if __name__ == "__main__":
528
- demo.launch()
 
316
  ensure_loaded()
317
  user_text = message.strip()
318
 
319
+ # Quiz evaluation step
 
 
 
 
 
320
  if quiz_state.get("active", False):
321
  evaluation_prompt = build_quiz_evaluation_prompt(
322
  quiz_state["language_mode"],
 
340
  lines.append(f"**Feedback:** {item['feedback']}")
341
 
342
  final_answer = "\n".join(lines)
343
+ history.append((user_text, final_answer))
344
 
345
  quiz_state = {
346
  "active": False,
 
351
 
352
  return history, quiz_state, ""
353
 
354
+ # Normal retrieval
 
 
355
  records = search_hybrid(user_text, shortlist_k=20, final_k=3)
356
  context = build_context(records)
357
 
358
+ # Quiz generation
 
 
359
  if mode == "Quiz Me":
360
  n_questions = choose_quiz_count(user_text, quiz_count_mode)
361
  prompt = build_quiz_generation_prompt(language_mode, user_text, context, n_questions)
 
380
  lines.append(make_sources(records))
381
 
382
  assistant_text = "\n".join(lines)
383
+ history.append((user_text, assistant_text))
384
 
385
  quiz_state = {
386
  "active": True,
 
391
 
392
  return history, quiz_state, ""
393
 
394
+ # Other modes
 
 
395
  prompt = build_tutor_prompt(mode, language_mode, user_text, context)
396
  answer = chat_text(prompt)
397
 
398
  if show_sources:
399
  answer += "\n\n---\n**Sources used:**\n" + make_sources(records)
400
 
401
+ history.append((user_text, answer))
402
  return history, quiz_state, ""
403
 
404
  except Exception as e:
405
+ history.append((message, f"Error: {str(e)}"))
406
  quiz_state["active"] = False
407
  return history, quiz_state, ""
408
 
 
446
  """
447
 
448
 
449
+ with gr.Blocks() as demo:
450
  quiz_state = gr.State({
451
  "active": False,
452
  "topic": None,
 
484
  - The system will ask questions, and your **next message will be evaluated automatically**
485
  """)
486
 
487
+ chatbot = gr.Chatbot(height=520)
488
  msg = gr.Textbox(
489
  placeholder="Ask a question or type a topic...",
490
  lines=1,
 
514
  )
515
 
516
  if __name__ == "__main__":
517
+ demo.launch(css=CSS)