Spaces:
Running
Running
capradeepgujaran
commited on
Commit
•
4feffac
1
Parent(s):
a623e85
Update app.py
Browse files
app.py
CHANGED
@@ -534,11 +534,11 @@ def create_quiz_interface():
|
|
534 |
quiz_app = QuizApp(os.getenv("GROQ_API_KEY"))
|
535 |
|
536 |
with gr.Blocks(title="CertifyMe AI", theme=gr.themes.Soft()) as demo:
|
537 |
-
|
538 |
# State management
|
539 |
current_questions = gr.State([])
|
|
|
540 |
|
541 |
-
#
|
542 |
gr.Markdown("""
|
543 |
# 🎓 CertifyMe AI
|
544 |
### Transform Your Knowledge into Recognized Achievements
|
@@ -572,14 +572,29 @@ def create_quiz_interface():
|
|
572 |
generate_btn = gr.Button("Generate Assessment", variant="primary", size="lg")
|
573 |
|
574 |
# Assessment Tab
|
575 |
-
with gr.Tab("📝 Step 2: Take Assessment"):
|
576 |
-
|
577 |
-
|
578 |
-
|
579 |
-
|
580 |
-
for
|
581 |
-
|
582 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
583 |
|
584 |
# Certification Tab
|
585 |
with gr.Tab("🎓 Step 3: Get Certified"):
|
@@ -590,38 +605,122 @@ def create_quiz_interface():
|
|
590 |
value="Professional Assessment Certification"
|
591 |
)
|
592 |
certificate_display = gr.Image(label="Your Certificate")
|
593 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
594 |
# Event handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
595 |
|
596 |
generate_btn.click(
|
597 |
-
fn=
|
598 |
inputs=[text_input, num_questions],
|
599 |
outputs=[
|
600 |
-
|
601 |
-
|
602 |
-
|
603 |
current_questions,
|
|
|
604 |
tabs
|
605 |
]
|
606 |
)
|
607 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
608 |
submit_btn.click(
|
609 |
-
fn=
|
610 |
-
inputs=[
|
611 |
outputs=[
|
612 |
feedback_box,
|
613 |
-
|
614 |
score_display,
|
615 |
result_message,
|
616 |
tabs
|
617 |
]
|
618 |
)
|
619 |
-
|
620 |
-
score_display.change(
|
621 |
-
fn=quiz_app.certificate_generator.generate,
|
622 |
-
inputs=[score_display, name, course_name, company_logo, participant_photo],
|
623 |
-
outputs=certificate_display
|
624 |
-
)
|
625 |
|
626 |
return demo
|
627 |
|
|
|
534 |
quiz_app = QuizApp(os.getenv("GROQ_API_KEY"))
|
535 |
|
536 |
with gr.Blocks(title="CertifyMe AI", theme=gr.themes.Soft()) as demo:
|
|
|
537 |
# State management
|
538 |
current_questions = gr.State([])
|
539 |
+
current_question_idx = gr.State(0)
|
540 |
|
541 |
+
# Header
|
542 |
gr.Markdown("""
|
543 |
# 🎓 CertifyMe AI
|
544 |
### Transform Your Knowledge into Recognized Achievements
|
|
|
572 |
generate_btn = gr.Button("Generate Assessment", variant="primary", size="lg")
|
573 |
|
574 |
# Assessment Tab
|
575 |
+
with gr.Tab("📝 Step 2: Take Assessment") as assessment_tab:
|
576 |
+
with gr.Box(visible=True):
|
577 |
+
# Question display
|
578 |
+
question_display = gr.Markdown("", label="Current Question")
|
579 |
+
|
580 |
+
# Single radio group for current question
|
581 |
+
current_options = gr.Radio(
|
582 |
+
choices=[],
|
583 |
+
label="Select your answer",
|
584 |
+
visible=False
|
585 |
+
)
|
586 |
+
|
587 |
+
# Navigation
|
588 |
+
with gr.Row():
|
589 |
+
prev_btn = gr.Button("← Previous", variant="secondary", size="sm")
|
590 |
+
question_counter = gr.Markdown("Question 1 of 3")
|
591 |
+
next_btn = gr.Button("Next →", variant="secondary", size="sm")
|
592 |
+
|
593 |
+
submit_btn = gr.Button("Submit Assessment", variant="primary", size="lg")
|
594 |
+
|
595 |
+
# Results section
|
596 |
+
with gr.Group(visible=False) as results_group:
|
597 |
+
feedback_box = gr.Markdown("")
|
598 |
|
599 |
# Certification Tab
|
600 |
with gr.Tab("🎓 Step 3: Get Certified"):
|
|
|
605 |
value="Professional Assessment Certification"
|
606 |
)
|
607 |
certificate_display = gr.Image(label="Your Certificate")
|
608 |
+
|
609 |
+
# Helper functions for navigation
|
610 |
+
def update_question_display(questions, idx):
|
611 |
+
if not questions or idx >= len(questions):
|
612 |
+
return {
|
613 |
+
question_display: "",
|
614 |
+
current_options: gr.update(choices=[], value=None, visible=False),
|
615 |
+
question_counter: ""
|
616 |
+
}
|
617 |
+
|
618 |
+
question = questions[idx]
|
619 |
+
return {
|
620 |
+
question_display: f"### Question {idx + 1}\n{question.question}",
|
621 |
+
current_options: gr.update(choices=question.options, value=None, visible=True),
|
622 |
+
question_counter: f"Question {idx + 1} of {len(questions)}"
|
623 |
+
}
|
624 |
+
|
625 |
+
def navigate(direction, current_idx, questions):
|
626 |
+
new_idx = max(0, min(len(questions) - 1, current_idx + direction))
|
627 |
+
return new_idx, update_question_display(questions, new_idx)
|
628 |
+
|
629 |
# Event handlers
|
630 |
+
def on_generate_questions(text, num_questions):
|
631 |
+
success, questions = quiz_app.generate_questions(text, num_questions)
|
632 |
+
if not success:
|
633 |
+
return {
|
634 |
+
question_display: "",
|
635 |
+
current_options: gr.update(choices=[], visible=False),
|
636 |
+
question_counter: "",
|
637 |
+
current_questions: [],
|
638 |
+
current_question_idx: 0,
|
639 |
+
tabs: gr.update(selected=1)
|
640 |
+
}
|
641 |
+
|
642 |
+
# Initialize first question
|
643 |
+
return {
|
644 |
+
**update_question_display(questions, 0),
|
645 |
+
current_questions: questions,
|
646 |
+
current_question_idx: 0,
|
647 |
+
tabs: gr.update(selected=1)
|
648 |
+
}
|
649 |
|
650 |
generate_btn.click(
|
651 |
+
fn=on_generate_questions,
|
652 |
inputs=[text_input, num_questions],
|
653 |
outputs=[
|
654 |
+
question_display,
|
655 |
+
current_options,
|
656 |
+
question_counter,
|
657 |
current_questions,
|
658 |
+
current_question_idx,
|
659 |
tabs
|
660 |
]
|
661 |
)
|
662 |
+
|
663 |
+
# Navigation event handlers
|
664 |
+
prev_btn.click(
|
665 |
+
fn=navigate,
|
666 |
+
inputs=[-1, current_question_idx, current_questions],
|
667 |
+
outputs=[current_question_idx, question_display, current_options, question_counter]
|
668 |
+
)
|
669 |
+
|
670 |
+
next_btn.click(
|
671 |
+
fn=navigate,
|
672 |
+
inputs=[1, current_question_idx, current_questions],
|
673 |
+
outputs=[current_question_idx, question_display, current_options, question_counter]
|
674 |
+
)
|
675 |
+
|
676 |
+
# Submission handler
|
677 |
+
def on_submit(questions, current_answer):
|
678 |
+
if not current_answer:
|
679 |
+
return {
|
680 |
+
feedback_box: "⚠️ Please answer all questions before submitting.",
|
681 |
+
results_group: gr.update(visible=False),
|
682 |
+
score_display: 0,
|
683 |
+
result_message: "",
|
684 |
+
tabs: gr.update(selected=1)
|
685 |
+
}
|
686 |
+
|
687 |
+
answers = [current_answer] # Collect answers from current question
|
688 |
+
score, passed, feedback = quiz_app.calculate_score(answers)
|
689 |
+
|
690 |
+
# Generate feedback HTML
|
691 |
+
feedback_html = "# Assessment Results\n\n"
|
692 |
+
for i, (q, f) in enumerate(zip(questions, feedback)):
|
693 |
+
color = "green" if f.is_correct else "red"
|
694 |
+
symbol = "✅" if f.is_correct else "❌"
|
695 |
+
feedback_html += f"""
|
696 |
+
### Question {i+1}
|
697 |
+
{q.question}
|
698 |
+
|
699 |
+
<div style="color: {color}; padding: 10px; margin: 5px 0; border-left: 3px solid {color};">
|
700 |
+
{symbol} Your answer: {f.selected or "No answer"}
|
701 |
+
{'' if f.is_correct else f'<br>Correct answer: {f.correct_answer}'}
|
702 |
+
</div>
|
703 |
+
"""
|
704 |
+
|
705 |
+
return {
|
706 |
+
feedback_box: feedback_html,
|
707 |
+
results_group: gr.update(visible=True),
|
708 |
+
score_display: score,
|
709 |
+
result_message: "🎉 Passed!" if passed else "Please try again",
|
710 |
+
tabs: gr.update(selected=2) if passed else gr.update()
|
711 |
+
}
|
712 |
+
|
713 |
submit_btn.click(
|
714 |
+
fn=on_submit,
|
715 |
+
inputs=[current_questions, current_options],
|
716 |
outputs=[
|
717 |
feedback_box,
|
718 |
+
results_group,
|
719 |
score_display,
|
720 |
result_message,
|
721 |
tabs
|
722 |
]
|
723 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
724 |
|
725 |
return demo
|
726 |
|