Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from datasets import load_dataset, Dataset
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
import io
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Load the datasets
|
| 8 |
+
SCORES_DATASET = "agents-course/unit4-students-scores"
|
| 9 |
+
CERTIFICATES_DATASET = "agents-course/course-certificates-of-excellence"
|
| 10 |
+
THRESOLD SCORE = 45
|
| 11 |
+
|
| 12 |
+
# Check the score based on username
|
| 13 |
+
def check_user_score(username):
|
| 14 |
+
score_data = load_dataset(SCORES_DATASET, split="train", download_mode="force_redownload")
|
| 15 |
+
matches = [row for row in score_data if row["username"] == username]
|
| 16 |
+
return matches[0] if matches else None
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Check if this user already generated a certificate
|
| 20 |
+
def has_certificate_entry(username):
|
| 21 |
+
cert_data = load_dataset(CERTIFICATES_DATASET, split="train", download_mode="force_redownload")
|
| 22 |
+
return any(row["username"] == username for row in cert_data)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def add_certificate_entry(username, name):
|
| 26 |
+
# Create a new entry
|
| 27 |
+
new_entry = {
|
| 28 |
+
"username": username,
|
| 29 |
+
"name": name,
|
| 30 |
+
"date": datetime.now().strftime("%Y-%m-%d"),
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
# Download the dataset, append and push
|
| 34 |
+
ds = load_dataset(CERTIFICATES_DATASET, split="train")
|
| 35 |
+
updated = ds.add_item(new_entry)
|
| 36 |
+
updated.push_to_hub(CERTIFICATES_DATASET)
|
| 37 |
+
|
| 38 |
+
def generate_certificate(name, score):
|
| 39 |
+
pass
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def handle_certificate(name, request: gr.Request):
|
| 43 |
+
username = request.username
|
| 44 |
+
|
| 45 |
+
if not username:
|
| 46 |
+
return "You must be logged in with your Hugging Face account.", None
|
| 47 |
+
|
| 48 |
+
user_score = check_user_score(username)
|
| 49 |
+
|
| 50 |
+
if not user_score:
|
| 51 |
+
return "You need to complete Unit 4 first.", None
|
| 52 |
+
|
| 53 |
+
score = user_score["score"]
|
| 54 |
+
|
| 55 |
+
if score < THRESOLD SCORE:
|
| 56 |
+
return f"Your score is {score}. You need at least 65 to pass.", None
|
| 57 |
+
|
| 58 |
+
# Passed: check if already in certificate dataset
|
| 59 |
+
if not has_certificate_entry(username):
|
| 60 |
+
add_certificate_entry(username, name)
|
| 61 |
+
|
| 62 |
+
certificate = generate_certificate(name, score)
|
| 63 |
+
return "Congratulations! Here's your certificate:", certificate
|
| 64 |
+
|
| 65 |
+
with gr.Blocks(auth=True) as demo:
|
| 66 |
+
gr.Markdown("# 🎓 Unit 4 Certificate Generator")
|
| 67 |
+
with gr.Row():
|
| 68 |
+
name_input = gr.Text(label="Enter your name")
|
| 69 |
+
generate_btn = gr.Button("Get my certificate")
|
| 70 |
+
output_text = gr.Textbox(label="Result")
|
| 71 |
+
cert_file = gr.File(label="Your Certificate", file_types=[".pdf"])
|
| 72 |
+
|
| 73 |
+
generate_btn.click(
|
| 74 |
+
fn=handle_certificate,
|
| 75 |
+
inputs=[name_input],
|
| 76 |
+
outputs=[output_text, cert_file]
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
demo.launch()
|