Update train_model.py
Browse files- train_model.py +15 -15
train_model.py
CHANGED
|
@@ -9,18 +9,18 @@ from transformers import (
|
|
| 9 |
DataCollatorForLanguageModeling
|
| 10 |
)
|
| 11 |
|
| 12 |
-
# β
Load token from secret
|
| 13 |
hf_token = os.environ.get("data3")
|
| 14 |
if not hf_token:
|
| 15 |
-
raise ValueError("Hugging Face token not found. Make sure 'data3' is
|
| 16 |
|
| 17 |
-
# β
Authenticate
|
| 18 |
login(token=hf_token)
|
| 19 |
|
| 20 |
-
# β
Load
|
| 21 |
df = pd.read_csv("custom_captions.csv")
|
| 22 |
|
| 23 |
-
# β
Build training prompt
|
| 24 |
def to_prompt(row):
|
| 25 |
return f"""Photo: {row['photo']}
|
| 26 |
Scenery: {row['scenery']}
|
|
@@ -29,38 +29,38 @@ Vibe to convey: {row['desired_vibe']}
|
|
| 29 |
Captions user likes: "{row['liked_captions']}"
|
| 30 |
"""
|
| 31 |
|
| 32 |
-
# β
|
| 33 |
df["text"] = df.apply(lambda row: to_prompt(row) + row["caption"], axis=1)
|
| 34 |
|
| 35 |
-
# β
Convert to Hugging Face Dataset
|
| 36 |
dataset = Dataset.from_pandas(df[["text"]])
|
| 37 |
|
| 38 |
-
# β
Load tokenizer and model
|
| 39 |
tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
| 40 |
-
tokenizer.pad_token = tokenizer.eos_token #
|
| 41 |
model = AutoModelForCausalLM.from_pretrained("gpt2")
|
| 42 |
|
| 43 |
-
# β
|
| 44 |
def tokenize(example):
|
| 45 |
return tokenizer(example["text"], truncation=True, padding="max_length", max_length=64)
|
| 46 |
|
| 47 |
tokenized = dataset.map(tokenize, batched=True)
|
| 48 |
|
| 49 |
-
# β
Data collator
|
| 50 |
data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False)
|
| 51 |
|
| 52 |
-
# β
|
| 53 |
training_args = TrainingArguments(
|
| 54 |
output_dir="./results",
|
| 55 |
per_device_train_batch_size=2,
|
| 56 |
num_train_epochs=1,
|
| 57 |
save_strategy="epoch",
|
| 58 |
push_to_hub=True,
|
| 59 |
-
hub_model_id="yuvalon/caption-crafter-model", # β
|
| 60 |
hub_token=hf_token
|
| 61 |
)
|
| 62 |
|
| 63 |
-
# β
Trainer
|
| 64 |
trainer = Trainer(
|
| 65 |
model=model,
|
| 66 |
args=training_args,
|
|
@@ -69,6 +69,6 @@ trainer = Trainer(
|
|
| 69 |
data_collator=data_collator
|
| 70 |
)
|
| 71 |
|
| 72 |
-
# β
Train and
|
| 73 |
trainer.train()
|
| 74 |
trainer.push_to_hub()
|
|
|
|
| 9 |
DataCollatorForLanguageModeling
|
| 10 |
)
|
| 11 |
|
| 12 |
+
# β
1. Load Hugging Face token from secret
|
| 13 |
hf_token = os.environ.get("data3")
|
| 14 |
if not hf_token:
|
| 15 |
+
raise ValueError("Hugging Face token not found. Make sure 'data3' is set in your Space secrets.")
|
| 16 |
|
| 17 |
+
# β
2. Authenticate
|
| 18 |
login(token=hf_token)
|
| 19 |
|
| 20 |
+
# β
3. Load structured caption dataset
|
| 21 |
df = pd.read_csv("custom_captions.csv")
|
| 22 |
|
| 23 |
+
# β
4. Build training prompt
|
| 24 |
def to_prompt(row):
|
| 25 |
return f"""Photo: {row['photo']}
|
| 26 |
Scenery: {row['scenery']}
|
|
|
|
| 29 |
Captions user likes: "{row['liked_captions']}"
|
| 30 |
"""
|
| 31 |
|
| 32 |
+
# β
5. Combine prompt + final caption
|
| 33 |
df["text"] = df.apply(lambda row: to_prompt(row) + row["caption"], axis=1)
|
| 34 |
|
| 35 |
+
# β
6. Convert to Hugging Face Dataset
|
| 36 |
dataset = Dataset.from_pandas(df[["text"]])
|
| 37 |
|
| 38 |
+
# β
7. Load tokenizer and base model
|
| 39 |
tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
| 40 |
+
tokenizer.pad_token = tokenizer.eos_token # Avoids padding issues
|
| 41 |
model = AutoModelForCausalLM.from_pretrained("gpt2")
|
| 42 |
|
| 43 |
+
# β
8. Tokenize training text
|
| 44 |
def tokenize(example):
|
| 45 |
return tokenizer(example["text"], truncation=True, padding="max_length", max_length=64)
|
| 46 |
|
| 47 |
tokenized = dataset.map(tokenize, batched=True)
|
| 48 |
|
| 49 |
+
# β
9. Data collator (for causal LM)
|
| 50 |
data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False)
|
| 51 |
|
| 52 |
+
# β
10. Training configuration β using your model repo
|
| 53 |
training_args = TrainingArguments(
|
| 54 |
output_dir="./results",
|
| 55 |
per_device_train_batch_size=2,
|
| 56 |
num_train_epochs=1,
|
| 57 |
save_strategy="epoch",
|
| 58 |
push_to_hub=True,
|
| 59 |
+
hub_model_id="yuvalon/caption-crafter-model", # β
Match the repo you created manually
|
| 60 |
hub_token=hf_token
|
| 61 |
)
|
| 62 |
|
| 63 |
+
# β
11. Trainer setup
|
| 64 |
trainer = Trainer(
|
| 65 |
model=model,
|
| 66 |
args=training_args,
|
|
|
|
| 69 |
data_collator=data_collator
|
| 70 |
)
|
| 71 |
|
| 72 |
+
# β
12. Train and upload to Hugging Face
|
| 73 |
trainer.train()
|
| 74 |
trainer.push_to_hub()
|