Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
# -*- coding: utf-8 -*-
|
2 |
import os
|
3 |
-
import time
|
4 |
import gradio as gr
|
5 |
import requests
|
6 |
from io import BytesIO
|
@@ -11,8 +10,8 @@ from train_tokenizer import train_tokenizer
|
|
11 |
from tokenizers import Tokenizer
|
12 |
|
13 |
# Ρυθμίσεις checkpointing
|
14 |
-
CHECKPOINT_FILE = "checkpoint.txt" #
|
15 |
-
CHUNK_SIZE = 1000 #
|
16 |
|
17 |
def fetch_splits(dataset_name):
|
18 |
try:
|
@@ -41,7 +40,6 @@ def fetch_splits(dataset_name):
|
|
41 |
def update_components(dataset_name):
|
42 |
if not dataset_name:
|
43 |
return [gr.Textbox.update(value=""), gr.Dropdown.update(choices=[], value=None), gr.HTML.update(value="")]
|
44 |
-
|
45 |
try:
|
46 |
splits_data = fetch_splits(dataset_name)
|
47 |
config_choices = list(splits_data['splits'].keys())
|
@@ -54,7 +52,6 @@ def update_components(dataset_name):
|
|
54 |
height="560px"
|
55 |
></iframe>
|
56 |
""" if first_config else "Δεν βρέθηκαν διαθέσιμα δεδομένα"
|
57 |
-
|
58 |
# Προτείνουμε ως προεπιλογή για πολλαπλά configs τα ελληνικά και αγγλικά
|
59 |
default_configs = "20231101.el,20231101.en" if first_config and "el" in first_config else first_config
|
60 |
return [
|
@@ -70,7 +67,6 @@ def update_split_choices(dataset_name, configs):
|
|
70 |
return gr.Dropdown.update(choices=[])
|
71 |
try:
|
72 |
splits_data = fetch_splits(dataset_name)
|
73 |
-
# Χρησιμοποιούμε το πρώτο config της λίστας για τις επιλογές του split
|
74 |
first_config = configs.split(",")[0].strip()
|
75 |
return gr.Dropdown.update(choices=splits_data['splits'].get(first_config, []))
|
76 |
except:
|
@@ -78,7 +74,7 @@ def update_split_choices(dataset_name, configs):
|
|
78 |
|
79 |
def create_iterator(dataset_name, configs, split):
|
80 |
"""
|
81 |
-
Για κάθε config
|
82 |
"""
|
83 |
configs_list = [c.strip() for c in configs.split(",") if c.strip()]
|
84 |
for config in configs_list:
|
@@ -98,7 +94,7 @@ def create_iterator(dataset_name, configs, split):
|
|
98 |
|
99 |
def append_to_checkpoint(texts, checkpoint_file):
|
100 |
"""
|
101 |
-
Αποθηκεύει τα
|
102 |
"""
|
103 |
with open(checkpoint_file, "a", encoding="utf-8") as f:
|
104 |
for t in texts:
|
@@ -106,49 +102,47 @@ def append_to_checkpoint(texts, checkpoint_file):
|
|
106 |
|
107 |
def load_checkpoint(checkpoint_file):
|
108 |
"""
|
109 |
-
Διαβάζει
|
110 |
"""
|
111 |
if os.path.exists(checkpoint_file):
|
112 |
with open(checkpoint_file, "r", encoding="utf-8") as f:
|
113 |
return f.read().splitlines()
|
114 |
return []
|
115 |
|
116 |
-
def
|
117 |
"""
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
Αν υπάρχει ήδη checkpoint, συνεχίζει από εκεί.
|
122 |
"""
|
123 |
-
|
|
|
124 |
all_texts = load_checkpoint(CHECKPOINT_FILE)
|
125 |
total_processed = len(all_texts)
|
126 |
-
|
127 |
|
128 |
# Δημιουργία iterator από τα streaming datasets
|
129 |
dataset_iterator = create_iterator(dataset_name, configs, split)
|
130 |
|
131 |
new_texts = []
|
132 |
chunk_count = 0
|
133 |
-
#
|
134 |
for text in dataset_iterator:
|
135 |
new_texts.append(text)
|
136 |
total_processed += 1
|
137 |
-
# Κάθε CHUNK_SIZE δείγματα αποθηκεύουμε στο checkpoint και ενημερώνουμε την πρόοδο
|
138 |
if len(new_texts) >= CHUNK_SIZE:
|
139 |
append_to_checkpoint(new_texts, CHECKPOINT_FILE)
|
140 |
chunk_count += 1
|
141 |
-
|
142 |
-
new_texts = []
|
143 |
-
|
144 |
-
# Αποθήκευση τυχόν υπολειπόμενων νέων δεδομένων
|
145 |
if new_texts:
|
146 |
append_to_checkpoint(new_texts, CHECKPOINT_FILE)
|
147 |
total_processed += len(new_texts)
|
148 |
chunk_count += 1
|
149 |
-
|
150 |
|
151 |
-
#
|
152 |
if custom_files:
|
153 |
custom_texts = []
|
154 |
for file_path in custom_files:
|
@@ -158,17 +152,17 @@ def train_and_test_streaming(dataset_name, configs, split, vocab_size, min_freq,
|
|
158 |
if content:
|
159 |
custom_texts.append(content)
|
160 |
except Exception as file_error:
|
161 |
-
|
162 |
if custom_texts:
|
163 |
append_to_checkpoint(custom_texts, CHECKPOINT_FILE)
|
164 |
total_processed += len(custom_texts)
|
165 |
-
|
166 |
|
167 |
-
#
|
168 |
all_texts = load_checkpoint(CHECKPOINT_FILE)
|
169 |
-
|
170 |
|
171 |
-
# Εκπαίδευση του tokenizer
|
172 |
tokenizer = train_tokenizer(all_texts, vocab_size, min_freq)
|
173 |
|
174 |
# Αποθήκευση και φόρτωση του εκπαιδευμένου tokenizer
|
@@ -197,11 +191,15 @@ def train_and_test_streaming(dataset_name, configs, split, vocab_size, min_freq,
|
|
197 |
"Αριθμός Tokens": len(encoded.tokens),
|
198 |
"Αγνώστων Tokens": sum(1 for t in encoded.tokens if t == "<unk>")
|
199 |
}
|
200 |
-
|
|
|
|
|
|
|
|
|
201 |
|
202 |
# Gradio Interface
|
203 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
204 |
-
gr.Markdown("## Wikipedia Tokenizer Trainer with Checkpointing
|
205 |
|
206 |
with gr.Row():
|
207 |
with gr.Column():
|
@@ -230,31 +228,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
230 |
custom_files = gr.File(
|
231 |
label="Προσαρμοσμένα Ελληνικά Κείμενα",
|
232 |
file_count="multiple",
|
233 |
-
type="filepath"
|
234 |
-
)
|
235 |
-
train_btn = gr.Button("Εκπαίδευση", variant="primary")
|
236 |
-
with gr.Column():
|
237 |
-
progress_box = gr.Textbox(label="Πρόοδος", interactive=False)
|
238 |
-
results_json = gr.JSON(label="��ποτελέσματα")
|
239 |
-
results_plot = gr.Image(label="Κατανομή Μηκών Tokens")
|
240 |
-
|
241 |
-
# Event handlers
|
242 |
-
dataset_name.change(
|
243 |
-
fn=update_components,
|
244 |
-
inputs=dataset_name,
|
245 |
-
outputs=[configs, split, gr.HTML(label="Dataset Preview")]
|
246 |
-
)
|
247 |
-
split.change(
|
248 |
-
fn=update_split_choices,
|
249 |
-
inputs=[dataset_name, configs],
|
250 |
-
outputs=split
|
251 |
-
)
|
252 |
-
train_btn.click(
|
253 |
-
fn=train_and_test_streaming,
|
254 |
-
inputs=[dataset_name, configs, split, vocab_size, min_freq, test_text, custom_files],
|
255 |
-
outputs=[progress_box, results_json, results_plot],
|
256 |
-
stream=True
|
257 |
-
)
|
258 |
-
|
259 |
-
if __name__ == "__main__":
|
260 |
-
demo.launch()
|
|
|
1 |
# -*- coding: utf-8 -*-
|
2 |
import os
|
|
|
3 |
import gradio as gr
|
4 |
import requests
|
5 |
from io import BytesIO
|
|
|
10 |
from tokenizers import Tokenizer
|
11 |
|
12 |
# Ρυθμίσεις checkpointing
|
13 |
+
CHECKPOINT_FILE = "checkpoint.txt" # Αρχείο που αποθηκεύει τα ήδη επεξεργασμένα κείμενα
|
14 |
+
CHUNK_SIZE = 1000 # Αριθμός δειγμάτων ανά chunk για αποθήκευση στο checkpoint
|
15 |
|
16 |
def fetch_splits(dataset_name):
|
17 |
try:
|
|
|
40 |
def update_components(dataset_name):
|
41 |
if not dataset_name:
|
42 |
return [gr.Textbox.update(value=""), gr.Dropdown.update(choices=[], value=None), gr.HTML.update(value="")]
|
|
|
43 |
try:
|
44 |
splits_data = fetch_splits(dataset_name)
|
45 |
config_choices = list(splits_data['splits'].keys())
|
|
|
52 |
height="560px"
|
53 |
></iframe>
|
54 |
""" if first_config else "Δεν βρέθηκαν διαθέσιμα δεδομένα"
|
|
|
55 |
# Προτείνουμε ως προεπιλογή για πολλαπλά configs τα ελληνικά και αγγλικά
|
56 |
default_configs = "20231101.el,20231101.en" if first_config and "el" in first_config else first_config
|
57 |
return [
|
|
|
67 |
return gr.Dropdown.update(choices=[])
|
68 |
try:
|
69 |
splits_data = fetch_splits(dataset_name)
|
|
|
70 |
first_config = configs.split(",")[0].strip()
|
71 |
return gr.Dropdown.update(choices=splits_data['splits'].get(first_config, []))
|
72 |
except:
|
|
|
74 |
|
75 |
def create_iterator(dataset_name, configs, split):
|
76 |
"""
|
77 |
+
Για κάθε config (χωρισμένα με κόμμα) φορτώνει το αντίστοιχο streaming dataset και παράγει τα κείμενα.
|
78 |
"""
|
79 |
configs_list = [c.strip() for c in configs.split(",") if c.strip()]
|
80 |
for config in configs_list:
|
|
|
94 |
|
95 |
def append_to_checkpoint(texts, checkpoint_file):
|
96 |
"""
|
97 |
+
Αποθηκεύει τα δεδομένα στο αρχείο checkpoint.
|
98 |
"""
|
99 |
with open(checkpoint_file, "a", encoding="utf-8") as f:
|
100 |
for t in texts:
|
|
|
102 |
|
103 |
def load_checkpoint(checkpoint_file):
|
104 |
"""
|
105 |
+
Διαβάζει τα δεδομένα από το checkpoint (αν υπάρχει).
|
106 |
"""
|
107 |
if os.path.exists(checkpoint_file):
|
108 |
with open(checkpoint_file, "r", encoding="utf-8") as f:
|
109 |
return f.read().splitlines()
|
110 |
return []
|
111 |
|
112 |
+
def train_and_test(dataset_name, configs, split, vocab_size, min_freq, test_text, custom_files):
|
113 |
"""
|
114 |
+
Εκπαιδεύει τον tokenizer με checkpointing.
|
115 |
+
Επιστρέφει στο τέλος την τελική πρόοδο, τα αποτελέσματα και το plot.
|
116 |
+
(Σημείωση: Σε αυτήν την έκδοση δεν υπάρχει streaming progress λόγω περιορισμών στο Gradio στο Spaces.)
|
|
|
117 |
"""
|
118 |
+
progress_messages = []
|
119 |
+
# Φόρτωση ήδη επεξεργασμένων δεδομένων από το checkpoint (αν υπάρχουν)
|
120 |
all_texts = load_checkpoint(CHECKPOINT_FILE)
|
121 |
total_processed = len(all_texts)
|
122 |
+
progress_messages.append(f"Έχετε {total_processed} δείγματα ήδη αποθηκευμένα σ��ο checkpoint.")
|
123 |
|
124 |
# Δημιουργία iterator από τα streaming datasets
|
125 |
dataset_iterator = create_iterator(dataset_name, configs, split)
|
126 |
|
127 |
new_texts = []
|
128 |
chunk_count = 0
|
129 |
+
# Επεξεργασία νέων δεδομένων σε chunks
|
130 |
for text in dataset_iterator:
|
131 |
new_texts.append(text)
|
132 |
total_processed += 1
|
|
|
133 |
if len(new_texts) >= CHUNK_SIZE:
|
134 |
append_to_checkpoint(new_texts, CHECKPOINT_FILE)
|
135 |
chunk_count += 1
|
136 |
+
progress_messages.append(f"Επεξεργάστηκαν {total_processed} δείγματα (chunk {chunk_count}).")
|
137 |
+
new_texts = []
|
138 |
+
# Αποθήκευση υπολειπόμενων δεδομένων
|
|
|
139 |
if new_texts:
|
140 |
append_to_checkpoint(new_texts, CHECKPOINT_FILE)
|
141 |
total_processed += len(new_texts)
|
142 |
chunk_count += 1
|
143 |
+
progress_messages.append(f"Τελικό chunk: συνολικά {total_processed} δείγματα αποθηκεύτηκαν.")
|
144 |
|
145 |
+
# Επεξεργασία των custom αρχείων, αν υπάρχουν
|
146 |
if custom_files:
|
147 |
custom_texts = []
|
148 |
for file_path in custom_files:
|
|
|
152 |
if content:
|
153 |
custom_texts.append(content)
|
154 |
except Exception as file_error:
|
155 |
+
progress_messages.append(f"Σφάλμα ανάγνωσης αρχείου {file_path}: {file_error}")
|
156 |
if custom_texts:
|
157 |
append_to_checkpoint(custom_texts, CHECKPOINT_FILE)
|
158 |
total_processed += len(custom_texts)
|
159 |
+
progress_messages.append(f"Προστέθηκαν {len(custom_texts)} δείγματα από custom αρχεία.")
|
160 |
|
161 |
+
# Φόρτωση όλων των δεδομένων για εκπαίδευση
|
162 |
all_texts = load_checkpoint(CHECKPOINT_FILE)
|
163 |
+
progress_messages.append(f"Ξεκινάει η εκπαίδευση του tokenizer σε {len(all_texts)} δείγματα...")
|
164 |
|
165 |
+
# Εκπαίδευση του tokenizer
|
166 |
tokenizer = train_tokenizer(all_texts, vocab_size, min_freq)
|
167 |
|
168 |
# Αποθήκευση και φόρτωση του εκπαιδευμένου tokenizer
|
|
|
191 |
"Αριθμός Tokens": len(encoded.tokens),
|
192 |
"Αγνώστων Tokens": sum(1 for t in encoded.tokens if t == "<unk>")
|
193 |
}
|
194 |
+
progress_messages.append("Η εκπαίδευση ολοκληρώθηκε!")
|
195 |
+
|
196 |
+
# Επιστρέφουμε τα μηνύματα προόδου μαζί με τα τελικά αποτελέσματα και το plot
|
197 |
+
final_progress = "\n".join(progress_messages)
|
198 |
+
return final_progress, results, img_buffer.getvalue()
|
199 |
|
200 |
# Gradio Interface
|
201 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
202 |
+
gr.Markdown("## Wikipedia Tokenizer Trainer with Checkpointing")
|
203 |
|
204 |
with gr.Row():
|
205 |
with gr.Column():
|
|
|
228 |
custom_files = gr.File(
|
229 |
label="Προσαρμοσμένα Ελληνικά Κείμενα",
|
230 |
file_count="multiple",
|
231 |
+
type="filepath"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|