sylvain471 commited on
Commit
b671043
·
verified ·
1 Parent(s): ef64188

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +37 -1
  2. app.py +118 -0
  3. run_script_venv.py +198 -0
README.md CHANGED
@@ -11,4 +11,40 @@ license: mit
11
  short_description: creates a venv, install dependencies and run your script
12
  ---
13
 
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  short_description: creates a venv, install dependencies and run your script
12
  ---
13
 
14
+ ## run_my_script
15
+
16
+ test script:
17
+ ```python
18
+ import os
19
+ import base64
20
+ from io import BytesIO
21
+ from PIL import Image
22
+ import json
23
+
24
+ input_path = os.environ["SCRIPT_INPUT"]
25
+ output_path = os.environ["SCRIPT_OUTPUT"]
26
+
27
+ # Load JSON input
28
+ with open(input_path, "r") as f:
29
+ data = json.load(f)
30
+ img_b64 = data["img"]
31
+
32
+ # Decode base64 to image
33
+ img_bytes = base64.b64decode(img_b64)
34
+ img = Image.open(BytesIO(img_bytes))
35
+ img.load() # ⬅️ Ensure image is fully loaded before processing
36
+
37
+ # Flip image horizontally
38
+ flipped = img.transpose(Image.FLIP_LEFT_RIGHT)
39
+
40
+ # Save output
41
+ flipped.save(os.path.join(output_path, "flipped.png"))
42
+ print("Image flipped and saved as flipped.png.")
43
+ ```
44
+
45
+ input:
46
+ ```json
47
+ {
48
+ "img": "iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAIAAADTED8xAAADMElEQVR4nOzVwQnAIBQFQYXff81RUkQCOyDj1YOPnbXWPmeTRef+/3O/OyBjzh3CD95BfqICMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tempfile
3
+ from pathlib import Path
4
+ import subprocess
5
+ import sys
6
+ import os
7
+ import shutil
8
+ import uuid
9
+ import json
10
+ import io
11
+ import zipfile
12
+ import time
13
+
14
+ from run_script_venv import main as run_in_venv
15
+
16
+
17
+ def cleanup_old_runs(base_dir: Path, max_age: int):
18
+ now = time.time()
19
+ for d in base_dir.glob("run_*/"):
20
+ if d.is_dir() and now - d.stat().st_mtime > max_age:
21
+ shutil.rmtree(d, ignore_errors=True)
22
+
23
+ def zip_artifacts(output_dir: Path, zip_path: Path):
24
+ with zipfile.ZipFile(zip_path, "w") as zf:
25
+ for file in output_dir.iterdir():
26
+ zf.write(file, arcname=file.name)
27
+
28
+
29
+ def run_script(code: str, user_input: str = "", cleanup_enabled: bool = True, cleanup_after: int = 600):
30
+ base_dir = Path("./script_runs")
31
+ base_dir.mkdir(exist_ok=True)
32
+ if cleanup_enabled:
33
+ cleanup_old_runs(base_dir, cleanup_after)
34
+
35
+ run_id = uuid.uuid4().hex[:8]
36
+ run_dir = base_dir / f"run_{run_id}"
37
+ run_dir.mkdir()
38
+ script_path = run_dir / "script.py"
39
+ input_path = run_dir / "input.txt"
40
+ output_path = run_dir / "output"
41
+ output_path.mkdir()
42
+
43
+ # Save the user's code
44
+ script_path.write_text(code)
45
+
46
+ # Always create input file
47
+ input_path.write_text(user_input or "")
48
+
49
+
50
+ # Redirect stdout/stderr
51
+ with tempfile.TemporaryFile(mode="w+") as output:
52
+ orig_stdout = sys.stdout
53
+ orig_stderr = sys.stderr
54
+ sys.stdout = sys.stderr = output
55
+
56
+ try:
57
+ sys.argv = [
58
+ "run_script_venv.py",
59
+ str(script_path),
60
+ "--keep-workdir",
61
+ "--extra", "pandas", # safe default dependency for common data handling
62
+ ]
63
+ os.environ["SCRIPT_INPUT"] = str(input_path)
64
+ os.environ["SCRIPT_OUTPUT"] = str(output_path)
65
+ run_in_venv()
66
+ except SystemExit:
67
+ pass
68
+ finally:
69
+ sys.stdout = orig_stdout
70
+ sys.stderr = orig_stderr
71
+
72
+ output.seek(0)
73
+ logs = output.read()
74
+
75
+
76
+ # Collect output artifacts
77
+ artifacts = {}
78
+ for item in output_path.iterdir():
79
+ if item.suffix in {".txt", ".csv", ".json", ".md"}:
80
+ artifacts[item.name] = item.read_text()
81
+ elif item.suffix.lower() in {".png", ".jpg", ".jpeg"}:
82
+ artifacts[item.name] = f"[image file: {item.name}]"
83
+ else:
84
+ artifacts[item.name] = f"[file saved: {item.name}]"
85
+
86
+ # Create zip
87
+ zip_path = run_dir / "output.zip"
88
+ zip_artifacts(output_path, zip_path)
89
+ artifacts["Download All (ZIP)"] = str(zip_path)
90
+
91
+ artifacts["__workdir__"] = str(run_dir)
92
+ return logs, artifacts, str(zip_path)
93
+
94
+
95
+ def launch_ui():
96
+ with gr.Blocks() as app:
97
+ gr.Markdown("# 🚀 Run My Script")
98
+ run_btn = gr.Button("Run My Script")
99
+ with gr.Row():
100
+ cleanup_toggle = gr.Checkbox(label="Enable Auto Cleanup", value=True)
101
+ cleanup_seconds = gr.Number(label="Cleanup After (seconds)", value=600, precision=0)
102
+ with gr.Row():
103
+ editor = gr.Code(label="Your Python Script", language="python")
104
+ user_input = gr.Textbox(label="Optional Input", lines=4, placeholder="Text, JSON, Markdown...")
105
+ with gr.Row():
106
+ output_log = gr.Textbox(label="Terminal Output", lines=3)
107
+ output_files = gr.JSON(label="Output Artifacts")
108
+ zip_file = gr.File(label="Download All Artifacts")
109
+ run_btn.click(
110
+ fn=run_script,
111
+ inputs=[editor, user_input, cleanup_toggle, cleanup_seconds],
112
+ outputs=[output_log, output_files, zip_file]
113
+ )
114
+
115
+ app.launch()
116
+
117
+ if __name__ == "__main__":
118
+ launch_ui()
run_script_venv.py ADDED
@@ -0,0 +1,198 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ run_script_venv.py
4
+ ==================
5
+ A utility that
6
+ 1. Accepts a path to a Python script.
7
+ 2. Creates an isolated working directory.
8
+ 3. Creates a virtual environment inside that directory using **uv**.
9
+ 4. Statically analyses the target script to discover third‑party dependencies.
10
+ 5. Installs the detected dependencies into the environment.
11
+ 6. Executes the target script inside the environment.
12
+
13
+ Usage
14
+ -----
15
+ $ python run_script_venv.py path/to/script.py [--workdir ./my_dir] [--keep-workdir]
16
+
17
+ Requirements
18
+ ------------
19
+ * Python ≥ 3.10 (for ``sys.stdlib_module_names``)
20
+ * ``uv`` must be available on the host Python used to run this helper.
21
+
22
+ Notes
23
+ -----
24
+ * The dependency detection is based on import statements only. Runtime/optional
25
+ dependencies that are conditionally imported or loaded by plugins are not
26
+ detected; you can pass extra packages manually via ``--extra``.
27
+ * A small mapping translates common import‑name↔package‑name mismatches
28
+ (e.g. ``cv2`` → ``opencv‑python``).
29
+ """
30
+
31
+ from __future__ import annotations
32
+
33
+ import argparse
34
+ import ast
35
+ import os
36
+ import shutil
37
+ import subprocess
38
+ import sys
39
+ import tempfile
40
+ from pathlib import Path
41
+ from typing import Iterable, Set
42
+
43
+ # --------------------------- Helpers -----------------------------------------
44
+
45
+ # Map import names to PyPI package names where they differ.
46
+ NAME_MAP: dict[str, str] = {
47
+ "cv2": "opencv-python",
48
+ "sklearn": "scikit-learn",
49
+ "PIL": "pillow",
50
+ "yaml": "pyyaml",
51
+ "Crypto": "pycryptodome",
52
+ }
53
+
54
+
55
+ def find_imports(script_path: Path) -> Set[str]:
56
+ """Return a set of *import names* found in *script_path*."""
57
+ root = ast.parse(script_path.read_text())
58
+ imports: set[str] = set()
59
+ for node in ast.walk(root):
60
+ if isinstance(node, ast.Import):
61
+ for alias in node.names:
62
+ imports.add(alias.name.split(".")[0])
63
+ elif isinstance(node, ast.ImportFrom):
64
+ if node.level == 0 and node.module: # skip relative imports
65
+ imports.add(node.module.split(".")[0])
66
+ return imports
67
+
68
+
69
+ def third_party_modules(modules: Iterable[str]) -> Set[str]:
70
+ """Filter *modules* to those not in the Python stdlib."""
71
+ stdlib = set(sys.stdlib_module_names) # Python ≥ 3.10
72
+ return {m for m in modules if m not in stdlib}
73
+
74
+
75
+ def translate_names(modules: Iterable[str]) -> Set[str]:
76
+ """Translate *modules* to their PyPI names using NAME_MAP."""
77
+ pkgs: set[str] = set()
78
+ for m in modules:
79
+ pkgs.add(NAME_MAP.get(m, m))
80
+ return pkgs
81
+
82
+
83
+ def create_venv(venv_dir: Path) -> Path:
84
+ """Create a venv at *venv_dir* using ``uv venv`` and return the Python exe."""
85
+ subprocess.check_call(["uv", "venv", str(venv_dir)])
86
+ python_exe = venv_dir / ("Scripts" if os.name == "nt" else "bin") / "python"
87
+ return python_exe
88
+
89
+
90
+ def install_packages(python_exe: Path, packages: Iterable[str]) -> None:
91
+ if not packages:
92
+ print("[+] No third‑party packages detected; skipping installation.")
93
+ return
94
+ print(f"[+] Installing: {' '.join(packages)}")
95
+ # subprocess.check_call([str(python_exe), "-m", "pip", "install", *packages])
96
+ subprocess.check_call(["uv", "pip", "install", "-p", str(python_exe), *packages])
97
+
98
+
99
+ # --------------------------- Main routine ------------------------------------
100
+
101
+ def main() -> None:
102
+ p = argparse.ArgumentParser(prog="run_script_venv")
103
+ p.add_argument("script", type=Path, help="Python script to execute")
104
+ p.add_argument(
105
+ "--workdir",
106
+ type=Path,
107
+ help="Directory to create the environment in (defaults to a temp dir)",
108
+ )
109
+ p.add_argument(
110
+ "--extra",
111
+ nargs="*",
112
+ default=[],
113
+ metavar="PKG",
114
+ help="Extra PyPI packages to install in addition to auto‑detected ones",
115
+ )
116
+ p.add_argument(
117
+ "--keep-workdir",
118
+ action="store_true",
119
+ help="Do not delete the temporary working directory on success",
120
+ )
121
+
122
+ args = p.parse_args()
123
+ script_path: Path = args.script.resolve()
124
+ if not script_path.is_file():
125
+ p.error(f"Script '{script_path}' not found.")
126
+
127
+ # ---------------------------------------------------------------------
128
+ # Prepare working directory and venv
129
+ # ---------------------------------------------------------------------
130
+ workdir: Path
131
+ temp_created = False
132
+ if args.workdir:
133
+ workdir = args.workdir.resolve()
134
+ workdir.mkdir(parents=True, exist_ok=True)
135
+ else:
136
+ workdir = Path(tempfile.mkdtemp(prefix=f"{script_path.stem}_work_"))
137
+ temp_created = True
138
+ venv_dir = workdir / ".venv"
139
+ print(f"[+] Working directory: {workdir}")
140
+
141
+ # ---------------------------------------------------------------------
142
+ # Detect and install dependencies
143
+ # ---------------------------------------------------------------------
144
+ mods = find_imports(script_path)
145
+ third_party = translate_names(third_party_modules(mods)) | set(args.extra)
146
+
147
+ print(f"[+] Third‑party imports: {sorted(third_party) if third_party else 'None'}")
148
+
149
+ py = create_venv(venv_dir)
150
+ install_packages(py, third_party)
151
+
152
+ # ---------------------------------------------------------------------
153
+ # Run the target script inside the environment
154
+ # ---------------------------------------------------------------------
155
+ env = os.environ.copy()
156
+ env["VIRTUAL_ENV"] = str(venv_dir)
157
+ env["PATH"] = str(py.parent) + os.pathsep + env["PATH"]
158
+
159
+ print("[+] Running script …\n----------------------------------------")
160
+
161
+ proc = subprocess.Popen(
162
+ [str(py), str(script_path)],
163
+ env=env,
164
+ stdout=subprocess.PIPE,
165
+ stderr=subprocess.STDOUT,
166
+ text=True,
167
+ bufsize=1,
168
+ )
169
+
170
+ # Stream the script output line by line
171
+ assert proc.stdout is not None
172
+ for line in proc.stdout:
173
+ print(line, end="")
174
+
175
+ retcode = proc.wait()
176
+
177
+ print("----------------------------------------")
178
+ if retcode == 0:
179
+ print("[+] Script finished successfully.")
180
+ else:
181
+ print(f"[!] Script exited with return code {retcode}.")
182
+ sys.exit(retcode)
183
+
184
+
185
+ # ---------------------------------------------------------------------
186
+ # Cleanup
187
+ # ---------------------------------------------------------------------
188
+ if temp_created and not args.keep_workdir:
189
+ shutil.rmtree(workdir)
190
+ print(f"[+] Removed temporary directory {workdir}")
191
+
192
+
193
+ if __name__ == "__main__":
194
+ try:
195
+ main()
196
+ except subprocess.CalledProcessError as e:
197
+ print("[!] A subprocess exited with a non‑zero status:", e, file=sys.stderr)
198
+ sys.exit(e.returncode)