Download train_model.py from YUVALON/TrialApp2: direct link, hf CLI and curl.
- Browser
- Download file 2.2 kB
-
https://huggingface.co/spaces/YUVALON/TrialApp2/resolve/main/train_model.py
- Command line
-
hf download hf://spaces/YUVALON/TrialApp2/train_model.py
-
curl -L -o train_model.py https://huggingface.co/spaces/YUVALON/TrialApp2/resolve/main/train_model.py
2.2 kB
| import os | |
| import pandas as pd | |
| from huggingface_hub import login, whoami | |
| from datasets import Dataset | |
| from transformers import ( | |
| AutoTokenizer, AutoModelForCausalLM, | |
| TrainingArguments, Trainer, | |
| DataCollatorForLanguageModeling | |
| ) | |
| # β 1. Load Hugging Face token securely | |
| hf_token = os.environ.get("data3") | |
| if not hf_token: | |
| raise ValueError("Missing HF token. Set 'data3' as a secret in your Space or Colab.") | |
| # β 2. Log in to Hugging Face | |
| login(token=hf_token) | |
| # β 3. Get your username dynamically | |
| username = whoami(token=hf_token)["name"] | |
| # β 4. Load your caption data | |
| df = pd.read_csv("custom_captions.csv") | |
| # β 5. Format prompt | |
| def to_prompt(row): | |
| return f"""Photo: {row['photo']} | |
| Scenery: {row['scenery']} | |
| Photo vibe: {row['photo_vibe']} | |
| Vibe to convey: {row['desired_vibe']} | |
| Captions user likes: "{row['liked_captions']}" | |
| """ | |
| # β 6. Build full training text | |
| df["text"] = df.apply(lambda row: to_prompt(row) + row["caption"], axis=1) | |
| # β 7. Convert to Hugging Face Dataset | |
| dataset = Dataset.from_pandas(df[["text"]]) | |
| # β 8. Load tokenizer and model (GPT-Neo) | |
| tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neo-125M") | |
| tokenizer.pad_token = tokenizer.eos_token # Required fix for GPT-style models | |
| model = AutoModelForCausalLM.from_pretrained("EleutherAI/gpt-neo-125M") | |
| # β 9. Tokenize the dataset | |
| def tokenize(example): | |
| return tokenizer(example["text"], truncation=True, padding="max_length", max_length=64) | |
| tokenized = dataset.map(tokenize, batched=True) | |
| # β 10. Prepare data collator | |
| data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False) | |
| # β 11. Set training arguments | |
| training_args = TrainingArguments( | |
| output_dir="./results", | |
| per_device_train_batch_size=2, | |
| num_train_epochs=1, | |
| save_strategy="epoch", | |
| push_to_hub=True, | |
| hub_model_id=f"{username}/caption-crafter-model", # Dynamic + personalized | |
| hub_token=hf_token | |
| ) | |
| # β 12. Set up Trainer | |
| trainer = Trainer( | |
| model=model, | |
| args=training_args, | |
| train_dataset=tokenized, | |
| tokenizer=tokenizer, | |
| data_collator=data_collator | |
| ) | |
| # β 13. Train and push to Hugging Face Hub | |
| trainer.train() | |
| trainer.push_to_hub() | |