Christen Millerdurai commited on
Commit
78924b5
·
1 Parent(s): 370a2d5

reduced gpu time

Browse files
Files changed (1) hide show
  1. app.py +19 -14
app.py CHANGED
@@ -2,6 +2,7 @@ from __future__ import annotations
2
 
3
  import importlib.util
4
  import os
 
5
  import runpy
6
  import shlex
7
  import shutil
@@ -428,20 +429,24 @@ def patch_upstream_gradio_for_zerogpu(demo_entrypoint: Path) -> None:
428
  raise RuntimeError(f"Could not insert ZeroGPU import in {demo_entrypoint}")
429
  source = source.replace("import torch\n", "import spaces\nimport torch\n", 1)
430
 
431
- if "@spaces.GPU(" not in source:
432
- if "def process_video(\n" not in source:
433
- raise RuntimeError(f"Could not locate process_video in {demo_entrypoint}")
434
- source = source.replace(
435
- "def process_video(\n",
436
- (
437
- "@spaces.GPU("
438
- f"duration={int(ZEROGPU_DURATION_SECONDS)}, "
439
- f"size={ZEROGPU_SIZE!r}"
440
- ")\n"
441
- "def process_video(\n"
442
- ),
443
- 1,
444
- )
 
 
 
 
445
 
446
  injected_css_loader = (
447
  "\n"
 
2
 
3
  import importlib.util
4
  import os
5
+ import re
6
  import runpy
7
  import shlex
8
  import shutil
 
429
  raise RuntimeError(f"Could not insert ZeroGPU import in {demo_entrypoint}")
430
  source = source.replace("import torch\n", "import spaces\nimport torch\n", 1)
431
 
432
+ if "def process_video(\n" not in source:
433
+ raise RuntimeError(f"Could not locate process_video in {demo_entrypoint}")
434
+
435
+ source = re.sub(
436
+ r"@spaces\.GPU\([^\n]*\)\n(?=def process_video\()",
437
+ "",
438
+ source,
439
+ count=1,
440
+ )
441
+
442
+ desired_decorator = (
443
+ "@spaces.GPU("
444
+ f"duration={int(ZEROGPU_DURATION_SECONDS)}, "
445
+ f"size={ZEROGPU_SIZE!r}"
446
+ ")\n"
447
+ )
448
+ if desired_decorator not in source:
449
+ source = source.replace("def process_video(\n", f"{desired_decorator}def process_video(\n", 1)
450
 
451
  injected_css_loader = (
452
  "\n"