Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -196,6 +196,175 @@ def create_quiz_interface():
|
|
196 |
# Certificate generation code here (same as before)
|
197 |
return "Certificate generated!"
|
198 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
199 |
|
200 |
score_display.change(
|
201 |
fn=generate_certificate,
|
|
|
196 |
# Certificate generation code here (same as before)
|
197 |
return "Certificate generated!"
|
198 |
return None
|
199 |
+
|
200 |
+
def generate_certificate(score, name, course_name, company_logo=None, participant_photo=None):
|
201 |
+
"""
|
202 |
+
Generate a certificate with custom styling and optional logo/photo
|
203 |
+
|
204 |
+
Args:
|
205 |
+
score (float): Assessment score (0-100)
|
206 |
+
name (str): Participant's name
|
207 |
+
course_name (str): Name of the course/assessment
|
208 |
+
company_logo (str, optional): Path to company logo image
|
209 |
+
participant_photo (str, optional): Path to participant's photo
|
210 |
+
|
211 |
+
Returns:
|
212 |
+
str: Path to generated certificate image file
|
213 |
+
"""
|
214 |
+
try:
|
215 |
+
# Create certificate with a light blue background
|
216 |
+
certificate = Image.new('RGB', (1200, 800), '#F0F8FF')
|
217 |
+
draw = ImageDraw.Draw(certificate)
|
218 |
+
|
219 |
+
# Try to load fonts, fallback to default if necessary
|
220 |
+
try:
|
221 |
+
title_font = ImageFont.truetype("arial.ttf", 60)
|
222 |
+
text_font = ImageFont.truetype("arial.ttf", 40)
|
223 |
+
subtitle_font = ImageFont.truetype("arial.ttf", 30)
|
224 |
+
except Exception as e:
|
225 |
+
print(f"Font loading error: {e}. Using default font.")
|
226 |
+
title_font = ImageFont.load_default()
|
227 |
+
text_font = ImageFont.load_default()
|
228 |
+
subtitle_font = ImageFont.load_default()
|
229 |
+
|
230 |
+
# Add decorative border
|
231 |
+
border_color = '#4682B4' # Steel blue
|
232 |
+
draw.rectangle([20, 20, 1180, 780], outline=border_color, width=3)
|
233 |
+
|
234 |
+
# Add inner border
|
235 |
+
draw.rectangle([40, 40, 1160, 760], outline=border_color, width=1)
|
236 |
+
|
237 |
+
# Add certificate content
|
238 |
+
# Title
|
239 |
+
draw.text(
|
240 |
+
(600, 100),
|
241 |
+
"CertifyMe AI",
|
242 |
+
font=title_font,
|
243 |
+
fill='#4682B4',
|
244 |
+
anchor="mm"
|
245 |
+
)
|
246 |
+
|
247 |
+
# Subtitle
|
248 |
+
draw.text(
|
249 |
+
(600, 160),
|
250 |
+
"Certificate of Achievement",
|
251 |
+
font=subtitle_font,
|
252 |
+
fill='#4682B4',
|
253 |
+
anchor="mm"
|
254 |
+
)
|
255 |
+
|
256 |
+
# Main content
|
257 |
+
draw.text(
|
258 |
+
(600, 300),
|
259 |
+
"This is to certify that",
|
260 |
+
font=text_font,
|
261 |
+
fill='black',
|
262 |
+
anchor="mm"
|
263 |
+
)
|
264 |
+
|
265 |
+
# Participant name
|
266 |
+
name = name.strip() if name else "Participant"
|
267 |
+
draw.text(
|
268 |
+
(600, 380),
|
269 |
+
name,
|
270 |
+
font=text_font,
|
271 |
+
fill='#4682B4',
|
272 |
+
anchor="mm"
|
273 |
+
)
|
274 |
+
|
275 |
+
# Completion text
|
276 |
+
draw.text(
|
277 |
+
(600, 460),
|
278 |
+
"has successfully completed",
|
279 |
+
font=text_font,
|
280 |
+
fill='black',
|
281 |
+
anchor="mm"
|
282 |
+
)
|
283 |
+
|
284 |
+
# Course name
|
285 |
+
course_name = course_name.strip() if course_name else "Assessment"
|
286 |
+
draw.text(
|
287 |
+
(600, 540),
|
288 |
+
course_name,
|
289 |
+
font=text_font,
|
290 |
+
fill='#4682B4',
|
291 |
+
anchor="mm"
|
292 |
+
)
|
293 |
+
|
294 |
+
# Score
|
295 |
+
draw.text(
|
296 |
+
(600, 620),
|
297 |
+
f"with a score of {score:.1f}%",
|
298 |
+
font=text_font,
|
299 |
+
fill='black',
|
300 |
+
anchor="mm"
|
301 |
+
)
|
302 |
+
|
303 |
+
# Date
|
304 |
+
current_date = datetime.now().strftime("%B %d, %Y")
|
305 |
+
draw.text(
|
306 |
+
(600, 700),
|
307 |
+
current_date,
|
308 |
+
font=text_font,
|
309 |
+
fill='black',
|
310 |
+
anchor="mm"
|
311 |
+
)
|
312 |
+
|
313 |
+
# Add logo if provided
|
314 |
+
if company_logo is not None:
|
315 |
+
try:
|
316 |
+
logo = Image.open(company_logo)
|
317 |
+
# Maintain aspect ratio
|
318 |
+
logo.thumbnail((150, 150))
|
319 |
+
# Calculate position to place logo
|
320 |
+
logo_x = 50
|
321 |
+
logo_y = 50
|
322 |
+
certificate.paste(logo, (logo_x, logo_y))
|
323 |
+
except Exception as e:
|
324 |
+
print(f"Error adding logo: {e}")
|
325 |
+
|
326 |
+
# Add photo if provided
|
327 |
+
if participant_photo is not None:
|
328 |
+
try:
|
329 |
+
photo = Image.open(participant_photo)
|
330 |
+
# Maintain aspect ratio
|
331 |
+
photo.thumbnail((150, 150))
|
332 |
+
# Calculate position to place photo
|
333 |
+
photo_x = 1000
|
334 |
+
photo_y = 50
|
335 |
+
certificate.paste(photo, (photo_x, photo_y))
|
336 |
+
except Exception as e:
|
337 |
+
print(f"Error adding photo: {e}")
|
338 |
+
|
339 |
+
# Add decorative corners
|
340 |
+
corner_size = 20
|
341 |
+
corner_color = '#4682B4'
|
342 |
+
|
343 |
+
# Top-left corner
|
344 |
+
draw.line([(20, 40), (20 + corner_size, 40)], fill=corner_color, width=2)
|
345 |
+
draw.line([(40, 20), (40, 20 + corner_size)], fill=corner_color, width=2)
|
346 |
+
|
347 |
+
# Top-right corner
|
348 |
+
draw.line([(1180 - corner_size, 40), (1180, 40)], fill=corner_color, width=2)
|
349 |
+
draw.line([(1160, 20), (1160, 20 + corner_size)], fill=corner_color, width=2)
|
350 |
+
|
351 |
+
# Bottom-left corner
|
352 |
+
draw.line([(20, 760), (20 + corner_size, 760)], fill=corner_color, width=2)
|
353 |
+
draw.line([(40, 780 - corner_size), (40, 780)], fill=corner_color, width=2)
|
354 |
+
|
355 |
+
# Bottom-right corner
|
356 |
+
draw.line([(1180 - corner_size, 760), (1180, 760)], fill=corner_color, width=2)
|
357 |
+
draw.line([(1160, 780 - corner_size), (1160, 780)], fill=corner_color, width=2)
|
358 |
+
|
359 |
+
# Save certificate
|
360 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
|
361 |
+
certificate.save(temp_file.name, 'PNG', quality=95)
|
362 |
+
return temp_file.name
|
363 |
+
|
364 |
+
except Exception as e:
|
365 |
+
print(f"Error generating certificate: {e}")
|
366 |
+
return None
|
367 |
+
|
368 |
|
369 |
score_display.change(
|
370 |
fn=generate_certificate,
|