Spaces:
Sleeping
Sleeping
NCTCMumbai
commited on
Commit
•
2e855f8
1
Parent(s):
120cf5e
Update app.py
Browse files
app.py
CHANGED
@@ -103,488 +103,488 @@ examples = ['My transhipment cargo is missing','can u explain and tabulate diffe
|
|
103 |
# })
|
104 |
|
105 |
|
106 |
-
def add_text(history, text):
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
|
111 |
|
112 |
-
def bot(history, cross_encoder):
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
|
121 |
-
|
122 |
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
|
135 |
-
|
136 |
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
|
151 |
|
152 |
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
|
168 |
-
|
169 |
-
|
170 |
|
171 |
-
|
172 |
-
|
173 |
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
|
178 |
-
|
179 |
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
def system_instructions(question_difficulty, topic,documents_str):
|
188 |
-
|
189 |
-
|
190 |
-
RAG_db = gr.State()
|
191 |
-
|
192 |
-
def load_model():
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
|
202 |
|
203 |
-
def generate_quiz(question_difficulty, topic):
|
204 |
-
|
205 |
-
|
206 |
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
|
216 |
-
|
217 |
-
|
218 |
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
|
245 |
-
|
246 |
-
|
247 |
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
|
254 |
-
|
255 |
-
|
256 |
|
257 |
-
|
258 |
-
|
259 |
|
260 |
-
|
261 |
-
|
262 |
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
|
274 |
-
|
275 |
|
276 |
-
def compare_answers(*user_answers):
|
277 |
-
|
278 |
-
|
279 |
|
280 |
-
|
281 |
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
|
289 |
-
|
290 |
|
291 |
-
#with gr.Blocks(theme='Insuz/SimpleIndigo') as demo:
|
292 |
-
with gr.Blocks(theme='NoCrypt/miku') as CHATBOT:
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
|
314 |
-
|
315 |
-
|
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 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
|
355 |
-
|
356 |
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
|
362 |
-
|
363 |
-
|
364 |
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
|
369 |
-
|
370 |
-
|
371 |
|
372 |
-
|
373 |
-
|
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 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
|
392 |
-
|
393 |
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
|
400 |
-
|
401 |
|
402 |
-
|
403 |
-
|
404 |
|
405 |
-
|
406 |
-
|
407 |
|
408 |
-
|
409 |
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
|
416 |
-
|
417 |
-
|
418 |
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
|
425 |
-
demo = gr.TabbedInterface([CHATBOT, QUIZBOT], ["AI ChatBot", "AI Quizbot"])
|
426 |
-
demo.queue()
|
427 |
-
demo.launch(debug=True)
|
428 |
|
429 |
-
|
430 |
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
#
|
445 |
-
|
446 |
-
|
447 |
-
|
448 |
-
|
449 |
|
450 |
|
451 |
-
|
452 |
|
453 |
-
|
454 |
-
|
455 |
-
|
456 |
-
|
457 |
|
458 |
|
459 |
-
|
460 |
-
|
461 |
|
462 |
-
|
463 |
-
|
464 |
-
|
465 |
|
466 |
-
|
467 |
|
468 |
-
|
469 |
-
|
470 |
-
|
471 |
-
|
472 |
-
|
473 |
-
|
474 |
|
475 |
|
476 |
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
-
|
492 |
-
|
493 |
-
|
494 |
-
|
495 |
-
|
496 |
-
|
497 |
-
|
498 |
-
|
499 |
-
|
500 |
-
|
501 |
-
|
502 |
|
503 |
|
504 |
-
|
505 |
-
|
506 |
|
507 |
-
|
508 |
|
509 |
-
|
510 |
|
511 |
|
512 |
|
513 |
-
|
514 |
-
|
515 |
-
|
516 |
|
517 |
-
|
518 |
-
|
519 |
|
520 |
-
|
521 |
-
|
522 |
|
523 |
-
|
524 |
-
|
525 |
-
|
526 |
-
|
527 |
-
|
528 |
|
529 |
-
|
530 |
-
|
531 |
|
532 |
-
|
533 |
-
|
534 |
-
|
535 |
-
|
536 |
-
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
|
542 |
-
|
543 |
-
|
544 |
-
|
545 |
-
|
546 |
-
|
547 |
-
|
548 |
|
549 |
-
|
550 |
|
551 |
-
|
552 |
|
553 |
-
|
554 |
|
555 |
-
|
556 |
|
557 |
-
|
558 |
-
|
559 |
-
|
560 |
-
|
561 |
|
562 |
-
|
563 |
|
564 |
-
|
565 |
-
|
566 |
-
|
567 |
-
|
568 |
-
|
569 |
-
|
570 |
|
571 |
-
|
572 |
|
573 |
-
|
574 |
-
|
575 |
-
|
576 |
-
|
577 |
-
|
578 |
-
|
579 |
-
|
580 |
-
|
581 |
-
|
582 |
|
583 |
-
|
584 |
|
585 |
|
586 |
|
587 |
# demo = gr.TabbedInterface([CHATBOT,QUIZBOT], ["AI ChatBot", "AI Quizbot"])
|
588 |
|
589 |
-
|
590 |
-
|
|
|
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 |
|
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):
|
565 |
+
answer_key = f"A{question_num}"
|
566 |
+
answer = quiz_data.get(quiz_data.get(answer_key))
|
567 |
+
if not answer:
|
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)
|