Spaces:
Sleeping
Sleeping
import gradio as gr | |
from datasets import load_dataset, Dataset | |
from datetime import datetime | |
import io | |
from fpdf import FPDF | |
import os | |
# Constants | |
SCORES_DATASET = "agents-course/unit4-students-scores" | |
CERTIFICATES_DATASET = "agents-course/course-certificates-of-excellence" | |
THRESHOLD_SCORE = 45 | |
# Function to check user score | |
def check_user_score(username): | |
score_data = load_dataset(SCORES_DATASET, split="train", download_mode="force_redownload") | |
matches = [row for row in score_data if row["username"] == username] | |
return matches[0] if matches else None | |
# Function to check if certificate entry exists | |
def has_certificate_entry(username): | |
cert_data = load_dataset(CERTIFICATES_DATASET, split="train", download_mode="force_redownload") | |
return any(row["username"] == username for row in cert_data) | |
# Function to add certificate entry | |
def add_certificate_entry(username, name): | |
new_entry = { | |
"username": username, | |
"name": name, | |
"date": datetime.now().strftime("%Y-%m-%d"), | |
} | |
ds = load_dataset(CERTIFICATES_DATASET, split="train") | |
ds = ds.add_item(new_entry) | |
ds.push_to_hub(CERTIFICATES_DATASET) | |
# Function to generate certificate PDF | |
def generate_certificate(name, score): | |
pdf = FPDF() | |
pdf.add_page() | |
pdf.set_font("Arial", size=24) | |
pdf.cell(200, 20, txt="Certificate of Excellence", ln=True, align="C") | |
pdf.set_font("Arial", size=16) | |
pdf.cell(200, 10, txt=f"Presented to {name}", ln=True, align="C") | |
pdf.cell(200, 10, txt=f"For passing Unit 4 with a score of {score}", ln=True, align="C") | |
pdf.cell(200, 10, txt=f"Date: {datetime.now().strftime('%Y-%m-%d')}", ln=True, align="C") | |
pdf_output = io.BytesIO() | |
pdf.output(pdf_output) | |
pdf_output.seek(0) | |
return pdf_output | |
# Main function to handle certificate generation | |
def handle_certificate(name, profile: gr.OAuthProfile | None): | |
if profile is None: | |
return "You must be logged in with your Hugging Face account.", None | |
username = profile.username | |
user_score = check_user_score(username) | |
if not user_score: | |
return "You need to complete Unit 4 first.", None | |
score = user_score["score"] | |
if score < THRESHOLD_SCORE: | |
return f"Your score is {score}. You need at least {THRESHOLD_SCORE} to pass.", None | |
if not has_certificate_entry(username): | |
add_certificate_entry(username, name) | |
certificate = generate_certificate(name, score) | |
return "Congratulations! Here's your certificate:", certificate | |
# Gradio interface | |
with gr.Blocks() as demo: | |
gr.Markdown("# 🎓 Unit 4 Certificate Generator") | |
gr.LoginButton() | |
with gr.Row(): | |
name_input = gr.Text(label="Enter your name") | |
generate_btn = gr.Button("Get my certificate") | |
output_text = gr.Textbox(label="Result") | |
cert_file = gr.File(label="Your Certificate", file_types=[".pdf"]) | |
generate_btn.click( | |
fn=handle_certificate, | |
inputs=[name_input, gr.OAuthProfile()], | |
outputs=[output_text, cert_file] | |
) | |
demo.launch() | |