# Flux Kontext fine tuning API

This documentation outlines the API endpoints for initiating and managing Flux Kontext fine-tuning requests in Segmind.

### Base URL

```
https://api.segmind.com
```

### Authentication

All requests require an API key for authentication. Include the API key in the headers as follows:

```
--header 'x-api-key: YOUR_API_KEY'
```

### 1. Initiate Fine-Tune Request

#### Description

Initiate a new fine-tuning request.

Request

**Headers**

* `x-api-key`: Your API key.
* `Content-Type`: Should be `application/json`.

**Dataset (data\_source\_path)**

* `data_source_path`: A URL pointing to a ZIP file containing your training dataset.\
  `Purpose`: Specifies the dataset source for fine-tuning. Must be a valid public URL or a private Segmind URL.\
  `Options`: "Public ZIP URL (must support GET & HEAD requests)", "Segmind Private URL using Presigned"\
  `Descriptions`:\
  \- `Public ZIP URL`: Must be directly accessible and respond properly to both HEAD and GET requests with headers like Content-Length.\
  \- `Segmind Presigned URL`: Use Segmind Presigned URL endpoint to upload as private zip file

**GPU Selection**

* `machine_type: Specifies the GPU used for the fine-tuning job`\
  `Purpose: Defines the hardware performance tier for training in request submit endpoint`\
  `Options: "NVIDIA_H100"`\
  `Descriptions:`\
  `- NVIDIA_H100 (Fastest training)` \\

**Body**

The request body must be in JSON format.

**Request**

```
curl --location 'https://api.segmind.com/finetune/request/submit' \
--header 'x-api-key: YOUR_API_KEY' \
--header 'Content-Type: application/json' \
--data '{
    "name": "fluxkontexttest1",
    "data_source_path": "Segmind-hosted Private ZIP URL (uploaded via presigned link)" or "any public zip url",
    "instance_prompt": "Change the woman's hair color to bright red",
    "base_model": "FLUX",
    "train_type": "LORA",
    "machine_type": "NVIDIA_H100",
    "theme": "KONTEXT",
    "segmind_public": false,
    "advance_parameters": {
        "learning_rate": 0.0004,
        "steps": 300
    }
}'
```

#### Sample Response

```
{
    "status": "REQUESTED",
    "finetune_id": "uuid",
    "name": "fine-tune-job-name"
}
```

### 2. Get the Details of Individual Fine-Tune Request

#### Description

Retrieve a fine-tuning request along with details.

**Request**

```
curl --location --request GET 'https://api.segmind.com/finetune/request/details' \
--header 'x-api-key: YOUR_API_KEY' \
--header 'Content-Type: application/json' \
--data '{
    "request_id": "FINETUNE_ID"
}'
```

#### Sample Response

```
{
  "finetune": {
    "id": "uuid",
    "finetune_id": "uuid",
    "name": "fine-tune-job-name",
    "data_source_path": "https://bucket.s3.amazonaws.com/path/to/dataset.zip",
    "instance_prompt": "sample instance prompt",
    "status": "AVAILABLE",
    "source_type": "AWS_S3",
    "base_model": "BASE_MODEL_NAME",
    "slug": "model-slug",
    "public_model": false,
    "error_message": null,
    "cloud_storage_url": "https://your-bucket.s3.amazonaws.com/path/to/model.safetensors",
    "created_ts": "2025-01-01T00:00:00Z",
    "updated_ts": "2025-01-01T00:00:00Z"
  }
}
```

### 3. Get the List of Fine-Tune Requests

#### Description

Retrieve a list of fine-tuning requests along with their details.

**Request**

```
curl --location 'https://api.segmind.com/finetune/request/list' \
--header 'x-api-key: YOUR_API_KEY'
```

#### Sample Response

```
[
  {
    "id": "uuid",
    "data_source_path": "https://your-bucket.s3.amazonaws.com/path/to/dataset.zip",
    "name": "fine-tune-job-name",
    "status": "AVAILABLE",
    "error_message": null,
    "segmind_model_path": null,
    "advance_parameters": {
      "steps": 10,
      "learning_rate": 0.0001,
      "prompt": "sample prompt",
      "theme": "sample-theme"
    },
    "segmind_public_model": false,
    "train_type": "LORA",
    "source_type": "AWS_S3",
    "base_model": "BASE_MODEL_NAME",
    "theme": "sample-theme",
    "cloud_storage_url": null,
    "finetune_id": "uuid",
    "model_information": {}
  }
]
```

### 4. Get Fine-Tune Data Upload Pre-Signed URL

#### Description

Obtain a pre-signed URL to securely upload fine-tuning data to cloud storage. This URL allows you to upload data directly from your local system or application without needing AWS credentials.

#### Usage

Call this endpoint to generate a temporary pre-signed URL. Use the returned URL to upload your dataset file to the specified location via a PUT request.

#### Request

```
curl --location --request GET 'https://api.segmind.com/finetune/request/upload/pre-signed-url' \
--header 'x-api-key: YOUR_API_KEY' \
--header 'Content-Type: application/json' \
--data '{
    "name": "NAME_OF_THE_FILE"
}'
```

#### Sample Response

```
{
  "presigned_url": "https://finetune-pipeline.s3.amazonaws.com/uploads/{user_id}/{file_id}-{filename}.zip?X-Amz-Algorithm=...&X-Amz-Signature=...",
  "s3_url": "https://finetune-pipeline.s3.amazonaws.com/uploads/{user_id}/{file_id}-{filename}.zip"
}
```

### 5. Update Fine-Tuned Model Access

#### Description

Update the access settings of a fine-tuned model (public/private).

#### Request

```
curl --location --request PUT 'https://api.segmind.com/finetune/request/access-update' \
--header 'x-api-key: YOUR_API_KEY' \
--form 'request_id="FINETUNE_REQUEST_ID"' \
--form 'segmind_public="True"'
```

#### Sample Response

```
200
```

### 6. Download the Fine-Tuned Safetensor File

#### Description

Generate a time-limited pre-signed URL to securely download the fine-tuned model file (.safetensors) from cloud storage. The URL is valid for 1 hour and allows direct download without requiring AWS credentials.

#### Request

```
curl --location --request GET 'https://api.segmind.com/finetune/request/file/download' \
--header 'x-api-key: YOUR_API_KEY' \
--form 'cloud_storage_url="CLOUD_STORAGE_URL"'
```

#### Sample Response

```
https://segmind-sd-models.s3.amazonaws.com/finetune/finetuned_models/job_id/filename.safetensors?AWSAccessKeyId=***&Signature=***&Expires=***
```

## Webhooks

Webhooks provide a way to get real-time updates about finetuning jobs programmatically. You can register a webhook for finetuning jobs in the Developer tab on console.

Once a webhook is created, test it, by having it send a sample payload to verify delivery. Once its up, create a finetuning job to receive status updates.\
\
Events are sent to webhooks on 3 status changes:\\

1. `TRAINING_COMPLETED` : The training is completed on model, and finetuned model is available for download on the `trained_model_url`.
2. `INFERENCE_QUEUED`: Model is being deployed on Segmind inference engine.
3. `AVAILABLE`: Model is deployed, and ready for inferences on `inference_api_url`

Note: You can create only 1 webhook at a time for finetune jobs. If you want to change the webhook, please delete the old webhook before creating a new one.
