PeteBleackley commited on
Commit
6eb15d0
·
1 Parent(s): 3e66483

Configured gradio app for training

Browse files
Files changed (2) hide show
  1. app.py +14 -2
  2. scripts.py +8 -9
app.py CHANGED
@@ -7,9 +7,21 @@ Created on Wed Oct 11 10:26:15 2023
7
  """
8
 
9
  import gradio as gr
 
 
10
 
11
  def greet(name):
12
  return "Hello " + name + "!!"
13
 
14
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
15
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
7
  """
8
 
9
  import gradio as gr
10
+ import scripts
11
+ import pandas
12
 
13
  def greet(name):
14
  return "Hello " + name + "!!"
15
 
16
+ def train():
17
+ history = scripts.train_models('PlayfulTechnology')
18
+ return pandas.DataFrame(history).plot.line(subplots=True)
19
+
20
+
21
+ with gr.Blocks() as trainer:
22
+ training_button = gr.Button(value="Train models")
23
+ loss_plot = gr.Plot()
24
+ training_button.click(train,inputs=[],outputs=[loss_plot])
25
+
26
+ trainer.launch()
27
+
scripts.py CHANGED
@@ -1,7 +1,6 @@
1
 
2
  import os
3
  import argparse
4
- import json
5
  import numpy
6
  import tokenizers
7
  import transformers
@@ -21,6 +20,7 @@ import scipy.stats
21
  import scipy.spatial
22
  import seaborn
23
  import tqdm
 
24
 
25
  class SequenceCrossEntropyLoss(torch.nn.Module):
26
  def __init__(self):
@@ -131,7 +131,7 @@ def prepare_training_datasets():
131
  reasoning.to_csv('corpora/reasoning_train.csv')
132
  consistency.to_csv('corpora/consistency.csv')
133
 
134
- def train_models(path):
135
  tokenizer = tokenizers.Tokenizer.from_pretrained('roberta-base')
136
  trainer = qarac.models.QaracTrainerModel.QaracTrainerModel('roberta-base',
137
  tokenizer)
@@ -146,10 +146,11 @@ def train_models(path):
146
  reasoning='corpora/reasoning_train.csv',
147
  consistency='corpora/consistency.csv')
148
  n_batches = len(training_data)
149
- history = []
150
  for epoch in range(10):
151
  print("Epoch",epoch)
152
- epoch_history = []
 
153
  for (batch,(X,Y)) in enumerate(tqdm.tqdm(training_data)):
154
  prediction = trainer(X['all_text'],
155
  X['offset_text'],
@@ -165,16 +166,14 @@ def train_models(path):
165
  optimizer.step()
166
  optimizer.zero_grad()
167
  if batch % 1024 == 0 or batch == n_batches-1:
168
- epoch_history.append({'batch':batch,
169
- 'loss':loss.item()})
170
  scheduler.step()
171
- history.append(epoch_history)
172
- with open('training_history.json','w') as jsonfile:
173
- json.dump(history,jsonfile)
174
  huggingface_hub.login(token=os.environ['HUGGINGFACE_TOKEN'])
175
  trainer.question_encoder.push_to_hub('{}/qarac-roberta-question-encoder'.format(path))
176
  trainer.answer_encoder.push_to_hub('{}/qarac-roberta-answer-encoder'.format(path))
177
  trainer.decoder.push_to_hub('{}/qarac-roberta-decoder'.format(path))
 
178
 
179
 
180
  def test_encode_decode(path):
 
1
 
2
  import os
3
  import argparse
 
4
  import numpy
5
  import tokenizers
6
  import transformers
 
20
  import scipy.spatial
21
  import seaborn
22
  import tqdm
23
+ import gradio
24
 
25
  class SequenceCrossEntropyLoss(torch.nn.Module):
26
  def __init__(self):
 
131
  reasoning.to_csv('corpora/reasoning_train.csv')
132
  consistency.to_csv('corpora/consistency.csv')
133
 
134
+ def train_models(path,progress=gradio.Progress(track_tqdm=True)):
135
  tokenizer = tokenizers.Tokenizer.from_pretrained('roberta-base')
136
  trainer = qarac.models.QaracTrainerModel.QaracTrainerModel('roberta-base',
137
  tokenizer)
 
146
  reasoning='corpora/reasoning_train.csv',
147
  consistency='corpora/consistency.csv')
148
  n_batches = len(training_data)
149
+ history = {}
150
  for epoch in range(10):
151
  print("Epoch",epoch)
152
+ epoch_label = 'Epoch {}'.format(epoch)
153
+ epoch_data = {}
154
  for (batch,(X,Y)) in enumerate(tqdm.tqdm(training_data)):
155
  prediction = trainer(X['all_text'],
156
  X['offset_text'],
 
166
  optimizer.step()
167
  optimizer.zero_grad()
168
  if batch % 1024 == 0 or batch == n_batches-1:
169
+ epoch_data[batch] = loss.item()
170
+ history[epoch_label] = epoch_data
171
  scheduler.step()
 
 
 
172
  huggingface_hub.login(token=os.environ['HUGGINGFACE_TOKEN'])
173
  trainer.question_encoder.push_to_hub('{}/qarac-roberta-question-encoder'.format(path))
174
  trainer.answer_encoder.push_to_hub('{}/qarac-roberta-answer-encoder'.format(path))
175
  trainer.decoder.push_to_hub('{}/qarac-roberta-decoder'.format(path))
176
+ return history
177
 
178
 
179
  def test_encode_decode(path):