import gradio as gr import uuid, os, tempfile, hashlib from reportlab.lib.pagesizes import A5, landscape from reportlab.pdfgen import canvas from reportlab.lib.units import mm from reportlab.lib.colors import HexColor from datetime import datetime # Certificate generation function (included for a self-contained script) def generate_certificate(name, score, total, instructor="Sudhir Gupta"): unique_id = str(uuid.uuid4()) filename = f"cert_{unique_id}.pdf" filepath = os.path.join(tempfile.gettempdir(), filename) c = canvas.Canvas(filepath, pagesize=landscape(A5)) width, height = landscape(A5) c.setFillColor(HexColor("#fffdf6")) c.rect(0, 0, width, height, stroke=0, fill=1) c.setStrokeColor(HexColor("#001858")) c.setLineWidth(3) margin = 10 * mm c.rect(margin, margin, width - 2 * margin, height - 2 * margin) c.setFillColor(HexColor("#001858")) c.setFont("Helvetica-Bold", 24) c.drawCentredString(width / 2, height - 60, "Certificate of Completion") c.setFont("Helvetica", 14) c.drawCentredString(width / 2, height - 100, "This is awarded to") c.setFont("Helvetica-Bold", 18) c.drawCentredString(width / 2, height - 130, name) c.setFont("Helvetica", 14) c.drawCentredString(width / 2, height - 160, "For successfully completing the quiz - Our Solar System") c.setFont("Helvetica", 12) c.drawCentredString(width / 2, height - 185, f"Score: {score} / {total}") c.setFont("Helvetica-Oblique", 10) c.drawString(margin + 10, margin + 20, f"Instructor: {instructor}") date_str = datetime.now().strftime("%d %B %Y") c.setFont("Helvetica", 10) c.drawRightString(width - margin - 10, margin + 20, f"Issued on: {date_str}") c.save() return filepath # Quiz data (answers are hashed) quiz_type = "Multiple Choice" questions = [{'question': 'What is the name of the star at the center of our solar system?', 'options': ['The Moon', 'The Sun', 'Mars', 'The Milky Way'], 'answer_hash': 'f99f996daf45ff7005d515a01538d792c7449aa121b7469b8f52d426f3185613'}, {'question': 'Which planet is known as the "Red Planet"?', 'options': ['Jupiter', 'Venus', 'Saturn', 'Mars'], 'answer_hash': 'd158be41db87d7055fa9548fe9c0e3cb685983fedb4f2d648d7a25c046d72d9d'}, {'question': 'What are the large beautiful rings of Saturn made of?', 'options': ['Solid rock', 'Dust and gas', 'Ice and rock', 'Liquid water'], 'answer_hash': 'c14f48114ce9363f80b4e33f8c523c25e2422a14d68c976832a75e47b2d12a1c'}, {'question': 'Which is the largest planet in our solar system?', 'options': ['Jupiter', 'Earth', 'Mercury', 'Saturn'], 'answer_hash': '1e9cc37678c0112d7a394909256c2a5998a6e57e2b0d82209900e1456e51b7eb'}, {'question': 'How many planets are in our solar system?', 'options': ['7', '8', '9', '10'], 'answer_hash': '2c624232cdd221771294dfbb310aca000a0df6ac8b66b696d90ef06fdefb64a3'}, {'question': 'Which planet is closest to the Sun?', 'options': ['Venus', 'Mercury', 'Earth', 'Mars'], 'answer_hash': 'b769a6983b42d565e79bb4f3f534623453f301d39784e57804a649a67ea05327'}, {'question': 'What is the name of the natural satellite that orbits the Earth?', 'options': ['Phobos', 'The Sun', 'The Moon', 'A comet'], 'answer_hash': '7bcd0f182d906d8e5c1dfbe656720d51d545da8f491f35da2c627badb29731cf'}, {'question': 'Which planet is known for its beautiful rings?', 'options': ['Saturn', 'Uranus', 'Neptune', 'Jupiter'], 'answer_hash': 'b988b5837c24ad1987f31266e0246b0fdaaf7948714cfa2a9f7757c52977ff14'}, {'question': 'What is the name of the third planet from the Sun?', 'options': ['Mercury', 'Mars', 'Earth', 'Jupiter'], 'answer_hash': '7b74b418a352d67108173c20c1b16b4b726bad8606be65711ff924dbf9a40670'}, {'question': 'The path a planet takes around the Sun is called an:', 'options': ['Axis', 'Rotation', 'Orbit', 'Revolution'], 'answer_hash': '4fa1a13ac468ac495f3390e859d76d5e8ef49806815b45a21de7711bcc624194'}] def eval_quiz(name, *answers): """ Evaluates the student's answers and generates a certificate only if the score is 80% or higher. """ if not name.strip(): name = "Anonymous" score = 0 for i, ans in enumerate(answers): if ans and hashlib.sha256(str(ans).lower().strip().encode()).hexdigest() == questions[i]["answer_hash"]: score += 1 total_questions = len(questions) passing_threshold = 0.8 result_message = f"Hi {name}, your score is: {score} / {total_questions}." cert_path = None # Default to no certificate # Check if the score meets the passing threshold if total_questions > 0 and (score / total_questions) >= passing_threshold: cert_path = generate_certificate(name, score, total_questions, instructor="Sudhir Gupta") result_message += " Congratulations, you passed and earned a certificate!" else: result_message += " A score of 80% is required to receive a certificate." return result_message, cert_path # Gradio interface for the student with gr.Blocks(theme=gr.themes.Soft()) as app: gr.Markdown("## Our Solar System") with gr.Row(): name = gr.Textbox(label="Enter Your Full Name to Generate Certificate", placeholder="e.g., Ada Lovelace") answer_inputs = [] for q in questions: gr.Markdown("**Question:** " + q['question']) if quiz_type == "Multiple Choice": answer_inputs.append(gr.Radio(choices=q["options"], label="Select your answer")) else: answer_inputs.append(gr.Textbox(label="Type your answer")) submit_btn = gr.Button("Submit Quiz") with gr.Row(): result_output = gr.Textbox(label="Your Result") certificate_output = gr.File(label="Download Your Certificate") submit_btn.click( fn=eval_quiz, inputs=[name] + answer_inputs, outputs=[result_output, certificate_output] ) app.launch(debug=True)