Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -35,7 +35,7 @@ class TrainingState:
|
|
| 35 |
|
| 36 |
def add_log(self, message):
|
| 37 |
self.logs.append(f"[{datetime.now().strftime('%H:%M:%S')}] {message}")
|
| 38 |
-
if len(self.logs) > 10:
|
| 39 |
self.logs.pop(0)
|
| 40 |
|
| 41 |
def complete_process(self):
|
|
@@ -59,10 +59,8 @@ class TrainingState:
|
|
| 59 |
state = TrainingState()
|
| 60 |
|
| 61 |
def test_model(input_text):
|
| 62 |
-
"""Enhanced test function with response variations"""
|
| 63 |
if not input_text.strip():
|
| 64 |
return "Please enter some text to test."
|
| 65 |
-
|
| 66 |
responses = [
|
| 67 |
f"Processed: '{input_text}'",
|
| 68 |
f"Model response to: {input_text}",
|
|
@@ -73,7 +71,6 @@ def test_model(input_text):
|
|
| 73 |
return random.choice(responses)
|
| 74 |
|
| 75 |
def simulate_process(duration, process_type, data_size):
|
| 76 |
-
"""Simulate long-running training/fine-tuning process"""
|
| 77 |
if process_type == "train":
|
| 78 |
state.start_training(data_size)
|
| 79 |
else:
|
|
@@ -84,8 +81,6 @@ def simulate_process(duration, process_type, data_size):
|
|
| 84 |
time.sleep(duration / steps)
|
| 85 |
progress = int((i / steps) * 100)
|
| 86 |
state.update_progress(progress)
|
| 87 |
-
|
| 88 |
-
# Add simulated log messages
|
| 89 |
if i % 3 == 0:
|
| 90 |
messages = [
|
| 91 |
f"Processing batch {i*5}/{steps*5}",
|
|
@@ -94,59 +89,53 @@ def simulate_process(duration, process_type, data_size):
|
|
| 94 |
f"Learning rate: {random.uniform(1e-5, 1e-3):.6f}"
|
| 95 |
]
|
| 96 |
state.add_log(random.choice(messages))
|
| 97 |
-
|
| 98 |
state.complete_process()
|
| 99 |
|
| 100 |
def train_model(dataset_text):
|
| 101 |
-
"""Training function with simulated processing"""
|
| 102 |
if not dataset_text.strip():
|
| 103 |
return "Please provide training data.", ""
|
| 104 |
-
|
| 105 |
data_size = len(dataset_text)
|
| 106 |
-
if state
|
| 107 |
return "Another process is already running. Please wait.", ""
|
| 108 |
-
|
| 109 |
-
# Start simulation in background thread
|
| 110 |
threading.Thread(
|
| 111 |
target=simulate_process,
|
| 112 |
args=(15, "train", data_size),
|
| 113 |
daemon=True
|
| 114 |
).start()
|
| 115 |
-
|
| 116 |
return "Training started successfully! Check status in the Status tab.", ""
|
| 117 |
|
| 118 |
def finetune_model(dataset_text):
|
| 119 |
-
"""Fine-tuning function with simulated processing"""
|
| 120 |
if not dataset_text.strip():
|
| 121 |
return "Please provide fine-tuning data.", ""
|
| 122 |
-
|
| 123 |
data_size = len(dataset_text)
|
| 124 |
-
if state
|
| 125 |
return "Another process is already running. Please wait.", ""
|
| 126 |
-
|
| 127 |
-
# Start simulation in background thread
|
| 128 |
threading.Thread(
|
| 129 |
target=simulate_process,
|
| 130 |
args=(10, "fine-tune", data_size),
|
| 131 |
daemon=True
|
| 132 |
).start()
|
| 133 |
-
|
| 134 |
return "Fine-tuning started successfully! Check status in the Status tab.", ""
|
| 135 |
|
| 136 |
def get_current_status():
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
return {
|
| 148 |
status_box: status_text,
|
| 149 |
-
progress_bar:
|
| 150 |
log_output: logs
|
| 151 |
}
|
| 152 |
|
|
@@ -187,16 +176,12 @@ with gr.Blocks(title="Pashto-Base-Bloom Trainer", theme="soft") as demo:
|
|
| 187 |
with gr.Row():
|
| 188 |
with gr.Column():
|
| 189 |
status_box = gr.Textbox(label="Current Status", interactive=False)
|
| 190 |
-
# Replace ProgressBar with Slider for progress indication
|
| 191 |
progress_bar = gr.Slider(label="Progress", minimum=0, maximum=100, value=0, interactive=False)
|
| 192 |
refresh_btn = gr.Button("Refresh Status", variant="secondary")
|
| 193 |
auto_refresh = gr.Checkbox(label="Auto-refresh every 5 seconds", value=True)
|
| 194 |
log_output = gr.Textbox(label="Process Logs", lines=8, interactive=False)
|
| 195 |
|
| 196 |
-
# Refresh actions
|
| 197 |
refresh_btn.click(get_current_status, outputs=[status_box, progress_bar, log_output])
|
| 198 |
-
|
| 199 |
-
# Initial status load
|
| 200 |
demo.load(get_current_status, outputs=[status_box, progress_bar, log_output])
|
| 201 |
|
| 202 |
if __name__ == "__main__":
|
|
|
|
| 35 |
|
| 36 |
def add_log(self, message):
|
| 37 |
self.logs.append(f"[{datetime.now().strftime('%H:%M:%S')}] {message}")
|
| 38 |
+
if len(self.logs) > 10:
|
| 39 |
self.logs.pop(0)
|
| 40 |
|
| 41 |
def complete_process(self):
|
|
|
|
| 59 |
state = TrainingState()
|
| 60 |
|
| 61 |
def test_model(input_text):
|
|
|
|
| 62 |
if not input_text.strip():
|
| 63 |
return "Please enter some text to test."
|
|
|
|
| 64 |
responses = [
|
| 65 |
f"Processed: '{input_text}'",
|
| 66 |
f"Model response to: {input_text}",
|
|
|
|
| 71 |
return random.choice(responses)
|
| 72 |
|
| 73 |
def simulate_process(duration, process_type, data_size):
|
|
|
|
| 74 |
if process_type == "train":
|
| 75 |
state.start_training(data_size)
|
| 76 |
else:
|
|
|
|
| 81 |
time.sleep(duration / steps)
|
| 82 |
progress = int((i / steps) * 100)
|
| 83 |
state.update_progress(progress)
|
|
|
|
|
|
|
| 84 |
if i % 3 == 0:
|
| 85 |
messages = [
|
| 86 |
f"Processing batch {i*5}/{steps*5}",
|
|
|
|
| 89 |
f"Learning rate: {random.uniform(1e-5, 1e-3):.6f}"
|
| 90 |
]
|
| 91 |
state.add_log(random.choice(messages))
|
|
|
|
| 92 |
state.complete_process()
|
| 93 |
|
| 94 |
def train_model(dataset_text):
|
|
|
|
| 95 |
if not dataset_text.strip():
|
| 96 |
return "Please provide training data.", ""
|
|
|
|
| 97 |
data_size = len(dataset_text)
|
| 98 |
+
if getattr(state, 'status', 'idle') != "idle":
|
| 99 |
return "Another process is already running. Please wait.", ""
|
|
|
|
|
|
|
| 100 |
threading.Thread(
|
| 101 |
target=simulate_process,
|
| 102 |
args=(15, "train", data_size),
|
| 103 |
daemon=True
|
| 104 |
).start()
|
|
|
|
| 105 |
return "Training started successfully! Check status in the Status tab.", ""
|
| 106 |
|
| 107 |
def finetune_model(dataset_text):
|
|
|
|
| 108 |
if not dataset_text.strip():
|
| 109 |
return "Please provide fine-tuning data.", ""
|
|
|
|
| 110 |
data_size = len(dataset_text)
|
| 111 |
+
if getattr(state, 'status', 'idle') != "idle":
|
| 112 |
return "Another process is already running. Please wait.", ""
|
|
|
|
|
|
|
| 113 |
threading.Thread(
|
| 114 |
target=simulate_process,
|
| 115 |
args=(10, "fine-tune", data_size),
|
| 116 |
daemon=True
|
| 117 |
).start()
|
|
|
|
| 118 |
return "Fine-tuning started successfully! Check status in the Status tab.", ""
|
| 119 |
|
| 120 |
def get_current_status():
|
| 121 |
+
try:
|
| 122 |
+
status_text = state.get_status()
|
| 123 |
+
except Exception as e:
|
| 124 |
+
status_text = f"❌ Error: {str(e)}"
|
| 125 |
+
|
| 126 |
+
try:
|
| 127 |
+
progress = state.progress
|
| 128 |
+
except:
|
| 129 |
+
progress = 0
|
| 130 |
+
|
| 131 |
+
try:
|
| 132 |
+
logs = "\n".join(state.logs[-10:]) if hasattr(state, 'logs') else "No logs available"
|
| 133 |
+
except:
|
| 134 |
+
logs = "❌ Failed to retrieve logs."
|
| 135 |
|
| 136 |
return {
|
| 137 |
status_box: status_text,
|
| 138 |
+
progress_bar: progress,
|
| 139 |
log_output: logs
|
| 140 |
}
|
| 141 |
|
|
|
|
| 176 |
with gr.Row():
|
| 177 |
with gr.Column():
|
| 178 |
status_box = gr.Textbox(label="Current Status", interactive=False)
|
|
|
|
| 179 |
progress_bar = gr.Slider(label="Progress", minimum=0, maximum=100, value=0, interactive=False)
|
| 180 |
refresh_btn = gr.Button("Refresh Status", variant="secondary")
|
| 181 |
auto_refresh = gr.Checkbox(label="Auto-refresh every 5 seconds", value=True)
|
| 182 |
log_output = gr.Textbox(label="Process Logs", lines=8, interactive=False)
|
| 183 |
|
|
|
|
| 184 |
refresh_btn.click(get_current_status, outputs=[status_box, progress_bar, log_output])
|
|
|
|
|
|
|
| 185 |
demo.load(get_current_status, outputs=[status_box, progress_bar, log_output])
|
| 186 |
|
| 187 |
if __name__ == "__main__":
|