vankhieu Codex commited on
Commit
2c15409
·
1 Parent(s): 4c96788

Add Space system deps and ZeroGPU loading

Browse files

Co-authored-by: Codex <codex@openai.com>

Files changed (4) hide show
  1. README.md +12 -1
  2. app.py +28 -15
  3. packages.txt +13 -0
  4. requirements.txt +4 -0
README.md CHANGED
@@ -5,7 +5,7 @@ colorFrom: yellow
5
  colorTo: yellow
6
  sdk: gradio
7
  sdk_version: 6.17.3
8
- python_version: '3.12'
9
  app_file: app.py
10
  pinned: false
11
  license: apache-2.0
@@ -13,3 +13,14 @@ short_description: 'AI-powered Math and Physics visualisation '
13
  ---
14
 
15
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
5
  colorTo: yellow
6
  sdk: gradio
7
  sdk_version: 6.17.3
8
+ python_version: '3.10'
9
  app_file: app.py
10
  pinned: false
11
  license: apache-2.0
 
13
  ---
14
 
15
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
16
+
17
+ ## System dependencies
18
+
19
+ This Space uses `packages.txt` to install system packages required by ManimCE,
20
+ including LaTeX, `dvisvgm`, Ghostscript, ffmpeg, Cairo, and Pango.
21
+
22
+ ## Runtime dependencies
23
+
24
+ Python dependencies are installed from `requirements.txt`. The app is designed
25
+ for ZeroGPU: model loading is deferred until the render callback and the callback
26
+ is decorated with `@spaces.GPU`.
app.py CHANGED
@@ -11,7 +11,21 @@ import gradio as gr
11
  from generator import build_default_agent
12
 
13
 
14
- agent = build_default_agent()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  custom_css = """
17
  @import url('https://fonts.googleapis.com/css2?family=Share+Tech+Mono&display=swap');
@@ -364,30 +378,29 @@ button:hover,
364
 
365
 
366
  def startup_status_html() -> str:
367
- if agent.ready:
368
- return '<div class="status-pill ready">MODEL CORE: ONLINE<br>LOCAL RENDER LOOP: ARMED</div>'
369
- escaped = html.escape(agent.model_error or "Model status unknown.")
370
- return f'<div class="status-pill">MODEL CORE: STANDBY<br><span>{escaped}</span></div>'
371
 
372
 
373
  def initial_terminal_log() -> str:
374
- if agent.ready:
375
- return (
376
- "[boot] SciVisual-Agent online\n"
377
- f"[model] Loaded adapter from {agent.model_path}\n"
378
- "[render] Waiting for prompt"
379
- )
380
  return (
381
  "[boot] SciVisual-Agent interface online\n"
382
- "[model] Model adapter runtime is not ready\n"
383
- f"[detail] {agent.model_error}\n"
384
- "[note] The app will show a diagnostic Manim scene until the model path is fixed."
385
  )
386
 
387
 
 
 
 
 
 
 
 
 
388
  def run_visual_lab(prompt: str, domain: str, retries: int) -> Tuple[str | None, str, str]:
389
  try:
390
- success, video_path, code, log = agent.generate_and_fix(
 
391
  user_prompt=prompt,
392
  domain=domain,
393
  max_retries=int(retries),
 
11
  from generator import build_default_agent
12
 
13
 
14
+ try:
15
+ import spaces
16
+ except ImportError: # Local development fallback when not running on HF ZeroGPU.
17
+ class _SpacesFallback:
18
+ @staticmethod
19
+ def GPU(*args, **kwargs):
20
+ def decorator(func):
21
+ return func
22
+
23
+ return decorator
24
+
25
+ spaces = _SpacesFallback()
26
+
27
+
28
+ agent = None
29
 
30
  custom_css = """
31
  @import url('https://fonts.googleapis.com/css2?family=Share+Tech+Mono&display=swap');
 
378
 
379
 
380
  def startup_status_html() -> str:
381
+ return '<div class="status-pill ready">MODEL CORE: READY<br>ZEROGPU LOADS ON DEMAND</div>'
 
 
 
382
 
383
 
384
  def initial_terminal_log() -> str:
 
 
 
 
 
 
385
  return (
386
  "[boot] SciVisual-Agent interface online\n"
387
+ "[model] Adapter loads on demand inside the ZeroGPU render callback\n"
388
+ "[render] Waiting for prompt"
 
389
  )
390
 
391
 
392
+ def get_agent():
393
+ global agent
394
+ if agent is None:
395
+ agent = build_default_agent()
396
+ return agent
397
+
398
+
399
+ @spaces.GPU(duration=300)
400
  def run_visual_lab(prompt: str, domain: str, retries: int) -> Tuple[str | None, str, str]:
401
  try:
402
+ visual_agent = get_agent()
403
+ success, video_path, code, log = visual_agent.generate_and_fix(
404
  user_prompt=prompt,
405
  domain=domain,
406
  max_retries=int(retries),
packages.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ffmpeg
2
+ ghostscript
3
+ dvisvgm
4
+ texlive
5
+ texlive-latex-base
6
+ texlive-latex-recommended
7
+ texlive-latex-extra
8
+ texlive-fonts-recommended
9
+ texlive-science
10
+ cm-super
11
+ libcairo2-dev
12
+ libpango1.0-dev
13
+ pkg-config
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  gradio>=5.0.0
 
2
  torch
3
  transformers
4
  accelerate
@@ -6,6 +7,9 @@ peft
6
  bitsandbytes
7
  unsloth
8
  huggingface_hub
 
 
 
9
  numpy
10
  scipy
11
  manim>=0.17.3
 
1
  gradio>=5.0.0
2
+ spaces
3
  torch
4
  transformers
5
  accelerate
 
7
  bitsandbytes
8
  unsloth
9
  huggingface_hub
10
+ safetensors
11
+ sentencepiece
12
+ protobuf
13
  numpy
14
  scipy
15
  manim>=0.17.3