Download utils.py from Roudrigus/pegavisao: direct link, hf CLI and curl.
- Browser
- Download file 2.47 kB
-
https://huggingface.co/spaces/Roudrigus/pegavisao/resolve/main/utils.py
- Command line
-
hf download hf://spaces/Roudrigus/pegavisao/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/Roudrigus/pegavisao/resolve/main/utils.py
2.47 kB
| # -*- coding: utf-8 -*- | |
| import os | |
| import io | |
| import hashlib | |
| from datetime import datetime | |
| from typing import Optional | |
| from reportlab.pdfgen import canvas | |
| from reportlab.lib.pagesizes import A4 | |
| from reportlab.lib.units import cm | |
| from reportlab.lib import colors | |
| import streamlit as st | |
| # Credenciais de admin via env (defina no Hugging Face Space Settings) | |
| ADMIN_USER = os.environ.get("ADMIN_USER", "admin") | |
| # Armazena hash sha256 da senha; por padrão 'admin' | |
| _admin_pass_default = os.environ.get("ADMIN_PASS", "admin") | |
| ADMIN_PASS_HASH = hashlib.sha256(_admin_pass_default.encode()).hexdigest() | |
| def ensure_dirs(): | |
| os.makedirs("data", exist_ok=True) | |
| os.makedirs("data/videos", exist_ok=True) | |
| os.makedirs("data/pdfs", exist_ok=True) | |
| os.makedirs("data/certificates", exist_ok=True) | |
| def save_uploaded_file(file, base_dir="data") -> str: | |
| ensure_dirs() | |
| filename = file.name | |
| safe_name = f"{int(datetime.utcnow().timestamp())}_{filename}" | |
| out_path = os.path.join(base_dir, safe_name) | |
| with open(out_path, "wb") as f: | |
| f.write(file.getbuffer()) | |
| return out_path | |
| def human_dt(dt: Optional[datetime]) -> str: | |
| if not dt: | |
| return "—" | |
| return dt.strftime("%d/%m/%Y %H:%M") | |
| def generate_certificate_pdf(student_name: str, course_title: str) -> str: | |
| ensure_dirs() | |
| fname = f"cert_{int(datetime.utcnow().timestamp())}.pdf" | |
| out_path = os.path.join("data/certificates", fname) | |
| c = canvas.Canvas(out_path, pagesize=A4) | |
| w, h = A4 | |
| # Moldura simples | |
| c.setStrokeColor(colors.HexColor("#2c3e50")) | |
| c.setLineWidth(6) | |
| c.rect(1*cm, 1*cm, w-2*cm, h-2*cm) | |
| # Título | |
| c.setFillColor(colors.HexColor("#2c3e50")) | |
| c.setFont("Helvetica-Bold", 28) | |
| c.drawCentredString(w/2, h-5*cm, "CERTIFICADO DE CONCLUSÃO") | |
| # Texto principal | |
| c.setFillColor(colors.black) | |
| c.setFont("Helvetica", 14) | |
| text = ( | |
| f"Certificamos que {student_name} concluiu com êxito o curso '{course_title}'." | |
| ) | |
| c.drawCentredString(w/2, h-8*cm, text) | |
| # Data | |
| c.setFont("Helvetica-Oblique", 12) | |
| c.drawCentredString(w/2, h-10*cm, datetime.now().strftime("Emitido em %d/%m/%Y")) | |
| # Assinatura (placeholder) | |
| c.line(w/2-5*cm, 4*cm, w/2+5*cm, 4*cm) | |
| c.setFont("Helvetica", 10) | |
| c.drawCentredString(w/2, 3.5*cm, "Coordenação — ARM Logística") | |
| c.showPage() | |
| c.save() | |
| return out_path |