Spaces:
broyang
/
Running on Zero

SkyeBrowse Developer Claude Opus 5.5 commited on
Commit
fec4e98
·
1 Parent(s): c479c65

Serve SEO head server-side, keep it out of embeds, skip theme redirect for crawlers

Browse files

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QPgCn7Hj1N72XftR46b9Za

Files changed (4) hide show
  1. AGENTS.md +1 -0
  2. app.py +14 -5
  3. gradio_runtime.py +47 -6
  4. tests/test_document_head.py +61 -0
AGENTS.md CHANGED
@@ -47,3 +47,4 @@ Type: code
47
  - API callers (`gradio_client`, webapp) never fire the hidden `.then`; no new API endpoint. Logs: `GPU_CALL "prebake":true`, `LABS_EVENT glb_done prebake=hit|joined`, `prebake_done`.
48
  - Dev, warm, observed: preview 5.1–6.3 s; GLB 5 s after the preview 0.8–1.0 s client (`extract_model` 0.07 s); immediate click joins (3.9–5.8 s); API GLB 4.5–5.5 s; one bake per generation in `reuse_report`.
49
  - Pitfall: the anonymous headless browser gets ZeroGPU 429 on dev, so the UI chain was replayed at the queue protocol level (`prebake_chain.py`: `queue/join` the private dep by `fn_index` in the same session).
 
 
47
  - API callers (`gradio_client`, webapp) never fire the hidden `.then`; no new API endpoint. Logs: `GPU_CALL "prebake":true`, `LABS_EVENT glb_done prebake=hit|joined`, `prebake_done`.
48
  - Dev, warm, observed: preview 5.1–6.3 s; GLB 5 s after the preview 0.8–1.0 s client (`extract_model` 0.07 s); immediate click joins (3.9–5.8 s); API GLB 4.5–5.5 s; one bake per generation in `reuse_report`.
49
  - Pitfall: the anonymous headless browser gets ZeroGPU 429 on dev, so the UI chain was replayed at the queue protocol level (`prebake_chain.py`: `queue/join` the private dep by `fn_index` in the same session).
50
+ - SEO head (2026-10-07): `build_launch_kwargs(..., title=PAGE_TITLE)` serves title/canonical/description/og/twitter tags SERVER-SIDE on `/` (strips Gradio's default "Gradio" og/twitter tags) and passes an EMPTY `head` to Gradio. Reason: Gradio copies config `head` into any page embedding the Space, so www.skyebrowse.com/news/posts/pocket-3d-ai rendered 3 canonicals (its own + 2× 3dai). Crawler UAs skip the `__theme=light` 307 so `/` matches the canonical. Tests: `tests.test_document_head`.
app.py CHANGED
@@ -1804,16 +1804,25 @@ schema_data = {
1804
  }
1805
  }
1806
 
 
 
 
 
1807
  head_html = f"""
1808
  <script type="application/ld+json">
1809
  {json.dumps(schema_data)}
1810
  </script>
1811
  <link rel="canonical" href="https://3dai.skyebrowse.com/" />
1812
- <meta name="description" content="Generate 3D models from a single image with SkyeBrowse Pocket 3D AI. Download GLB and STL files instantly.">
1813
- <meta property="og:title" content="Pocket 3D AI | Powered by SkyeBrowse">
 
1814
  <meta property="og:type" content="website">
1815
  <meta property="og:url" content="https://3dai.skyebrowse.com/">
1816
- <meta property="og:image" content="https://www.skyebrowse.com/logo.png">
 
 
 
 
1817
  """
1818
 
1819
 
@@ -1872,7 +1881,7 @@ custom_js = """
1872
  }
1873
  """
1874
 
1875
- with gr.Blocks(title="Pocket 3D AI | SkyeBrowse") as demo:
1876
  default_ss_steps = 12
1877
  default_slat_steps = 12
1878
 
@@ -2299,7 +2308,7 @@ if __name__ == "__main__":
2299
 
2300
  logger.info("Launching runtime: %s", TRELLIS_RUNTIME)
2301
  app_kwargs = gradio_runtime.build_launch_kwargs(
2302
- theme=gr.themes.Soft(), css=css, js=custom_js, head=head_html
2303
  )
2304
  if RUNNING_ON_SPACES:
2305
  logger.info("Launching on HuggingFace Spaces")
 
1804
  }
1805
  }
1806
 
1807
+ PAGE_TITLE = "Pocket 3D AI: Free Image to 3D Model Generator (GLB, STL) | SkyeBrowse"
1808
+ PAGE_DESCRIPTION = "Turn one photo or 2-6 views into a textured 3D model in your browser. Free, no install. Download GLB for Blender and game engines or STL for 3D printing."
1809
+ PAGE_IMAGE = "https://skyebrowse-prod-web-assets.s3.us-east-2.amazonaws.com/pocket_3d_ai_hero_adac81efac.jpg"
1810
+
1811
  head_html = f"""
1812
  <script type="application/ld+json">
1813
  {json.dumps(schema_data)}
1814
  </script>
1815
  <link rel="canonical" href="https://3dai.skyebrowse.com/" />
1816
+ <meta name="description" content="{PAGE_DESCRIPTION}">
1817
+ <meta property="og:title" content="{PAGE_TITLE}">
1818
+ <meta property="og:description" content="{PAGE_DESCRIPTION}">
1819
  <meta property="og:type" content="website">
1820
  <meta property="og:url" content="https://3dai.skyebrowse.com/">
1821
+ <meta property="og:image" content="{PAGE_IMAGE}">
1822
+ <meta name="twitter:card" content="summary_large_image">
1823
+ <meta name="twitter:title" content="{PAGE_TITLE}">
1824
+ <meta name="twitter:description" content="{PAGE_DESCRIPTION}">
1825
+ <meta name="twitter:image" content="{PAGE_IMAGE}">
1826
  """
1827
 
1828
 
 
1881
  }
1882
  """
1883
 
1884
+ with gr.Blocks(title=PAGE_TITLE) as demo:
1885
  default_ss_steps = 12
1886
  default_slat_steps = 12
1887
 
 
2308
 
2309
  logger.info("Launching runtime: %s", TRELLIS_RUNTIME)
2310
  app_kwargs = gradio_runtime.build_launch_kwargs(
2311
+ theme=gr.themes.Soft(), css=css, js=custom_js, head=head_html, title=PAGE_TITLE
2312
  )
2313
  if RUNNING_ON_SPACES:
2314
  logger.info("Launching on HuggingFace Spaces")
gradio_runtime.py CHANGED
@@ -1,13 +1,28 @@
 
1
  from urllib.parse import urlencode
2
 
3
  from starlette.middleware import Middleware
4
  from starlette.middleware.base import BaseHTTPMiddleware
5
- from starlette.responses import RedirectResponse
6
 
7
 
8
  THEME_QUERY_PARAM = "__theme"
9
  LIGHT_THEME = "light"
10
  FOOTER_LINKS_WITHOUT_API = ["gradio", "settings"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
 
13
  async def force_light_theme(request, call_next):
@@ -16,6 +31,7 @@ async def force_light_theme(request, call_next):
16
  request.url.path == "/"
17
  and request.query_params.get(THEME_QUERY_PARAM) != LIGHT_THEME
18
  and wants_html
 
19
  ):
20
  params = dict(request.query_params)
21
  params[THEME_QUERY_PARAM] = LIGHT_THEME
@@ -23,15 +39,40 @@ async def force_light_theme(request, call_next):
23
  return await call_next(request)
24
 
25
 
26
- def build_launch_kwargs(theme, css, js, head):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  return {
28
  "theme": theme,
29
  "css": css,
30
  "js": js,
31
- "head": head,
32
  "footer_links": list(FOOTER_LINKS_WITHOUT_API),
33
  "ssr_mode": False,
34
- "app_kwargs": {
35
- "middleware": [Middleware(BaseHTTPMiddleware, dispatch=force_light_theme)]
36
- },
37
  }
 
1
+ import re
2
  from urllib.parse import urlencode
3
 
4
  from starlette.middleware import Middleware
5
  from starlette.middleware.base import BaseHTTPMiddleware
6
+ from starlette.responses import RedirectResponse, Response
7
 
8
 
9
  THEME_QUERY_PARAM = "__theme"
10
  LIGHT_THEME = "light"
11
  FOOTER_LINKS_WITHOUT_API = ["gradio", "settings"]
12
+ CRAWLER_USER_AGENT = re.compile(
13
+ r"bot|crawl|spider|slurp|preview|facebookexternalhit|embedly|quora link|whatsapp|vkshare",
14
+ re.IGNORECASE,
15
+ )
16
+ GRADIO_SOCIAL_META = re.compile(
17
+ r'<meta\s+(?:property="og:[^"]*"|name="twitter:[^"]*")\s+content="[^"]*"\s*/?>\s*',
18
+ re.IGNORECASE,
19
+ )
20
+ DOCUMENT_TITLE = re.compile(r"<title>.*?</title>\s*", re.IGNORECASE | re.DOTALL)
21
+ HEAD_OPEN = re.compile(r"<head[^>]*>", re.IGNORECASE)
22
+
23
+
24
+ def is_crawler(request):
25
+ return bool(CRAWLER_USER_AGENT.search(request.headers.get("user-agent", "")))
26
 
27
 
28
  async def force_light_theme(request, call_next):
 
31
  request.url.path == "/"
32
  and request.query_params.get(THEME_QUERY_PARAM) != LIGHT_THEME
33
  and wants_html
34
+ and not is_crawler(request)
35
  ):
36
  params = dict(request.query_params)
37
  params[THEME_QUERY_PARAM] = LIGHT_THEME
 
39
  return await call_next(request)
40
 
41
 
42
+ def with_document_head(html, title, head):
43
+ stripped = DOCUMENT_TITLE.sub("", GRADIO_SOCIAL_META.sub("", html))
44
+ opening = HEAD_OPEN.search(stripped)
45
+ if not opening:
46
+ return html
47
+ tags = f"<title>{title}</title>\n{head}\n"
48
+ return stripped[: opening.end()] + tags + stripped[opening.end():]
49
+
50
+
51
+ def document_head_middleware(title, head):
52
+ async def serve_document_head(request, call_next):
53
+ response = await call_next(request)
54
+ if request.url.path != "/" or "text/html" not in response.headers.get("content-type", ""):
55
+ return response
56
+ body = b"".join([chunk async for chunk in response.body_iterator])
57
+ html = with_document_head(body.decode("utf-8"), title, head)
58
+ headers = {k: v for k, v in response.headers.items() if k.lower() != "content-length"}
59
+ return Response(html, status_code=response.status_code, headers=headers)
60
+
61
+ return serve_document_head
62
+
63
+
64
+ def build_launch_kwargs(theme, css, js, head, title=""):
65
+ middleware = [Middleware(BaseHTTPMiddleware, dispatch=force_light_theme)]
66
+ if title:
67
+ middleware.append(
68
+ Middleware(BaseHTTPMiddleware, dispatch=document_head_middleware(title, head))
69
+ )
70
  return {
71
  "theme": theme,
72
  "css": css,
73
  "js": js,
74
+ "head": "" if title else head,
75
  "footer_links": list(FOOTER_LINKS_WITHOUT_API),
76
  "ssr_mode": False,
77
+ "app_kwargs": {"middleware": middleware},
 
 
78
  }
tests/test_document_head.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ import unittest
3
+ from pathlib import Path
4
+
5
+ sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
6
+
7
+ from starlette.applications import Starlette # noqa: E402
8
+ from starlette.responses import HTMLResponse, JSONResponse # noqa: E402
9
+ from starlette.routing import Route # noqa: E402
10
+ from starlette.testclient import TestClient # noqa: E402
11
+
12
+ import gradio_runtime # noqa: E402
13
+
14
+ GRADIO_INDEX = """<!doctype html><html><head>
15
+ <meta property="og:title" content="Gradio" />
16
+ <meta name="twitter:creator" content="@Gradio" />
17
+ <title>Gradio</title>
18
+ </head><body>app</body></html>"""
19
+ TITLE = "Pocket 3D AI | SkyeBrowse"
20
+ HEAD = '<link rel="canonical" href="https://3dai.skyebrowse.com/" />'
21
+ GOOGLEBOT = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
22
+ CHROME = "Mozilla/5.0 (Macintosh) AppleWebKit/537.36 Chrome/141.0 Safari/537.36"
23
+
24
+
25
+ def served_app(title=TITLE):
26
+ kwargs = gradio_runtime.build_launch_kwargs(None, "", "", HEAD, title=title)
27
+ routes = [
28
+ Route("/", lambda request: HTMLResponse(GRADIO_INDEX)),
29
+ Route("/config", lambda request: JSONResponse({"head": kwargs["head"]})),
30
+ ]
31
+ return TestClient(Starlette(routes=routes, **kwargs["app_kwargs"]), follow_redirects=False)
32
+
33
+
34
+ class DocumentHeadTest(unittest.TestCase):
35
+ def test_page_html_carries_title_and_head_without_gradio_defaults(self):
36
+ html = served_app().get("/?__theme=light", headers={"Accept": "text/html"}).text
37
+ self.assertIn(f"<title>{TITLE}</title>", html)
38
+ self.assertIn(HEAD, html)
39
+ self.assertNotIn('content="Gradio"', html)
40
+ self.assertNotIn("@Gradio", html)
41
+ self.assertEqual(html.count("<title>"), 1)
42
+
43
+ def test_config_head_is_empty_so_embeds_do_not_inherit_it(self):
44
+ self.assertEqual(served_app().get("/config").json()["head"], "")
45
+
46
+ def test_without_title_head_stays_in_config(self):
47
+ self.assertEqual(served_app(title="").get("/config").json()["head"], HEAD)
48
+
49
+ def test_browsers_are_redirected_to_light_theme(self):
50
+ response = served_app().get("/", headers={"Accept": "text/html", "User-Agent": CHROME})
51
+ self.assertEqual(response.status_code, 307)
52
+ self.assertIn("__theme=light", response.headers["location"])
53
+
54
+ def test_crawlers_get_the_page_without_a_redirect(self):
55
+ response = served_app().get("/", headers={"Accept": "text/html", "User-Agent": GOOGLEBOT})
56
+ self.assertEqual(response.status_code, 200)
57
+ self.assertIn(HEAD, response.text)
58
+
59
+
60
+ if __name__ == "__main__":
61
+ unittest.main()