rameshmoorthy
commited on
Commit
•
b193cd4
1
Parent(s):
676f4e3
Update app.py
Browse files
app.py
CHANGED
@@ -179,9 +179,12 @@ def bot(history, cross_encoder):
|
|
179 |
yield history, prompt_html
|
180 |
#store_message(db,history[-1][0],history[-1][1],cross_encoder)
|
181 |
|
|
|
|
|
|
|
182 |
|
183 |
#with gr.Blocks(theme='Insuz/SimpleIndigo') as demo:
|
184 |
-
with gr.Blocks(theme='NoCrypt/miku') as
|
185 |
with gr.Row():
|
186 |
with gr.Column(scale=10):
|
187 |
# gr.Markdown(
|
@@ -264,5 +267,153 @@ with gr.Blocks(theme='NoCrypt/miku') as demo:
|
|
264 |
# Examples
|
265 |
gr.Examples(examples, txt)
|
266 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
267 |
demo.queue()
|
268 |
demo.launch(debug=True)
|
|
|
179 |
yield history, prompt_html
|
180 |
#store_message(db,history[-1][0],history[-1][1],cross_encoder)
|
181 |
|
182 |
+
def system_instructions(question_difficulty, topic,document_summaries):
|
183 |
+
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, {'\n'.join(document_summaries)} 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]"""
|
184 |
+
|
185 |
|
186 |
#with gr.Blocks(theme='Insuz/SimpleIndigo') as demo:
|
187 |
+
with gr.Blocks(theme='NoCrypt/miku') as CHATBOT:
|
188 |
with gr.Row():
|
189 |
with gr.Column(scale=10):
|
190 |
# gr.Markdown(
|
|
|
267 |
# Examples
|
268 |
gr.Examples(examples, txt)
|
269 |
|
270 |
+
|
271 |
+
RAG_db=gr.State()
|
272 |
+
|
273 |
+
with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green", secondary_hue="green"), css="style.css") as QUIZBOT:
|
274 |
+
def load_model():
|
275 |
+
RAG= RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
|
276 |
+
RAG_db.value=RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
|
277 |
+
return 'Ready to Go!!'
|
278 |
+
with gr.Columns(scale=4):
|
279 |
+
gr.HTML("""
|
280 |
+
<center>
|
281 |
+
<h1><span style="color: purple;">AI NANBAN</span> - CBSE Class Quiz Maker</h1>
|
282 |
+
<h2>AI-powered Learning Game</h2>
|
283 |
+
<i>⚠️ Students create quiz from any topic /CBSE Chapter ! ⚠️</i>
|
284 |
+
</center>
|
285 |
+
""")
|
286 |
+
#gr.Warning('Retrieving using ColBERT.. First time query will take a minute for model to load..pls wait')
|
287 |
+
with gr.Columns(scale=2):
|
288 |
+
load_btn = gr.Button("Click to Load!🚀")
|
289 |
+
load_text=gr.Textbox()
|
290 |
+
load_btn.click(load_model,[],load_text)
|
291 |
+
|
292 |
+
|
293 |
+
topic = gr.Textbox(label="Enter the Topic for Quiz", placeholder="Write any topic from CBSE notes")
|
294 |
+
|
295 |
+
with gr.Row():
|
296 |
+
radio = gr.Radio(
|
297 |
+
["easy", "average", "hard"], label="How difficult should the quiz be?"
|
298 |
+
)
|
299 |
+
|
300 |
+
|
301 |
+
generate_quiz_btn = gr.Button("Generate Quiz!🚀")
|
302 |
+
|
303 |
+
question_radios = [gr.Radio(visible=False), gr.Radio(visible=False), gr.Radio(
|
304 |
+
visible=False), gr.Radio(visible=False), gr.Radio(visible=False), gr.Radio(visible=False), gr.Radio(visible=False), gr.Radio(
|
305 |
+
visible=False), gr.Radio(visible=False), gr.Radio(visible=False)]
|
306 |
+
|
307 |
+
print(question_radios)
|
308 |
+
|
309 |
+
@spaces.GPU
|
310 |
+
@generate_quiz_btn.click(inputs=[radio, topic], outputs=question_radios, api_name="generate_quiz")
|
311 |
+
def generate_quiz(question_difficulty, topic, user_prompt):
|
312 |
+
top_k_rank=10
|
313 |
+
RAG_db_=RAG_db.value
|
314 |
+
documents_full=RAG_db_.search(topic,k=top_k_rank)
|
315 |
+
|
316 |
+
documents=[item['content'] for item in documents_full]
|
317 |
+
document_summaries = [f"[DOCUMENT {i+1}]: {summary}" for i, summary in enumerate(documents)]
|
318 |
+
formatted_prompt = system_instructions(
|
319 |
+
question_difficulty, topic,document_summaries)
|
320 |
+
print(formatted_prompt)
|
321 |
+
pre_prompt = [
|
322 |
+
{"role": "system", "content": formatted_prompt}
|
323 |
+
]
|
324 |
+
|
325 |
+
generate_kwargs = dict(
|
326 |
+
temperature=0.2,
|
327 |
+
max_new_tokens=4000,
|
328 |
+
top_p=0.95,
|
329 |
+
repetition_penalty=1.0,
|
330 |
+
do_sample=True,
|
331 |
+
seed=42,
|
332 |
+
)
|
333 |
+
while True:
|
334 |
+
try:
|
335 |
+
response = client.text_generation(
|
336 |
+
formatted_prompt, **generate_kwargs, stream=False, details=False, return_full_text=False,
|
337 |
+
)
|
338 |
+
output_json = json.loads(f"{response}")
|
339 |
+
break
|
340 |
+
except Exception as e:
|
341 |
+
print(f"Exception occurred: {e}")
|
342 |
+
print("Trying again...please wait")
|
343 |
+
continue
|
344 |
+
response = client.text_generation(
|
345 |
+
formatted_prompt, **generate_kwargs, stream=False, details=False, return_full_text=False,
|
346 |
+
)
|
347 |
+
|
348 |
+
print(response)
|
349 |
+
print('output json', output_json)
|
350 |
+
|
351 |
+
global quiz_data
|
352 |
+
|
353 |
+
quiz_data = output_json
|
354 |
+
|
355 |
+
question_radio_list = []
|
356 |
+
|
357 |
+
for question_num in range(1, 11):
|
358 |
+
question_key = f"Q{question_num}"
|
359 |
+
answer_key = f"A{question_num}"
|
360 |
+
|
361 |
+
question = quiz_data.get(question_key)
|
362 |
+
answer = quiz_data.get(quiz_data.get(answer_key))
|
363 |
+
|
364 |
+
if not question or not answer:
|
365 |
+
continue
|
366 |
+
|
367 |
+
choice_keys = [f"{question_key}:C{i}" for i in range(1, 5)]
|
368 |
+
choice_list = []
|
369 |
+
for choice_key in choice_keys:
|
370 |
+
choice = quiz_data.get(choice_key, "Choice not found")
|
371 |
+
choice_list.append(f"{choice}")
|
372 |
+
|
373 |
+
radio = gr.Radio(choices=choice_list, label=question,
|
374 |
+
visible=True, interactive=True)
|
375 |
+
|
376 |
+
question_radio_list.append(radio)
|
377 |
+
|
378 |
+
print('Question radio list ' , question_radio_list)
|
379 |
+
|
380 |
+
return question_radio_list
|
381 |
+
|
382 |
+
check_button = gr.Button("Check Score")
|
383 |
+
|
384 |
+
score_textbox = gr.Markdown()
|
385 |
+
|
386 |
+
@check_button.click(inputs=question_radios, outputs=score_textbox)
|
387 |
+
def compare_answers(*user_answers):
|
388 |
+
user_anwser_list = []
|
389 |
+
user_anwser_list = user_answers
|
390 |
+
|
391 |
+
answers_list = []
|
392 |
+
|
393 |
+
for question_num in range(1, 20):
|
394 |
+
answer_key = f"A{question_num}"
|
395 |
+
answer = quiz_data.get(quiz_data.get(answer_key))
|
396 |
+
if not answer:
|
397 |
+
break
|
398 |
+
answers_list.append(answer)
|
399 |
+
|
400 |
+
score = 0
|
401 |
+
|
402 |
+
for item in user_anwser_list:
|
403 |
+
if item in answers_list:
|
404 |
+
score += 1
|
405 |
+
if score>5:
|
406 |
+
message = f"### Good ! You got {score} over 10!"
|
407 |
+
elif score>7:
|
408 |
+
message = f"### Excellent ! You got {score} over 10!"
|
409 |
+
else:
|
410 |
+
message = f"### You got {score} over 10! Dont worry . You can prepare well and try better next time !"
|
411 |
+
|
412 |
+
return message
|
413 |
+
|
414 |
+
|
415 |
+
|
416 |
+
demo = gr.TabbedInterface([CHATBOT,QUIZBOT], ["AI ChatBot", "AI Nanban-Quizbot"])
|
417 |
+
|
418 |
demo.queue()
|
419 |
demo.launch(debug=True)
|