Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -118,7 +118,7 @@ def bot(history, api_kind):
|
|
118 |
print('An unexpected error occurred during generation:', str(e))
|
119 |
yield f"An unexpected error occurred during generation: {str(e)}"
|
120 |
|
121 |
-
with gr.Blocks(theme='WeixuanYuan/Soft_dark') as
|
122 |
# Beautiful heading with logo
|
123 |
gr.HTML(value="""
|
124 |
<div style="display: flex; align-items: center; justify-content: space-between;">
|
@@ -173,6 +173,173 @@ with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
|
|
173 |
|
174 |
# Examples
|
175 |
gr.Examples(examples, txt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
|
177 |
demo.queue()
|
178 |
demo.launch(debug=True)
|
|
|
118 |
print('An unexpected error occurred during generation:', str(e))
|
119 |
yield f"An unexpected error occurred during generation: {str(e)}"
|
120 |
|
121 |
+
with gr.Blocks(theme='WeixuanYuan/Soft_dark') as CHATBOT:
|
122 |
# Beautiful heading with logo
|
123 |
gr.HTML(value="""
|
124 |
<div style="display: flex; align-items: center; justify-content: space-between;">
|
|
|
173 |
|
174 |
# Examples
|
175 |
gr.Examples(examples, txt)
|
176 |
+
|
177 |
+
# QUIZBOT CODE
|
178 |
+
RAG_db=gr.State()
|
179 |
+
|
180 |
+
|
181 |
+
|
182 |
+
with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green", secondary_hue="green"), css="style.css") as QUIZBOT:
|
183 |
+
def system_instructions(question_difficulty, topic,documents_str):
|
184 |
+
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]"""
|
185 |
+
|
186 |
+
def load_model():
|
187 |
+
RAG= RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
|
188 |
+
RAG_db.value=RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
|
189 |
+
return 'Ready to Go!!'
|
190 |
+
with gr.Column(scale=4):
|
191 |
+
gr.HTML("""
|
192 |
+
<center>
|
193 |
+
<h1><span style="color: purple;">AI NANBAN</span> - CBSE Class Quiz Maker</h1>
|
194 |
+
<h2>AI-powered Learning Game</h2>
|
195 |
+
<i>⚠️ Students create quiz from any topic /CBSE Chapter ! ⚠️</i>
|
196 |
+
</center>
|
197 |
+
""")
|
198 |
+
#gr.Warning('Retrieving using ColBERT.. First time query will take a minute for model to load..pls wait')
|
199 |
+
with gr.Column(scale=2):
|
200 |
+
load_btn = gr.Button("Click to Load!🚀")
|
201 |
+
load_text=gr.Textbox()
|
202 |
+
load_btn.click(load_model,[],load_text)
|
203 |
+
|
204 |
+
|
205 |
+
topic = gr.Textbox(label="Enter the Topic for Quiz", placeholder="Write any topic from CBSE notes")
|
206 |
+
|
207 |
+
with gr.Row():
|
208 |
+
radio = gr.Radio(
|
209 |
+
["easy", "average", "hard"], label="How difficult should the quiz be?"
|
210 |
+
)
|
211 |
+
|
212 |
+
|
213 |
+
generate_quiz_btn = gr.Button("Generate Quiz!🚀")
|
214 |
+
quiz_msg=gr.Textbox()
|
215 |
+
|
216 |
+
question_radios = [gr.Radio(visible=False), gr.Radio(visible=False), gr.Radio(
|
217 |
+
visible=False), gr.Radio(visible=False), gr.Radio(visible=False), gr.Radio(visible=False), gr.Radio(visible=False), gr.Radio(
|
218 |
+
visible=False), gr.Radio(visible=False), gr.Radio(visible=False)]
|
219 |
+
|
220 |
+
print(question_radios)
|
221 |
+
|
222 |
+
@spaces.GPU
|
223 |
+
@generate_quiz_btn.click(inputs=[radio, topic], outputs=[quiz_msg]+question_radios, api_name="generate_quiz")
|
224 |
+
def generate_quiz(question_difficulty, topic):
|
225 |
+
top_k_rank=10
|
226 |
+
RAG_db_=RAG_db.value
|
227 |
+
documents_full=RAG_db_.search(topic,k=top_k_rank)
|
228 |
+
|
229 |
+
|
230 |
+
|
231 |
+
generate_kwargs = dict(
|
232 |
+
temperature=0.2,
|
233 |
+
max_new_tokens=4000,
|
234 |
+
top_p=0.95,
|
235 |
+
repetition_penalty=1.0,
|
236 |
+
do_sample=True,
|
237 |
+
seed=42,
|
238 |
+
)
|
239 |
+
question_radio_list = []
|
240 |
+
count=0
|
241 |
+
while count<=3:
|
242 |
+
try:
|
243 |
+
documents=[item['content'] for item in documents_full]
|
244 |
+
document_summaries = [f"[DOCUMENT {i+1}]: {summary}{count}" for i, summary in enumerate(documents)]
|
245 |
+
documents_str='\n'.join(document_summaries)
|
246 |
+
formatted_prompt = system_instructions(
|
247 |
+
question_difficulty, topic,documents_str)
|
248 |
+
print(formatted_prompt)
|
249 |
+
pre_prompt = [
|
250 |
+
{"role": "system", "content": formatted_prompt}
|
251 |
+
]
|
252 |
+
response = client.text_generation(
|
253 |
+
formatted_prompt, **generate_kwargs, stream=False, details=False, return_full_text=False,
|
254 |
+
)
|
255 |
+
output_json = json.loads(f"{response}")
|
256 |
+
|
257 |
+
|
258 |
+
print(response)
|
259 |
+
print('output json', output_json)
|
260 |
+
|
261 |
+
global quiz_data
|
262 |
+
|
263 |
+
quiz_data = output_json
|
264 |
+
|
265 |
+
|
266 |
+
|
267 |
+
for question_num in range(1, 11):
|
268 |
+
question_key = f"Q{question_num}"
|
269 |
+
answer_key = f"A{question_num}"
|
270 |
+
|
271 |
+
question = quiz_data.get(question_key)
|
272 |
+
answer = quiz_data.get(quiz_data.get(answer_key))
|
273 |
+
|
274 |
+
if not question or not answer:
|
275 |
+
continue
|
276 |
+
|
277 |
+
choice_keys = [f"{question_key}:C{i}" for i in range(1, 5)]
|
278 |
+
choice_list = []
|
279 |
+
for choice_key in choice_keys:
|
280 |
+
choice = quiz_data.get(choice_key, "Choice not found")
|
281 |
+
choice_list.append(f"{choice}")
|
282 |
+
|
283 |
+
radio = gr.Radio(choices=choice_list, label=question,
|
284 |
+
visible=True, interactive=True)
|
285 |
+
|
286 |
+
question_radio_list.append(radio)
|
287 |
+
if len(question_radio_list)==10:
|
288 |
+
break
|
289 |
+
else:
|
290 |
+
print('10 questions not generated . So trying again!')
|
291 |
+
count+=1
|
292 |
+
continue
|
293 |
+
except Exception as e:
|
294 |
+
count+=1
|
295 |
+
print(f"Exception occurred: {e}")
|
296 |
+
if count==3:
|
297 |
+
print('Retry exhausted')
|
298 |
+
gr.Warning('Sorry. Pls try with another topic !')
|
299 |
+
else:
|
300 |
+
print(f"Trying again..{count} time...please wait")
|
301 |
+
continue
|
302 |
+
|
303 |
+
print('Question radio list ' , question_radio_list)
|
304 |
+
|
305 |
+
return ['Quiz Generated!']+ question_radio_list
|
306 |
+
|
307 |
+
check_button = gr.Button("Check Score")
|
308 |
+
|
309 |
+
score_textbox = gr.Markdown()
|
310 |
+
|
311 |
+
@check_button.click(inputs=question_radios, outputs=score_textbox)
|
312 |
+
def compare_answers(*user_answers):
|
313 |
+
user_anwser_list = []
|
314 |
+
user_anwser_list = user_answers
|
315 |
+
|
316 |
+
answers_list = []
|
317 |
+
|
318 |
+
for question_num in range(1, 20):
|
319 |
+
answer_key = f"A{question_num}"
|
320 |
+
answer = quiz_data.get(quiz_data.get(answer_key))
|
321 |
+
if not answer:
|
322 |
+
break
|
323 |
+
answers_list.append(answer)
|
324 |
+
|
325 |
+
score = 0
|
326 |
+
|
327 |
+
for item in user_anwser_list:
|
328 |
+
if item in answers_list:
|
329 |
+
score += 1
|
330 |
+
if score>5:
|
331 |
+
message = f"### Good ! You got {score} over 10!"
|
332 |
+
elif score>7:
|
333 |
+
message = f"### Excellent ! You got {score} over 10!"
|
334 |
+
else:
|
335 |
+
message = f"### You got {score} over 10! Dont worry . You can prepare well and try better next time !"
|
336 |
+
|
337 |
+
return message
|
338 |
+
|
339 |
+
|
340 |
+
|
341 |
+
demo = gr.TabbedInterface([CHATBOT,QUIZBOT], ["AI ChatBot", "AI Nanban-Quizbot"])
|
342 |
+
|
343 |
|
344 |
demo.queue()
|
345 |
demo.launch(debug=True)
|