MonishRaman's picture
Update app.py
d43b3c5 verified
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()