# Train a model with autotrain on huggingface using the API

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrain-a-model-with-autotrain-on-huggingface-using-the-api%2F89428%3E%5C
**Category:** 🤗AutoTrain\
**Created:** [May 30, 2024, 1:53pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrain-a-model-with-autotrain-on-huggingface-using-the-api%2F89428 "2024-05-30T13:53:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chsafouane](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/chsafouane/32/26450_2.png) [@chsafouane](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fchsafouane)%5C
**Post date:** [May 30, 2024, 1:53pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrain-a-model-with-autotrain-on-huggingface-using-the-api%2F89428%2F1 "2024-05-30T13:53:23Z")

</div>

Hello,

Up to now, I’ve been using the autotrain ui (space) on huggingface to train models.

I would like to know if there is a way to start a model training using autotrain powered bu HF GPUs but only using the API.  
I’ve spotted a page in the documentation that talks about this but one has to start the app first: `autotrain app --port 8000 --host 127.0.0.1`

Is there a way to bypass this and directly train the model on HF ?

Thanks

---

<div class="post-metadata">

**Author:** ![abhishek](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/abhishek/32/32360_2.png) [@abhishek](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fabhishek)%5C
**Post date:** [May 30, 2024, 2:11pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrain-a-model-with-autotrain-on-huggingface-using-the-api%2F89428%2F2 "2024-05-30T14:11:15Z")

</div>

there are multiple ways.

1. you can use config files:

example config:

```auto
task: llm-sft
base_model: openai-community/gpt2
project_name: autotrain-gpt2-finetuned-guanaco
log: tensorboard
backend: local

data:
  path: timdettmers/openassistant-guanaco
  train_split: train
  valid_split: null
  chat_template: null
  column_mapping:
    text_column: text

params:
  block_size: 1024
  model_max_length: 2048
  max_prompt_length: 512
  epochs: 3
  batch_size: 2
  lr: 3e-5
  padding: right
  optimizer: adamw_torch
  scheduler: linear
  gradient_accumulation: 4
  mixed_precision: fp16

hub:
  username: ${HF_USERNAME}
  token: ${HF_TOKEN}
  push_to_hub: true

```

change backend from local to one of the spaces backends:

```auto
    "spaces-a10g-large": "a10g-large",
    "spaces-a10g-small": "a10g-small",
    "spaces-a100-large": "a100-large",
    "spaces-t4-medium": "t4-medium",
    "spaces-t4-small": "t4-small",
    "spaces-cpu-upgrade": "cpu-upgrade",
    "spaces-cpu-basic": "cpu-basic",
    "spaces-l4x1": "l4x1",
    "spaces-l4x4": "l4x4",
    "spaces-a10g-largex2": "a10g-largex2",
    "spaces-a10g-largex4": "a10g-largex4",

```

e.g. `backend: spaces-a10g-largex2`

This will start training directly on spaces.

1. API way:

start the app: `autotrain app --port 8000 --host 127.0.0.1`  
you can see api docs at 127.0.0.1/docs. api endpoint to create project is available at /api/create\_project

Note: local datasets are not supported in api mode.

please let me know if you have any questions.

---

<div class="post-metadata">

**Author:** ![chsafouane](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/chsafouane/32/26450_2.png) [@chsafouane](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fchsafouane)%5C
**Post date:** [May 30, 2024, 2:48pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrain-a-model-with-autotrain-on-huggingface-using-the-api%2F89428%2F3 "2024-05-30T14:48:16Z")

</div>

> [@abhishek](#):
>
> ` "spaces-a10g-large"`

It works like a charm.Thank you

I guess the next think I’d like to know is if there is a way to do it using python directly by passing a dict that includes all parameters (as I see that it parses a json containing the configuration under the hood)

---

<div class="post-metadata">

**Author:** ![abhishek](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/abhishek/32/32360_2.png) [@abhishek](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fabhishek)%5C
**Post date:** [May 30, 2024, 4:03pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrain-a-model-with-autotrain-on-huggingface-using-the-api%2F89428%2F4 "2024-05-30T16:03:40Z")

</div>

you can also use the autotrain cli, im pushing some improvements there so ill update here when its done 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/hellohellohello/original/2X/d/de4155eb4aa4108ecb32a1389d7cc37ae69f88b7.png) [@system](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsystem)%5C
**Post date:** [May 31, 2024, 4:04am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrain-a-model-with-autotrain-on-huggingface-using-the-api%2F89428%2F5 "2024-05-31T04:04:33Z")

</div>

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
