Kevin Fink commited on
Commit
88838d4
·
1 Parent(s): 7d1a420
Files changed (1) hide show
  1. app.py +12 -53
app.py CHANGED
@@ -2,15 +2,15 @@ import spaces
2
  import gradio as gr
3
  from transformers import Trainer, TrainingArguments, AutoTokenizer, AutoModelForSeq2SeqLM
4
  from transformers import DataCollatorForSeq2Seq, AutoConfig
5
- #from datasets import load_dataset, concatenate_datasets, load_from_disk, DatasetDict
6
  import traceback
7
- #from sklearn.metrics import accuracy_score
8
- #import numpy as np
9
  import torch
10
  import os
11
- #import evaluate
12
- #from huggingface_hub import login
13
- #from peft import get_peft_model, LoraConfig
14
 
15
  os.environ['HF_HOME'] = '/data/.huggingface'
16
  '''
@@ -31,7 +31,7 @@ def fine_tune_model(model, dataset_name, hub_id, api_key, num_epochs, batch_size
31
  try:
32
  torch.cuda.empty_cache()
33
  torch.nn.CrossEntropyLoss()
34
- rouge_metric = evaluate.load("rouge", cache_dir='/data/cache')
35
  #def compute_metrics(eval_preds):
36
  #preds, labels = eval_preds
37
  #if isinstance(preds, tuple):
@@ -96,7 +96,6 @@ def fine_tune_model(model, dataset_name, hub_id, api_key, num_epochs, batch_size
96
  #if os.path.exists(training_args.output_dir) and os.listdir(training_args.output_dir):
97
  #print("Loading model from checkpoint...")
98
  #model = AutoModelForSeq2SeqLM.from_pretrained(training_args.output_dir)
99
- config = AutoConfig.from_pretrained("google/t5-efficient-tiny-nh8")
100
  tokenizer = AutoTokenizer.from_pretrained('google/t5-efficient-tiny-nh8', use_fast=True, trust_remote_code=True)
101
 
102
  #max_length = model.get_input_embeddings().weight.shape[0]
@@ -234,45 +233,15 @@ def fine_tune_model(model, dataset_name, hub_id, api_key, num_epochs, batch_size
234
  #)
235
 
236
  # Fine-tune the model
237
- if os.path.exists(training_args.output_dir) and os.listdir(training_args.output_dir):
238
- train_result = trainer.train(resume_from_checkpoint=True)
239
- else:
240
- train_result = trainer.train()
241
  trainer.push_to_hub(commit_message="Training complete!")
242
  except Exception as e:
243
  return f"An error occurred: {str(e)}, TB: {traceback.format_exc()}"
244
  return 'DONE!'#train_result
245
 
246
- # Define Gradio interface
247
- @spaces.GPU
248
- def predict(text):
249
- print(text)
250
- config = AutoConfig.from_pretrained("shorecode/t5-efficient-tiny-nh8-summarizer")
251
- model = AutoModelForSeq2SeqLM.from_config(config)
252
- #initialize_weights(model)
253
- tokenizer = AutoTokenizer.from_pretrained('shorecode/t5-efficient-tiny-nh8')
254
- inputs = tokenizer(text, return_tensors="pt", padding='max_length', max_length=512, truncation=True)
255
-
256
- # Move model and inputs to GPU if available
257
- if torch.cuda.is_available():
258
- model = model.to('cuda')
259
- inputs = {key: value.to('cuda') for key, value in inputs.items()}
260
- print('xxxxxxxxxxxxxxxxxxxxxxx')
261
- print(inputs)
262
- # Generate outputs
263
- with torch.no_grad(): # Disable gradient calculation for inference
264
- outputs = model.generate(inputs['input_ids'])
265
-
266
- ## Decode the generated output
267
- #predictions = tokenizer.decode(outputs[0], skip_special_tokens=True)
268
- #inputs = tokenizer(text, padding='max_length', max_length=512, truncation=True)
269
- #with torch.no_grad(): # Disable gradient calculation for inference
270
- #outputs = model.generate(inputs)
271
- predictions = tokenizer.decode(outputs[0], skip_special_tokens=True)
272
- print('xxxxxxxxxxxxxxxxxxxxxxx')
273
- print(predictions)
274
- return predictions
275
-
276
 
277
  @spaces.GPU(duration=120)
278
  def run_train(dataset_name, hub_id, api_key, num_epochs, batch_size, lr, grad):
@@ -312,17 +281,7 @@ try:
312
  title="Fine-Tune Hugging Face Model",
313
  description="This interface allows you to fine-tune a Hugging Face model on a specified dataset."
314
  )
315
- '''
316
- iface = gr.Interface(
317
- fn=predict,
318
- inputs=[
319
- gr.Textbox(label="Query"),
320
- ],
321
- outputs="text",
322
- title="Fine-Tune Hugging Face Model",
323
- description="This interface allows you to test a fine-tune Hugging Face model."
324
- )
325
- '''
326
  # Launch the interface
327
  iface.launch()
328
  except Exception as e:
 
2
  import gradio as gr
3
  from transformers import Trainer, TrainingArguments, AutoTokenizer, AutoModelForSeq2SeqLM
4
  from transformers import DataCollatorForSeq2Seq, AutoConfig
5
+ from datasets import load_dataset, concatenate_datasets, load_from_disk, DatasetDict
6
  import traceback
7
+ from sklearn.metrics import accuracy_score
8
+ import numpy as np
9
  import torch
10
  import os
11
+ import evaluate
12
+ from huggingface_hub import login
13
+ from peft import get_peft_model, LoraConfig
14
 
15
  os.environ['HF_HOME'] = '/data/.huggingface'
16
  '''
 
31
  try:
32
  torch.cuda.empty_cache()
33
  torch.nn.CrossEntropyLoss()
34
+ #rouge_metric = evaluate.load("rouge", cache_dir='/data/cache')
35
  #def compute_metrics(eval_preds):
36
  #preds, labels = eval_preds
37
  #if isinstance(preds, tuple):
 
96
  #if os.path.exists(training_args.output_dir) and os.listdir(training_args.output_dir):
97
  #print("Loading model from checkpoint...")
98
  #model = AutoModelForSeq2SeqLM.from_pretrained(training_args.output_dir)
 
99
  tokenizer = AutoTokenizer.from_pretrained('google/t5-efficient-tiny-nh8', use_fast=True, trust_remote_code=True)
100
 
101
  #max_length = model.get_input_embeddings().weight.shape[0]
 
233
  #)
234
 
235
  # Fine-tune the model
236
+ #if os.path.exists(training_args.output_dir) and os.listdir(training_args.output_dir):
237
+ #train_result = trainer.train(resume_from_checkpoint=True)
238
+ #else:
239
+ train_result = trainer.train()
240
  trainer.push_to_hub(commit_message="Training complete!")
241
  except Exception as e:
242
  return f"An error occurred: {str(e)}, TB: {traceback.format_exc()}"
243
  return 'DONE!'#train_result
244
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
245
 
246
  @spaces.GPU(duration=120)
247
  def run_train(dataset_name, hub_id, api_key, num_epochs, batch_size, lr, grad):
 
281
  title="Fine-Tune Hugging Face Model",
282
  description="This interface allows you to fine-tune a Hugging Face model on a specified dataset."
283
  )
284
+
 
 
 
 
 
 
 
 
 
 
285
  # Launch the interface
286
  iface.launch()
287
  except Exception as e: