"""Custom The Fin AI page for Gradio-backed demo Spaces. Gradio only serves the page and the `/chat` API (gr.api); the whole UI is our own HTML/CSS/JS (page/), which talks to the API with @gradio/client. Works with Gradio 5 (head/css on Blocks) and Gradio 6 (head/css on launch). page = fa_page.Page(cfg) # cfg: see build_body() with gr.Blocks(**page.blocks_kwargs()) as demo: page.render() gr.api(chat, api_name="chat") demo.queue().launch(**page.launch_kwargs()) """ import html import json import os import gradio as gr HERE = os.path.dirname(os.path.abspath(__file__)) PAGE = os.path.join(HERE, "page") ASSETS = os.path.join(HERE, "assets") gr.set_static_paths(paths=[ASSETS]) GRADIO6 = int(gr.__version__.split(".")[0]) >= 6 CLIENT = "2.7.1" if GRADIO6 else "1.19.1" # @gradio/client matching the Gradio major version e = lambda s: html.escape(str(s), quote=True) def asset(path): return f"/gradio_api/file={os.path.join(ASSETS, path)}" def _read(name): with open(os.path.join(PAGE, name), encoding="utf-8") as f: return f.read() ICON_SEND = '' ICON_STOP = '' ICON_IMG = '' class Page: def __init__(self, cfg): self.cfg = cfg def head(self): c = self.cfg chat = {"client": CLIENT, "endpoint": "/chat", "image": c.get("image", False), "avatar": asset(c["avatar"]), "greeting": c["greeting"], "params": c["params"], "readyText": c.get("ready_text", "Ready"), "examples": [{**x, "image": asset(x["image"]) if x.get("image") else None} for x in c["examples"]]} fonts = ('' '') return (fonts + f'' f'') def body(self): c = self.cfg nav = "".join(f'{e(l)}' for l, u in c["nav"]) stats = "".join(f'
0{e(k)}
' for v, k, d in c["stats"]) pills = "".join(f'{e(l)}' for l, u in c["links"]) orgs = "".join(f'' f'{e(n)}' for i, (n, f, u, k) in enumerate(c["orgs"])) attach = (f'' '') if c.get("image") else "" return f'''
{e(c["kicker"])}

{c["title_html"]}

{c["lead_html"]}

{stats}

Try an example

Generation settings
Connecting…
{attach}

{c["hint_html"]}

People & partners

Built together

{c["orgs_sub_html"]}

{orgs}

{c["disclaimer"]}

''' def render(self): return gr.HTML(self.body()) def blocks_kwargs(self): kw = dict(title=self.cfg["page_title"], fill_width=True) if not GRADIO6: kw["head"] = self.head() return kw def launch_kwargs(self): kw = {} if not GRADIO6 else dict(footer_links=[]) if GRADIO6: kw["head"] = self.head() kw["ssr_mode"] = False return kw