NCTCMumbai commited on
Commit
f72c98d
1 Parent(s): 699a5e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +274 -491
app.py CHANGED
@@ -1,4 +1,207 @@
1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from ragatouille import RAGPretrainedModel
3
  import subprocess
4
  import json
@@ -13,552 +216,139 @@ import gradio as gr
13
  from jinja2 import Environment, FileSystemLoader
14
  import numpy as np
15
  from sentence_transformers import CrossEncoder
16
- from huggingface_hub import InferenceClient
17
  from os import getenv
18
-
19
  from backend.query_llm import generate_hf, generate_openai
20
  from backend.semantic_search import table, retriever
21
- from huggingface_hub import InferenceClient
22
-
23
 
24
  VECTOR_COLUMN_NAME = "vector"
25
  TEXT_COLUMN_NAME = "text"
26
- HF_TOKEN = getenv("HUGGING_FACE_HUB_TOKEN")
27
  proj_dir = Path(__file__).parent
 
28
  # Setting up the logging
29
  logging.basicConfig(level=logging.INFO)
30
  logger = logging.getLogger(__name__)
31
- client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1",token=HF_TOKEN)
 
 
 
32
  # Set up the template environment with the templates directory
33
  env = Environment(loader=FileSystemLoader(proj_dir / 'templates'))
34
 
35
  # Load the templates directly from the environment
36
  template = env.get_template('template.j2')
37
  template_html = env.get_template('template_html.j2')
38
- #___________________
39
- # service_account_key='firebase.json'
40
- # # Create a Certificate object from the service account info
41
- # cred = credentials.Certificate(service_account_key)
42
- # # Initialize the Firebase Admin
43
- # firebase_admin.initialize_app(cred)
44
-
45
- # # # Create a reference to the Firestore database
46
- # db = firestore.client()
47
- # #db usage
48
- # collection_name = 'Nirvachana' # Replace with your collection name
49
- # field_name = 'message_count' # Replace with your field name for count
50
- # Examples
51
- examples = ['My transhipment cargo is missing','can u explain and tabulate difference between b 17 bond and a warehousing bond',
52
- 'What are benefits of the AEO Scheme and eligibility criteria?',
53
- 'What are penalties for customs offences? ', 'what are penalties to customs officers misusing their powers under customs act?','What are eligibility criteria for exemption from cost recovery charges','list in detail what is procedure for obtaining new approval for openeing a CFS attached to an ICD']
54
-
55
-
56
-
57
- # def get_and_increment_value_count(db , collection_name, field_name):
58
- # """
59
- # Retrieves a value count from the specified Firestore collection and field,
60
- # increments it by 1, and updates the field with the new value."""
61
- # collection_ref = db.collection(collection_name)
62
- # doc_ref = collection_ref.document('count_doc') # Assuming a dedicated document for count
63
-
64
- # # Use a transaction to ensure consistency across reads and writes
65
- # try:
66
- # with db.transaction() as transaction:
67
- # # Get the current value count (or initialize to 0 if it doesn't exist)
68
- # current_count_doc = doc_ref.get()
69
- # current_count_data = current_count_doc.to_dict()
70
- # if current_count_data:
71
- # current_count = current_count_data.get(field_name, 0)
72
- # else:
73
- # current_count = 0
74
- # # Increment the count
75
- # new_count = current_count + 1
76
- # # Update the document with the new count
77
- # transaction.set(doc_ref, {field_name: new_count})
78
- # return new_count
79
- # except Exception as e:
80
- # print(f"Error retrieving and updating value count: {e}")
81
- # return None # Indicate error
82
-
83
- # def update_count_html():
84
- # usage_count = get_and_increment_value_count(db ,collection_name, field_name)
85
- # ccount_html = gr.HTML(value=f"""
86
- # <div style="display: flex; justify-content: flex-end;">
87
- # <span style="font-weight: bold; color: maroon; font-size: 18px;">No of Usages:</span>
88
- # <span style="font-weight: bold; color: maroon; font-size: 18px;">{usage_count}</span>
89
- # </div>
90
- # """)
91
- # return count_html
92
-
93
- # def store_message(db,query,answer,cross_encoder):
94
- # timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
95
- # # Create a new document reference with a dynamic document name based on timestamp
96
- # new_completion= db.collection('Nirvachana').document(f"chatlogs_{timestamp}")
97
- # new_completion.set({
98
- # 'query': query,
99
- # 'answer':answer,
100
- # 'created_time': firestore.SERVER_TIMESTAMP,
101
- # 'embedding': cross_encoder,
102
- # 'title': 'Expenditure observer bot'
103
- # })
104
-
105
-
106
- # def add_text(history, text):
107
- # history = [] if history is None else history
108
- # history = history + [(text, None)]
109
- # return history, gr.Textbox(value="", interactive=False)
110
-
111
-
112
- # def bot(history, cross_encoder):
113
- # top_rerank = 25
114
- # top_k_rank = 20
115
- # query = history[-1][0]
116
-
117
- # if not query:
118
- # gr.Warning("Please submit a non-empty string as a prompt")
119
- # raise ValueError("Empty string was submitted")
120
-
121
- # logger.warning('Retrieving documents...')
122
-
123
- # # if COLBERT RAGATATOUILLE PROCEDURE :
124
- # if cross_encoder=='(HIGH ACCURATE) ColBERT':
125
- # gr.Warning('Retrieving using ColBERT.. First time query will take a minute for model to load..pls wait')
126
- # RAG= RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
127
- # RAG_db=RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
128
- # documents_full=RAG_db.search(query,k=top_k_rank)
129
-
130
- # documents=[item['content'] for item in documents_full]
131
- # # Create Prompt
132
- # prompt = template.render(documents=documents, query=query)
133
- # prompt_html = template_html.render(documents=documents, query=query)
134
-
135
- # generate_fn = generate_hf
136
-
137
- # history[-1][1] = ""
138
- # for character in generate_fn(prompt, history[:-1]):
139
- # history[-1][1] = character
140
- # yield history, prompt_html
141
- # print('Final history is ',history)
142
- # #store_message(db,history[-1][0],history[-1][1],cross_encoder)
143
- # else:
144
- # # Retrieve documents relevant to query
145
- # document_start = perf_counter()
146
-
147
- # query_vec = retriever.encode(query)
148
- # logger.warning(f'Finished query vec')
149
- # doc1 = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_k_rank)
150
-
151
-
152
-
153
- # logger.warning(f'Finished search')
154
- # documents = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_rerank).to_list()
155
- # documents = [doc[TEXT_COLUMN_NAME] for doc in documents]
156
- # logger.warning(f'start cross encoder {len(documents)}')
157
- # # Retrieve documents relevant to query
158
- # query_doc_pair = [[query, doc] for doc in documents]
159
- # if cross_encoder=='(FAST) MiniLM-L6v2' :
160
- # cross_encoder1 = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
161
- # elif cross_encoder=='(ACCURATE) BGE reranker':
162
- # cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base')
163
-
164
- # cross_scores = cross_encoder1.predict(query_doc_pair)
165
- # sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
166
- # logger.warning(f'Finished cross encoder {len(documents)}')
167
-
168
- # documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]]
169
- # logger.warning(f'num documents {len(documents)}')
170
-
171
- # document_time = perf_counter() - document_start
172
- # logger.warning(f'Finished Retrieving documents in {round(document_time, 2)} seconds...')
173
-
174
- # # Create Prompt
175
- # prompt = template.render(documents=documents, query=query)
176
- # prompt_html = template_html.render(documents=documents, query=query)
177
-
178
- # generate_fn = generate_hf
179
-
180
- # history[-1][1] = ""
181
- # for character in generate_fn(prompt, history[:-1]):
182
- # history[-1][1] = character
183
- # yield history, prompt_html
184
- # print('Final history is ',history)
185
- # #store_message(db,history[-1][0],history[-1][1],cross_encoder)
186
-
187
- def system_instructions(question_difficulty, topic,documents_str):
188
- return f"""<s> [INST] Your are a great teacher and your task is to create 10 questions with 4 choices with a {question_difficulty} difficulty about topic request " {topic} " only from the below given documents, {documents_str} then create an 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#". [/INST]"""
189
-
190
- # RAG_db = gr.State()
191
-
192
- # def load_model():
193
- # try:
194
- # # Initialize the model
195
- # RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
196
- # # Load the RAG database
197
- # RAG_db.value = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
198
- # return 'Ready to Go!!'
199
- # except Exception as e:
200
- # return f"Error loading model: {e}"
201
-
202
-
203
- # def generate_quiz(question_difficulty, topic):
204
- # if not topic.strip():
205
- # return ['Please enter a valid topic.'] + [gr.Radio(visible=False) for _ in range(10)]
206
-
207
- # top_k_rank = 10
208
- # # Load the model and database within the generate_quiz function
209
- # try:
210
- # RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
211
- # RAG_db_ = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
212
- # gr.Warning('Model loaded!')
213
- # except Exception as e:
214
- # return [f"Error loading model: {e}"] + [gr.Radio(visible=False) for _ in range(10)]
215
-
216
- # RAG_db_ = RAG_db.value
217
- # documents_full = RAG_db_.search(topic, k=top_k_rank)
218
-
219
- # generate_kwargs = dict(
220
- # temperature=0.2,
221
- # max_new_tokens=4000,
222
- # top_p=0.95,
223
- # repetition_penalty=1.0,
224
- # do_sample=True,
225
- # seed=42,
226
- # )
227
-
228
- # question_radio_list = []
229
- # count = 0
230
- # while count <= 3:
231
- # try:
232
- # documents = [item['content'] for item in documents_full]
233
- # document_summaries = [f"[DOCUMENT {i+1}]: {summary}{count}" for i, summary in enumerate(documents)]
234
- # documents_str = '\n'.join(document_summaries)
235
- # formatted_prompt = system_instructions(question_difficulty, topic, documents_str)
236
-
237
- # pre_prompt = [
238
- # {"role": "system", "content": formatted_prompt}
239
- # ]
240
- # response = client.text_generation(
241
- # formatted_prompt, **generate_kwargs, stream=False, details=False, return_full_text=False,
242
- # )
243
- # output_json = json.loads(f"{response}")
244
-
245
- # global quiz_data
246
- # quiz_data = output_json
247
-
248
- # for question_num in range(1, 11):
249
- # question_key = f"Q{question_num}"
250
- # answer_key = f"A{question_num}"
251
- # question = quiz_data.get(question_key)
252
- # answer = quiz_data.get(quiz_data.get(answer_key))
253
-
254
- # if not question or not answer:
255
- # continue
256
-
257
- # choice_keys = [f"{question_key}:C{i}" for i in range(1, 5)]
258
- # choice_list = [quiz_data.get(choice_key, "Choice not found") for choice_key in choice_keys]
259
-
260
- # radio = gr.Radio(choices=choice_list, label=question, visible=True, interactive=True)
261
- # question_radio_list.append(radio)
262
-
263
- # if len(question_radio_list) == 10:
264
- # break
265
- # else:
266
- # count += 1
267
- # continue
268
- # except Exception as e:
269
- # count += 1
270
- # if count == 3:
271
- # return ['Sorry. Pls try with another topic!'] + [gr.Radio(visible=False) for _ in range(10)]
272
- # continue
273
-
274
- # return ['Quiz Generated!'] + question_radio_list
275
-
276
- # def compare_answers(*user_answers):
277
- # user_answer_list = user_answers
278
- # answers_list = [quiz_data.get(quiz_data.get(f"A{question_num}")) for question_num in range(1, 11)]
279
-
280
- # score = sum(1 for answer in user_answer_list if answer in answers_list)
281
-
282
- # if score > 7:
283
- # message = f"### Excellent! You got {score} out of 10!"
284
- # elif score > 5:
285
- # message = f"### Good! You got {score} out of 10!"
286
- # else:
287
- # message = f"### You got {score} out of 10! Don’t worry, you can prepare well and try better next time!"
288
-
289
- # return message
290
-
291
- # #with gr.Blocks(theme='Insuz/SimpleIndigo') as demo:
292
- # with gr.Blocks(theme='NoCrypt/miku') as CHATBOT:
293
- # with gr.Row():
294
- # with gr.Column(scale=10):
295
- # # gr.Markdown(
296
- # # """
297
- # # # Theme preview: `paris`
298
- # # To use this theme, set `theme='earneleh/paris'` in `gr.Blocks()` or `gr.Interface()`.
299
- # # You can append an `@` and a semantic version expression, e.g. @>=1.0.0,<2.0.0 to pin to a given version
300
- # # of this theme.
301
- # # """
302
- # # )
303
- # gr.HTML(value="""<div style="color: #FF4500;"><h1>ADWITIYA-</h1> <h1><span style="color: #008000">Custom Manual Chatbot and Quizbot</span></h1>
304
- # </div>""", elem_id='heading')
305
-
306
- # gr.HTML(value=f"""
307
- # <p style="font-family: sans-serif; font-size: 16px;">
308
- # Using GenAI for CBIC Capacity Building - A free chat bot developed by National Customs Targeting Center using Open source LLMs for CBIC Officers
309
- # </p>
310
- # """, elem_id='Sub-heading')
311
- # #usage_count = get_and_increment_value_count(db,collection_name, field_name)
312
- # gr.HTML(value=f"""<p style="font-family: Arial, sans-serif; font-size: 14px;">Developed by NCTC,Mumbai . Suggestions may be sent to <a href="mailto:nctc-admin@gov.in" style="color: #00008B; font-style: italic;">ramyadevi1607@yahoo.com</a>.</p>""", elem_id='Sub-heading1 ')
313
-
314
- # with gr.Column(scale=3):
315
- # gr.Image(value='logo.png',height=200,width=200)
316
-
317
-
318
- # # gr.HTML(value="""<div style="color: #FF4500;"><h1>CHEERFULL CBSE-</h1> <h1><span style="color: #008000">AI Assisted Fun Learning</span></h1>
319
- # # <img src='logo.png' alt="Chatbot" width="50" height="50" />
320
- # # </div>""", elem_id='heading')
321
-
322
- # # gr.HTML(value=f"""
323
- # # <p style="font-family: sans-serif; font-size: 16px;">
324
- # # A free Artificial Intelligence Chatbot assistant trained on CBSE Class 10 Science Notes to engage and help students and teachers of Puducherry.
325
- # # </p>
326
- # # """, elem_id='Sub-heading')
327
- # # #usage_count = get_and_increment_value_count(db,collection_name, field_name)
328
- # # gr.HTML(value=f"""<p style="font-family: Arial, sans-serif; font-size: 16px;">Developed by K M Ramyasri , PGT . Suggestions may be sent to <a href="mailto:ramyadevi1607@yahoo.com" style="color: #00008B; font-style: italic;">ramyadevi1607@yahoo.com</a>.</p>""", elem_id='Sub-heading1 ')
329
- # # # count_html = gr.HTML(value=f"""
330
- # # # <div style="display: flex; justify-content: flex-end;">
331
- # # # <span style="font-weight: bold; color: maroon; font-size: 18px;">No of Usages:</span>
332
- # # # <span style="font-weight: bold; color: maroon; font-size: 18px;">{usage_count}</span>
333
- # # # </div>
334
- # # # """)
335
-
336
- # chatbot = gr.Chatbot(
337
- # [],
338
- # elem_id="chatbot",
339
- # avatar_images=('https://aui.atlassian.com/aui/8.8/docs/images/avatar-person.svg',
340
- # 'https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo.svg'),
341
- # bubble_full_width=False,
342
- # show_copy_button=True,
343
- # show_share_button=True,
344
- # )
345
-
346
- # with gr.Row():
347
- # txt = gr.Textbox(
348
- # scale=3,
349
- # show_label=False,
350
- # placeholder="Enter text and press enter",
351
- # container=False,
352
- # )
353
- # txt_btn = gr.Button(value="Submit text", scale=1)
354
-
355
- # cross_encoder = gr.Radio(choices=['(FAST) MiniLM-L6v2','(ACCURATE) BGE reranker','(HIGH ACCURATE) ColBERT'], value='(ACCURATE) BGE reranker',label="Embeddings", info="Only First query to Colbert may take litte time)")
356
 
357
- # prompt_html = gr.HTML()
358
- # # Turn off interactivity while generating if you click
359
- # txt_msg = txt_btn.click(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
360
- # bot, [chatbot, cross_encoder], [chatbot, prompt_html])#.then(update_count_html,[],[count_html])
361
 
362
- # # Turn it back on
363
- # txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
364
-
365
- # # Turn off interactivity while generating if you hit enter
366
- # txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
367
- # bot, [chatbot, cross_encoder], [chatbot, prompt_html])#.then(update_count_html,[],[count_html])
368
-
369
- # # Turn it back on
370
- # txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
371
-
372
- # # Examples
373
- # gr.Examples(examples, txt)
374
-
375
-
376
-
377
-
378
- # with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green", secondary_hue="green"), css="style.css") as QUIZBOT:
379
- # with gr.Column(scale=4):
380
- # gr.HTML("""
381
- # <center>
382
- # <h1><span style="color: purple;">ADWITIYA</span> Customs Manual Quizbot</h1>
383
- # <h2>Generative AI-powered Capacity building for Training Officers</h2>
384
- # <i>⚠️ NACIN Faculties create quiz from any topic dynamically for classroom evaluation after their sessions! ⚠️</i>
385
- # </center>
386
- # """)
387
-
388
- # with gr.Column(scale=2):
389
- # gr.HTML("""
390
- # <center>
391
-
392
- # <h2>Ready!</h2>
393
-
394
- # </center>
395
- # """)
396
- # # load_btn = gr.Button("Click to Load!🚀")
397
- # # load_text = gr.Textbox()
398
- # # load_btn.click(fn=load_model, outputs=load_text)
399
-
400
- # topic = gr.Textbox(label="Enter the Topic for Quiz", placeholder="Write any topic/details from Customs Manual")
401
-
402
- # with gr.Row():
403
- # radio = gr.Radio(["easy", "average", "hard"], label="How difficult should the quiz be?")
404
-
405
- # generate_quiz_btn = gr.Button("Generate Quiz!🚀")
406
- # quiz_msg = gr.Textbox()
407
-
408
- # question_radios = [gr.Radio(visible=False) for _ in range(10)]
409
-
410
- # generate_quiz_btn.click(
411
- # fn=generate_quiz,
412
- # inputs=[radio, topic],
413
- # outputs=[quiz_msg] + question_radios
414
- # )
415
-
416
- # check_button = gr.Button("Check Score")
417
- # score_textbox = gr.Markdown()
418
-
419
- # check_button.click(
420
- # fn=compare_answers,
421
- # inputs=question_radios,
422
- # outputs=score_textbox
423
- # )
424
-
425
- # demo = gr.TabbedInterface([CHATBOT, QUIZBOT], ["AI ChatBot", "AI Quizbot"])
426
- # demo.queue()
427
- # demo.launch(debug=True)
428
-
429
- RAG_db=gr.State()
430
 
431
  with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green", secondary_hue="green"), css="style.css") as QUIZBOT:
432
  def load_model():
433
- RAG= RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
434
- RAG_db.value=RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
435
  return 'Ready to Go!!'
 
436
  with gr.Column(scale=4):
437
  gr.HTML("""
438
- <center>
439
- <h1><span style="color: purple;">ADWITIYA</span> Customs Manual Quizbot</h1>
440
- <h2>Generative AI-powered Capacity building for Training Officers</h2>
441
- <i>⚠️ NACIN Faculties create quiz from any topic dynamically for classroom evaluation after their sessions ! ⚠️</i>
442
- </center>
443
- """)
444
- #gr.Warning('Retrieving using ColBERT.. First time query will take a minute for model to load..pls wait')
445
  with gr.Column(scale=2):
446
  load_btn = gr.Button("Click to Load!🚀")
447
- load_text=gr.Textbox()
448
- load_btn.click(load_model,[],load_text)
449
-
450
-
451
  topic = gr.Textbox(label="Enter the Topic for Quiz", placeholder="Write any topic/details from Customs Manual")
452
 
453
  with gr.Row():
454
- radio = gr.Radio(
455
- ["easy", "average", "hard"], label="How difficult should the quiz be?"
456
- )
457
-
458
 
459
  generate_quiz_btn = gr.Button("Generate Quiz!🚀")
460
- quiz_msg=gr.Textbox()
461
-
462
- question_radios = [gr.Radio(visible=False), gr.Radio(visible=False), gr.Radio(
463
- visible=False), gr.Radio(visible=False), gr.Radio(visible=False), gr.Radio(visible=False), gr.Radio(visible=False), gr.Radio(
464
- visible=False), gr.Radio(visible=False), gr.Radio(visible=False)]
465
 
466
- print(question_radios)
467
 
468
  @spaces.GPU
469
- @generate_quiz_btn.click(inputs=[radio, topic], outputs=[quiz_msg]+question_radios, api_name="generate_quiz")
470
  def generate_quiz(question_difficulty, topic):
471
- top_k_rank=10
472
- RAG_db_=RAG_db.value
473
- documents_full=RAG_db_.search(topic,k=top_k_rank)
474
- gr.Warning('Generation of Quiz may take 1 to 2 minute. Pls wait')
475
-
476
 
477
- generate_kwargs = dict(
478
- temperature=0.2,
479
- max_new_tokens=4000,
480
- top_p=0.95,
481
- repetition_penalty=1.0,
482
- do_sample=True,
483
- seed=42,
484
- )
485
  question_radio_list = []
486
- count=0
487
- while count<=3:
488
  try:
489
- documents=[item['content'] for item in documents_full]
490
- document_summaries = [f"[DOCUMENT {i+1}]: {summary}{count}" for i, summary in enumerate(documents)]
491
- documents_str='\n'.join(document_summaries)
492
- formatted_prompt = system_instructions(
493
- question_difficulty, topic,documents_str)
494
  print(formatted_prompt)
495
- pre_prompt = [
496
- {"role": "system", "content": formatted_prompt}
497
- ]
498
- response = client.text_generation(
499
- formatted_prompt, **generate_kwargs, stream=False, details=False, return_full_text=False,
 
 
500
  )
501
- output_json = json.loads(f"{response}")
502
 
503
-
504
- print(response)
505
  print('output json', output_json)
506
-
507
  global quiz_data
508
-
509
  quiz_data = output_json
510
-
511
-
512
-
513
  for question_num in range(1, 11):
514
  question_key = f"Q{question_num}"
515
  answer_key = f"A{question_num}"
516
-
517
  question = quiz_data.get(question_key)
518
  answer = quiz_data.get(quiz_data.get(answer_key))
519
-
520
  if not question or not answer:
521
  continue
522
-
523
  choice_keys = [f"{question_key}:C{i}" for i in range(1, 5)]
524
- choice_list = []
525
- for choice_key in choice_keys:
526
- choice = quiz_data.get(choice_key, "Choice not found")
527
- choice_list.append(f"{choice}")
528
-
529
- radio = gr.Radio(choices=choice_list, label=question,
530
- visible=True, interactive=True)
531
-
532
  question_radio_list.append(radio)
533
- if len(question_radio_list)==10:
 
534
  break
535
  else:
536
- print('10 questions not generated . So trying again!')
537
- count+=1
538
  continue
539
  except Exception as e:
540
- count+=1
541
  print(f"Exception occurred: {e}")
542
- if count==3:
543
  print('Retry exhausted')
544
- gr.Warning('Sorry. Pls try with another topic !')
545
  else:
546
- print(f"Trying again..{count} time...please wait")
547
  continue
548
 
549
- print('Question radio list ' , question_radio_list)
550
-
551
- return ['Quiz Generated!']+ question_radio_list
552
 
553
  check_button = gr.Button("Check Score")
554
-
555
  score_textbox = gr.Markdown()
556
 
557
  @check_button.click(inputs=question_radios, outputs=score_textbox)
558
  def compare_answers(*user_answers):
559
- user_anwser_list = []
560
- user_anwser_list = user_answers
561
-
562
  answers_list = []
563
 
564
  for question_num in range(1, 20):
@@ -568,23 +358,16 @@ with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green",
568
  break
569
  answers_list.append(answer)
570
 
571
- score = 0
572
 
573
- for item in user_anwser_list:
574
- if item in answers_list:
575
- score += 1
576
- if score>5:
577
- message = f"### Good ! You got {score} over 10!"
578
- elif score>7:
579
- message = f"### Excellent ! You got {score} over 10!"
580
  else:
581
- message = f"### You got {score} over 10! Dont worry . You can prepare well and try better next time !"
582
 
583
  return message
584
 
585
-
586
-
587
- # demo = gr.TabbedInterface([CHATBOT,QUIZBOT], ["AI ChatBot", "AI Quizbot"])
588
-
589
  QUIZBOT.queue()
590
  QUIZBOT.launch(debug=True)
 
1
 
2
+ # from ragatouille import RAGPretrainedModel
3
+ # import subprocess
4
+ # import json
5
+ # import spaces
6
+ # import firebase_admin
7
+ # from firebase_admin import credentials, firestore
8
+ # import logging
9
+ # from pathlib import Path
10
+ # from time import perf_counter
11
+ # from datetime import datetime
12
+ # import gradio as gr
13
+ # from jinja2 import Environment, FileSystemLoader
14
+ # import numpy as np
15
+ # from sentence_transformers import CrossEncoder
16
+ # from huggingface_hub import InferenceClient
17
+ # from os import getenv
18
+
19
+ # from backend.query_llm import generate_hf, generate_openai
20
+ # from backend.semantic_search import table, retriever
21
+ # from huggingface_hub import InferenceClient
22
+
23
+
24
+ # VECTOR_COLUMN_NAME = "vector"
25
+ # TEXT_COLUMN_NAME = "text"
26
+ # HF_TOKEN = getenv("HUGGING_FACE_HUB_TOKEN")
27
+ # proj_dir = Path(__file__).parent
28
+ # # Setting up the logging
29
+ # logging.basicConfig(level=logging.INFO)
30
+ # logger = logging.getLogger(__name__)
31
+ # client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1",token=HF_TOKEN)
32
+ # # Set up the template environment with the templates directory
33
+ # env = Environment(loader=FileSystemLoader(proj_dir / 'templates'))
34
+
35
+ # # Load the templates directly from the environment
36
+ # template = env.get_template('template.j2')
37
+ # template_html = env.get_template('template_html.j2')
38
+
39
+ # def system_instructions(question_difficulty, topic,documents_str):
40
+ # return f"""<s> [INST] Your are a great teacher and your task is to create 10 questions with 4 choices with a {question_difficulty} difficulty about topic request " {topic} " only from the below given documents, {documents_str} then create an 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#". [/INST]"""
41
+
42
+
43
+ # RAG_db=gr.State()
44
+
45
+ # with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green", secondary_hue="green"), css="style.css") as QUIZBOT:
46
+ # def load_model():
47
+ # RAG= RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
48
+ # RAG_db.value=RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
49
+ # return 'Ready to Go!!'
50
+ # with gr.Column(scale=4):
51
+ # gr.HTML("""
52
+ # <center>
53
+ # <h1><span style="color: purple;">ADWITIYA</span> Customs Manual Quizbot</h1>
54
+ # <h2>Generative AI-powered Capacity building for Training Officers</h2>
55
+ # <i>⚠️ NACIN Faculties create quiz from any topic dynamically for classroom evaluation after their sessions ! ⚠️</i>
56
+ # </center>
57
+ # """)
58
+ # #gr.Warning('Retrieving using ColBERT.. First time query will take a minute for model to load..pls wait')
59
+ # with gr.Column(scale=2):
60
+ # load_btn = gr.Button("Click to Load!🚀")
61
+ # load_text=gr.Textbox()
62
+ # load_btn.click(load_model,[],load_text)
63
+
64
+
65
+ # topic = gr.Textbox(label="Enter the Topic for Quiz", placeholder="Write any topic/details from Customs Manual")
66
+
67
+ # with gr.Row():
68
+ # radio = gr.Radio(
69
+ # ["easy", "average", "hard"], label="How difficult should the quiz be?"
70
+ # )
71
+
72
+
73
+ # generate_quiz_btn = gr.Button("Generate Quiz!🚀")
74
+ # quiz_msg=gr.Textbox()
75
+
76
+ # question_radios = [gr.Radio(visible=False), gr.Radio(visible=False), gr.Radio(
77
+ # visible=False), gr.Radio(visible=False), gr.Radio(visible=False), gr.Radio(visible=False), gr.Radio(visible=False), gr.Radio(
78
+ # visible=False), gr.Radio(visible=False), gr.Radio(visible=False)]
79
+
80
+ # print(question_radios)
81
+
82
+ # @spaces.GPU
83
+ # @generate_quiz_btn.click(inputs=[radio, topic], outputs=[quiz_msg]+question_radios, api_name="generate_quiz")
84
+ # def generate_quiz(question_difficulty, topic):
85
+ # top_k_rank=10
86
+ # RAG_db_=RAG_db.value
87
+ # documents_full=RAG_db_.search(topic,k=top_k_rank)
88
+ # gr.Warning('Generation of Quiz may take 1 to 2 minute. Pls wait')
89
+
90
+
91
+ # generate_kwargs = dict(
92
+ # temperature=0.2,
93
+ # max_new_tokens=4000,
94
+ # top_p=0.95,
95
+ # repetition_penalty=1.0,
96
+ # do_sample=True,
97
+ # seed=42,
98
+ # )
99
+ # question_radio_list = []
100
+ # count=0
101
+ # while count<=3:
102
+ # try:
103
+ # documents=[item['content'] for item in documents_full]
104
+ # document_summaries = [f"[DOCUMENT {i+1}]: {summary}{count}" for i, summary in enumerate(documents)]
105
+ # documents_str='\n'.join(document_summaries)
106
+ # formatted_prompt = system_instructions(
107
+ # question_difficulty, topic,documents_str)
108
+ # print(formatted_prompt)
109
+ # pre_prompt = [
110
+ # {"role": "system", "content": formatted_prompt}
111
+ # ]
112
+ # response = client.text_generation(
113
+ # formatted_prompt, **generate_kwargs, stream=False, details=False, return_full_text=False,
114
+ # )
115
+ # output_json = json.loads(f"{response}")
116
+
117
+
118
+ # print(response)
119
+ # print('output json', output_json)
120
+
121
+ # global quiz_data
122
+
123
+ # quiz_data = output_json
124
+
125
+
126
+
127
+ # for question_num in range(1, 11):
128
+ # question_key = f"Q{question_num}"
129
+ # answer_key = f"A{question_num}"
130
+
131
+ # question = quiz_data.get(question_key)
132
+ # answer = quiz_data.get(quiz_data.get(answer_key))
133
+
134
+ # if not question or not answer:
135
+ # continue
136
+
137
+ # choice_keys = [f"{question_key}:C{i}" for i in range(1, 5)]
138
+ # choice_list = []
139
+ # for choice_key in choice_keys:
140
+ # choice = quiz_data.get(choice_key, "Choice not found")
141
+ # choice_list.append(f"{choice}")
142
+
143
+ # radio = gr.Radio(choices=choice_list, label=question,
144
+ # visible=True, interactive=True)
145
+
146
+ # question_radio_list.append(radio)
147
+ # if len(question_radio_list)==10:
148
+ # break
149
+ # else:
150
+ # print('10 questions not generated . So trying again!')
151
+ # count+=1
152
+ # continue
153
+ # except Exception as e:
154
+ # count+=1
155
+ # print(f"Exception occurred: {e}")
156
+ # if count==3:
157
+ # print('Retry exhausted')
158
+ # gr.Warning('Sorry. Pls try with another topic !')
159
+ # else:
160
+ # print(f"Trying again..{count} time...please wait")
161
+ # continue
162
+
163
+ # print('Question radio list ' , question_radio_list)
164
+
165
+ # return ['Quiz Generated!']+ question_radio_list
166
+
167
+ # check_button = gr.Button("Check Score")
168
+
169
+ # score_textbox = gr.Markdown()
170
+
171
+ # @check_button.click(inputs=question_radios, outputs=score_textbox)
172
+ # def compare_answers(*user_answers):
173
+ # user_anwser_list = []
174
+ # user_anwser_list = user_answers
175
+
176
+ # answers_list = []
177
+
178
+ # for question_num in range(1, 20):
179
+ # answer_key = f"A{question_num}"
180
+ # answer = quiz_data.get(quiz_data.get(answer_key))
181
+ # if not answer:
182
+ # break
183
+ # answers_list.append(answer)
184
+
185
+ # score = 0
186
+
187
+ # for item in user_anwser_list:
188
+ # if item in answers_list:
189
+ # score += 1
190
+ # if score>5:
191
+ # message = f"### Good ! You got {score} over 10!"
192
+ # elif score>7:
193
+ # message = f"### Excellent ! You got {score} over 10!"
194
+ # else:
195
+ # message = f"### You got {score} over 10! Dont worry . You can prepare well and try better next time !"
196
+
197
+ # return message
198
+
199
+
200
+ # QUIZBOT.queue()
201
+ # QUIZBOT.launch(debug=True)
202
+
203
+
204
+ ################################################
205
  from ragatouille import RAGPretrainedModel
206
  import subprocess
207
  import json
 
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#". [/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
 
306
+ output_json = json.loads(f"{response}")
 
307
  print('output json', output_json)
308
+
309
  global quiz_data
 
310
  quiz_data = output_json
311
+
 
 
312
  for question_num in range(1, 11):
313
  question_key = f"Q{question_num}"
314
  answer_key = f"A{question_num}"
315
+
316
  question = quiz_data.get(question_key)
317
  answer = quiz_data.get(quiz_data.get(answer_key))
318
+
319
  if not question or not answer:
320
  continue
321
+
322
  choice_keys = [f"{question_key}:C{i}" for i in range(1, 5)]
323
+ choice_list = [quiz_data.get(choice_key, "Choice not found") for choice_key in choice_keys]
324
+
325
+ radio = gr.Radio(choices=choice_list, label=question, visible=True, interactive=True)
 
 
 
 
 
326
  question_radio_list.append(radio)
327
+
328
+ if len(question_radio_list) == 10:
329
  break
330
  else:
331
+ print('10 questions not generated. Trying again!')
332
+ count += 1
333
  continue
334
  except Exception as e:
335
+ count += 1
336
  print(f"Exception occurred: {e}")
337
+ if count == 3:
338
  print('Retry exhausted')
339
+ gr.Warning('Sorry. Please try with another topic!')
340
  else:
341
+ print(f"Trying again.. {count} time... please wait")
342
  continue
343
 
344
+ return ['Quiz Generated!'] + question_radio_list
 
 
345
 
346
  check_button = gr.Button("Check Score")
 
347
  score_textbox = gr.Markdown()
348
 
349
  @check_button.click(inputs=question_radios, outputs=score_textbox)
350
  def compare_answers(*user_answers):
351
+ user_anwser_list = list(user_answers)
 
 
352
  answers_list = []
353
 
354
  for question_num in range(1, 20):
 
358
  break
359
  answers_list.append(answer)
360
 
361
+ score = sum(1 for item in user_anwser_list if item in answers_list)
362
 
363
+ if score > 5:
364
+ message = f"### Good! You got {score} out of 10!"
365
+ elif score > 7:
366
+ message = f"### Excellent! You got {score} out of 10!"
 
 
 
367
  else:
368
+ message = f"### You got {score} out of 10! Don't worry. You can prepare well and try better next time!"
369
 
370
  return message
371
 
 
 
 
 
372
  QUIZBOT.queue()
373
  QUIZBOT.launch(debug=True)