Kevin Fink
commited on
Commit
·
194731c
1
Parent(s):
a9e3a19
init
Browse files- README.md +4 -6
- app.py +88 -0
- requirements.txt +2 -0
README.md
CHANGED
@@ -1,14 +1,12 @@
|
|
1 |
---
|
2 |
-
title: Gradio
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.8.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
-
license: mit
|
11 |
-
short_description: gradio
|
12 |
---
|
13 |
|
14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Gradio 2
|
3 |
+
emoji: ⚡
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: red
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.8.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
|
|
|
|
10 |
---
|
11 |
|
12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import Trainer, TrainingArguments, AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
+
from datasets import load_dataset
|
4 |
+
import traceback
|
5 |
+
|
6 |
+
def fine_tune_model(model_name, dataset_name, hub_id, num_epochs, batch_size, lr, grad):
|
7 |
+
'''
|
8 |
+
try:
|
9 |
+
|
10 |
+
# Load the dataset
|
11 |
+
dataset = load_dataset(dataset_name)
|
12 |
+
|
13 |
+
# Load the model and tokenizer
|
14 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name, num_labels=2)
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
16 |
+
|
17 |
+
# Tokenize the dataset
|
18 |
+
def tokenize_function(examples):
|
19 |
+
return tokenizer(examples['text'], padding="max_length", truncation=True)
|
20 |
+
|
21 |
+
tokenized_datasets = dataset.map(tokenize_function, batched=True)
|
22 |
+
|
23 |
+
# Set training arguments
|
24 |
+
training_args = TrainingArguments(
|
25 |
+
output_dir='./results',
|
26 |
+
evaluation_strategy="epoch",
|
27 |
+
learning_rate=lr,
|
28 |
+
per_device_train_batch_size=batch_size,
|
29 |
+
per_device_eval_batch_size=batch_size,
|
30 |
+
num_train_epochs=num_epochs,
|
31 |
+
weight_decay=0.01,
|
32 |
+
evaluation_strategy='epoch',
|
33 |
+
gradient_accumulation_steps=grad,
|
34 |
+
load_best_model_at_end=True,
|
35 |
+
metric_for_best_model="accuracy",
|
36 |
+
greater_is_better=True,
|
37 |
+
logging_dir='./logs',
|
38 |
+
logging_steps=10,
|
39 |
+
push_to_hub=True,
|
40 |
+
hub_model_id=hub_id,
|
41 |
+
)
|
42 |
+
|
43 |
+
# Create Trainer
|
44 |
+
trainer = Trainer(
|
45 |
+
model=model,
|
46 |
+
args=training_args,
|
47 |
+
train_dataset=tokenized_datasets['train'],
|
48 |
+
eval_dataset=tokenized_datasets['validation'],
|
49 |
+
)
|
50 |
+
|
51 |
+
# Fine-tune the model
|
52 |
+
trainer.train()
|
53 |
+
trainer.push_to_hub(commit_message="Training complete!")
|
54 |
+
except Exception as e:
|
55 |
+
return f"An error occurred: {str(e)}, TB: {traceback.format_exc()}"
|
56 |
+
'''
|
57 |
+
return 'DONE!'#model
|
58 |
+
'''
|
59 |
+
# Define Gradio interface
|
60 |
+
def predict(text):
|
61 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
62 |
+
outputs = model(inputs)
|
63 |
+
predictions = outputs.logits.argmax(dim=-1)
|
64 |
+
return "Positive" if predictions.item() == 1 else "Negative"
|
65 |
+
'''
|
66 |
+
# Create Gradio interface
|
67 |
+
try:
|
68 |
+
|
69 |
+
iface = gr.Interface(
|
70 |
+
fine_tune_model,
|
71 |
+
inputs=[
|
72 |
+
gr.inputs.Textbox(label="Model Name (e.g., 'google/t5-efficient-tiny-nh8')"),
|
73 |
+
gr.inputs.Textbox(label="Dataset Name (e.g., 'imdb')"),
|
74 |
+
gr.inputs.Textbox(label="HF hub to push to after training"),
|
75 |
+
gr.inputs.Slider(minimum=1, maximum=10, default=3, label="Number of Epochs"),
|
76 |
+
gr.inputs.Slider(minimum=1, maximum=16, default=4, label="Batch Size"),
|
77 |
+
gr.inputs.Slider(minimum=1, maximum=100, default=50, label="Learning Rate (e-5)"),
|
78 |
+
gr.inputs.Slider(minimum=1, maximum=100, default=1, label="Gradient accumulation (e-1)"),
|
79 |
+
],
|
80 |
+
outputs="text",
|
81 |
+
title="Fine-Tune Hugging Face Model",
|
82 |
+
description="This interface allows you to fine-tune a Hugging Face model on a specified dataset."
|
83 |
+
)
|
84 |
+
except Exception as e:
|
85 |
+
print(f"An error occurred: {str(e)}, TB: {traceback.format_exc()}")
|
86 |
+
|
87 |
+
# Launch the interface
|
88 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
datasets
|