sylvain471 commited on
Commit
fe02574
·
verified ·
1 Parent(s): 9c5c048

Upload 2 files

Browse files

fix run_dir folder issues

Files changed (1) hide show
  1. app.py +61 -12
app.py CHANGED
@@ -20,11 +20,11 @@ from io import BytesIO
20
  from PIL import Image
21
  import json
22
 
23
- input_path = os.environ["SCRIPT_INPUT"]
24
- output_path = os.environ["SCRIPT_OUTPUT"]
25
 
26
  # Load JSON input
27
- with open(input_path, "r") as f:
28
  data = json.load(f)
29
  img_b64 = data["img"]
30
 
@@ -37,7 +37,8 @@ img.load() # ⬅️ Ensure image is fully loaded before processing
37
  flipped = img.transpose(Image.FLIP_LEFT_RIGHT)
38
 
39
  # Save output
40
- flipped.save(os.path.join(output_path, "flipped.png"))
 
41
  print("Image flipped and saved as flipped.png.")
42
  """
43
 
@@ -56,7 +57,8 @@ def cleanup_old_runs(base_dir: Path, max_age: int):
56
  def zip_artifacts(output_dir: Path, zip_path: Path):
57
  with zipfile.ZipFile(zip_path, "w") as zf:
58
  for file in output_dir.iterdir():
59
- zf.write(file, arcname=file.name)
 
60
 
61
 
62
  def run_script(code: str, user_input: str = "", cleanup_enabled: bool = True, cleanup_after: int = 600):
@@ -134,12 +136,33 @@ def run_script(code: str, user_input: str = "", cleanup_enabled: bool = True, cl
134
  orig_stderr = sys.stderr
135
  sys.stdout = sys.stderr = output
136
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
  try:
138
  sys.argv = [
139
  "run_script_venv.py",
140
- str(script_path),
 
141
  "--keep-workdir",
142
- "--extra", "pandas", # safe default dependency for common data handling
143
  ]
144
  os.environ["SCRIPT_INPUT"] = str(input_path)
145
  os.environ["SCRIPT_OUTPUT"] = str(output_path)
@@ -147,16 +170,40 @@ def run_script(code: str, user_input: str = "", cleanup_enabled: bool = True, cl
147
  except SystemExit:
148
  pass
149
  finally:
 
150
  sys.stdout = orig_stdout
151
  sys.stderr = orig_stderr
152
 
 
153
  output.seek(0)
154
  logs = output.read()
155
 
156
 
157
- # Collect output artifacts
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
  artifacts = {}
159
- for item in output_path.iterdir():
 
 
 
 
 
 
160
  if item.suffix in {".txt", ".csv", ".json", ".md"}:
161
  artifacts[item.name] = item.read_text()
162
  elif item.suffix.lower() in {".png", ".jpg", ".jpeg"}:
@@ -164,12 +211,14 @@ def run_script(code: str, user_input: str = "", cleanup_enabled: bool = True, cl
164
  else:
165
  artifacts[item.name] = f"[file saved: {item.name}]"
166
 
167
- # Create zip
168
  zip_path = run_dir / "output.zip"
169
- zip_artifacts(output_path, zip_path)
170
  artifacts["Download All (ZIP)"] = str(zip_path)
171
-
172
  artifacts["__workdir__"] = str(run_dir)
 
 
 
173
  return logs, artifacts, str(zip_path)
174
 
175
 
 
20
  from PIL import Image
21
  import json
22
 
23
+ # input_path = os.environ["SCRIPT_INPUT"]
24
+ # output_path = os.environ["SCRIPT_OUTPUT"]
25
 
26
  # Load JSON input
27
+ with open("input.txt", "r") as f:
28
  data = json.load(f)
29
  img_b64 = data["img"]
30
 
 
37
  flipped = img.transpose(Image.FLIP_LEFT_RIGHT)
38
 
39
  # Save output
40
+ # flipped.save(os.path.join(output_path, "flipped.png"))
41
+ flipped.save()"flipped.png")
42
  print("Image flipped and saved as flipped.png.")
43
  """
44
 
 
57
  def zip_artifacts(output_dir: Path, zip_path: Path):
58
  with zipfile.ZipFile(zip_path, "w") as zf:
59
  for file in output_dir.iterdir():
60
+ if "output.zip" not in file.name:
61
+ zf.write(file, arcname=file.name)
62
 
63
 
64
  def run_script(code: str, user_input: str = "", cleanup_enabled: bool = True, cleanup_after: int = 600):
 
136
  orig_stderr = sys.stderr
137
  sys.stdout = sys.stderr = output
138
 
139
+ # try:
140
+ # sys.argv = [
141
+ # "run_script_venv.py",
142
+ # str(script_path),
143
+ # "--keep-workdir",
144
+ # "--extra", "pandas", # safe default dependency for common data handling
145
+ # ]
146
+ # os.environ["SCRIPT_INPUT"] = str(input_path)
147
+ # os.environ["SCRIPT_OUTPUT"] = str(output_path)
148
+ # run_in_venv()
149
+ # except SystemExit:
150
+ # pass
151
+ # finally:
152
+ # sys.stdout = orig_stdout
153
+ # sys.stderr = orig_stderr
154
+
155
+ # Set working directory so user script writes to run_dir
156
+ old_cwd = os.getcwd()
157
+ os.chdir(run_dir)
158
+
159
  try:
160
  sys.argv = [
161
  "run_script_venv.py",
162
+ # str(script_path),
163
+ "script.py",
164
  "--keep-workdir",
165
+ "--extra", "pandas",
166
  ]
167
  os.environ["SCRIPT_INPUT"] = str(input_path)
168
  os.environ["SCRIPT_OUTPUT"] = str(output_path)
 
170
  except SystemExit:
171
  pass
172
  finally:
173
+ os.chdir(old_cwd)
174
  sys.stdout = orig_stdout
175
  sys.stderr = orig_stderr
176
 
177
+
178
  output.seek(0)
179
  logs = output.read()
180
 
181
 
182
+ # # Collect output artifacts
183
+ # artifacts = {}
184
+ # for item in output_path.iterdir():
185
+ # if item.suffix in {".txt", ".csv", ".json", ".md"}:
186
+ # artifacts[item.name] = item.read_text()
187
+ # elif item.suffix.lower() in {".png", ".jpg", ".jpeg"}:
188
+ # artifacts[item.name] = f"[image file: {item.name}]"
189
+ # else:
190
+ # artifacts[item.name] = f"[file saved: {item.name}]"
191
+
192
+ # # Create zip
193
+ # zip_path = run_dir / "output.zip"
194
+ # zip_artifacts(output_path, zip_path)
195
+ # artifacts["Download All (ZIP)"] = str(zip_path)
196
+ # artifacts["__workdir__"] = str(run_dir)
197
+
198
+ # Collect output artifacts (from output/ and run_dir/)
199
  artifacts = {}
200
+ # ignored = {"script.py", "input.txt", "output.zip"}
201
+ ignored = {"output.zip"}
202
+ for item in run_dir.iterdir():
203
+ if item.name in ignored or item.name.startswith(".venv"):
204
+ continue
205
+ if item.is_dir():
206
+ continue # Skip subdirectories except output
207
  if item.suffix in {".txt", ".csv", ".json", ".md"}:
208
  artifacts[item.name] = item.read_text()
209
  elif item.suffix.lower() in {".png", ".jpg", ".jpeg"}:
 
211
  else:
212
  artifacts[item.name] = f"[file saved: {item.name}]"
213
 
214
+ # Still allow zipped output/ as a bonus
215
  zip_path = run_dir / "output.zip"
216
+ zip_artifacts(run_dir, zip_path)
217
  artifacts["Download All (ZIP)"] = str(zip_path)
 
218
  artifacts["__workdir__"] = str(run_dir)
219
+
220
+
221
+
222
  return logs, artifacts, str(zip_path)
223
 
224