Spaces:
Runtime error
Runtime error
New Logic Added
Browse files
app.py
CHANGED
|
@@ -1,79 +1,119 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Constants
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
else:
|
| 15 |
-
return []
|
| 16 |
-
|
| 17 |
-
# 2. Replace this function with your actual agent logic
|
| 18 |
def process_question(question):
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
def
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
"username": username,
|
| 26 |
-
"
|
| 27 |
-
"
|
| 28 |
}
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
with gr.Blocks() as demo:
|
| 63 |
-
gr.Markdown("#
|
| 64 |
-
gr.Markdown("
|
|
|
|
| 65 |
|
| 66 |
-
with gr.
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
-
fn=
|
| 75 |
-
inputs=[
|
| 76 |
-
outputs=output_text
|
| 77 |
)
|
| 78 |
|
| 79 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from datasets import load_dataset, Dataset
|
| 3 |
+
from datetime import datetime, date
|
| 4 |
+
import os
|
| 5 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 6 |
+
from huggingface_hub import login
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
|
| 9 |
+
# Authenticate with Hugging Face
|
| 10 |
+
login(token=os.environ["HUGGINGFACE_TOKEN"])
|
| 11 |
|
| 12 |
# Constants
|
| 13 |
+
SCORES_DATASET = "agents-course/unit4-students-scores"
|
| 14 |
+
CERTIFICATES_DATASET = "agents-course/course-certificates-of-excellence"
|
| 15 |
+
THRESHOLD_SCORE = 30
|
| 16 |
+
|
| 17 |
+
# Load text-generation pipeline (FLAN-T5)
|
| 18 |
+
qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-base")
|
| 19 |
+
|
| 20 |
+
# Process question using model
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def process_question(question):
|
| 22 |
+
if not question or not isinstance(question, str):
|
| 23 |
+
return "I don't understand the question."
|
| 24 |
+
result = qa_pipeline(question, max_new_tokens=50)
|
| 25 |
+
return result[0]["generated_text"].strip()
|
| 26 |
+
|
| 27 |
+
# Function to check user score
|
| 28 |
+
def check_user_score(username):
|
| 29 |
+
score_data = load_dataset(SCORES_DATASET, split="train", download_mode="force_redownload")
|
| 30 |
+
matches = [row for row in score_data if row["username"] == username]
|
| 31 |
+
return matches[0] if matches else None
|
| 32 |
+
|
| 33 |
+
# Check if user already has certificate
|
| 34 |
+
def has_certificate_entry(username):
|
| 35 |
+
cert_data = load_dataset(CERTIFICATES_DATASET, split="train", download_mode="force_redownload")
|
| 36 |
+
return any(row["username"] == username for row in cert_data)
|
| 37 |
|
| 38 |
+
# Add certificate entry
|
| 39 |
+
def add_certificate_entry(username, name, score):
|
| 40 |
+
ds = load_dataset(CERTIFICATES_DATASET, split="train", download_mode="force_redownload")
|
| 41 |
+
filtered_rows = [row for row in ds if row["username"] != username]
|
| 42 |
+
new_entry = {
|
| 43 |
"username": username,
|
| 44 |
+
"score": score,
|
| 45 |
+
"timestamp": datetime.now().isoformat()
|
| 46 |
}
|
| 47 |
+
filtered_rows.append(new_entry)
|
| 48 |
+
updated_ds = Dataset.from_list(filtered_rows)
|
| 49 |
+
updated_ds.push_to_hub(CERTIFICATES_DATASET)
|
| 50 |
+
|
| 51 |
+
# Generate certificate
|
| 52 |
+
def generate_certificate(name, score):
|
| 53 |
+
certificate_path = os.path.join(os.path.dirname(__file__), "templates", "certificate.png")
|
| 54 |
+
im = Image.open(certificate_path)
|
| 55 |
+
d = ImageDraw.Draw(im)
|
| 56 |
+
|
| 57 |
+
name_font = ImageFont.truetype("Quattrocento-Regular.ttf", 100)
|
| 58 |
+
date_font = ImageFont.truetype("Quattrocento-Regular.ttf", 48)
|
| 59 |
+
|
| 60 |
+
name = name.title()
|
| 61 |
+
d.text((1000, 740), name, fill="black", anchor="mm", font=name_font)
|
| 62 |
+
d.text((1480, 1170), str(date.today()), fill="black", anchor="mm", font=date_font)
|
| 63 |
+
|
| 64 |
+
pdf = im.convert("RGB")
|
| 65 |
+
pdf.save("certificate.pdf")
|
| 66 |
+
|
| 67 |
+
return im, "certificate.pdf"
|
| 68 |
+
|
| 69 |
+
# Handle certificate logic
|
| 70 |
+
def handle_certificate(name, profile: gr.OAuthProfile):
|
| 71 |
+
if profile is None:
|
| 72 |
+
return "You must be logged in with your Hugging Face account.", None, None
|
| 73 |
+
|
| 74 |
+
username = profile.username
|
| 75 |
+
user_score = check_user_score(username)
|
| 76 |
+
|
| 77 |
+
if not user_score:
|
| 78 |
+
return "You need to complete Unit 4 first.", None, None
|
| 79 |
+
|
| 80 |
+
score = user_score["score"]
|
| 81 |
+
|
| 82 |
+
if score < THRESHOLD_SCORE:
|
| 83 |
+
return f"Your score is {score}. You need at least {THRESHOLD_SCORE} to pass.", None, None
|
| 84 |
+
|
| 85 |
+
certificate_image, certificate_pdf = generate_certificate(name, score)
|
| 86 |
+
add_certificate_entry(username, name, score)
|
| 87 |
+
return "π Congratulations! Here's your certificate:", certificate_image, certificate_pdf
|
| 88 |
+
|
| 89 |
+
# Gradio UI
|
| 90 |
with gr.Blocks() as demo:
|
| 91 |
+
gr.Markdown("# π Agents Course - Get Your Final Certificate")
|
| 92 |
+
gr.Markdown("Welcome! Follow the steps below to receive your official certificate:")
|
| 93 |
+
gr.Markdown("β οΈ **Note**: Due to high demand, you might experience occasional bugs. If something doesn't work, please try again after a moment!")
|
| 94 |
|
| 95 |
+
with gr.Group():
|
| 96 |
+
gr.Markdown("## β
How it works")
|
| 97 |
+
gr.Markdown("""
|
| 98 |
+
1. **Sign in** with your Hugging Face account using the button below.
|
| 99 |
+
2. **Enter your full name** (this will appear on the certificate).
|
| 100 |
+
3. Click **'Get My Certificate'** to check your score and download your certificate.
|
| 101 |
+
""")
|
| 102 |
+
gr.Markdown("---")
|
| 103 |
+
gr.Markdown("π **Note**: You must have completed [Unit 4](https://huggingface.co/learn/agents-course/unit4/introduction) and your Agent must have scored **above 30** to get your certificate.")
|
| 104 |
|
| 105 |
+
gr.LoginButton()
|
| 106 |
+
with gr.Row():
|
| 107 |
+
name_input = gr.Text(label="Enter your name (this will appear on the certificate)")
|
| 108 |
+
generate_btn = gr.Button("Get my certificate")
|
| 109 |
+
output_text = gr.Textbox(label="Result")
|
| 110 |
+
cert_image = gr.Image(label="Your Certificate")
|
| 111 |
+
cert_file = gr.File(label="Download Certificate (PDF)", file_types=[".pdf"])
|
| 112 |
|
| 113 |
+
generate_btn.click(
|
| 114 |
+
fn=handle_certificate,
|
| 115 |
+
inputs=[name_input],
|
| 116 |
+
outputs=[output_text, cert_image, cert_file]
|
| 117 |
)
|
| 118 |
|
| 119 |
demo.launch()
|