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
def generate_certificate(name, score, total, instructor="Instructor"):
|
| 3 |
+
unique_id = str(uuid.uuid4())
|
| 4 |
+
filename = f"cert_{unique_id}.pdf"
|
| 5 |
+
filepath = os.path.join(tempfile.gettempdir(), filename)
|
| 6 |
+
|
| 7 |
+
c = canvas.Canvas(filepath, pagesize=landscape(A5))
|
| 8 |
+
width, height = landscape(A5)
|
| 9 |
+
|
| 10 |
+
c.setFillColor(HexColor("#fffdf6"))
|
| 11 |
+
c.rect(0, 0, width, height, stroke=0, fill=1)
|
| 12 |
+
|
| 13 |
+
c.setStrokeColor(HexColor("#001858"))
|
| 14 |
+
c.setLineWidth(3)
|
| 15 |
+
margin = 10 * mm
|
| 16 |
+
c.rect(margin, margin, width - 2 * margin, height - 2 * margin)
|
| 17 |
+
|
| 18 |
+
c.setFillColor(HexColor("#001858"))
|
| 19 |
+
c.setFont("Helvetica-Bold", 24)
|
| 20 |
+
c.drawCentredString(width / 2, height - 60, "Certificate of Completion")
|
| 21 |
+
|
| 22 |
+
c.setFont("Helvetica", 14)
|
| 23 |
+
c.drawCentredString(width / 2, height - 100, "This is awarded to")
|
| 24 |
+
|
| 25 |
+
c.setFont("Helvetica-Bold", 18)
|
| 26 |
+
c.drawCentredString(width / 2, height - 130, name)
|
| 27 |
+
|
| 28 |
+
c.setFont("Helvetica", 14)
|
| 29 |
+
c.drawCentredString(width / 2, height - 160, "For successfully completing the quiz")
|
| 30 |
+
|
| 31 |
+
c.setFont("Helvetica", 12)
|
| 32 |
+
c.drawCentredString(width / 2, height - 185, f"Score: {score} / {total}")
|
| 33 |
+
|
| 34 |
+
c.setFont("Helvetica-Oblique", 10)
|
| 35 |
+
c.drawString(margin + 10, margin + 20, f"Instructor: {instructor}")
|
| 36 |
+
|
| 37 |
+
date_str = datetime.now().strftime("%d %B %Y")
|
| 38 |
+
c.setFont("Helvetica", 10)
|
| 39 |
+
c.drawRightString(width - margin - 10, margin + 20, f"Issued on: {date_str}")
|
| 40 |
+
|
| 41 |
+
c.save()
|
| 42 |
+
return filepath
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# --- Main Quiz Code Generator ---
|
| 46 |
+
def generate_python_code(title, instructor, quiz_type, questions_text):
|
| 47 |
+
# Parse questions and hash answers
|
| 48 |
+
parsed_questions = []
|
| 49 |
+
for line in questions_text.strip().split("\n"):
|
| 50 |
+
if not line.strip():
|
| 51 |
+
continue
|
| 52 |
+
parts = [p.strip() for p in line.split(",")]
|
| 53 |
+
|
| 54 |
+
if quiz_type == "Multiple Choice":
|
| 55 |
+
q_text = parts[0]
|
| 56 |
+
options = parts[1:-1] # all except question and last (answer)
|
| 57 |
+
answer = parts[-1]
|
| 58 |
+
parsed_questions.append({
|
| 59 |
+
"question": q_text,
|
| 60 |
+
"options": options,
|
| 61 |
+
"answer_hash": hashlib.sha256(answer.lower().encode()).hexdigest()
|
| 62 |
+
})
|
| 63 |
+
else:
|
| 64 |
+
q_text = parts[0]
|
| 65 |
+
answer = parts[1]
|
| 66 |
+
parsed_questions.append({
|
| 67 |
+
"question": q_text,
|
| 68 |
+
"answer_hash": hashlib.sha256(answer.lower().encode()).hexdigest()
|
| 69 |
+
})
|
| 70 |
+
|
| 71 |
+
# Generate code for the student quiz
|
| 72 |
+
python_code = f'''
|
| 73 |
+
import gradio as gr
|
| 74 |
+
import uuid, os, tempfile, hashlib
|
| 75 |
+
from reportlab.lib.pagesizes import A5, landscape
|
| 76 |
+
from reportlab.pdfgen import canvas
|
| 77 |
+
from reportlab.lib.units import mm
|
| 78 |
+
from reportlab.lib.colors import HexColor
|
| 79 |
+
from datetime import datetime
|
| 80 |
+
|
| 81 |
+
def generate_certificate(name, score, total, instructor="{instructor}"):
|
| 82 |
+
unique_id = str(uuid.uuid4())
|
| 83 |
+
filename = f"cert_{{unique_id}}.pdf"
|
| 84 |
+
filepath = os.path.join(tempfile.gettempdir(), filename)
|
| 85 |
+
c = canvas.Canvas(filepath, pagesize=landscape(A5))
|
| 86 |
+
width, height = landscape(A5)
|
| 87 |
+
c.setFillColor(HexColor("#fffdf6"))
|
| 88 |
+
c.rect(0, 0, width, height, stroke=0, fill=1)
|
| 89 |
+
c.setStrokeColor(HexColor("#001858"))
|
| 90 |
+
c.setLineWidth(3)
|
| 91 |
+
margin = 10 * mm
|
| 92 |
+
c.rect(margin, margin, width - 2 * margin, height - 2 * margin)
|
| 93 |
+
c.setFillColor(HexColor("#001858"))
|
| 94 |
+
c.setFont("Helvetica-Bold", 24)
|
| 95 |
+
c.drawCentredString(width / 2, height - 60, "Certificate of Completion")
|
| 96 |
+
c.setFont("Helvetica", 14)
|
| 97 |
+
c.drawCentredString(width / 2, height - 100, "This is awarded to")
|
| 98 |
+
c.setFont("Helvetica-Bold", 18)
|
| 99 |
+
c.drawCentredString(width / 2, height - 130, name)
|
| 100 |
+
c.setFont("Helvetica", 14)
|
| 101 |
+
c.drawCentredString(width / 2, height - 160, "For successfully completing the quiz")
|
| 102 |
+
c.setFont("Helvetica", 12)
|
| 103 |
+
c.drawCentredString(width / 2, height - 185, f"Score: {{score}} / {{total}}")
|
| 104 |
+
c.setFont("Helvetica-Oblique", 10)
|
| 105 |
+
c.drawString(margin + 10, margin + 20, f"Instructor: {{instructor}}")
|
| 106 |
+
date_str = datetime.now().strftime("%d %B %Y")
|
| 107 |
+
c.setFont("Helvetica", 10)
|
| 108 |
+
c.drawRightString(width - margin - 10, margin + 20, f"Issued on: {{date_str}}")
|
| 109 |
+
c.save()
|
| 110 |
+
return filepath
|
| 111 |
+
|
| 112 |
+
quiz_type = "{quiz_type}"
|
| 113 |
+
questions = {parsed_questions}
|
| 114 |
+
|
| 115 |
+
def eval_quiz(name, *answers):
|
| 116 |
+
score = 0
|
| 117 |
+
for i, ans in enumerate(answers):
|
| 118 |
+
if hashlib.sha256(ans.lower().strip().encode()).hexdigest() == questions[i]["answer_hash"]:
|
| 119 |
+
score += 1
|
| 120 |
+
cert_path = generate_certificate(name, score, len(questions), instructor="{instructor}")
|
| 121 |
+
return f"Score: {{score}} / {{len(questions)}}", cert_path
|
| 122 |
+
|
| 123 |
+
with gr.Blocks() as app:
|
| 124 |
+
gr.Markdown("## {title}")
|
| 125 |
+
name = gr.Textbox(label="Your Name")
|
| 126 |
+
answer_inputs = []
|
| 127 |
+
for q in questions:
|
| 128 |
+
gr.Markdown(q["question"])
|
| 129 |
+
if quiz_type == "Multiple Choice":
|
| 130 |
+
answer_inputs.append(gr.Radio(choices=q["options"], label="Select Answer"))
|
| 131 |
+
else:
|
| 132 |
+
answer_inputs.append(gr.Textbox(label="Your Answer"))
|
| 133 |
+
submit = gr.Button("Submit")
|
| 134 |
+
result = gr.Textbox(label="Result")
|
| 135 |
+
cert = gr.File(label="Download Certificate")
|
| 136 |
+
submit.click(fn=eval_quiz, inputs=[name] + answer_inputs, outputs=[result, cert])
|
| 137 |
+
|
| 138 |
+
app.launch()
|
| 139 |
+
'''
|
| 140 |
+
return python_code
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
# --- Instructor Interface ---
|
| 144 |
+
with gr.Blocks() as interface:
|
| 145 |
+
gr.Markdown("## Instructor Quiz Generator")
|
| 146 |
+
|
| 147 |
+
title = gr.Textbox(label="Quiz Title", placeholder="e.g. Python Basics Quiz")
|
| 148 |
+
instructor = gr.Textbox(label="Instructor Name", placeholder="e.g. Dr. Smith")
|
| 149 |
+
quiz_type = gr.Dropdown(choices=["Multiple Choice", "Text Answer"], label="Quiz Type", value="Multiple Choice")
|
| 150 |
+
questions = gr.Textbox(lines=10, label="Questions and Answers",
|
| 151 |
+
placeholder="For MCQ: Question, Option1, Option2, Option3, Correct Option\nFor Text: Question, Correct Answer")
|
| 152 |
+
|
| 153 |
+
generate_btn = gr.Button("Generate Python Quiz Code")
|
| 154 |
+
output = gr.Code(label="Generated Python Code")
|
| 155 |
+
|
| 156 |
+
generate_btn.click(fn=generate_python_code, inputs=[title, instructor, quiz_type, questions], outputs=output)
|
| 157 |
+
|
| 158 |
+
interface.launch()
|