Spaces:
Runtime error
Runtime error
import gradio as gr | |
import qrcode | |
import numpy as np | |
from delta import * | |
import pyspark | |
import pyspark.sql.functions as F | |
from PIL import Image, ImageDraw, ImageFont, ImageOps | |
import os | |
from huggingface_hub import HfApi, hf_hub_download, Repository | |
# Configurar dataset de voluntarios | |
api = HfApi() | |
DATASET_REPO_URL = "https://huggingface.co/datasets/ieeecsuna-comite/bd-voluntarios" | |
VOLUNTARIOS_TABLE = "voluntarios" | |
VOLUNTARIOS_DIR = "voluntarios_repo" | |
HF_TOKEN = os.environ.get("HF_TOKEN") | |
repo = Repository( | |
local_dir=VOLUNTARIOS_DIR, clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN | |
) | |
repo.git_pull() | |
# Crear la app de Spark | |
builder = pyspark.sql.SparkSession.builder.appName("IEEECSTOOLS") \ | |
.config("spark.sql.extensions", "io.delta.sql.DeltaSparkSessionExtension") \ | |
.config("spark.sql.catalog.spark_catalog", "org.apache.spark.sql.delta.catalog.DeltaCatalog") | |
spark = configure_spark_with_delta_pip(builder).getOrCreate() | |
bd_cc = spark.read.format("delta").load(os.path.join(VOLUNTARIOS_DIR, VOLUNTARIOS_TABLE)) | |
# Paths | |
font_path = "./fonts" | |
modelos_path = "./modelos" | |
# Configuraciones | |
name_font = ImageFont.truetype(os.path.join(font_path, "Montserrat-Bold.ttf"), 45) | |
comite_font = ImageFont.truetype(os.path.join(font_path, "Montserrat-Regular.ttf"), 35) | |
codigo_font = ImageFont.truetype(os.path.join(font_path, "Montserrat-Regular.ttf"), 25) | |
def generar_portanombre(id = "CSUNA-XX0000"): | |
im = Image.open(os.path.join(modelos_path, "portanombres.png")).convert("RGBA") | |
try: | |
# Obtener voluntario | |
voluntario = bd_cc.filter(F.col("cod_voluntario") == id).collect()[0] | |
# Construir el nombre y el c贸digo de voluntario | |
name = (str(voluntario["Nombre"]) + " " + str(voluntario["Apellido"])).upper() | |
codigo = id | |
# Generar c贸digo QR | |
codigo_qr = codigo_qr = qrcode.make(codigo).convert("RGBA").resize((290, 290)) | |
mascara_qr = Image.open(os.path.join(modelos_path, "mascara_qr.png")).convert("L").resize((290, 290)) | |
im.paste(codigo_qr, (645, 91), mask=mascara_qr) | |
# Poner los nombres | |
d = ImageDraw.Draw(im) | |
# Nombre | |
if len(name) < 30: | |
d.text((505, 517), name, fill="black", anchor="mm", font=name_font) | |
else: | |
d.text((505, 517), name, fill="black", anchor="mm", font=ImageFont.truetype(os.path.join(font_path, "Montserrat-Bold.ttf"), 35)) | |
# Comite | |
d.text((505, 573), "Comit茅 de Cursos y Charlas", fill="black", anchor="mm", font=comite_font) | |
# Codigo | |
d.text((790, 361), codigo, fill="gray", anchor="mm", font=codigo_font) | |
except Exception as e: | |
print("Hubo un error al generar: " + e) | |
finally: | |
return im.convert("RGB") | |
with gr.Blocks() as portanombre_demo: | |
id = gr.Textbox(label="Identificador") | |
portanombre = gr.Image(label="Portanombre") | |
generar_boton = gr.Button("Generar Portanombre") | |
generar_boton.click(fn=generar_portanombre, inputs=id, outputs=portanombre, api_name="portanombre") | |
def generar_qr(texto): | |
return np.array(qrcode.make(texto).convert("L")) | |
qr_demo = gr.Interface(fn=generar_qr, | |
inputs=gr.Text(label="Texto/Link"), | |
outputs=gr.Image(label="C贸digo QR")) | |
demo = gr.TabbedInterface([portanombre_demo, | |
qr_demo], | |
["馃挸 Generar Portanombre", | |
"馃 Generador de C贸digo QR"], | |
title="Herramientas de IEEE CS SBC UNA") | |
demo.launch() | |