NCTCMumbai
commited on
Commit
•
1fad4eb
1
Parent(s):
d668f09
Update app.py
Browse files
app.py
CHANGED
@@ -187,6 +187,98 @@ def bot(history, cross_encoder):
|
|
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 |
|
191 |
#with gr.Blocks(theme='Insuz/SimpleIndigo') as demo:
|
192 |
with gr.Blocks(theme='NoCrypt/miku') as CHATBOT:
|
@@ -274,89 +366,6 @@ with gr.Blocks(theme='NoCrypt/miku') as CHATBOT:
|
|
274 |
|
275 |
|
276 |
|
277 |
-
RAG_db = gr.State()
|
278 |
-
|
279 |
-
def load_model():
|
280 |
-
RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
|
281 |
-
RAG_db.value = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
|
282 |
-
return 'Ready to Go!!'
|
283 |
-
|
284 |
-
def generate_quiz(question_difficulty, topic):
|
285 |
-
top_k_rank = 10
|
286 |
-
RAG_db_ = RAG_db.value
|
287 |
-
documents_full = RAG_db_.search(topic, k=top_k_rank)
|
288 |
-
|
289 |
-
generate_kwargs = dict(
|
290 |
-
temperature=0.2,
|
291 |
-
max_new_tokens=4000,
|
292 |
-
top_p=0.95,
|
293 |
-
repetition_penalty=1.0,
|
294 |
-
do_sample=True,
|
295 |
-
seed=42,
|
296 |
-
)
|
297 |
-
|
298 |
-
question_radio_list = []
|
299 |
-
count = 0
|
300 |
-
while count <= 3:
|
301 |
-
try:
|
302 |
-
documents = [item['content'] for item in documents_full]
|
303 |
-
document_summaries = [f"[DOCUMENT {i+1}]: {summary}{count}" for i, summary in enumerate(documents)]
|
304 |
-
documents_str = '\n'.join(document_summaries)
|
305 |
-
formatted_prompt = system_instructions(question_difficulty, topic, documents_str)
|
306 |
-
|
307 |
-
pre_prompt = [
|
308 |
-
{"role": "system", "content": formatted_prompt}
|
309 |
-
]
|
310 |
-
response = client.text_generation(
|
311 |
-
formatted_prompt, **generate_kwargs, stream=False, details=False, return_full_text=False,
|
312 |
-
)
|
313 |
-
output_json = json.loads(f"{response}")
|
314 |
-
|
315 |
-
global quiz_data
|
316 |
-
quiz_data = output_json
|
317 |
-
|
318 |
-
for question_num in range(1, 11):
|
319 |
-
question_key = f"Q{question_num}"
|
320 |
-
answer_key = f"A{question_num}"
|
321 |
-
question = quiz_data.get(question_key)
|
322 |
-
answer = quiz_data.get(quiz_data.get(answer_key))
|
323 |
-
|
324 |
-
if not question or not answer:
|
325 |
-
continue
|
326 |
-
|
327 |
-
choice_keys = [f"{question_key}:C{i}" for i in range(1, 5)]
|
328 |
-
choice_list = [quiz_data.get(choice_key, "Choice not found") for choice_key in choice_keys]
|
329 |
-
|
330 |
-
radio = gr.Radio(choices=choice_list, label=question, visible=True, interactive=True)
|
331 |
-
question_radio_list.append(radio)
|
332 |
-
|
333 |
-
if len(question_radio_list) == 10:
|
334 |
-
break
|
335 |
-
else:
|
336 |
-
count += 1
|
337 |
-
continue
|
338 |
-
except Exception as e:
|
339 |
-
count += 1
|
340 |
-
if count == 3:
|
341 |
-
return ['Sorry. Pls try with another topic!'] + [gr.Radio(visible=False) for _ in range(10)]
|
342 |
-
continue
|
343 |
-
|
344 |
-
return ['Quiz Generated!'] + question_radio_list
|
345 |
-
|
346 |
-
def compare_answers(*user_answers):
|
347 |
-
user_answer_list = user_answers
|
348 |
-
answers_list = [quiz_data.get(quiz_data.get(f"A{question_num}")) for question_num in range(1, 11)]
|
349 |
-
|
350 |
-
score = sum(1 for answer in user_answer_list if answer in answers_list)
|
351 |
-
|
352 |
-
if score > 7:
|
353 |
-
message = f"### Excellent! You got {score} out of 10!"
|
354 |
-
elif score > 5:
|
355 |
-
message = f"### Good! You got {score} out of 10!"
|
356 |
-
else:
|
357 |
-
message = f"### You got {score} out of 10! Don’t worry, you can prepare well and try better next time!"
|
358 |
-
|
359 |
-
return message
|
360 |
|
361 |
with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green", secondary_hue="green"), css="style.css") as QUIZBOT:
|
362 |
with gr.Column(scale=4):
|
|
|
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 |
+
RAG_db_ = RAG_db.value
|
209 |
+
documents_full = RAG_db_.search(topic, k=top_k_rank)
|
210 |
+
|
211 |
+
generate_kwargs = dict(
|
212 |
+
temperature=0.2,
|
213 |
+
max_new_tokens=4000,
|
214 |
+
top_p=0.95,
|
215 |
+
repetition_penalty=1.0,
|
216 |
+
do_sample=True,
|
217 |
+
seed=42,
|
218 |
+
)
|
219 |
+
|
220 |
+
question_radio_list = []
|
221 |
+
count = 0
|
222 |
+
while count <= 3:
|
223 |
+
try:
|
224 |
+
documents = [item['content'] for item in documents_full]
|
225 |
+
document_summaries = [f"[DOCUMENT {i+1}]: {summary}{count}" for i, summary in enumerate(documents)]
|
226 |
+
documents_str = '\n'.join(document_summaries)
|
227 |
+
formatted_prompt = system_instructions(question_difficulty, topic, documents_str)
|
228 |
+
|
229 |
+
pre_prompt = [
|
230 |
+
{"role": "system", "content": formatted_prompt}
|
231 |
+
]
|
232 |
+
response = client.text_generation(
|
233 |
+
formatted_prompt, **generate_kwargs, stream=False, details=False, return_full_text=False,
|
234 |
+
)
|
235 |
+
output_json = json.loads(f"{response}")
|
236 |
+
|
237 |
+
global quiz_data
|
238 |
+
quiz_data = output_json
|
239 |
+
|
240 |
+
for question_num in range(1, 11):
|
241 |
+
question_key = f"Q{question_num}"
|
242 |
+
answer_key = f"A{question_num}"
|
243 |
+
question = quiz_data.get(question_key)
|
244 |
+
answer = quiz_data.get(quiz_data.get(answer_key))
|
245 |
+
|
246 |
+
if not question or not answer:
|
247 |
+
continue
|
248 |
+
|
249 |
+
choice_keys = [f"{question_key}:C{i}" for i in range(1, 5)]
|
250 |
+
choice_list = [quiz_data.get(choice_key, "Choice not found") for choice_key in choice_keys]
|
251 |
+
|
252 |
+
radio = gr.Radio(choices=choice_list, label=question, visible=True, interactive=True)
|
253 |
+
question_radio_list.append(radio)
|
254 |
+
|
255 |
+
if len(question_radio_list) == 10:
|
256 |
+
break
|
257 |
+
else:
|
258 |
+
count += 1
|
259 |
+
continue
|
260 |
+
except Exception as e:
|
261 |
+
count += 1
|
262 |
+
if count == 3:
|
263 |
+
return ['Sorry. Pls try with another topic!'] + [gr.Radio(visible=False) for _ in range(10)]
|
264 |
+
continue
|
265 |
+
|
266 |
+
return ['Quiz Generated!'] + question_radio_list
|
267 |
+
|
268 |
+
def compare_answers(*user_answers):
|
269 |
+
user_answer_list = user_answers
|
270 |
+
answers_list = [quiz_data.get(quiz_data.get(f"A{question_num}")) for question_num in range(1, 11)]
|
271 |
+
|
272 |
+
score = sum(1 for answer in user_answer_list if answer in answers_list)
|
273 |
+
|
274 |
+
if score > 7:
|
275 |
+
message = f"### Excellent! You got {score} out of 10!"
|
276 |
+
elif score > 5:
|
277 |
+
message = f"### Good! You got {score} out of 10!"
|
278 |
+
else:
|
279 |
+
message = f"### You got {score} out of 10! Don’t worry, you can prepare well and try better next time!"
|
280 |
+
|
281 |
+
return message
|
282 |
|
283 |
#with gr.Blocks(theme='Insuz/SimpleIndigo') as demo:
|
284 |
with gr.Blocks(theme='NoCrypt/miku') as CHATBOT:
|
|
|
366 |
|
367 |
|
368 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
369 |
|
370 |
with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green", secondary_hue="green"), css="style.css") as QUIZBOT:
|
371 |
with gr.Column(scale=4):
|