aagamjtdev commited on
Commit
bdfc013
Β·
1 Parent(s): f3ce0ca

delete cache button

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py CHANGED
@@ -66,6 +66,49 @@ def retrieve_model():
66
  return log_output, None, gr.Button(visible=False)
67
 
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  def train_model(dataset_file: gr.File, batch_size: int, epochs: int, lr: float, max_len: int, progress=gr.Progress()):
70
  """
71
  Handles the Gradio submission and executes the training script using subprocess.
@@ -280,6 +323,7 @@ with gr.Blocks(title="LayoutLMv3 Fine-Tuning App", theme=gr.themes.Soft()) as de
280
 
281
  train_button = gr.Button("πŸ”₯ Start Training", variant="primary", size="lg")
282
  check_button = gr.Button("πŸ” Check Model Status/Download", variant="secondary", size="lg")
 
283
 
284
  with gr.Column(scale=2):
285
  gr.Markdown("### πŸ“Š Training Progress (Real-Time Logs)")
@@ -312,6 +356,8 @@ with gr.Blocks(title="LayoutLMv3 Fine-Tuning App", theme=gr.themes.Soft()) as de
312
  outputs=[log_output, model_path_state, download_btn]
313
  )
314
 
 
 
315
  # File output for download
316
  model_download = gr.File(
317
  label="Your trained model will appear here after clicking Download",
@@ -319,6 +365,12 @@ with gr.Blocks(title="LayoutLMv3 Fine-Tuning App", theme=gr.themes.Soft()) as de
319
  visible=True
320
  )
321
 
 
 
 
 
 
 
322
  gr.Markdown(
323
  """
324
  **πŸ“₯ Download Instructions:**
 
66
  return log_output, None, gr.Button(visible=False)
67
 
68
 
69
+
70
+
71
+ def clear_memory(dataset_file: gr.File):
72
+ """
73
+ Deletes the model output directory and the uploaded dataset file.
74
+ """
75
+ MODEL_OUTPUT_DIR = "checkpoints"
76
+
77
+ log_output = f"--- Memory Clear Started: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} ---\n"
78
+
79
+ # 1. Clear Model Checkpoints Directory
80
+ if os.path.exists(MODEL_OUTPUT_DIR):
81
+ try:
82
+ shutil.rmtree(MODEL_OUTPUT_DIR)
83
+ log_output += f"βœ… Successfully deleted model directory: {MODEL_OUTPUT_DIR}\n"
84
+ except Exception as e:
85
+ log_output += f"❌ ERROR deleting model directory {MODEL_OUTPUT_DIR}: {e}\n"
86
+ else:
87
+ log_output += f"ℹ️ Model directory not found: {MODEL_OUTPUT_DIR} (Nothing to delete)\n"
88
+
89
+ # 2. Clear Uploaded Dataset File (Temporary file cleanup)
90
+ if dataset_file is not None:
91
+ input_path = dataset_file.name if hasattr(dataset_file, 'name') else str(dataset_file)
92
+ if os.path.exists(input_path):
93
+ try:
94
+ os.remove(input_path)
95
+ log_output += f"βœ… Successfully deleted uploaded dataset file: {input_path}\n"
96
+ except Exception as e:
97
+ log_output += f"❌ ERROR deleting dataset file {input_path}: {e}\n"
98
+ else:
99
+ log_output += f"ℹ️ Uploaded dataset file not found at {input_path}.\n"
100
+ else:
101
+ log_output += f"ℹ️ No dataset file currently tracked for deletion.\n"
102
+
103
+ # 3. Final message and state reset
104
+ log_output += f"--- Memory Clear Complete: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} ---\n"
105
+ log_output += "✨ Files and checkpoints have been removed. You can now start a fresh training run."
106
+
107
+ # Reset log_output, model_path_state, download_btn visibility, and model_download component
108
+ return log_output, None, gr.Button(visible=False), None
109
+
110
+
111
+
112
  def train_model(dataset_file: gr.File, batch_size: int, epochs: int, lr: float, max_len: int, progress=gr.Progress()):
113
  """
114
  Handles the Gradio submission and executes the training script using subprocess.
 
323
 
324
  train_button = gr.Button("πŸ”₯ Start Training", variant="primary", size="lg")
325
  check_button = gr.Button("πŸ” Check Model Status/Download", variant="secondary", size="lg")
326
+ clear_button = gr.Button("🧹 Clear Model/Dataset Files", variant="stop", size="lg")
327
 
328
  with gr.Column(scale=2):
329
  gr.Markdown("### πŸ“Š Training Progress (Real-Time Logs)")
 
356
  outputs=[log_output, model_path_state, download_btn]
357
  )
358
 
359
+
360
+
361
  # File output for download
362
  model_download = gr.File(
363
  label="Your trained model will appear here after clicking Download",
 
365
  visible=True
366
  )
367
 
368
+ clear_button.click(
369
+ fn=clear_memory,
370
+ inputs=[file_input], # Pass the uploaded file object to delete the temp file
371
+ outputs=[log_output, model_path_state, download_btn, model_download]
372
+ )
373
+
374
  gr.Markdown(
375
  """
376
  **πŸ“₯ Download Instructions:**