File size: 2,601 Bytes
59ecc58 98edeea fdade21 1c74407 d43b3c5 1c74407 0accc0a d43b3c5 59ecc58 d43b3c5 fdade21 98edeea cba6db9 fdade21 59ecc58 fdade21 59ecc58 d43b3c5 1c74407 98edeea 1c74407 cba6db9 1c74407 e25e0b8 d43b3c5 1c74407 e25e0b8 1c74407 d43b3c5 1c74407 e25e0b8 d43b3c5 e25e0b8 1c74407 0accc0a e25e0b8 d43b3c5 98edeea e25e0b8 98edeea d43b3c5 98edeea fdade21 98edeea 1c74407 98edeea cba6db9 fdade21 98edeea 1c74407 98edeea cba6db9 59ecc58 98edeea |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
import gradio as gr
import tempfile
import uuid
from gradio_client import Client, handle_file
# --------------------------------
# CONFIG
# --------------------------------
SPACE_A = "MonishRaman/Hackathon_certificate_api"
# Create client once
client = Client(SPACE_A)
# --------------------------------
# HTML TEMPLATE
# --------------------------------
CERT_HTML = """
<!DOCTYPE html>
<html>
<body style="
width:2000px;
height:1414px;
margin:0;
display:flex;
justify-content:center;
align-items:center;
background:linear-gradient(135deg,#FF6B35,#FF8C00);
font-family:Arial;">
<div style="background:white;padding:80px;text-align:center;width:85%;height:80%;">
<h1>Certificate of Participation</h1>
<h2>{name}</h2>
<p>Project: {project}</p>
<p>Track: {track}</p>
<p>Certificate ID: {cid}</p>
</div>
</body>
</html>
"""
# --------------------------------
# CORE FUNCTION
# --------------------------------
def generate_certificate(name, project, track):
if not name or not project or not track:
return None, "β All fields are required"
cert_id = f"CERT-{uuid.uuid4().hex[:8].upper()}"
html = CERT_HTML.format(
name=name.strip(),
project=project.strip(),
track=track.strip(),
cid=cert_id
)
# Save HTML
with tempfile.NamedTemporaryFile(delete=False, suffix=".html", mode="w", encoding="utf-8") as f:
f.write(html)
html_path = f.name
try:
# π₯ CALL SPACE A (CORRECT ENDPOINT)
result = client.predict(
handle_file(html_path),
api_name="/predict"
)
# Normalize output
if isinstance(result, list):
result = result[0]
if not result:
return None, "β No image returned from Space A"
return result, f"β
Certificate generated successfully\nID: `{cert_id}`"
except Exception as e:
return None, f"β Error: {str(e)}"
# --------------------------------
# UI
# --------------------------------
with gr.Blocks(title="Hackathon Certificate Generator") as demo:
gr.Markdown("# π Hackathon Certificate Generator")
name = gr.Textbox(label="Participant Name")
project = gr.Textbox(label="Project Name")
track = gr.Textbox(label="Track")
generate_btn = gr.Button("Generate Certificate", variant="primary")
output = gr.File(label="Download Certificate")
status = gr.Markdown()
generate_btn.click(
fn=generate_certificate,
inputs=[name, project, track],
outputs=[output, status]
)
demo.launch()
|