Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -730,6 +730,134 @@ def create_quiz_interface():
|
|
730 |
gr.update(visible=True)
|
731 |
]
|
732 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
733 |
# Event Handlers
|
734 |
generate_btn.click(
|
735 |
fn=on_generate_questions,
|
|
|
730 |
gr.update(visible=True)
|
731 |
]
|
732 |
|
733 |
+
def on_submit(questions, answers, current_idx, current_answer):
|
734 |
+
"""Handle quiz submission with proper HTML rendering"""
|
735 |
+
final_answers = list(answers)
|
736 |
+
if 0 <= current_idx < len(final_answers):
|
737 |
+
final_answers[current_idx] = current_answer
|
738 |
+
|
739 |
+
if not all(a is not None for a in final_answers[:len(questions)]):
|
740 |
+
return [
|
741 |
+
gr.Markdown("⚠️ Please answer all questions before submitting."),
|
742 |
+
gr.update(visible=True),
|
743 |
+
0,
|
744 |
+
"",
|
745 |
+
gr.update(visible=True),
|
746 |
+
gr.update(visible=True),
|
747 |
+
gr.update(visible=False),
|
748 |
+
gr.update(visible=False),
|
749 |
+
gr.update(visible=False)
|
750 |
+
]
|
751 |
+
|
752 |
+
score, passed, feedback = quiz_app.calculate_score(final_answers[:len(questions)])
|
753 |
+
|
754 |
+
# Create feedback content using proper HTML rendering
|
755 |
+
feedback_content = f"""# Assessment Results
|
756 |
+
|
757 |
+
{' '.join(['✅' if f.is_correct else '❌' for f in feedback])} Score: {score:.1f}%
|
758 |
+
|
759 |
+
"""
|
760 |
+
|
761 |
+
for i, (q, f) in enumerate(zip(questions, feedback)):
|
762 |
+
feedback_content += f"""### Question {i+1}
|
763 |
+
{q.question}
|
764 |
+
|
765 |
+
{'✅' if f.is_correct else '❌'} Your answer: **{f.selected or 'No answer'}**
|
766 |
+
{' ' if f.is_correct else f' \nCorrect answer: **{f.correct_answer}**'}
|
767 |
+
|
768 |
+
"""
|
769 |
+
|
770 |
+
# Add summary box
|
771 |
+
if passed:
|
772 |
+
feedback_content += """
|
773 |
+
---
|
774 |
+
### 🎉 Congratulations!
|
775 |
+
You passed the assessment! Click the "View Certificate" button below to get your certificate.
|
776 |
+
"""
|
777 |
+
else:
|
778 |
+
feedback_content += """
|
779 |
+
---
|
780 |
+
### Try Again
|
781 |
+
You need 80% or higher to pass. Click "Reset Quiz" to try again.
|
782 |
+
"""
|
783 |
+
|
784 |
+
return [
|
785 |
+
gr.Markdown(feedback_content), # feedback_box
|
786 |
+
gr.update(visible=True), # results_group
|
787 |
+
score, # score_display
|
788 |
+
"🎉 Passed!" if passed else "Please try again", # result_message
|
789 |
+
gr.update(visible=False), # question_box
|
790 |
+
gr.update(visible=not passed), # reset_btn
|
791 |
+
gr.update(visible=passed), # view_cert_btn
|
792 |
+
gr.update(visible=True), # back_to_assessment
|
793 |
+
gr.update(visible=False) # profile_tab
|
794 |
+
]
|
795 |
+
|
796 |
+
def create_quiz_interface():
|
797 |
+
if not os.getenv("GROQ_API_KEY"):
|
798 |
+
raise EnvironmentError("Please set your GROQ_API_KEY environment variable")
|
799 |
+
|
800 |
+
quiz_app = QuizApp(os.getenv("GROQ_API_KEY"))
|
801 |
+
|
802 |
+
with gr.Blocks(title="CertifyMe AI", theme=gr.themes.Soft()) as demo:
|
803 |
+
# State management
|
804 |
+
current_questions = gr.State([])
|
805 |
+
current_question_idx = gr.State(0)
|
806 |
+
answer_state = gr.State([None] * 5)
|
807 |
+
|
808 |
+
with gr.Tabs() as tabs:
|
809 |
+
# Profile Setup Tab
|
810 |
+
with gr.Tab(id=1, label="📋 Step 1: Profile Setup"):
|
811 |
+
# ... (previous profile setup code remains the same)
|
812 |
+
pass
|
813 |
+
|
814 |
+
# Assessment Tab
|
815 |
+
with gr.Tab(id=2, label="📝 Step 2: Take Assessment") as assessment_tab:
|
816 |
+
with gr.Column() as main_container:
|
817 |
+
# Questions Section
|
818 |
+
with gr.Column(visible=True) as question_box:
|
819 |
+
question_display = gr.Markdown("")
|
820 |
+
current_options = gr.Radio(
|
821 |
+
choices=[],
|
822 |
+
label="Select your answer:",
|
823 |
+
visible=False
|
824 |
+
)
|
825 |
+
|
826 |
+
with gr.Row():
|
827 |
+
prev_btn = gr.Button("← Previous", variant="secondary")
|
828 |
+
question_counter = gr.Markdown("Question 1")
|
829 |
+
next_btn = gr.Button("Next →", variant="secondary")
|
830 |
+
|
831 |
+
submit_btn = gr.Button(
|
832 |
+
"Submit Assessment",
|
833 |
+
variant="primary",
|
834 |
+
size="lg"
|
835 |
+
)
|
836 |
+
|
837 |
+
# Results Section
|
838 |
+
with gr.Column(visible=False) as results_group:
|
839 |
+
feedback_box = gr.Markdown("")
|
840 |
+
with gr.Row():
|
841 |
+
reset_btn = gr.Button(
|
842 |
+
"Reset Quiz",
|
843 |
+
variant="secondary",
|
844 |
+
size="lg",
|
845 |
+
visible=False
|
846 |
+
)
|
847 |
+
view_cert_btn = gr.Button(
|
848 |
+
"View Certificate",
|
849 |
+
variant="primary",
|
850 |
+
size="lg",
|
851 |
+
visible=False
|
852 |
+
)
|
853 |
+
|
854 |
+
# Certification Tab
|
855 |
+
with gr.Tab(id=3, label="🎓 Step 3: Get Certified"):
|
856 |
+
score_display = gr.Number(label="Your Score", visible=False)
|
857 |
+
certificate_display = gr.Image(label="Your Certificate")
|
858 |
+
|
859 |
+
|
860 |
+
|
861 |
# Event Handlers
|
862 |
generate_btn.click(
|
863 |
fn=on_generate_questions,
|