thecollabagepatch commited on
Commit
6cd5ac5
·
1 Parent(s): b3e81ab

fastrtc issues solved mostly in gradio app. html web tester still better ux for the moment

Browse files
Files changed (1) hide show
  1. app.py +12 -59
app.py CHANGED
@@ -1752,12 +1752,9 @@ async def ws_jam(websocket: WebSocket):
1752
  pass
1753
 
1754
  # --- FastRTC Gradio Integration (only in serve mode) ---
1755
- GRADIO_AT_ROOT = False
1756
-
1757
  if SPACE_MODE == "serve":
1758
  try:
1759
  from fastrtc_magenta import create_magenta_stream, FASTRTC_AVAILABLE
1760
-
1761
  if FASTRTC_AVAILABLE:
1762
  magenta_stream = create_magenta_stream(
1763
  get_mrt_fn=get_mrt,
@@ -1766,92 +1763,48 @@ if SPACE_MODE == "serve":
1766
  concurrency_limit=1,
1767
  time_limit=3600,
1768
  )
1769
-
1770
- # FastRTC (WebRTC) endpoint
1771
  magenta_stream.mount(app, path="/rtc")
1772
-
1773
- # IMPORTANT: mount Gradio at root to avoid path/proxy weirdness on HF Spaces
1774
- app = gr.mount_gradio_app(app, magenta_stream.ui, path="/")
1775
- GRADIO_AT_ROOT = True
1776
- print("✓ FastRTC Gradio interface mounted at /", flush=True)
1777
-
1778
  except Exception as e:
1779
- print(f"⚠ FastRTC integration skipped: {e}", flush=True)
1780
 
1781
 
1782
  @app.get("/ping")
1783
  def ping():
1784
  return {"ok": True}
1785
 
1786
-
1787
- # Serve docs at /docs always (even when Gradio is at /)
1788
- @app.get("/docs", response_class=Response)
1789
- def docs():
1790
- """Documentation page (moved off / so Gradio can own root)."""
1791
  try:
1792
  html_file = Path(__file__).parent / "documentation.html"
1793
- html_content = html_file.read_text(encoding="utf-8")
1794
  except FileNotFoundError:
 
1795
  html_content = """
1796
  <!DOCTYPE html>
1797
  <html><body>
1798
  <h1>MagentaRT Research API</h1>
1799
- <p>documentation.html not found.</p>
1800
  </body></html>
1801
  """
1802
  return Response(content=html_content, media_type="text/html")
1803
 
1804
-
1805
- # Only provide our own / route if Gradio is NOT mounted at /
1806
- if not GRADIO_AT_ROOT:
1807
- @app.get("/", response_class=Response)
1808
- def read_root():
1809
- """Root endpoint that explains what this API does"""
1810
- try:
1811
- html_file = Path(__file__).parent / "documentation.html"
1812
- html_content = html_file.read_text(encoding="utf-8")
1813
- except FileNotFoundError:
1814
- html_content = """
1815
- <!DOCTYPE html>
1816
- <html><body>
1817
- <h1>MagentaRT Research API</h1>
1818
- <p>Documentation file not found. Please check documentation.html</p>
1819
- </body></html>
1820
- """
1821
- return Response(content=html_content, media_type="text/html")
1822
-
1823
-
1824
- # Optional: silence browser manifest.json 404 noise (harmless, but annoying)
1825
- @app.get("/manifest.json")
1826
- def manifest():
1827
- return {
1828
- "name": "magenta-retry",
1829
- "short_name": "magenta-retry",
1830
- "start_url": "/",
1831
- "display": "standalone",
1832
- "icons": [],
1833
- }
1834
-
1835
-
1836
  @app.get("/lil_demo_540p.mp4")
1837
  def demo_video():
1838
  return FileResponse(Path(__file__).parent / "lil_demo_540p.mp4", media_type="video/mp4")
1839
 
1840
-
1841
  @app.get("/tester", response_class=HTMLResponse)
1842
  def tester():
1843
  html_path = Path(__file__).parent / "magentaRT_rt_tester.html"
1844
  return HTMLResponse(
1845
  html_path.read_text(encoding="utf-8"),
1846
- headers={"Cache-Control": "no-store"},
1847
  )
1848
 
1849
-
1850
  @app.get("/magenta_prompts.js")
1851
  def magenta_prompts_js():
1852
  js_path = Path(__file__).parent / "magenta_prompts.js"
1853
- return FileResponse(
1854
- js_path,
1855
- media_type="text/javascript",
1856
- headers={"Cache-Control": "no-store"},
1857
- )
 
1752
  pass
1753
 
1754
  # --- FastRTC Gradio Integration (only in serve mode) ---
 
 
1755
  if SPACE_MODE == "serve":
1756
  try:
1757
  from fastrtc_magenta import create_magenta_stream, FASTRTC_AVAILABLE
 
1758
  if FASTRTC_AVAILABLE:
1759
  magenta_stream = create_magenta_stream(
1760
  get_mrt_fn=get_mrt,
 
1763
  concurrency_limit=1,
1764
  time_limit=3600,
1765
  )
 
 
1766
  magenta_stream.mount(app, path="/rtc")
1767
+ app = gr.mount_gradio_app(app, magenta_stream.ui, path="/gradio")
1768
+ print("✓ FastRTC Gradio interface available at /gradio")
 
 
 
 
1769
  except Exception as e:
1770
+ print(f"⚠ FastRTC integration skipped: {e}")
1771
 
1772
 
1773
  @app.get("/ping")
1774
  def ping():
1775
  return {"ok": True}
1776
 
1777
+ @app.get("/", response_class=Response)
1778
+ def read_root():
1779
+ """Root endpoint that explains what this API does"""
 
 
1780
  try:
1781
  html_file = Path(__file__).parent / "documentation.html"
1782
+ html_content = html_file.read_text(encoding='utf-8')
1783
  except FileNotFoundError:
1784
+ # Fallback if file is missing
1785
  html_content = """
1786
  <!DOCTYPE html>
1787
  <html><body>
1788
  <h1>MagentaRT Research API</h1>
1789
+ <p>Documentation file not found. Please check documentation.html</p>
1790
  </body></html>
1791
  """
1792
  return Response(content=html_content, media_type="text/html")
1793
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1794
  @app.get("/lil_demo_540p.mp4")
1795
  def demo_video():
1796
  return FileResponse(Path(__file__).parent / "lil_demo_540p.mp4", media_type="video/mp4")
1797
 
 
1798
  @app.get("/tester", response_class=HTMLResponse)
1799
  def tester():
1800
  html_path = Path(__file__).parent / "magentaRT_rt_tester.html"
1801
  return HTMLResponse(
1802
  html_path.read_text(encoding="utf-8"),
1803
+ headers={"Cache-Control": "no-store"} # avoid sticky caches while iterating
1804
  )
1805
 
 
1806
  @app.get("/magenta_prompts.js")
1807
  def magenta_prompts_js():
1808
  js_path = Path(__file__).parent / "magenta_prompts.js"
1809
+ return FileResponse(js_path, media_type="text/javascript",
1810
+ headers={"Cache-Control": "no-store"})