NCTCMumbai commited on
Commit
0aecf54
1 Parent(s): 68b7587

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +286 -55
app.py CHANGED
@@ -201,36 +201,214 @@
201
  # QUIZBOT.launch(debug=True)
202
 
203
 
204
- ################################################
205
- from ragatouille import RAGPretrainedModel
206
- import subprocess
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
  import json
208
- import spaces
209
- import firebase_admin
210
- from firebase_admin import credentials, firestore
211
- import logging
212
- from pathlib import Path
213
- from time import perf_counter
214
- from datetime import datetime
215
  import gradio as gr
 
 
 
216
  from jinja2 import Environment, FileSystemLoader
217
- import numpy as np
218
- from sentence_transformers import CrossEncoder
219
- from os import getenv
220
- from backend.query_llm import generate_hf, generate_openai
221
- from backend.semantic_search import table, retriever
222
- from gradio_client import Client # Modified here
223
 
224
  VECTOR_COLUMN_NAME = "vector"
225
  TEXT_COLUMN_NAME = "text"
226
  proj_dir = Path(__file__).parent
227
 
228
  # Setting up the logging
 
229
  logging.basicConfig(level=logging.INFO)
230
  logger = logging.getLogger(__name__)
231
 
232
  # Replace Mixtral client with Qwen Client
233
- client = Client("Qwen/Qwen1.5-110B-Chat-demo") # Modified here
234
 
235
  # Set up the template environment with the templates directory
236
  env = Environment(loader=FileSystemLoader(proj_dir / 'templates'))
@@ -245,6 +423,55 @@ def system_instructions(question_difficulty, topic, documents_str):
245
  # RA
246
  RAG_db = gr.State()
247
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248
  with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green", secondary_hue="green"), css="style.css") as QUIZBOT:
249
  def load_model():
250
  RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
@@ -275,8 +502,8 @@ with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green",
275
 
276
  question_radios = [gr.Radio(visible=False) for _ in range(10)]
277
 
278
- @spaces.GPU
279
- @generate_quiz_btn.click(inputs=[radio, topic], outputs=[quiz_msg] + question_radios, api_name="generate_quiz")
280
  def generate_quiz(question_difficulty, topic):
281
  top_k_rank = 10
282
  RAG_db_ = RAG_db.value
@@ -285,6 +512,8 @@ with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green",
285
  gr.Warning('Generation of Quiz may take 1 to 2 minutes. Please wait.')
286
 
287
  question_radio_list = []
 
 
288
  count = 0
289
  while count <= 3:
290
  try:
@@ -299,56 +528,58 @@ with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green",
299
  response = client.predict(
300
  query=formatted_prompt,
301
  history=[],
302
- system="You are a helpful assistant.", # Modified to match Qwen's API
303
  api_name="/model_chat"
304
  )
305
  print(response)
306
- response1=response[1][0][1]
 
307
  # Find the first and last curly braces
308
  start_index = response1.find('{')
309
  end_index = response1.rfind('}')
310
 
311
  # Extract only the JSON part
312
  if start_index != -1 and end_index != -1:
313
- cleaned_response = response1[start_index:end_index + 1] # Include the last closing brace
314
 
315
  # Try parsing the cleaned JSON
316
  try:
317
  output_json = json.loads(cleaned_response)
318
- print('Parsed JSON:', output_json) # Successfully print the dictionary
319
- except json.JSONDecodeError as e:
320
- print(f"Failed to decode JSON: {e}")
321
- else:
322
- print("No valid JSON found in the response.")
323
- # output_json = json.loads(f"{response}")
324
- # print('output json', output_json)
325
-
326
- global quiz_data
327
- quiz_data = output_json
328
 
329
- for question_num in range(1, 11):
330
- question_key = f"Q{question_num}"
331
- answer_key = f"A{question_num}"
332
 
333
- question = quiz_data.get(question_key)
334
- answer = quiz_data.get(quiz_data.get(answer_key))
 
335
 
336
- if not question or not answer:
337
- continue
338
 
339
- choice_keys = [f"{question_key}:C{i}" for i in range(1, 5)]
340
- choice_list = [quiz_data.get(choice_key, "Choice not found") for choice_key in choice_keys]
341
 
342
- radio = gr.Radio(choices=choice_list, label=question, visible=True, interactive=True)
343
- question_radio_list.append(radio)
344
- print('question_radio_list',question_radio_list)
345
 
346
- if len(question_radio_list) == 10:
347
- break
 
 
 
 
 
 
 
 
 
 
348
  else:
349
- print('10 questions not generated. Trying again!')
350
- count += 1
351
- continue
352
  except Exception as e:
353
  count += 1
354
  print(f"Exception occurred: {e}")
@@ -359,14 +590,14 @@ with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green",
359
  print(f"Trying again.. {count} time... please wait")
360
  continue
361
 
362
- return ['Quiz Generated!'] + question_radio_list
363
 
364
  check_button = gr.Button("Check Score")
365
  score_textbox = gr.Markdown()
366
 
367
  @check_button.click(inputs=question_radios, outputs=score_textbox)
368
  def compare_answers(*user_answers):
369
- user_anwser_list = list(user_answers)
370
  answers_list = []
371
 
372
  for question_num in range(1, 20):
@@ -376,12 +607,12 @@ with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green",
376
  break
377
  answers_list.append(answer)
378
 
379
- score = sum(1 for item in user_anwser_list if item in answers_list)
380
 
381
- if score > 5:
382
- message = f"### Good! You got {score} out of 10!"
383
- elif score > 7:
384
  message = f"### Excellent! You got {score} out of 10!"
 
 
385
  else:
386
  message = f"### You got {score} out of 10! Don't worry. You can prepare well and try better next time!"
387
 
 
201
  # QUIZBOT.launch(debug=True)
202
 
203
 
204
+ # ################################################
205
+ # from ragatouille import RAGPretrainedModel
206
+ # import subprocess
207
+ # import json
208
+ # import spaces
209
+ # import firebase_admin
210
+ # from firebase_admin import credentials, firestore
211
+ # import logging
212
+ # from pathlib import Path
213
+ # from time import perf_counter
214
+ # from datetime import datetime
215
+ # import gradio as gr
216
+ # from jinja2 import Environment, FileSystemLoader
217
+ # import numpy as np
218
+ # from sentence_transformers import CrossEncoder
219
+ # from os import getenv
220
+ # from backend.query_llm import generate_hf, generate_openai
221
+ # from backend.semantic_search import table, retriever
222
+ # from gradio_client import Client # Modified here
223
+
224
+ # VECTOR_COLUMN_NAME = "vector"
225
+ # TEXT_COLUMN_NAME = "text"
226
+ # proj_dir = Path(__file__).parent
227
+
228
+ # # Setting up the logging
229
+ # logging.basicConfig(level=logging.INFO)
230
+ # logger = logging.getLogger(__name__)
231
+
232
+ # # Replace Mixtral client with Qwen Client
233
+ # client = Client("Qwen/Qwen1.5-110B-Chat-demo") # Modified here
234
+
235
+ # # Set up the template environment with the templates directory
236
+ # env = Environment(loader=FileSystemLoader(proj_dir / 'templates'))
237
+
238
+ # # Load the templates directly from the environment
239
+ # template = env.get_template('template.j2')
240
+ # template_html = env.get_template('template_html.j2')
241
+
242
+ # def system_instructions(question_difficulty, topic, documents_str):
243
+ # 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]"""
244
+
245
+ # # RA
246
+ # RAG_db = gr.State()
247
+
248
+ # with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green", secondary_hue="green"), css="style.css") as QUIZBOT:
249
+ # def load_model():
250
+ # RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
251
+ # RAG_db.value = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
252
+ # return 'Ready to Go!!'
253
+
254
+ # with gr.Column(scale=4):
255
+ # gr.HTML("""
256
+ # <center>
257
+ # <h1><span style="color: purple;">ADWITIYA</span> Customs Manual Quizbot</h1>
258
+ # <h2>Generative AI-powered Capacity building for Training Officers</h2>
259
+ # <i>⚠️ NACIN Faculties create quiz from any topic dynamically for classroom evaluation after their sessions ! ⚠️</i>
260
+ # </center>
261
+ # """)
262
+
263
+ # with gr.Column(scale=2):
264
+ # load_btn = gr.Button("Click to Load!🚀")
265
+ # load_text = gr.Textbox()
266
+ # load_btn.click(load_model, [], load_text)
267
+
268
+ # topic = gr.Textbox(label="Enter the Topic for Quiz", placeholder="Write any topic/details from Customs Manual")
269
+
270
+ # with gr.Row():
271
+ # radio = gr.Radio(["easy", "average", "hard"], label="How difficult should the quiz be?")
272
+
273
+ # generate_quiz_btn = gr.Button("Generate Quiz!🚀")
274
+ # quiz_msg = gr.Textbox()
275
+
276
+ # question_radios = [gr.Radio(visible=False) for _ in range(10)]
277
+
278
+ # @spaces.GPU
279
+ # @generate_quiz_btn.click(inputs=[radio, topic], outputs=[quiz_msg] + question_radios, api_name="generate_quiz")
280
+ # def generate_quiz(question_difficulty, topic):
281
+ # top_k_rank = 10
282
+ # RAG_db_ = RAG_db.value
283
+ # documents_full = RAG_db_.search(topic, k=top_k_rank)
284
+
285
+ # gr.Warning('Generation of Quiz may take 1 to 2 minutes. Please wait.')
286
+
287
+ # question_radio_list = []
288
+ # count = 0
289
+ # while count <= 3:
290
+ # try:
291
+ # documents = [item['content'] for item in documents_full]
292
+ # document_summaries = [f"[DOCUMENT {i + 1}]: {summary}{count}" for i, summary in enumerate(documents)]
293
+ # documents_str = '\n'.join(document_summaries)
294
+ # formatted_prompt = system_instructions(question_difficulty, topic, documents_str)
295
+
296
+ # print(formatted_prompt)
297
+
298
+ # # Use Qwen Client for quiz generation
299
+ # response = client.predict(
300
+ # query=formatted_prompt,
301
+ # history=[],
302
+ # system="You are a helpful assistant.", # Modified to match Qwen's API
303
+ # api_name="/model_chat"
304
+ # )
305
+ # print(response)
306
+ # response1=response[1][0][1]
307
+ # # Find the first and last curly braces
308
+ # start_index = response1.find('{')
309
+ # end_index = response1.rfind('}')
310
+
311
+ # # Extract only the JSON part
312
+ # if start_index != -1 and end_index != -1:
313
+ # cleaned_response = response1[start_index:end_index + 1] # Include the last closing brace
314
+
315
+ # # Try parsing the cleaned JSON
316
+ # try:
317
+ # output_json = json.loads(cleaned_response)
318
+ # print('Parsed JSON:', output_json) # Successfully print the dictionary
319
+ # except json.JSONDecodeError as e:
320
+ # print(f"Failed to decode JSON: {e}")
321
+ # else:
322
+ # print("No valid JSON found in the response.")
323
+ # # output_json = json.loads(f"{response}")
324
+ # # print('output json', output_json)
325
+
326
+ # global quiz_data
327
+ # quiz_data = output_json
328
+
329
+ # for question_num in range(1, 11):
330
+ # question_key = f"Q{question_num}"
331
+ # answer_key = f"A{question_num}"
332
+
333
+ # question = quiz_data.get(question_key)
334
+ # answer = quiz_data.get(quiz_data.get(answer_key))
335
+
336
+ # if not question or not answer:
337
+ # continue
338
+
339
+ # choice_keys = [f"{question_key}:C{i}" for i in range(1, 5)]
340
+ # choice_list = [quiz_data.get(choice_key, "Choice not found") for choice_key in choice_keys]
341
+
342
+ # radio = gr.Radio(choices=choice_list, label=question, visible=True, interactive=True)
343
+ # question_radio_list.append(radio)
344
+ # print('question_radio_list',question_radio_list)
345
+
346
+ # if len(question_radio_list) == 10:
347
+ # break
348
+ # else:
349
+ # print('10 questions not generated. Trying again!')
350
+ # count += 1
351
+ # continue
352
+ # except Exception as e:
353
+ # count += 1
354
+ # print(f"Exception occurred: {e}")
355
+ # if count == 3:
356
+ # print('Retry exhausted')
357
+ # gr.Warning('Sorry. Please try with another topic!')
358
+ # else:
359
+ # print(f"Trying again.. {count} time... please wait")
360
+ # continue
361
+
362
+ # return ['Quiz Generated!'] + question_radio_list
363
+
364
+ # check_button = gr.Button("Check Score")
365
+ # score_textbox = gr.Markdown()
366
+
367
+ # @check_button.click(inputs=question_radios, outputs=score_textbox)
368
+ # def compare_answers(*user_answers):
369
+ # user_anwser_list = list(user_answers)
370
+ # answers_list = []
371
+
372
+ # for question_num in range(1, 20):
373
+ # answer_key = f"A{question_num}"
374
+ # answer = quiz_data.get(quiz_data.get(answer_key))
375
+ # if not answer:
376
+ # break
377
+ # answers_list.append(answer)
378
+
379
+ # score = sum(1 for item in user_anwser_list if item in answers_list)
380
+
381
+ # if score > 5:
382
+ # message = f"### Good! You got {score} out of 10!"
383
+ # elif score > 7:
384
+ # message = f"### Excellent! You got {score} out of 10!"
385
+ # else:
386
+ # message = f"### You got {score} out of 10! Don't worry. You can prepare well and try better next time!"
387
+
388
+ # return message
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
 
401
  VECTOR_COLUMN_NAME = "vector"
402
  TEXT_COLUMN_NAME = "text"
403
  proj_dir = Path(__file__).parent
404
 
405
  # Setting up the logging
406
+ import logging
407
  logging.basicConfig(level=logging.INFO)
408
  logger = logging.getLogger(__name__)
409
 
410
  # Replace Mixtral client with Qwen Client
411
+ client = Client("Qwen/Qwen1.5-110B-Chat-demo")
412
 
413
  # Set up the template environment with the templates directory
414
  env = Environment(loader=FileSystemLoader(proj_dir / 'templates'))
 
423
  # RA
424
  RAG_db = gr.State()
425
 
426
+ def json_to_excel(output_json):
427
+ # Initialize list for DataFrame
428
+ data = []
429
+
430
+ for i in range(1, 11): # Assuming there are 10 questions
431
+ question_key = f"Q{i}"
432
+ answer_key = f"A{i}"
433
+
434
+ question = output_json.get(question_key, '')
435
+ correct_answer_key = output_json.get(answer_key, '')
436
+ correct_answer = correct_answer_key.split(':')[-1] if correct_answer_key else ''
437
+
438
+ # Extract options
439
+ option_keys = [f"{question_key}:C{i}" for i in range(1, 6)]
440
+ options = [output_json.get(key, '') for key in option_keys]
441
+
442
+ # Add data row
443
+ data.append([
444
+ question, # Question Text
445
+ "Multiple Choice", # Question Type
446
+ options[0], # Option 1
447
+ options[1], # Option 2
448
+ options[2] if len(options) > 2 else '', # Option 3
449
+ options[3] if len(options) > 3 else '', # Option 4
450
+ options[4] if len(options) > 4 else '', # Option 5
451
+ correct_answer, # Correct Answer
452
+ 30, # Time in seconds
453
+ '' # Image Link
454
+ ])
455
+
456
+ # Create DataFrame
457
+ df = pd.DataFrame(data, columns=[
458
+ "Question Text",
459
+ "Question Type",
460
+ "Option 1",
461
+ "Option 2",
462
+ "Option 3",
463
+ "Option 4",
464
+ "Option 5",
465
+ "Correct Answer",
466
+ "Time in seconds",
467
+ "Image Link"
468
+ ])
469
+
470
+ # Save to Excel file
471
+ excel_path = proj_dir / "quiz_questions.xlsx"
472
+ df.to_excel(excel_path, index=False)
473
+ return excel_path
474
+
475
  with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green", secondary_hue="green"), css="style.css") as QUIZBOT:
476
  def load_model():
477
  RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
 
502
 
503
  question_radios = [gr.Radio(visible=False) for _ in range(10)]
504
 
505
+ @gr.dependencies.GPU
506
+ @generate_quiz_btn.click(inputs=[radio, topic], outputs=[quiz_msg] + question_radios + [gr.File(label="Download Excel")], api_name="generate_quiz")
507
  def generate_quiz(question_difficulty, topic):
508
  top_k_rank = 10
509
  RAG_db_ = RAG_db.value
 
512
  gr.Warning('Generation of Quiz may take 1 to 2 minutes. Please wait.')
513
 
514
  question_radio_list = []
515
+ excel_file = None
516
+
517
  count = 0
518
  while count <= 3:
519
  try:
 
528
  response = client.predict(
529
  query=formatted_prompt,
530
  history=[],
531
+ system="You are a helpful assistant.",
532
  api_name="/model_chat"
533
  )
534
  print(response)
535
+ response1 = response[1][0][1]
536
+
537
  # Find the first and last curly braces
538
  start_index = response1.find('{')
539
  end_index = response1.rfind('}')
540
 
541
  # Extract only the JSON part
542
  if start_index != -1 and end_index != -1:
543
+ cleaned_response = response1[start_index:end_index + 1]
544
 
545
  # Try parsing the cleaned JSON
546
  try:
547
  output_json = json.loads(cleaned_response)
548
+ print('Parsed JSON:', output_json)
549
+ global quiz_data
550
+ quiz_data = output_json
 
 
 
 
 
 
 
551
 
552
+ # Generate the Excel file
553
+ excel_file = json_to_excel(output_json)
 
554
 
555
+ for question_num in range(1, 11):
556
+ question_key = f"Q{question_num}"
557
+ answer_key = f"A{question_num}"
558
 
559
+ question = quiz_data.get(question_key)
560
+ answer = quiz_data.get(quiz_data.get(answer_key))
561
 
562
+ if not question or not answer:
563
+ continue
564
 
565
+ choice_keys = [f"{question_key}:C{i}" for i in range(1, 5)]
566
+ choice_list = [quiz_data.get(choice_key, "Choice not found") for choice_key in choice_keys]
 
567
 
568
+ radio = gr.Radio(choices=choice_list, label=question, visible=True, interactive=True)
569
+ question_radio_list.append(radio)
570
+ print('question_radio_list', question_radio_list)
571
+
572
+ if len(question_radio_list) == 10:
573
+ break
574
+ else:
575
+ print('10 questions not generated. Trying again!')
576
+ count += 1
577
+ continue
578
+ except json.JSONDecodeError as e:
579
+ print(f"Failed to decode JSON: {e}")
580
  else:
581
+ print("No valid JSON found in the response.")
582
+
 
583
  except Exception as e:
584
  count += 1
585
  print(f"Exception occurred: {e}")
 
590
  print(f"Trying again.. {count} time... please wait")
591
  continue
592
 
593
+ return ['Quiz Generated!'] + question_radio_list + [excel_file]
594
 
595
  check_button = gr.Button("Check Score")
596
  score_textbox = gr.Markdown()
597
 
598
  @check_button.click(inputs=question_radios, outputs=score_textbox)
599
  def compare_answers(*user_answers):
600
+ user_answer_list = list(user_answers)
601
  answers_list = []
602
 
603
  for question_num in range(1, 20):
 
607
  break
608
  answers_list.append(answer)
609
 
610
+ score = sum(1 for item in user_answer_list if item in answers_list)
611
 
612
+ if score > 7:
 
 
613
  message = f"### Excellent! You got {score} out of 10!"
614
+ elif score > 5:
615
+ message = f"### Good! You got {score} out of 10!"
616
  else:
617
  message = f"### You got {score} out of 10! Don't worry. You can prepare well and try better next time!"
618