shiveshnavin commited on
Commit
55107ff
·
1 Parent(s): d8ff96c
Files changed (1) hide show
  1. app.py +25 -7
app.py CHANGED
@@ -3,7 +3,7 @@ import tempfile
3
  import subprocess
4
  import shlex
5
  from pathlib import Path
6
- from typing import Optional
7
 
8
  import gradio as gr
9
  import requests
@@ -52,6 +52,24 @@ def _ensure_image(path: Path) -> Path:
52
  return path
53
 
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  def run_glitch(
56
  image_url: Optional[str],
57
  image_file: Optional[Path],
@@ -69,7 +87,6 @@ def run_glitch(
69
 
70
  with tempfile.TemporaryDirectory() as td:
71
  tdir = Path(td)
72
- # Source image handling
73
  src_path: Optional[Path] = None
74
  if image_file is not None:
75
  src_path = Path(image_file)
@@ -80,10 +97,8 @@ def run_glitch(
80
 
81
  _ensure_image(src_path)
82
 
83
- # Output path
84
  out_path = tdir / "glitched.mp4"
85
 
86
- # Build CLI
87
  cmd = [
88
  "python", str(GLITCH_SCRIPT),
89
  str(src_path),
@@ -105,7 +120,6 @@ def run_glitch(
105
  if blur is not None:
106
  cmd += ["--blur", str(blur)]
107
 
108
- # Run
109
  print("Running:", shlex.join(cmd))
110
  try:
111
  subprocess.run(cmd, check=True)
@@ -113,9 +127,13 @@ def run_glitch(
113
  raise gr.Error(f"glitch.py failed: {e}")
114
 
115
  if not out_path.exists():
116
- raise gr.Error("Output file not produced by glitch.py")
 
 
 
 
 
117
 
118
- # Return file path; Gradio will host and provide a URL.
119
  return str(out_path)
120
 
121
 
 
3
  import subprocess
4
  import shlex
5
  from pathlib import Path
6
+ from typing import Optional, List
7
 
8
  import gradio as gr
9
  import requests
 
52
  return path
53
 
54
 
55
+ def _pick_output(candidates: List[Path]) -> Optional[Path]:
56
+ """Prefer *_final.mp4 > *_vfx.mp4 > *_raw.mp4 > any .mp4 > any .gif."""
57
+ if not candidates:
58
+ return None
59
+ prefer = [
60
+ lambda p: p.suffix.lower() == ".mp4" and p.name.endswith("_final.mp4"),
61
+ lambda p: p.suffix.lower() == ".mp4" and p.name.endswith("_vfx.mp4"),
62
+ lambda p: p.suffix.lower() == ".mp4" and p.name.endswith("_raw.mp4"),
63
+ lambda p: p.suffix.lower() == ".mp4",
64
+ lambda p: p.suffix.lower() == ".gif",
65
+ ]
66
+ for rule in prefer:
67
+ for p in candidates:
68
+ if rule(p):
69
+ return p
70
+ return candidates[0]
71
+
72
+
73
  def run_glitch(
74
  image_url: Optional[str],
75
  image_file: Optional[Path],
 
87
 
88
  with tempfile.TemporaryDirectory() as td:
89
  tdir = Path(td)
 
90
  src_path: Optional[Path] = None
91
  if image_file is not None:
92
  src_path = Path(image_file)
 
97
 
98
  _ensure_image(src_path)
99
 
 
100
  out_path = tdir / "glitched.mp4"
101
 
 
102
  cmd = [
103
  "python", str(GLITCH_SCRIPT),
104
  str(src_path),
 
120
  if blur is not None:
121
  cmd += ["--blur", str(blur)]
122
 
 
123
  print("Running:", shlex.join(cmd))
124
  try:
125
  subprocess.run(cmd, check=True)
 
127
  raise gr.Error(f"glitch.py failed: {e}")
128
 
129
  if not out_path.exists():
130
+ cands = list(tdir.glob("**/*.mp4")) + list(tdir.glob("**/*.gif"))
131
+ picked = _pick_output(cands)
132
+ if not picked:
133
+ tree = "\n".join(str(p) for p in tdir.rglob("*"))
134
+ raise gr.Error("Output file not produced by glitch.py (no .mp4/.gif found). Files:\n" + tree)
135
+ out_path = picked
136
 
 
137
  return str(out_path)
138
 
139