Kevin Fink commited on
Commit
612c19e
·
1 Parent(s): f72ef0e

gradio update

Browse files
Files changed (1) hide show
  1. app.py +74 -11
app.py CHANGED
@@ -1,15 +1,78 @@
1
-
2
  import gradio as gr
3
- import spaces
4
- import torch
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- zero = torch.Tensor([0]).cuda()
7
- print(zero.device) # <-- 'cpu' 🤔
 
 
 
 
8
 
9
- @spaces.GPU
10
- def greet(n):
11
- print(zero.device) # <-- 'cuda:0' 🤗
12
- return f"Hello {zero + n} Tensor"
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- demo = gr.Interface(fn=greet, inputs=gr.Number(), outputs=gr.Text())
15
- demo.launch()
 
 
1
  import gradio as gr
2
+ from transformers import Trainer, TrainingArguments, AutoTokenizer, AutoModelForSeq2SeqLM
3
+ from datasets import load_dataset
4
+
5
+ def fine_tune_model(model_name, dataset_name, hub_id, num_epochs, batch_size, lr, grad):
6
+ # Load the dataset
7
+ dataset = load_dataset(dataset_name)
8
+
9
+ # Load the model and tokenizer
10
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name, num_labels=2)
11
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
12
+
13
+ # Tokenize the dataset
14
+ def tokenize_function(examples):
15
+ return tokenizer(examples['text'], padding="max_length", truncation=True)
16
+
17
+ tokenized_datasets = dataset.map(tokenize_function, batched=True)
18
+
19
+ # Set training arguments
20
+ training_args = TrainingArguments(
21
+ output_dir='./results',
22
+ evaluation_strategy="epoch",
23
+ learning_rate=lr,
24
+ per_device_train_batch_size=batch_size,
25
+ per_device_eval_batch_size=batch_size,
26
+ num_train_epochs=num_epochs,
27
+ weight_decay=0.01,
28
+ evaluation_strategy='epoch',
29
+ gradient_accumulation_steps=grad,
30
+ load_best_model_at_end=True,
31
+ metric_for_best_model="accuracy",
32
+ greater_is_better=True,
33
+ logging_dir='./logs',
34
+ logging_steps=10,
35
+ push_to_hub=True,
36
+ hub_model_id=hub_id,
37
+ )
38
+
39
+ # Create Trainer
40
+ trainer = Trainer(
41
+ model=model,
42
+ args=training_args,
43
+ train_dataset=tokenized_datasets['train'],
44
+ eval_dataset=tokenized_datasets['validation'],
45
+ )
46
+
47
+ # Fine-tune the model
48
+ trainer.train()
49
+ trainer.push_to_hub(commit_message="Training complete!")
50
+ return 'DONE!'#model
51
 
52
+ # Define Gradio interface
53
+ def predict(text):
54
+ inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
55
+ outputs = model(inputs)
56
+ predictions = outputs.logits.argmax(dim=-1)
57
+ return "Positive" if predictions.item() == 1 else "Negative"
58
 
59
+ # Create Gradio interface
60
+ iface = gr.Interface(
61
+ fn=fine_tune_model,
62
+ inputs=[
63
+ gr.inputs.Textbox(label="Model Name (e.g., 'google/t5-efficient-tiny-nh8')"),
64
+ gr.inputs.Textbox(label="Dataset Name (e.g., 'imdb')"),
65
+ gr.inputs.Textbox(label="HF hub to push to after training"),
66
+ gr.inputs.Slider(minimum=1, maximum=10, default=3, label="Number of Epochs"),
67
+ gr.inputs.Slider(minimum=1, maximum=16, default=4, label="Batch Size"),
68
+ gr.inputs.Slider(minimum=1, maximum=16, default=4, label="Batch Size"),
69
+ gr.inputs.Slider(minimum=1, maximum=1000, default=50, label="Learning Rate (e-6)"),
70
+ gr.inputs.Slider(minimum=1, maximum=100, default=1, label="Gradient accumulation (e-1)"),
71
+ ],
72
+ outputs="text",
73
+ title="Fine-Tune Hugging Face Model",
74
+ description="This interface allows you to fine-tune a Hugging Face model on a specified dataset."
75
+ )
76
 
77
+ # Launch the interface
78
+ iface.launch()