Simon Strandgaard commited on
Commit
5e8aef1
·
1 Parent(s): cd2e83b

Download output as a zip file

Browse files
Files changed (1) hide show
  1. src/plan/app_text2plan.py +50 -6
src/plan/app_text2plan.py CHANGED
@@ -16,6 +16,8 @@ from src.plan.speedvsdetail import SpeedVsDetailEnum
16
  from src.prompt.prompt_catalog import PromptCatalog
17
  from src.utils.get_env_as_string import get_env_as_string
18
  from src.utils.time_since_last_modification import time_since_last_modification
 
 
19
 
20
  # Global variables
21
  active_proc = None
@@ -87,6 +89,26 @@ class MarkdownBuilder:
87
 
88
  def to_markdown(self):
89
  return "\n".join(self.rows)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
  def run_planner(submit_or_retry_button: str, plan_prompt: str, llm_model: str, speedvsdetail: str):
92
  """
@@ -95,7 +117,8 @@ def run_planner(submit_or_retry_button: str, plan_prompt: str, llm_model: str, s
95
  1. Prepare the run directory.
96
  2. Updates environment with RUN_ID and LLM_MODEL.
97
  3. Launches the Python pipeline script.
98
- 5. Polls the output dir (every second) to yield a Markdown-formatted file listing.
 
99
  If a stop is requested or if the pipeline-complete file is detected, it exits.
100
  """
101
  global active_proc, stop_event, latest_run_dir, latest_run_id
@@ -145,6 +168,8 @@ def run_planner(submit_or_retry_button: str, plan_prompt: str, llm_model: str, s
145
  env["SPEED_VS_DETAIL"] = speedvsdetail
146
 
147
  start_time = time.perf_counter()
 
 
148
 
149
  # Launch the pipeline as a separate Python process.
150
  command = [sys.executable, "-m", MODULE_PATH_PIPELINE]
@@ -192,7 +217,10 @@ def run_planner(submit_or_retry_button: str, plan_prompt: str, llm_model: str, s
192
  markdown_builder.status("Process terminated by user.")
193
  markdown_builder.path_to_run_dir(absolute_path_to_run_dir)
194
  markdown_builder.list_files(run_path)
195
- yield markdown_builder.to_markdown()
 
 
 
196
  break
197
 
198
  last_update = ceil(time_since_last_modification(run_path))
@@ -200,7 +228,16 @@ def run_planner(submit_or_retry_button: str, plan_prompt: str, llm_model: str, s
200
  markdown_builder.status(f"Working. {duration} seconds elapsed. Last output update was {last_update} seconds ago.")
201
  markdown_builder.path_to_run_dir(absolute_path_to_run_dir)
202
  markdown_builder.list_files(run_path)
203
- yield markdown_builder.to_markdown()
 
 
 
 
 
 
 
 
 
204
 
205
  # If the pipeline complete file is found, finish streaming.
206
  if has_pipeline_complete_file(run_path):
@@ -230,7 +267,13 @@ def run_planner(submit_or_retry_button: str, plan_prompt: str, llm_model: str, s
230
  markdown_builder.status(f"{status_message} {duration} seconds elapsed.")
231
  markdown_builder.path_to_run_dir(absolute_path_to_run_dir)
232
  markdown_builder.list_files(run_path)
233
- yield markdown_builder.to_markdown()
 
 
 
 
 
 
234
 
235
  def stop_planner():
236
  """
@@ -287,6 +330,7 @@ with gr.Blocks(title="PlanExe") as demo_text2plan:
287
 
288
  output_markdown = gr.Markdown("Output will appear here...")
289
  status_markdown = gr.Markdown("Status messages will appear here...")
 
290
 
291
  with gr.Column(scale=1, min_width=300):
292
  examples = gr.Examples(
@@ -320,10 +364,10 @@ with gr.Blocks(title="PlanExe") as demo_text2plan:
320
  """)
321
 
322
  # Start the planning assistant with streaming updates.
323
- submit_btn.click(fn=run_planner, inputs=[submit_btn, prompt_input, model_radio, speedvsdetail_radio], outputs=output_markdown)
324
  # The stop button simply calls stop_planner and displays its status message.
325
  stop_btn.click(fn=stop_planner, outputs=status_markdown)
326
- retry_btn.click(fn=run_planner, inputs=[retry_btn, prompt_input, model_radio, speedvsdetail_radio], outputs=output_markdown)
327
  open_dir_btn.click(fn=open_output_dir, outputs=status_markdown)
328
 
329
  if __name__ == "__main__":
 
16
  from src.prompt.prompt_catalog import PromptCatalog
17
  from src.utils.get_env_as_string import get_env_as_string
18
  from src.utils.time_since_last_modification import time_since_last_modification
19
+ import shutil
20
+ import zipfile
21
 
22
  # Global variables
23
  active_proc = None
 
89
 
90
  def to_markdown(self):
91
  return "\n".join(self.rows)
92
+
93
+ def create_zip_archive(directory_to_zip: str) -> str:
94
+ """
95
+ Creates a zip archive of the given directory, excluding 'log.txt'.
96
+ Returns the path to the zip file, or None if an error occurred.
97
+ """
98
+ zip_file_path = os.path.join(os.path.dirname(directory_to_zip), os.path.basename(directory_to_zip) + ".zip")
99
+ try:
100
+ with zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
101
+ for root, _, files in os.walk(directory_to_zip):
102
+ for file in files:
103
+ if file == "log.txt":
104
+ continue # Skip the log file
105
+
106
+ file_path = os.path.join(root, file)
107
+ zipf.write(file_path, os.path.relpath(file_path, directory_to_zip))
108
+ return zip_file_path
109
+ except Exception as e:
110
+ print(f"Error creating zip archive: {e}")
111
+ return None
112
 
113
  def run_planner(submit_or_retry_button: str, plan_prompt: str, llm_model: str, speedvsdetail: str):
114
  """
 
117
  1. Prepare the run directory.
118
  2. Updates environment with RUN_ID and LLM_MODEL.
119
  3. Launches the Python pipeline script.
120
+ 4. Polls the output dir (every second) to yield a Markdown-formatted file listing.
121
+ 5. Creates a zip archive (every 10 seconds) with the latest output.
122
  If a stop is requested or if the pipeline-complete file is detected, it exits.
123
  """
124
  global active_proc, stop_event, latest_run_dir, latest_run_id
 
168
  env["SPEED_VS_DETAIL"] = speedvsdetail
169
 
170
  start_time = time.perf_counter()
171
+ last_zip_time = time.time() # Initialize the last zip creation time
172
+ most_recent_zip_file = None # Store the path to the latest created zip
173
 
174
  # Launch the pipeline as a separate Python process.
175
  command = [sys.executable, "-m", MODULE_PATH_PIPELINE]
 
217
  markdown_builder.status("Process terminated by user.")
218
  markdown_builder.path_to_run_dir(absolute_path_to_run_dir)
219
  markdown_builder.list_files(run_path)
220
+ zip_file_path = create_zip_archive(run_path)
221
+ if zip_file_path:
222
+ most_recent_zip_file = zip_file_path
223
+ yield markdown_builder.to_markdown(), most_recent_zip_file
224
  break
225
 
226
  last_update = ceil(time_since_last_modification(run_path))
 
228
  markdown_builder.status(f"Working. {duration} seconds elapsed. Last output update was {last_update} seconds ago.")
229
  markdown_builder.path_to_run_dir(absolute_path_to_run_dir)
230
  markdown_builder.list_files(run_path)
231
+
232
+ # Check if it's time to create a new zip archive (every 10 seconds)
233
+ current_time = time.time()
234
+ if current_time - last_zip_time >= 10:
235
+ zip_file_path = create_zip_archive(run_path)
236
+ if zip_file_path: # Only update the path if the creation succeeded
237
+ most_recent_zip_file = zip_file_path
238
+ last_zip_time = current_time
239
+
240
+ yield markdown_builder.to_markdown(), most_recent_zip_file
241
 
242
  # If the pipeline complete file is found, finish streaming.
243
  if has_pipeline_complete_file(run_path):
 
267
  markdown_builder.status(f"{status_message} {duration} seconds elapsed.")
268
  markdown_builder.path_to_run_dir(absolute_path_to_run_dir)
269
  markdown_builder.list_files(run_path)
270
+
271
+ # Create zip archive
272
+ zip_file_path = create_zip_archive(run_path)
273
+ if zip_file_path:
274
+ most_recent_zip_file = zip_file_path
275
+
276
+ yield markdown_builder.to_markdown(), most_recent_zip_file
277
 
278
  def stop_planner():
279
  """
 
330
 
331
  output_markdown = gr.Markdown("Output will appear here...")
332
  status_markdown = gr.Markdown("Status messages will appear here...")
333
+ download_output = gr.File(label="Download latest output (excluding log.txt) as zip")
334
 
335
  with gr.Column(scale=1, min_width=300):
336
  examples = gr.Examples(
 
364
  """)
365
 
366
  # Start the planning assistant with streaming updates.
367
+ submit_btn.click(fn=run_planner, inputs=[submit_btn, prompt_input, model_radio, speedvsdetail_radio], outputs=[output_markdown, download_output])
368
  # The stop button simply calls stop_planner and displays its status message.
369
  stop_btn.click(fn=stop_planner, outputs=status_markdown)
370
+ retry_btn.click(fn=run_planner, inputs=[retry_btn, prompt_input, model_radio, speedvsdetail_radio], outputs=[output_markdown, download_output])
371
  open_dir_btn.click(fn=open_output_dir, outputs=status_markdown)
372
 
373
  if __name__ == "__main__":