sudhir2016 commited on
Commit
7d4cb09
·
verified ·
1 Parent(s): 1c438fe

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -0
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import uuid, os, tempfile, hashlib
3
+ from reportlab.lib.pagesizes import A5, landscape
4
+ from reportlab.pdfgen import canvas
5
+ from reportlab.lib.units import mm
6
+ from reportlab.lib.colors import HexColor
7
+ from datetime import datetime
8
+
9
+ # Certificate generation function (included for a self-contained script)
10
+ def generate_certificate(name, score, total, instructor="Sudhir Gupta"):
11
+ unique_id = str(uuid.uuid4())
12
+ filename = f"cert_{unique_id}.pdf"
13
+ filepath = os.path.join(tempfile.gettempdir(), filename)
14
+ c = canvas.Canvas(filepath, pagesize=landscape(A5))
15
+ width, height = landscape(A5)
16
+ c.setFillColor(HexColor("#fffdf6"))
17
+ c.rect(0, 0, width, height, stroke=0, fill=1)
18
+ c.setStrokeColor(HexColor("#001858"))
19
+ c.setLineWidth(3)
20
+ margin = 10 * mm
21
+ c.rect(margin, margin, width - 2 * margin, height - 2 * margin)
22
+ c.setFillColor(HexColor("#001858"))
23
+ c.setFont("Helvetica-Bold", 24)
24
+ c.drawCentredString(width / 2, height - 60, "Certificate of Completion")
25
+ c.setFont("Helvetica", 14)
26
+ c.drawCentredString(width / 2, height - 100, "This is awarded to")
27
+ c.setFont("Helvetica-Bold", 18)
28
+ c.drawCentredString(width / 2, height - 130, name)
29
+ c.setFont("Helvetica", 14)
30
+ c.drawCentredString(width / 2, height - 160, "For successfully completing the quiz - Our Solar System")
31
+ c.setFont("Helvetica", 12)
32
+ c.drawCentredString(width / 2, height - 185, f"Score: {score} / {total}")
33
+ c.setFont("Helvetica-Oblique", 10)
34
+ c.drawString(margin + 10, margin + 20, f"Instructor: {instructor}")
35
+ date_str = datetime.now().strftime("%d %B %Y")
36
+ c.setFont("Helvetica", 10)
37
+ c.drawRightString(width - margin - 10, margin + 20, f"Issued on: {date_str}")
38
+ c.save()
39
+ return filepath
40
+
41
+ # Quiz data (answers are hashed)
42
+ quiz_type = "Multiple Choice"
43
+ 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'}]
44
+
45
+ def eval_quiz(name, *answers):
46
+ """
47
+ Evaluates the student's answers and generates a certificate only if the score is 80% or higher.
48
+ """
49
+ if not name.strip():
50
+ name = "Anonymous"
51
+ score = 0
52
+ for i, ans in enumerate(answers):
53
+ if ans and hashlib.sha256(str(ans).lower().strip().encode()).hexdigest() == questions[i]["answer_hash"]:
54
+ score += 1
55
+
56
+ total_questions = len(questions)
57
+ passing_threshold = 0.8
58
+
59
+ result_message = f"Hi {name}, your score is: {score} / {total_questions}."
60
+ cert_path = None # Default to no certificate
61
+
62
+ # Check if the score meets the passing threshold
63
+ if total_questions > 0 and (score / total_questions) >= passing_threshold:
64
+ cert_path = generate_certificate(name, score, total_questions, instructor="Sudhir Gupta")
65
+ result_message += " Congratulations, you passed and earned a certificate!"
66
+ else:
67
+ result_message += " A score of 80% is required to receive a certificate."
68
+
69
+ return result_message, cert_path
70
+
71
+ # Gradio interface for the student
72
+ with gr.Blocks(theme=gr.themes.Soft()) as app:
73
+ gr.Markdown("## Our Solar System")
74
+
75
+ with gr.Row():
76
+ name = gr.Textbox(label="Enter Your Full Name to Generate Certificate", placeholder="e.g., Ada Lovelace")
77
+
78
+ answer_inputs = []
79
+ for q in questions:
80
+ gr.Markdown("**Question:** " + q['question'])
81
+ if quiz_type == "Multiple Choice":
82
+ answer_inputs.append(gr.Radio(choices=q["options"], label="Select your answer"))
83
+ else:
84
+ answer_inputs.append(gr.Textbox(label="Type your answer"))
85
+
86
+ submit_btn = gr.Button("Submit Quiz")
87
+
88
+ with gr.Row():
89
+ result_output = gr.Textbox(label="Your Result")
90
+ certificate_output = gr.File(label="Download Your Certificate")
91
+
92
+ submit_btn.click(
93
+ fn=eval_quiz,
94
+ inputs=[name] + answer_inputs,
95
+ outputs=[result_output, certificate_output]
96
+ )
97
+
98
+ app.launch(debug=True)