NCTCMumbai commited on
Commit
8febad9
1 Parent(s): 8ba0205

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +313 -122
app.py CHANGED
@@ -389,21 +389,259 @@
389
 
390
  # QUIZBOT.queue()
391
  # QUIZBOT.launch(debug=True)
392
- ##############??????????????????????????????
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
393
  import pandas as pd
394
  import json
395
  import gradio as gr
396
  from pathlib import Path
397
  from ragatouille import RAGPretrainedModel
398
  from gradio_client import Client
399
- from jinja2 import Environment, FileSystemLoader
400
  from tempfile import NamedTemporaryFile
 
 
 
401
 
402
  VECTOR_COLUMN_NAME = "vector"
403
  TEXT_COLUMN_NAME = "text"
404
- #proj_dir = Path(__file__).parent
405
- proj_dir = Path.cwd()
406
- # Setting up the logging
407
  import logging
408
  logging.basicConfig(level=logging.INFO)
409
  logger = logging.getLogger(__name__)
@@ -411,15 +649,8 @@ logger = logging.getLogger(__name__)
411
  # Replace Mixtral client with Qwen Client
412
  client = Client("Qwen/Qwen1.5-110B-Chat-demo")
413
 
414
- # Set up the template environment with the templates directory
415
- env = Environment(loader=FileSystemLoader(proj_dir / 'templates'))
416
-
417
- # Load the templates directly from the environment
418
- template = env.get_template('template.j2')
419
- template_html = env.get_template('template_html.j2')
420
-
421
  def system_instructions(question_difficulty, topic, documents_str):
422
- return f"""<s> [INST] You are a great teacher and your task is to create 10 questions with 4 choices with {question_difficulty} difficulty about the topic request "{topic}" only from the below given documents, {documents_str}. Then create answers. Index in JSON format, the questions as "Q#":"" to "Q#":"", the four choices as "Q#:C1":"" to "Q#:C4":"", and the answers as "A#":"Q#:C#" to "A#":"Q#:C#". example is 'A10':'Q10:C3' [/INST]"""
423
 
424
  # RA
425
  RAG_db = gr.State()
@@ -427,7 +658,7 @@ RAG_db = gr.State()
427
  def json_to_excel(output_json):
428
  # Initialize list for DataFrame
429
  data = []
430
- gr.Warning('Generating Shareable file link..',duration=30)
431
  for i in range(1, 11): # Assuming there are 10 questions
432
  question_key = f"Q{i}"
433
  answer_key = f"A{i}"
@@ -467,134 +698,93 @@ def json_to_excel(output_json):
467
  "Time in seconds",
468
  "Image Link"
469
  ])
470
- # Create a temporary file and save the DataFrame to it
471
  temp_file = NamedTemporaryFile(delete=False, suffix=".xlsx")
472
  df.to_excel(temp_file.name, index=False)
473
-
474
- # # Save to Excel file
475
- # excel_path = proj_dir / "quiz_questions.xlsx"
476
- # df.to_excel(str(excel_path), index=False)
477
  return temp_file.name
478
 
479
  with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green", secondary_hue="green"), css="style.css") as QUIZBOT:
480
- def load_model():
481
- RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
482
- RAG_db.value = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
483
- return 'Ready to Go!!'
484
-
485
- with gr.Column(scale=4):
486
- gr.HTML("""
487
- <center>
488
- <h1><span style="color: purple;">ADWITIYA</span> Customs Manual Quizbot</h1>
489
- <h2>Generative AI-powered Capacity building for Training Officers</h2>
490
- <i>⚠️ NACIN Faculties create quiz from any topic dynamically for classroom evaluation after their sessions ! ⚠️</i>
491
- </center>
492
- """)
493
-
494
- with gr.Column(scale=2):
495
- load_btn = gr.Button("Click to Load!🚀")
496
- load_text = gr.Textbox()
497
- load_btn.click(load_model, [], load_text)
498
-
499
  topic = gr.Textbox(label="Enter the Topic for Quiz", placeholder="Write any topic/details from Customs Manual")
500
 
501
  with gr.Row():
502
- radio = gr.Radio(["easy", "average", "hard"], label="How difficult should the quiz be?")
 
 
 
503
 
504
  generate_quiz_btn = gr.Button("Generate Quiz!🚀")
505
  quiz_msg = gr.Textbox()
506
 
507
  question_radios = [gr.Radio(visible=False) for _ in range(10)]
508
 
509
- #@gr.dependencies.GPU
510
- @generate_quiz_btn.click(inputs=[radio, topic], outputs=[quiz_msg] + question_radios + [gr.File(label="Download Excel")], api_name="generate_quiz")
511
- def generate_quiz(question_difficulty, topic):
512
  top_k_rank = 10
513
- RAG_db_ = RAG_db.value
514
- documents_full = RAG_db_.search(topic, k=top_k_rank)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
515
 
516
- gr.Warning('Generation of Quiz may take 1 to 2 minutes. Please wait.',duration=60)
 
 
517
 
518
- question_radio_list = []
519
- excel_file = None
 
 
 
520
 
521
- count = 0
522
- while count <= 3:
523
- try:
524
- documents = [item['content'] for item in documents_full]
525
- document_summaries = [f"[DOCUMENT {i + 1}]: {summary}{count}" for i, summary in enumerate(documents)]
526
- documents_str = '\n'.join(document_summaries)
527
- formatted_prompt = system_instructions(question_difficulty, topic, documents_str)
528
-
529
- print(formatted_prompt)
530
-
531
- # Use Qwen Client for quiz generation
532
- response = client.predict(
533
- query=formatted_prompt,
534
- history=[],
535
- system="You are a helpful assistant.",
536
- api_name="/model_chat"
537
- )
538
- print(response)
539
- response1 = response[1][0][1]
540
-
541
- # Find the first and last curly braces
542
- start_index = response1.find('{')
543
- end_index = response1.rfind('}')
544
-
545
- # Extract only the JSON part
546
- if start_index != -1 and end_index != -1:
547
- cleaned_response = response1[start_index:end_index + 1]
548
-
549
- # Try parsing the cleaned JSON
550
- try:
551
- output_json = json.loads(cleaned_response)
552
- print('Parsed JSON:', output_json)
553
- global quiz_data
554
- quiz_data = output_json
555
-
556
- # Generate the Excel file
557
- excel_file = json_to_excel(output_json)
558
-
559
- for question_num in range(1, 11):
560
- question_key = f"Q{question_num}"
561
- answer_key = f"A{question_num}"
562
-
563
- question = quiz_data.get(question_key)
564
- answer = quiz_data.get(quiz_data.get(answer_key))
565
-
566
- if not question or not answer:
567
- continue
568
-
569
- choice_keys = [f"{question_key}:C{i}" for i in range(1, 5)]
570
- choice_list = [quiz_data.get(choice_key, "Choice not found") for choice_key in choice_keys]
571
-
572
- radio = gr.Radio(choices=choice_list, label=question, visible=True, interactive=True)
573
- question_radio_list.append(radio)
574
- print('question_radio_list', question_radio_list)
575
-
576
- if len(question_radio_list) == 10:
577
- break
578
- else:
579
- print('10 questions not generated. Trying again!')
580
- count += 1
581
- continue
582
- except json.JSONDecodeError as e:
583
- print(f"Failed to decode JSON: {e}")
584
- else:
585
- print("No valid JSON found in the response.")
586
-
587
- except Exception as e:
588
- count += 1
589
- print(f"Exception occurred: {e}")
590
- if count == 3:
591
- print('Retry exhausted')
592
- gr.Warning('Sorry. Please try with another topic!')
593
- else:
594
- print(f"Trying again.. {count} time... please wait")
595
  continue
596
 
597
- return ['Quiz Generated!'] + question_radio_list + [excel_file]
 
 
 
 
 
 
 
 
 
598
 
599
  check_button = gr.Button("Check Score")
600
  score_textbox = gr.Markdown()
@@ -624,3 +814,4 @@ with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green",
624
 
625
  QUIZBOT.queue()
626
  QUIZBOT.launch(debug=True)
 
 
389
 
390
  # QUIZBOT.queue()
391
  # QUIZBOT.launch(debug=True)
392
+ # ##############??????????????????????????????
393
+ # import pandas as pd
394
+ # import json
395
+ # import gradio as gr
396
+ # from pathlib import Path
397
+ # from ragatouille import RAGPretrainedModel
398
+ # from gradio_client import Client
399
+ # from jinja2 import Environment, FileSystemLoader
400
+ # from tempfile import NamedTemporaryFile
401
+
402
+ # VECTOR_COLUMN_NAME = "vector"
403
+ # TEXT_COLUMN_NAME = "text"
404
+ # #proj_dir = Path(__file__).parent
405
+ # proj_dir = Path.cwd()
406
+ # # Setting up the logging
407
+ # import logging
408
+ # logging.basicConfig(level=logging.INFO)
409
+ # logger = logging.getLogger(__name__)
410
+
411
+ # # Replace Mixtral client with Qwen Client
412
+ # client = Client("Qwen/Qwen1.5-110B-Chat-demo")
413
+
414
+ # # Set up the template environment with the templates directory
415
+ # env = Environment(loader=FileSystemLoader(proj_dir / 'templates'))
416
+
417
+ # # Load the templates directly from the environment
418
+ # template = env.get_template('template.j2')
419
+ # template_html = env.get_template('template_html.j2')
420
+
421
+ # def system_instructions(question_difficulty, topic, documents_str):
422
+ # return f"""<s> [INST] You are a great teacher and your task is to create 10 questions with 4 choices with {question_difficulty} difficulty about the topic request "{topic}" only from the below given documents, {documents_str}. Then create answers. Index in JSON format, the questions as "Q#":"" to "Q#":"", the four choices as "Q#:C1":"" to "Q#:C4":"", and the answers as "A#":"Q#:C#" to "A#":"Q#:C#". example is 'A10':'Q10:C3' [/INST]"""
423
+
424
+ # # RA
425
+ # RAG_db = gr.State()
426
+
427
+ # def json_to_excel(output_json):
428
+ # # Initialize list for DataFrame
429
+ # data = []
430
+ # gr.Warning('Generating Shareable file link..',duration=30)
431
+ # for i in range(1, 11): # Assuming there are 10 questions
432
+ # question_key = f"Q{i}"
433
+ # answer_key = f"A{i}"
434
+
435
+ # question = output_json.get(question_key, '')
436
+ # correct_answer_key = output_json.get(answer_key, '')
437
+ # correct_answer = correct_answer_key.split(':')[-1] if correct_answer_key else ''
438
+
439
+ # # Extract options
440
+ # option_keys = [f"{question_key}:C{i}" for i in range(1, 6)]
441
+ # options = [output_json.get(key, '') for key in option_keys]
442
+
443
+ # # Add data row
444
+ # data.append([
445
+ # question, # Question Text
446
+ # "Multiple Choice", # Question Type
447
+ # options[0], # Option 1
448
+ # options[1], # Option 2
449
+ # options[2] if len(options) > 2 else '', # Option 3
450
+ # options[3] if len(options) > 3 else '', # Option 4
451
+ # options[4] if len(options) > 4 else '', # Option 5
452
+ # correct_answer, # Correct Answer
453
+ # 30, # Time in seconds
454
+ # '' # Image Link
455
+ # ])
456
+
457
+ # # Create DataFrame
458
+ # df = pd.DataFrame(data, columns=[
459
+ # "Question Text",
460
+ # "Question Type",
461
+ # "Option 1",
462
+ # "Option 2",
463
+ # "Option 3",
464
+ # "Option 4",
465
+ # "Option 5",
466
+ # "Correct Answer",
467
+ # "Time in seconds",
468
+ # "Image Link"
469
+ # ])
470
+ # # Create a temporary file and save the DataFrame to it
471
+ # temp_file = NamedTemporaryFile(delete=False, suffix=".xlsx")
472
+ # df.to_excel(temp_file.name, index=False)
473
+
474
+ # # # Save to Excel file
475
+ # # excel_path = proj_dir / "quiz_questions.xlsx"
476
+ # # df.to_excel(str(excel_path), index=False)
477
+ # return temp_file.name
478
+
479
+ # with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green", secondary_hue="green"), css="style.css") as QUIZBOT:
480
+ # def load_model():
481
+ # RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
482
+ # RAG_db.value = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
483
+ # return 'Ready to Go!!'
484
+
485
+ # with gr.Column(scale=4):
486
+ # gr.HTML("""
487
+ # <center>
488
+ # <h1><span style="color: purple;">ADWITIYA</span> Customs Manual Quizbot</h1>
489
+ # <h2>Generative AI-powered Capacity building for Training Officers</h2>
490
+ # <i>⚠️ NACIN Faculties create quiz from any topic dynamically for classroom evaluation after their sessions ! ⚠️</i>
491
+ # </center>
492
+ # """)
493
+
494
+ # with gr.Column(scale=2):
495
+ # load_btn = gr.Button("Click to Load!🚀")
496
+ # load_text = gr.Textbox()
497
+ # load_btn.click(load_model, [], load_text)
498
+
499
+ # topic = gr.Textbox(label="Enter the Topic for Quiz", placeholder="Write any topic/details from Customs Manual")
500
+
501
+ # with gr.Row():
502
+ # radio = gr.Radio(["easy", "average", "hard"], label="How difficult should the quiz be?")
503
+
504
+ # generate_quiz_btn = gr.Button("Generate Quiz!🚀")
505
+ # quiz_msg = gr.Textbox()
506
+
507
+ # question_radios = [gr.Radio(visible=False) for _ in range(10)]
508
+
509
+ # #@gr.dependencies.GPU
510
+ # @generate_quiz_btn.click(inputs=[radio, topic], outputs=[quiz_msg] + question_radios + [gr.File(label="Download Excel")], api_name="generate_quiz")
511
+ # def generate_quiz(question_difficulty, topic):
512
+ # top_k_rank = 10
513
+ # RAG_db_ = RAG_db.value
514
+ # documents_full = RAG_db_.search(topic, k=top_k_rank)
515
+
516
+ # gr.Warning('Generation of Quiz may take 1 to 2 minutes. Please wait.',duration=60)
517
+
518
+ # question_radio_list = []
519
+ # excel_file = None
520
+
521
+ # count = 0
522
+ # while count <= 3:
523
+ # try:
524
+ # documents = [item['content'] for item in documents_full]
525
+ # document_summaries = [f"[DOCUMENT {i + 1}]: {summary}{count}" for i, summary in enumerate(documents)]
526
+ # documents_str = '\n'.join(document_summaries)
527
+ # formatted_prompt = system_instructions(question_difficulty, topic, documents_str)
528
+
529
+ # print(formatted_prompt)
530
+
531
+ # # Use Qwen Client for quiz generation
532
+ # response = client.predict(
533
+ # query=formatted_prompt,
534
+ # history=[],
535
+ # system="You are a helpful assistant.",
536
+ # api_name="/model_chat"
537
+ # )
538
+ # print(response)
539
+ # response1 = response[1][0][1]
540
+
541
+ # # Find the first and last curly braces
542
+ # start_index = response1.find('{')
543
+ # end_index = response1.rfind('}')
544
+
545
+ # # Extract only the JSON part
546
+ # if start_index != -1 and end_index != -1:
547
+ # cleaned_response = response1[start_index:end_index + 1]
548
+
549
+ # # Try parsing the cleaned JSON
550
+ # try:
551
+ # output_json = json.loads(cleaned_response)
552
+ # print('Parsed JSON:', output_json)
553
+ # global quiz_data
554
+ # quiz_data = output_json
555
+
556
+ # # Generate the Excel file
557
+ # excel_file = json_to_excel(output_json)
558
+
559
+ # for question_num in range(1, 11):
560
+ # question_key = f"Q{question_num}"
561
+ # answer_key = f"A{question_num}"
562
+
563
+ # question = quiz_data.get(question_key)
564
+ # answer = quiz_data.get(quiz_data.get(answer_key))
565
+
566
+ # if not question or not answer:
567
+ # continue
568
+
569
+ # choice_keys = [f"{question_key}:C{i}" for i in range(1, 5)]
570
+ # choice_list = [quiz_data.get(choice_key, "Choice not found") for choice_key in choice_keys]
571
+
572
+ # radio = gr.Radio(choices=choice_list, label=question, visible=True, interactive=True)
573
+ # question_radio_list.append(radio)
574
+ # print('question_radio_list', question_radio_list)
575
+
576
+ # if len(question_radio_list) == 10:
577
+ # break
578
+ # else:
579
+ # print('10 questions not generated. Trying again!')
580
+ # count += 1
581
+ # continue
582
+ # except json.JSONDecodeError as e:
583
+ # print(f"Failed to decode JSON: {e}")
584
+ # else:
585
+ # print("No valid JSON found in the response.")
586
+
587
+ # except Exception as e:
588
+ # count += 1
589
+ # print(f"Exception occurred: {e}")
590
+ # if count == 3:
591
+ # print('Retry exhausted')
592
+ # gr.Warning('Sorry. Please try with another topic!')
593
+ # else:
594
+ # print(f"Trying again.. {count} time... please wait")
595
+ # continue
596
+
597
+ # return ['Quiz Generated!'] + question_radio_list + [excel_file]
598
+
599
+ # check_button = gr.Button("Check Score")
600
+ # score_textbox = gr.Markdown()
601
+
602
+ # @check_button.click(inputs=question_radios, outputs=score_textbox)
603
+ # def compare_answers(*user_answers):
604
+ # user_answer_list = list(user_answers)
605
+ # answers_list = []
606
+
607
+ # for question_num in range(1, 20):
608
+ # answer_key = f"A{question_num}"
609
+ # answer = quiz_data.get(quiz_data.get(answer_key))
610
+ # if not answer:
611
+ # break
612
+ # answers_list.append(answer)
613
+
614
+ # score = sum(1 for item in user_answer_list if item in answers_list)
615
+
616
+ # if score > 7:
617
+ # message = f"### Excellent! You got {score} out of 10!"
618
+ # elif score > 5:
619
+ # message = f"### Good! You got {score} out of 10!"
620
+ # else:
621
+ # message = f"### You got {score} out of 10! Don't worry. You can prepare well and try better next time!"
622
+
623
+ # return message
624
+
625
+ # QUIZBOT.queue()
626
+ # QUIZBOT.launch(debug=True)
627
+
628
+ #?????????????????????????????????
629
  import pandas as pd
630
  import json
631
  import gradio as gr
632
  from pathlib import Path
633
  from ragatouille import RAGPretrainedModel
634
  from gradio_client import Client
 
635
  from tempfile import NamedTemporaryFile
636
+ from sentence_transformers import CrossEncoder
637
+ import numpy as np
638
+ from time import perf_counter
639
 
640
  VECTOR_COLUMN_NAME = "vector"
641
  TEXT_COLUMN_NAME = "text"
642
+ proj_dir = Path.cwd()
643
+
644
+ # Set up logging
645
  import logging
646
  logging.basicConfig(level=logging.INFO)
647
  logger = logging.getLogger(__name__)
 
649
  # Replace Mixtral client with Qwen Client
650
  client = Client("Qwen/Qwen1.5-110B-Chat-demo")
651
 
 
 
 
 
 
 
 
652
  def system_instructions(question_difficulty, topic, documents_str):
653
+ return f"""<s> [INST] You are a great teacher and your task is to create 10 questions with 4 choices with {question_difficulty} difficulty about the topic request "{topic}" only from the below given documents, {documents_str}. Then create answers. Index in JSON format, the questions as "Q#":"" to "Q#":"", the four choices as "Q#:C1":"" to "Q#:C4":"", and the answers as "A#":"Q#:C#" to "A#":"Q#:C#". Example: 'A10':'Q10:C3' [/INST]"""
654
 
655
  # RA
656
  RAG_db = gr.State()
 
658
  def json_to_excel(output_json):
659
  # Initialize list for DataFrame
660
  data = []
661
+ gr.Warning('Generating Shareable file link..', duration=30)
662
  for i in range(1, 11): # Assuming there are 10 questions
663
  question_key = f"Q{i}"
664
  answer_key = f"A{i}"
 
698
  "Time in seconds",
699
  "Image Link"
700
  ])
701
+
702
  temp_file = NamedTemporaryFile(delete=False, suffix=".xlsx")
703
  df.to_excel(temp_file.name, index=False)
 
 
 
 
704
  return temp_file.name
705
 
706
  with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green", secondary_hue="green"), css="style.css") as QUIZBOT:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
707
  topic = gr.Textbox(label="Enter the Topic for Quiz", placeholder="Write any topic/details from Customs Manual")
708
 
709
  with gr.Row():
710
+ difficulty_radio = gr.Radio(["easy", "average", "hard"], label="How difficult should the quiz be?")
711
+ model_radio = gr.Radio(choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker', '(HIGH ACCURATE) ColBERT'],
712
+ value='(ACCURATE) BGE reranker', label="Embeddings",
713
+ info="First query to ColBERT may take a little time")
714
 
715
  generate_quiz_btn = gr.Button("Generate Quiz!🚀")
716
  quiz_msg = gr.Textbox()
717
 
718
  question_radios = [gr.Radio(visible=False) for _ in range(10)]
719
 
720
+ @generate_quiz_btn.click(inputs=[difficulty_radio, topic, model_radio], outputs=[quiz_msg] + question_radios + [gr.File(label="Download Excel")])
721
+ def generate_quiz(question_difficulty, topic, cross_encoder):
 
722
  top_k_rank = 10
723
+ documents = []
724
+ gr.Warning('Generating Quiz may take 1-2 minutes. Please wait.', duration=60)
725
+
726
+ if cross_encoder == '(HIGH ACCURATE) ColBERT':
727
+ gr.Warning('Retrieving using ColBERT.. First-time query will take a minute for model to load.. please wait')
728
+ RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
729
+ RAG_db.value = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
730
+ documents_full = RAG_db.value.search(topic, k=top_k_rank)
731
+ documents = [item['content'] for item in documents_full]
732
+
733
+ else:
734
+ document_start = perf_counter()
735
+ query_vec = retriever.encode(topic)
736
+ doc1 = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_k_rank)
737
+
738
+ documents = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_k_rank).to_list()
739
+ documents = [doc[TEXT_COLUMN_NAME] for doc in documents]
740
+
741
+ query_doc_pair = [[topic, doc] for doc in documents]
742
+
743
+ if cross_encoder == '(FAST) MiniLM-L6v2':
744
+ cross_encoder1 = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
745
+ elif cross_encoder == '(ACCURATE) BGE reranker':
746
+ cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base')
747
+
748
+ cross_scores = cross_encoder1.predict(query_doc_pair)
749
+ sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
750
+ documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]]
751
+
752
+ formatted_prompt = system_instructions(question_difficulty, topic, '\n'.join(documents))
753
 
754
+ try:
755
+ response = client.predict(query=formatted_prompt, history=[], system="You are a helpful assistant.", api_name="/model_chat")
756
+ response1 = response[1][0][1]
757
 
758
+ # Extract JSON
759
+ start_index = response1.find('{')
760
+ end_index = response1.rfind('}')
761
+ cleaned_response = response1[start_index:end_index + 1] if start_index != -1 and end_index != -1 else ''
762
+ output_json = json.loads(cleaned_response)
763
 
764
+ # Generate the Excel file
765
+ excel_file = json_to_excel(output_json)
766
+
767
+ question_radio_list = []
768
+ for question_num in range(1, 11):
769
+ question_key = f"Q{question_num}"
770
+ answer_key = f"A{question_num}"
771
+
772
+ question = output_json.get(question_key)
773
+ answer = output_json.get(output_json.get(answer_key))
774
+
775
+ if not question or not answer:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
776
  continue
777
 
778
+ choice_keys = [f"{question_key}:C{i}" for i in range(1, 5)]
779
+ choice_list = [output_json.get(choice_key, "Choice not found") for choice_key in choice_keys]
780
+
781
+ radio = gr.Radio(choices=choice_list, label=question, visible=True, interactive=True)
782
+ question_radio_list.append(radio)
783
+
784
+ return ['Quiz Generated!'] + question_radio_list + [excel_file]
785
+
786
+ except json.JSONDecodeError as e:
787
+ print(f"Failed to decode JSON: {e}")
788
 
789
  check_button = gr.Button("Check Score")
790
  score_textbox = gr.Markdown()
 
814
 
815
  QUIZBOT.queue()
816
  QUIZBOT.launch(debug=True)
817
+