app.py
CHANGED
@@ -1,9 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
-
import torch
|
3 |
from datasets import Dataset
|
4 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, Trainer
|
5 |
import pandas as pd
|
6 |
from huggingface_hub import login
|
|
|
7 |
|
8 |
def train_model(file, hf_token):
|
9 |
try:
|
@@ -16,20 +16,27 @@ def train_model(file, hf_token):
|
|
16 |
df = pd.read_csv(file.name)
|
17 |
dataset = Dataset.from_pandas(df)
|
18 |
|
19 |
-
# Model setup
|
20 |
model_name = "facebook/opt-125m"
|
|
|
21 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
22 |
-
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
|
|
|
|
|
23 |
|
24 |
# Training configuration
|
25 |
training_args = TrainingArguments(
|
26 |
output_dir="./results",
|
27 |
num_train_epochs=3,
|
28 |
-
per_device_train_batch_size=
|
29 |
learning_rate=3e-5,
|
30 |
save_strategy="epoch",
|
31 |
push_to_hub=True,
|
32 |
-
hub_token=hf_token
|
|
|
|
|
33 |
)
|
34 |
|
35 |
# Initialize trainer
|
@@ -43,6 +50,9 @@ def train_model(file, hf_token):
|
|
43 |
# Run training
|
44 |
trainer.train()
|
45 |
|
|
|
|
|
|
|
46 |
return "Training completed successfully!"
|
47 |
|
48 |
except Exception as e:
|
@@ -57,8 +67,8 @@ demo = gr.Interface(
|
|
57 |
],
|
58 |
outputs="text",
|
59 |
title="Product Classifier Training",
|
60 |
-
description="Upload your CSV data to train a product classifier model."
|
61 |
)
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from datasets import Dataset
|
3 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, Trainer
|
4 |
import pandas as pd
|
5 |
from huggingface_hub import login
|
6 |
+
import torch
|
7 |
|
8 |
def train_model(file, hf_token):
|
9 |
try:
|
|
|
16 |
df = pd.read_csv(file.name)
|
17 |
dataset = Dataset.from_pandas(df)
|
18 |
|
19 |
+
# Model setup - force CPU
|
20 |
model_name = "facebook/opt-125m"
|
21 |
+
device_map = "cpu" # Force CPU usage
|
22 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
23 |
+
model = AutoModelForCausalLM.from_pretrained(
|
24 |
+
model_name,
|
25 |
+
device_map=device_map,
|
26 |
+
torch_dtype=torch.float32 # Use float32 for CPU
|
27 |
+
)
|
28 |
|
29 |
# Training configuration
|
30 |
training_args = TrainingArguments(
|
31 |
output_dir="./results",
|
32 |
num_train_epochs=3,
|
33 |
+
per_device_train_batch_size=1, # Reduced for CPU
|
34 |
learning_rate=3e-5,
|
35 |
save_strategy="epoch",
|
36 |
push_to_hub=True,
|
37 |
+
hub_token=hf_token,
|
38 |
+
no_cuda=True, # Force CPU usage
|
39 |
+
report_to="none" # Disable wandb logging
|
40 |
)
|
41 |
|
42 |
# Initialize trainer
|
|
|
50 |
# Run training
|
51 |
trainer.train()
|
52 |
|
53 |
+
# Push to hub
|
54 |
+
model.push_to_hub(f"cheberle/product-classifier-{pd.Timestamp.now().strftime('%Y%m%d')}")
|
55 |
+
|
56 |
return "Training completed successfully!"
|
57 |
|
58 |
except Exception as e:
|
|
|
67 |
],
|
68 |
outputs="text",
|
69 |
title="Product Classifier Training",
|
70 |
+
description="Upload your CSV data to train a product classifier model on CPU."
|
71 |
)
|
72 |
|
73 |
if __name__ == "__main__":
|
74 |
+
demo.launch(share=False)
|