from __future__ import annotations
import base64
from pathlib import Path
import html
import gradio as gr
ROOT = Path(__file__).resolve().parent
REPO = "https://huggingface.co/spaces/zcahjl3/figmirror-code-aug10"
CSS = """
"""
def image_data(path: Path) -> str:
data = base64.b64encode(path.read_bytes()).decode("ascii")
return f"data:image/png;base64,{data}"
def rel_url(slug: str, filename: str) -> str:
return f"{REPO}/resolve/main/{slug}/{filename}"
def build_html() -> str:
cases = sorted(p for p in ROOT.iterdir() if p.is_dir() and (p / "comparison.png").exists())
cards = []
for case in cases:
slug = case.name
parts = slug.split("_", 2)
chart_type = html.escape(parts[1] if len(parts) > 1 else slug)
label = html.escape(parts[2] if len(parts) > 2 else slug)
img = image_data(case / "comparison.png")
cards.append(f"""
{chart_type} · {label}
Both sides are generated from code. Left: unchanged original.py. Right: augmented.py, preserving the same data and chart topology while improving figure aesthetics.
original render
original code
augmented render
augmented code
comparison PNG
""")
return CSS + f"""
{''.join(cards)}
"""
with gr.Blocks(title="FigMirror Code Augmentation") as demo:
gr.HTML(build_html())
if __name__ == "__main__":
demo.launch()