Spaces:
Runtime error
Runtime error
Web Changes
Browse files
app.py
CHANGED
@@ -1,51 +1,207 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
def test_model(input_text):
|
5 |
-
"""
|
6 |
if not input_text.strip():
|
7 |
return "Please enter some text to test."
|
8 |
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
def train_model(dataset_text):
|
13 |
-
"""Training function"""
|
14 |
if not dataset_text.strip():
|
15 |
-
return "Please provide training data."
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
def finetune_model(dataset_text):
|
20 |
-
"""Fine-tuning function"""
|
21 |
if not dataset_text.strip():
|
22 |
-
return "Please provide fine-tuning data."
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
# Create interface
|
27 |
-
with gr.Blocks(title="
|
28 |
-
gr.Markdown(
|
|
|
29 |
|
30 |
-
with gr.Tab("Test"):
|
|
|
31 |
with gr.Row():
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
35 |
test_btn.click(test_model, inputs=test_input, outputs=test_output)
|
36 |
|
37 |
-
with gr.Tab("Train"):
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
41 |
train_btn.click(train_model, inputs=train_input, outputs=train_output)
|
42 |
|
43 |
-
with gr.Tab("Fine-tune"):
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
47 |
finetune_btn.click(finetune_model, inputs=finetune_input, outputs=finetune_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
if __name__ == "__main__":
|
50 |
-
demo.launch()
|
51 |
-
launch(share=True)
|
|
|
1 |
import gradio as gr
|
2 |
+
import time
|
3 |
+
import threading
|
4 |
+
import random
|
5 |
+
from datetime import datetime
|
6 |
+
|
7 |
+
# Global state to track training/fine-tuning status
|
8 |
+
class TrainingState:
|
9 |
+
def __init__(self):
|
10 |
+
self.status = "idle"
|
11 |
+
self.progress = 0
|
12 |
+
self.logs = []
|
13 |
+
self.start_time = None
|
14 |
+
self.model_name = "tasal9/pashto-base-bloom"
|
15 |
+
self.active_process = None
|
16 |
+
|
17 |
+
def start_training(self, data_size):
|
18 |
+
self.status = "training"
|
19 |
+
self.progress = 0
|
20 |
+
self.logs = [f"Training started at {datetime.now().strftime('%H:%M:%S')}"]
|
21 |
+
self.logs.append(f"Training data size: {data_size} characters")
|
22 |
+
self.start_time = time.time()
|
23 |
+
|
24 |
+
def start_finetuning(self, data_size):
|
25 |
+
self.status = "fine-tuning"
|
26 |
+
self.progress = 0
|
27 |
+
self.logs = [f"Fine-tuning started at {datetime.now().strftime('%H:%M:%S')}"]
|
28 |
+
self.logs.append(f"Fine-tuning data size: {data_size} characters")
|
29 |
+
self.start_time = time.time()
|
30 |
+
|
31 |
+
def update_progress(self, progress):
|
32 |
+
self.progress = min(100, max(0, progress))
|
33 |
+
if progress >= 100 and self.status != "idle":
|
34 |
+
self.complete_process()
|
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: # Keep only last 10 logs
|
39 |
+
self.logs.pop(0)
|
40 |
+
|
41 |
+
def complete_process(self):
|
42 |
+
elapsed = time.time() - self.start_time
|
43 |
+
self.add_log(f"{self.status.capitalize()} completed in {elapsed:.1f} seconds!")
|
44 |
+
self.status = "idle"
|
45 |
+
self.progress = 100
|
46 |
+
|
47 |
+
def get_status(self):
|
48 |
+
status_map = {
|
49 |
+
"idle": "β
Ready",
|
50 |
+
"training": "π Training in progress",
|
51 |
+
"fine-tuning": "π Fine-tuning in progress"
|
52 |
+
}
|
53 |
+
return status_map.get(self.status, "β Unknown status")
|
54 |
+
|
55 |
+
# Create global state
|
56 |
+
state = TrainingState()
|
57 |
|
58 |
def test_model(input_text):
|
59 |
+
"""Enhanced test function with response variations"""
|
60 |
if not input_text.strip():
|
61 |
return "Please enter some text to test."
|
62 |
|
63 |
+
responses = [
|
64 |
+
f"Processed: '{input_text}'",
|
65 |
+
f"Model response to: {input_text}",
|
66 |
+
f"Analysis: This appears to be Pashto text with {len(input_text)} characters",
|
67 |
+
f"β
Received: {input_text}",
|
68 |
+
f"Generated continuation: {input_text}... [simulated output]"
|
69 |
+
]
|
70 |
+
return random.choice(responses)
|
71 |
+
|
72 |
+
def simulate_process(duration, process_type, data_size):
|
73 |
+
"""Simulate long-running training/fine-tuning process"""
|
74 |
+
if process_type == "train":
|
75 |
+
state.start_training(data_size)
|
76 |
+
else:
|
77 |
+
state.start_finetuning(data_size)
|
78 |
+
|
79 |
+
steps = 10
|
80 |
+
for i in range(steps + 1):
|
81 |
+
time.sleep(duration / steps)
|
82 |
+
progress = int((i / steps) * 100)
|
83 |
+
state.update_progress(progress)
|
84 |
+
|
85 |
+
# Add simulated log messages
|
86 |
+
if i % 3 == 0:
|
87 |
+
messages = [
|
88 |
+
f"Processing batch {i*5}/{steps*5}",
|
89 |
+
f"Loss: {random.uniform(0.1, 1.0):.4f}",
|
90 |
+
f"Accuracy: {random.uniform(80, 95):.1f}%",
|
91 |
+
f"Learning rate: {random.uniform(1e-5, 1e-3):.6f}"
|
92 |
+
]
|
93 |
+
state.add_log(random.choice(messages))
|
94 |
+
|
95 |
+
state.complete_process()
|
96 |
|
97 |
def train_model(dataset_text):
|
98 |
+
"""Training function with simulated processing"""
|
99 |
if not dataset_text.strip():
|
100 |
+
return "Please provide training data.", ""
|
101 |
+
|
102 |
+
data_size = len(dataset_text)
|
103 |
+
if state.status != "idle":
|
104 |
+
return "Another process is already running. Please wait.", ""
|
105 |
|
106 |
+
# Start simulation in background thread
|
107 |
+
threading.Thread(
|
108 |
+
target=simulate_process,
|
109 |
+
args=(15, "train", data_size),
|
110 |
+
daemon=True
|
111 |
+
).start()
|
112 |
+
|
113 |
+
return "Training started successfully! Check status in the Status tab.", ""
|
114 |
|
115 |
def finetune_model(dataset_text):
|
116 |
+
"""Fine-tuning function with simulated processing"""
|
117 |
if not dataset_text.strip():
|
118 |
+
return "Please provide fine-tuning data.", ""
|
119 |
+
|
120 |
+
data_size = len(dataset_text)
|
121 |
+
if state.status != "idle":
|
122 |
+
return "Another process is already running. Please wait.", ""
|
123 |
|
124 |
+
# Start simulation in background thread
|
125 |
+
threading.Thread(
|
126 |
+
target=simulate_process,
|
127 |
+
args=(10, "fine-tune", data_size),
|
128 |
+
daemon=True
|
129 |
+
).start()
|
130 |
+
|
131 |
+
return "Fine-tuning started successfully! Check status in the Status tab.", ""
|
132 |
+
|
133 |
+
def get_current_status():
|
134 |
+
"""Get current system status"""
|
135 |
+
status_text = state.get_status()
|
136 |
+
|
137 |
+
# Add progress information
|
138 |
+
if state.status != "idle":
|
139 |
+
status_text += f" - {state.progress}% complete"
|
140 |
+
|
141 |
+
# Format logs
|
142 |
+
logs = "\n".join(state.logs) if state.logs else "No logs available"
|
143 |
+
|
144 |
+
return {
|
145 |
+
status_box: status_text,
|
146 |
+
progress_bar: state.progress / 100,
|
147 |
+
log_output: logs
|
148 |
+
}
|
149 |
|
150 |
# Create interface
|
151 |
+
with gr.Blocks(title="Pashto-Base-Bloom Trainer", theme="soft") as demo:
|
152 |
+
gr.Markdown("# πΈ Pashto-Base-Bloom Training Space")
|
153 |
+
gr.Markdown("Train and fine-tune Pashto language model tasal9/pashto-base-bloom")
|
154 |
|
155 |
+
with gr.Tab("Test Model"):
|
156 |
+
gr.Markdown("### Test Model with Sample Text")
|
157 |
with gr.Row():
|
158 |
+
with gr.Column():
|
159 |
+
test_input = gr.Textbox(label="Input Text", lines=3, placeholder="Enter Pashto text here...")
|
160 |
+
test_btn = gr.Button("Run Test", variant="primary")
|
161 |
+
test_output = gr.Textbox(label="Model Output", lines=4, interactive=False)
|
162 |
test_btn.click(test_model, inputs=test_input, outputs=test_output)
|
163 |
|
164 |
+
with gr.Tab("Train Model"):
|
165 |
+
gr.Markdown("### Train Model with New Data")
|
166 |
+
with gr.Row():
|
167 |
+
with gr.Column():
|
168 |
+
train_input = gr.Textbox(label="Training Data", lines=8, placeholder="Paste training dataset here...")
|
169 |
+
train_btn = gr.Button("Start Training", variant="primary")
|
170 |
+
train_output = gr.Textbox(label="Training Status", lines=2, interactive=False)
|
171 |
train_btn.click(train_model, inputs=train_input, outputs=train_output)
|
172 |
|
173 |
+
with gr.Tab("Fine-tune Model"):
|
174 |
+
gr.Markdown("### Fine-tune Model with Specialized Data")
|
175 |
+
with gr.Row():
|
176 |
+
with gr.Column():
|
177 |
+
finetune_input = gr.Textbox(label="Fine-tuning Data", lines=8, placeholder="Paste fine-tuning dataset here...")
|
178 |
+
finetune_btn = gr.Button("Start Fine-tuning", variant="primary")
|
179 |
+
finetune_output = gr.Textbox(label="Fine-tuning Status", lines=2, interactive=False)
|
180 |
finetune_btn.click(finetune_model, inputs=finetune_input, outputs=finetune_output)
|
181 |
+
|
182 |
+
with gr.Tab("Status"):
|
183 |
+
gr.Markdown("### System Status")
|
184 |
+
with gr.Row():
|
185 |
+
with gr.Column():
|
186 |
+
status_box = gr.Textbox(label="Current Status", interactive=False)
|
187 |
+
progress_bar = gr.ProgressBar(label="Progress")
|
188 |
+
refresh_btn = gr.Button("Refresh Status", variant="secondary")
|
189 |
+
auto_refresh = gr.Checkbox(label="Auto-refresh every 5 seconds", value=True)
|
190 |
+
log_output = gr.Textbox(label="Process Logs", lines=8, interactive=False)
|
191 |
+
|
192 |
+
# Auto-refresh component
|
193 |
+
auto_refresh_component = gr.Interval(5, interactive=False)
|
194 |
+
|
195 |
+
# Refresh actions
|
196 |
+
refresh_btn.click(get_current_status, outputs=[status_box, progress_bar, log_output])
|
197 |
+
auto_refresh_component.change(
|
198 |
+
fn=lambda: get_current_status() if auto_refresh.value else None,
|
199 |
+
outputs=[status_box, progress_bar, log_output]
|
200 |
+
)
|
201 |
+
auto_refresh.change(lambda x: gr.update(interactive=x), inputs=auto_refresh, outputs=auto_refresh_component)
|
202 |
+
|
203 |
+
# Initial status load
|
204 |
+
demo.load(get_current_status, outputs=[status_box, progress_bar, log_output])
|
205 |
|
206 |
if __name__ == "__main__":
|
207 |
+
demo.launch(share=True)
|
|