Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tempfile
|
| 3 |
+
from playwright.sync_api import sync_playwright
|
| 4 |
+
|
| 5 |
+
# --------------------------------
|
| 6 |
+
# HTML → PNG function
|
| 7 |
+
# --------------------------------
|
| 8 |
+
def html_to_png(html_path):
|
| 9 |
+
output_png = tempfile.NamedTemporaryFile(
|
| 10 |
+
delete=False, suffix=".png"
|
| 11 |
+
).name
|
| 12 |
+
|
| 13 |
+
with sync_playwright() as p:
|
| 14 |
+
browser = p.chromium.launch(
|
| 15 |
+
args=["--no-sandbox", "--disable-dev-shm-usage"]
|
| 16 |
+
)
|
| 17 |
+
page = browser.new_page(
|
| 18 |
+
viewport={"width": 2000, "height": 1414}
|
| 19 |
+
)
|
| 20 |
+
page.goto(f"file://{html_path}", wait_until="networkidle")
|
| 21 |
+
page.screenshot(path=output_png)
|
| 22 |
+
browser.close()
|
| 23 |
+
|
| 24 |
+
return output_png # ✅ MUST RETURN FILE PATH
|
| 25 |
+
|
| 26 |
+
# --------------------------------
|
| 27 |
+
# Gradio Interface (API)
|
| 28 |
+
# --------------------------------
|
| 29 |
+
demo = gr.Interface(
|
| 30 |
+
fn=html_to_png,
|
| 31 |
+
inputs=gr.File(type="filepath", label="HTML Input"),
|
| 32 |
+
outputs=gr.File(type="filepath", label="PNG Output"),
|
| 33 |
+
title="HTML → PNG Certificate API",
|
| 34 |
+
description="Production-ready HTML to PNG API"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
demo.launch(ssr_mode=False)
|