Spaces:
Sleeping
Sleeping
capradeepgujaran
commited on
Commit
•
47646d0
1
Parent(s):
10d4f78
Update app.py
Browse files
app.py
CHANGED
@@ -530,6 +530,7 @@ def create_quiz_interface():
|
|
530 |
if not os.getenv("GROQ_API_KEY"):
|
531 |
raise EnvironmentError("Please set your GROQ_API_KEY environment variable")
|
532 |
|
|
|
533 |
quiz_app = QuizApp(os.getenv("GROQ_API_KEY"))
|
534 |
|
535 |
with gr.Blocks(title="CertifyMe AI", theme=gr.themes.Soft()) as demo:
|
@@ -571,27 +572,24 @@ def create_quiz_interface():
|
|
571 |
|
572 |
generate_btn = gr.Button("Generate Assessment", variant="primary", size="lg")
|
573 |
|
|
|
574 |
with gr.Tab("📝 Step 2: Take Assessment") as assessment_tab:
|
575 |
with gr.Column(visible=True) as question_box:
|
576 |
# Question section
|
577 |
with gr.Group():
|
578 |
-
# Question display
|
579 |
question_display = gr.Markdown("", label="Current Question")
|
580 |
-
|
581 |
-
# Single radio group for current question
|
582 |
current_options = gr.Radio(
|
583 |
choices=[],
|
584 |
label="Select your answer:",
|
585 |
visible=False
|
586 |
)
|
587 |
|
588 |
-
# Navigation
|
589 |
with gr.Row():
|
590 |
prev_btn = gr.Button("← Previous", variant="secondary", size="sm")
|
591 |
question_counter = gr.Markdown("Question 1 of 3")
|
592 |
next_btn = gr.Button("Next →", variant="secondary", size="sm")
|
593 |
|
594 |
-
gr.Markdown("---")
|
595 |
|
596 |
with gr.Row():
|
597 |
submit_btn = gr.Button(
|
@@ -622,9 +620,6 @@ def create_quiz_interface():
|
|
622 |
visible=True
|
623 |
)
|
624 |
|
625 |
-
|
626 |
-
|
627 |
-
|
628 |
# Certification Tab
|
629 |
with gr.Tab("🎓 Step 3: Get Certified"):
|
630 |
score_display = gr.Number(label="Your Score")
|
@@ -634,46 +629,48 @@ def create_quiz_interface():
|
|
634 |
value="Professional Assessment Certification"
|
635 |
)
|
636 |
certificate_display = gr.Image(label="Your Certificate")
|
|
|
637 |
# Helper Functions
|
638 |
|
639 |
-
def on_generate_questions(text, num_questions):
|
640 |
-
|
641 |
-
|
642 |
-
|
643 |
-
|
644 |
-
|
645 |
-
|
646 |
-
|
647 |
-
|
648 |
-
|
649 |
-
|
650 |
-
|
651 |
-
|
652 |
-
|
653 |
-
|
654 |
-
|
655 |
-
|
656 |
-
|
657 |
-
|
658 |
-
|
659 |
-
|
660 |
-
|
661 |
-
|
662 |
-
|
663 |
-
|
664 |
-
|
665 |
-
|
666 |
-
|
667 |
-
|
668 |
-
|
669 |
-
|
670 |
-
|
671 |
-
|
672 |
-
|
673 |
-
|
674 |
-
|
675 |
-
|
676 |
-
|
|
|
677 |
|
678 |
def goto_take_assessment():
|
679 |
"""Navigate to Take Assessment tab"""
|
@@ -698,8 +695,7 @@ def create_quiz_interface():
|
|
698 |
)
|
699 |
|
700 |
|
701 |
-
def navigate(direction, current_idx, questions, answers, current_answer):
|
702 |
-
"""Handle navigation between questions"""
|
703 |
if not questions:
|
704 |
return [
|
705 |
0,
|
@@ -733,16 +729,16 @@ def create_quiz_interface():
|
|
733 |
|
734 |
def handle_prev(current_idx, questions, answers, current_answer):
|
735 |
return navigate(-1, current_idx, questions, answers, current_answer)
|
736 |
-
|
737 |
def handle_next(current_idx, questions, answers, current_answer):
|
738 |
return navigate(1, current_idx, questions, answers, current_answer)
|
739 |
-
|
740 |
def update_answer_state(answer, idx, current_answers):
|
741 |
new_answers = list(current_answers)
|
742 |
if 0 <= idx < len(new_answers):
|
743 |
new_answers[idx] = answer
|
744 |
return new_answers
|
745 |
-
|
746 |
def on_submit(questions, answers, current_idx, current_answer):
|
747 |
final_answers = list(answers)
|
748 |
if 0 <= current_idx < len(final_answers):
|
@@ -756,7 +752,7 @@ def create_quiz_interface():
|
|
756 |
"",
|
757 |
gr.update(visible=True),
|
758 |
gr.update(selected=1),
|
759 |
-
gr.update(visible=False)
|
760 |
]
|
761 |
|
762 |
score, passed, feedback = quiz_app.calculate_score(final_answers[:len(questions)])
|
@@ -766,23 +762,23 @@ def create_quiz_interface():
|
|
766 |
color = "green" if f.is_correct else "red"
|
767 |
symbol = "✅" if f.is_correct else "❌"
|
768 |
feedback_html += f"""### Question {i+1}
|
769 |
-
{q.question}
|
770 |
-
|
771 |
-
<div style="color: {color}; padding: 10px; margin: 5px 0; border-left: 3px solid {color};">
|
772 |
-
|
773 |
-
|
774 |
-
</div>
|
775 |
-
"""
|
776 |
|
777 |
result_msg = "🎉 Passed!" if passed else "Please try again"
|
778 |
if not passed:
|
779 |
feedback_html += f"""
|
780 |
-
<div style="background-color: #ffe6e6; padding: 20px; margin-top: 20px; border-radius: 10px;">
|
781 |
-
|
782 |
-
|
783 |
-
|
784 |
-
</div>
|
785 |
-
"""
|
786 |
|
787 |
return [
|
788 |
feedback_html,
|
@@ -791,9 +787,9 @@ def create_quiz_interface():
|
|
791 |
result_msg,
|
792 |
gr.update(visible=not passed),
|
793 |
gr.update(selected=1),
|
794 |
-
gr.update(visible=passed)
|
795 |
]
|
796 |
-
|
797 |
def reset_quiz(text, num_questions):
|
798 |
"""Handle quiz reset"""
|
799 |
return on_generate_questions(text, num_questions)
|
@@ -803,7 +799,7 @@ def create_quiz_interface():
|
|
803 |
return gr.update(selected=2)
|
804 |
|
805 |
# Event handlers
|
806 |
-
|
807 |
fn=on_generate_questions,
|
808 |
inputs=[text_input, num_questions],
|
809 |
outputs=[
|
@@ -818,12 +814,11 @@ def create_quiz_interface():
|
|
818 |
results_group,
|
819 |
view_cert_btn
|
820 |
]
|
821 |
-
).then(
|
822 |
-
fn=lambda: gr.
|
823 |
outputs=tabs
|
824 |
)
|
825 |
|
826 |
-
# Navigation handlers
|
827 |
prev_btn.click(
|
828 |
fn=handle_prev,
|
829 |
inputs=[
|
@@ -860,14 +855,12 @@ def create_quiz_interface():
|
|
860 |
]
|
861 |
)
|
862 |
|
863 |
-
# Answer state update
|
864 |
current_options.change(
|
865 |
fn=update_answer_state,
|
866 |
inputs=[current_options, current_question_idx, answer_state],
|
867 |
outputs=answer_state
|
868 |
)
|
869 |
|
870 |
-
# Quiz submission
|
871 |
submit_btn.click(
|
872 |
fn=on_submit,
|
873 |
inputs=[
|
@@ -887,7 +880,6 @@ def create_quiz_interface():
|
|
887 |
]
|
888 |
)
|
889 |
|
890 |
-
# Reset quiz
|
891 |
reset_btn.click(
|
892 |
fn=reset_quiz,
|
893 |
inputs=[text_input, num_questions],
|
@@ -905,27 +897,24 @@ def create_quiz_interface():
|
|
905 |
]
|
906 |
)
|
907 |
|
908 |
-
# View certificate navigation
|
909 |
view_cert_btn.click(
|
910 |
-
fn=lambda: gr.
|
911 |
outputs=tabs
|
912 |
)
|
913 |
|
914 |
-
# Back to assessment navigation
|
915 |
back_to_assessment.click(
|
916 |
-
fn=lambda: gr.
|
917 |
outputs=tabs
|
918 |
)
|
919 |
|
920 |
-
# Certificate generation
|
921 |
score_display.change(
|
922 |
-
fn=quiz_app.certificate_generator.generate,
|
923 |
inputs=[score_display, name, course_name, company_logo, participant_photo],
|
924 |
outputs=certificate_display
|
925 |
)
|
926 |
-
|
927 |
return demo
|
928 |
|
929 |
if __name__ == "__main__":
|
930 |
demo = create_quiz_interface()
|
931 |
-
demo.launch()
|
|
|
530 |
if not os.getenv("GROQ_API_KEY"):
|
531 |
raise EnvironmentError("Please set your GROQ_API_KEY environment variable")
|
532 |
|
533 |
+
global quiz_app # Make quiz_app accessible to helper functions
|
534 |
quiz_app = QuizApp(os.getenv("GROQ_API_KEY"))
|
535 |
|
536 |
with gr.Blocks(title="CertifyMe AI", theme=gr.themes.Soft()) as demo:
|
|
|
572 |
|
573 |
generate_btn = gr.Button("Generate Assessment", variant="primary", size="lg")
|
574 |
|
575 |
+
# Assessment Tab
|
576 |
with gr.Tab("📝 Step 2: Take Assessment") as assessment_tab:
|
577 |
with gr.Column(visible=True) as question_box:
|
578 |
# Question section
|
579 |
with gr.Group():
|
|
|
580 |
question_display = gr.Markdown("", label="Current Question")
|
|
|
|
|
581 |
current_options = gr.Radio(
|
582 |
choices=[],
|
583 |
label="Select your answer:",
|
584 |
visible=False
|
585 |
)
|
586 |
|
|
|
587 |
with gr.Row():
|
588 |
prev_btn = gr.Button("← Previous", variant="secondary", size="sm")
|
589 |
question_counter = gr.Markdown("Question 1 of 3")
|
590 |
next_btn = gr.Button("Next →", variant="secondary", size="sm")
|
591 |
|
592 |
+
gr.Markdown("---")
|
593 |
|
594 |
with gr.Row():
|
595 |
submit_btn = gr.Button(
|
|
|
620 |
visible=True
|
621 |
)
|
622 |
|
|
|
|
|
|
|
623 |
# Certification Tab
|
624 |
with gr.Tab("🎓 Step 3: Get Certified"):
|
625 |
score_display = gr.Number(label="Your Score")
|
|
|
629 |
value="Professional Assessment Certification"
|
630 |
)
|
631 |
certificate_display = gr.Image(label="Your Certificate")
|
632 |
+
|
633 |
# Helper Functions
|
634 |
|
635 |
+
def on_generate_questions(text: str, num_questions: int) -> List:
|
636 |
+
success, questions = quiz_app.generate_questions(text, num_questions)
|
637 |
+
tab_index = 1 # Index for "Take Assessment" tab
|
638 |
+
|
639 |
+
if not success:
|
640 |
+
return [
|
641 |
+
"",
|
642 |
+
gr.update(choices=[], visible=False),
|
643 |
+
"",
|
644 |
+
gr.update(visible=False),
|
645 |
+
[],
|
646 |
+
0,
|
647 |
+
[None] * 5,
|
648 |
+
gr.update(selected=tab_index),
|
649 |
+
gr.update(visible=False),
|
650 |
+
gr.update(visible=False)
|
651 |
+
]
|
652 |
+
|
653 |
+
initial_answers = [None] * len(questions)
|
654 |
+
question = questions[0]
|
655 |
+
|
656 |
+
return [
|
657 |
+
f"## Question 1\n{question.question}\n\nPlease select one answer:",
|
658 |
+
gr.update(
|
659 |
+
choices=question.options,
|
660 |
+
value=None,
|
661 |
+
visible=True,
|
662 |
+
label="Select your answer for Question 1:"
|
663 |
+
),
|
664 |
+
f"Question 1 of {len(questions)}",
|
665 |
+
gr.update(visible=True),
|
666 |
+
questions,
|
667 |
+
0,
|
668 |
+
initial_answers,
|
669 |
+
gr.update(selected=tab_index),
|
670 |
+
gr.update(visible=False),
|
671 |
+
gr.update(visible=False)
|
672 |
+
]
|
673 |
+
|
674 |
|
675 |
def goto_take_assessment():
|
676 |
"""Navigate to Take Assessment tab"""
|
|
|
695 |
)
|
696 |
|
697 |
|
698 |
+
def navigate(direction: int, current_idx: int, questions: List, answers: List, current_answer: Optional[str]) -> List:
|
|
|
699 |
if not questions:
|
700 |
return [
|
701 |
0,
|
|
|
729 |
|
730 |
def handle_prev(current_idx, questions, answers, current_answer):
|
731 |
return navigate(-1, current_idx, questions, answers, current_answer)
|
732 |
+
|
733 |
def handle_next(current_idx, questions, answers, current_answer):
|
734 |
return navigate(1, current_idx, questions, answers, current_answer)
|
735 |
+
|
736 |
def update_answer_state(answer, idx, current_answers):
|
737 |
new_answers = list(current_answers)
|
738 |
if 0 <= idx < len(new_answers):
|
739 |
new_answers[idx] = answer
|
740 |
return new_answers
|
741 |
+
|
742 |
def on_submit(questions, answers, current_idx, current_answer):
|
743 |
final_answers = list(answers)
|
744 |
if 0 <= current_idx < len(final_answers):
|
|
|
752 |
"",
|
753 |
gr.update(visible=True),
|
754 |
gr.update(selected=1),
|
755 |
+
gr.update(visible=False)
|
756 |
]
|
757 |
|
758 |
score, passed, feedback = quiz_app.calculate_score(final_answers[:len(questions)])
|
|
|
762 |
color = "green" if f.is_correct else "red"
|
763 |
symbol = "✅" if f.is_correct else "❌"
|
764 |
feedback_html += f"""### Question {i+1}
|
765 |
+
{q.question}
|
766 |
+
|
767 |
+
<div style="color: {color}; padding: 10px; margin: 5px 0; border-left: 3px solid {color};">
|
768 |
+
{symbol} Your answer: {f.selected or "No answer"}
|
769 |
+
{'' if f.is_correct else f'<br>Correct answer: {f.correct_answer}'}
|
770 |
+
</div>
|
771 |
+
"""
|
772 |
|
773 |
result_msg = "🎉 Passed!" if passed else "Please try again"
|
774 |
if not passed:
|
775 |
feedback_html += f"""
|
776 |
+
<div style="background-color: #ffe6e6; padding: 20px; margin-top: 20px; border-radius: 10px;">
|
777 |
+
<h3 style="color: #cc0000;">Please Try Again</h3>
|
778 |
+
<p>Your score: {score:.1f}%</p>
|
779 |
+
<p>You need 80% or higher to pass and receive a certificate.</p>
|
780 |
+
</div>
|
781 |
+
"""
|
782 |
|
783 |
return [
|
784 |
feedback_html,
|
|
|
787 |
result_msg,
|
788 |
gr.update(visible=not passed),
|
789 |
gr.update(selected=1),
|
790 |
+
gr.update(visible=passed)
|
791 |
]
|
792 |
+
|
793 |
def reset_quiz(text, num_questions):
|
794 |
"""Handle quiz reset"""
|
795 |
return on_generate_questions(text, num_questions)
|
|
|
799 |
return gr.update(selected=2)
|
800 |
|
801 |
# Event handlers
|
802 |
+
generate_btn.click(
|
803 |
fn=on_generate_questions,
|
804 |
inputs=[text_input, num_questions],
|
805 |
outputs=[
|
|
|
814 |
results_group,
|
815 |
view_cert_btn
|
816 |
]
|
817 |
+
).then(
|
818 |
+
fn=lambda: gr.update(selected=1),
|
819 |
outputs=tabs
|
820 |
)
|
821 |
|
|
|
822 |
prev_btn.click(
|
823 |
fn=handle_prev,
|
824 |
inputs=[
|
|
|
855 |
]
|
856 |
)
|
857 |
|
|
|
858 |
current_options.change(
|
859 |
fn=update_answer_state,
|
860 |
inputs=[current_options, current_question_idx, answer_state],
|
861 |
outputs=answer_state
|
862 |
)
|
863 |
|
|
|
864 |
submit_btn.click(
|
865 |
fn=on_submit,
|
866 |
inputs=[
|
|
|
880 |
]
|
881 |
)
|
882 |
|
|
|
883 |
reset_btn.click(
|
884 |
fn=reset_quiz,
|
885 |
inputs=[text_input, num_questions],
|
|
|
897 |
]
|
898 |
)
|
899 |
|
|
|
900 |
view_cert_btn.click(
|
901 |
+
fn=lambda: gr.update(selected=2),
|
902 |
outputs=tabs
|
903 |
)
|
904 |
|
|
|
905 |
back_to_assessment.click(
|
906 |
+
fn=lambda: gr.update(selected=1),
|
907 |
outputs=tabs
|
908 |
)
|
909 |
|
|
|
910 |
score_display.change(
|
911 |
+
fn=lambda s, n, c, l, p: quiz_app.certificate_generator.generate(s, n, c, l, p) or gr.update(value=None),
|
912 |
inputs=[score_display, name, course_name, company_logo, participant_photo],
|
913 |
outputs=certificate_display
|
914 |
)
|
915 |
+
|
916 |
return demo
|
917 |
|
918 |
if __name__ == "__main__":
|
919 |
demo = create_quiz_interface()
|
920 |
+
demo.launch()
|