Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,19 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import tempfile
|
| 3 |
import uuid
|
| 4 |
-
import os
|
| 5 |
from gradio_client import Client, handle_file
|
| 6 |
|
| 7 |
-
# ----------------
|
| 8 |
-
|
|
|
|
| 9 |
SPACE_A = "MonishRaman/Hackathon_certificate_api"
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
raise RuntimeError("HF_TOKEN not found in Space secrets")
|
| 14 |
-
|
| 15 |
-
# Create client ONCE
|
| 16 |
-
client = Client(SPACE_A, token=HF_TOKEN)
|
| 17 |
-
|
| 18 |
-
# ---------------- HTML TEMPLATE ----------------
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
CERT_HTML = """
|
| 21 |
<!DOCTYPE html>
|
| 22 |
<html>
|
|
@@ -40,8 +37,9 @@ CERT_HTML = """
|
|
| 40 |
</html>
|
| 41 |
"""
|
| 42 |
|
| 43 |
-
# ----------------
|
| 44 |
-
|
|
|
|
| 45 |
def generate_certificate(name, project, track):
|
| 46 |
if not name or not project or not track:
|
| 47 |
return None, "β All fields are required"
|
|
@@ -55,15 +53,16 @@ def generate_certificate(name, project, track):
|
|
| 55 |
cid=cert_id
|
| 56 |
)
|
| 57 |
|
| 58 |
-
#
|
| 59 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".html", mode="w", encoding="utf-8") as f:
|
| 60 |
f.write(html)
|
| 61 |
html_path = f.name
|
| 62 |
|
| 63 |
try:
|
|
|
|
| 64 |
result = client.predict(
|
| 65 |
handle_file(html_path),
|
| 66 |
-
api_name="/
|
| 67 |
)
|
| 68 |
|
| 69 |
# Normalize output
|
|
@@ -73,14 +72,14 @@ def generate_certificate(name, project, track):
|
|
| 73 |
if not result:
|
| 74 |
return None, "β No image returned from Space A"
|
| 75 |
|
| 76 |
-
return result, f"β
Certificate generated successfully
|
| 77 |
|
| 78 |
except Exception as e:
|
| 79 |
-
print("ERROR:", e)
|
| 80 |
return None, f"β Error: {str(e)}"
|
| 81 |
|
| 82 |
-
# ----------------
|
| 83 |
-
|
|
|
|
| 84 |
with gr.Blocks(title="Hackathon Certificate Generator") as demo:
|
| 85 |
gr.Markdown("# π Hackathon Certificate Generator")
|
| 86 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tempfile
|
| 3 |
import uuid
|
|
|
|
| 4 |
from gradio_client import Client, handle_file
|
| 5 |
|
| 6 |
+
# --------------------------------
|
| 7 |
+
# CONFIG
|
| 8 |
+
# --------------------------------
|
| 9 |
SPACE_A = "MonishRaman/Hackathon_certificate_api"
|
| 10 |
|
| 11 |
+
# Create client once
|
| 12 |
+
client = Client(SPACE_A)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# --------------------------------
|
| 15 |
+
# HTML TEMPLATE
|
| 16 |
+
# --------------------------------
|
| 17 |
CERT_HTML = """
|
| 18 |
<!DOCTYPE html>
|
| 19 |
<html>
|
|
|
|
| 37 |
</html>
|
| 38 |
"""
|
| 39 |
|
| 40 |
+
# --------------------------------
|
| 41 |
+
# CORE FUNCTION
|
| 42 |
+
# --------------------------------
|
| 43 |
def generate_certificate(name, project, track):
|
| 44 |
if not name or not project or not track:
|
| 45 |
return None, "β All fields are required"
|
|
|
|
| 53 |
cid=cert_id
|
| 54 |
)
|
| 55 |
|
| 56 |
+
# Save HTML
|
| 57 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".html", mode="w", encoding="utf-8") as f:
|
| 58 |
f.write(html)
|
| 59 |
html_path = f.name
|
| 60 |
|
| 61 |
try:
|
| 62 |
+
# π₯ CALL SPACE A (CORRECT ENDPOINT)
|
| 63 |
result = client.predict(
|
| 64 |
handle_file(html_path),
|
| 65 |
+
api_name="/predict"
|
| 66 |
)
|
| 67 |
|
| 68 |
# Normalize output
|
|
|
|
| 72 |
if not result:
|
| 73 |
return None, "β No image returned from Space A"
|
| 74 |
|
| 75 |
+
return result, f"β
Certificate generated successfully\nID: `{cert_id}`"
|
| 76 |
|
| 77 |
except Exception as e:
|
|
|
|
| 78 |
return None, f"β Error: {str(e)}"
|
| 79 |
|
| 80 |
+
# --------------------------------
|
| 81 |
+
# UI
|
| 82 |
+
# --------------------------------
|
| 83 |
with gr.Blocks(title="Hackathon Certificate Generator") as demo:
|
| 84 |
gr.Markdown("# π Hackathon Certificate Generator")
|
| 85 |
|