Spaces:
Running
on
A10G
Running
on
A10G
File size: 8,631 Bytes
dbdb417 498c6f5 dbdb417 498c6f5 dbdb417 498c6f5 dbdb417 498c6f5 dbdb417 498c6f5 dbdb417 498c6f5 dbdb417 498c6f5 dbdb417 498c6f5 dbdb417 498c6f5 dbdb417 498c6f5 dbdb417 498c6f5 dbdb417 498c6f5 dbdb417 498c6f5 dbdb417 498c6f5 dbdb417 a97446f dbdb417 e2b8e29 dbdb417 498c6f5 dbdb417 1cb59a2 498c6f5 1cb59a2 dbdb417 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
import glob
import os
import shutil
import sys
import re
import tempfile
import zipfile
from pathlib import Path
import gradio as gr
from finetune import finetune_model, log
from language import languages
from task import tasks
import matplotlib.pyplot as plt
def load_markdown():
with open("intro.md", "r") as f:
return f.read()
def read_logs(temp_dir):
if not os.path.exists(f"{temp_dir}/output.log"):
return "Log file not found."
try:
with open(f"{temp_dir}/output.log", "r") as f:
return f.read()
except:
return None
def plot_loss_acc(temp_dir, log_every):
sys.stdout.flush()
lines = []
if not os.path.exists(f"{temp_dir}/output.log"):
return None, None
with open(f"{temp_dir}/output.log", "r") as f:
for line in f.readlines():
if re.match(r"^\[\d+\] - loss: \d+\.\d+ - acc: \d+\.\d+$", line):
lines.append(line)
losses = []
acces = []
if len(lines) == 0:
return None, None
for line in lines:
_, loss, acc = line.split(" - ")
losses.append(float(loss.split(":")[1].strip()))
acces.append(float(acc.split(":")[1].strip()))
x = [i * log_every for i in range(1, len(losses) + 1)]
plt.plot(x, losses, label="loss")
plt.xlim(log_every // 2, x[-1] + log_every // 2)
plt.savefig(f"{temp_dir}/loss.png")
plt.clf()
plt.plot(x, acces, label="acc")
plt.xlim(log_every // 2, x[-1] + log_every // 2)
plt.savefig(f"{temp_dir}/acc.png")
plt.clf()
return f"{temp_dir}/acc.png", f"{temp_dir}/loss.png"
def upload_file(fileobj, temp_dir):
"""
Upload a file and check the uploaded zip file.
"""
# First check if a file is a zip file.
if not zipfile.is_zipfile(fileobj.name):
log(temp_dir, "Please upload a zip file.")
raise gr.Error("Please upload a zip file.")
# Then unzip file
log(temp_dir, "Unzipping file...")
shutil.unpack_archive(fileobj.name, temp_dir)
# check zip file
if not os.path.exists(os.path.join(temp_dir, "text")):
log(temp_dir, "Please upload a valid zip file.")
raise gr.Error("Please upload a valid zip file.")
if not os.path.exists(os.path.join(temp_dir, "text_ctc")):
log(temp_dir, "Please upload a valid zip file.")
raise gr.Error("Please upload a valid zip file.")
if not os.path.exists(os.path.join(temp_dir, "audio")):
log(temp_dir, "Please upload a valid zip file.")
raise gr.Error("Please upload a valid zip file.")
# check if all texts and audio matches
log(temp_dir, "Checking if all texts and audio matches...")
audio_ids = []
with open(os.path.join(temp_dir, "text"), "r") as f:
for line in f.readlines():
audio_ids.append(line.split(maxsplit=1)[0])
with open(os.path.join(temp_dir, "text_ctc"), "r") as f:
ctc_audio_ids = []
for line in f.readlines():
ctc_audio_ids.append(line.split(maxsplit=1)[0])
if len(audio_ids) != len(ctc_audio_ids):
raise gr.Error(
f"Length of `text` ({len(audio_ids)}) and `text_ctc` ({len(ctc_audio_ids)}) is different."
)
if set(audio_ids) != set(ctc_audio_ids):
log(temp_dir, f"`text` and `text_ctc` have different audio ids.")
raise gr.Error(f"`text` and `text_ctc` have different audio ids.")
for audio_id in glob.glob(os.path.join(temp_dir, "audio", "*")):
if not Path(audio_id).stem in audio_ids:
raise gr.Error(f"Audio id {audio_id} is not in `text` or `text_ctc`.")
log(temp_dir, "Successfully uploaded and validated zip file.")
gr.Info("Successfully uploaded and validated zip file.")
return [fileobj]
def delete_tmp_dir(tmp_dir):
if os.path.exists(tmp_dir):
shutil.rmtree(tmp_dir)
print(f"Deleted temporary directory: {tmp_dir}")
else:
print("Temporary directory already deleted")
def create_tmp_dir():
tmp_dir = tempfile.mkdtemp()
print(f"Created temporary directory: {tmp_dir}")
return tmp_dir
with gr.Blocks(title="OWSM-finetune") as demo:
tempdir_path=gr.State(create_tmp_dir, delete_callback=delete_tmp_dir, time_to_live=600)
gr.Markdown(
"""# OWSM finetune demo!
Finetune `owsm_v3.1_ebf_base` with your own dataset!
Due to resource limitation, you can only train 5 epochs on maximum.
## Upload dataset and define settings
"""
)
# main contents
with gr.Row():
with gr.Column():
file_output = gr.File()
upload_button = gr.UploadButton("Click to Upload a File", file_count="single")
upload_button.upload(
upload_file, [upload_button, tempdir_path], [file_output]
)
with gr.Column():
lang = gr.Dropdown(
languages["espnet/owsm_v3.1_ebf_base"],
label="Language",
info="Choose language!",
value="jpn",
interactive=True,
)
task = gr.Dropdown(
tasks["espnet/owsm_v3.1_ebf_base"],
label="Task",
info="Choose task!",
value="asr",
interactive=True,
)
gr.Markdown("## Set training settings")
with gr.Row():
with gr.Column():
log_every = gr.Number(value=10, label="log_every", interactive=True)
max_epoch = gr.Slider(1, 5, step=1, label="max_epoch", interactive=True)
scheduler = gr.Dropdown(
["warmuplr"], label="warmup", value="warmuplr", interactive=True
)
warmup_steps = gr.Number(
value=100, label="warmup_steps", interactive=True
)
with gr.Column():
optimizer = gr.Dropdown(
["adam", "adamw", "sgd", "adadelta", "adagrad", "adamax", "asgd", "rmsprop"],
label="optimizer",
value="adam",
interactive=True
)
learning_rate = gr.Number(
value=1e-4, label="learning_rate", interactive=True
)
weight_decay = gr.Number(
value=0.000001, label="weight_decay", interactive=True
)
gr.Markdown("## Logs and plots")
with gr.Row():
with gr.Column():
log_output = gr.Textbox(
show_label=False,
interactive=False,
max_lines=23,
lines=23,
)
demo.load(read_logs, [tempdir_path], log_output, every=2)
with gr.Column():
log_acc = gr.Image(label="Accuracy", show_label=True, interactive=False)
log_loss = gr.Image(label="Loss", show_label=True, interactive=False)
demo.load(plot_loss_acc, [tempdir_path, log_every], [log_acc, log_loss], every=10)
with gr.Row():
with gr.Column():
ref_text = gr.Textbox(
label="Reference text",
show_label=True,
interactive=False,
max_lines=10,
lines=10,
)
with gr.Column():
base_text = gr.Textbox(
label="Baseline text",
show_label=True,
interactive=False,
max_lines=10,
lines=10,
)
with gr.Row():
with gr.Column():
hyp_text = gr.Textbox(
label="Hypothesis text",
show_label=True,
interactive=False,
max_lines=10,
lines=10,
)
with gr.Column():
trained_model = gr.File(
label="Trained model",
interactive=False,
)
with gr.Row():
finetune_btn = gr.Button("Finetune Model", variant="primary")
finetune_btn.click(
finetune_model,
[
lang,
task,
tempdir_path,
log_every,
max_epoch,
scheduler,
warmup_steps,
optimizer,
learning_rate,
weight_decay,
],
[trained_model, ref_text, base_text, hyp_text]
)
gr.Markdown(load_markdown())
if __name__ == "__main__":
try:
demo.queue().launch()
except:
print("Unexpected error:", sys.exc_info()[0])
raise
finally:
shutil.rmtree(os.environ['TEMP_DIR'])
|