# Generating Videos From Generated Images with Moderation

Source: https://docs.prodia.com/workflows/generating-videos-from-generated-images-with-moderation/

### Project Setup

```bash
# Create a project directory.
mkdir prodia-video-generation-from-generated-images
cd prodia-video-generation-from-generated-images
```

### prodia-js

Install Node (if not already installed):

### macOS

```bash
brew install node
# Close the current terminal and open a new one so that node is available.
```

### Linux

```bash
apt install node
# Close the current terminal and open a new one so that node is available.
```

### Windows

```bash
winget install -e --id OpenJS.NodeJS.LTS
# Close the current terminal and open a new one so that node is available.
```

Create project skeleton:

```bash
# Requires node --version >= 18
# Initialize the project with npm.
npm init -y

# Install the prodia-js library.
npm install prodia --save
```

### requests

Install Python (if not already installed):

### macOS

```bash
brew install python
# Close the current terminal and open a new one so that python is available.
```

### Linux

```bash
apt install python3 python3-venv python-is-python3
# Close the current terminal and open a new one so that python is available.
```

### Windows

```bash
winget install -e --id Python.Python.3.12
# Close the current terminal and open a new one so that python is available.
```

```bash
# Requires python --version >= 3.12
python -m venv venv
source venv/bin/activate
pip install requests
```

### curl

Install curl (if not already installed):

### macOS

```bash
brew install curl
# Close the current terminal and open a new one so that curl is available.
```

### Linux

```bash
apt install curl
# Close the current terminal and open a new one so that curl is available.
```

### Windows

```bash
# NOTE: Windows 10 and up have curl installed by default and this can be
# skipped.
winget install -e --id cURL.cURL
# Close the current terminal and open a new one so that curl is available.
```

```bash
# Export your token so it can be used by the main code.
export PRODIA_TOKEN=your-token-here
```

> note:
>
> Your token is exported to an environment variable. If you close or switch your
> shell you'll need to run `export PRODIA_TOKEN=your-token-here` again.

Create a main file for your project:

### prodia-js

```js title="main.js"
const { createProdia } = require("prodia/v2");

const prodia = createProdia({
    token: process.env.PRODIA_TOKEN // get it from environment
});
```

### requests

Create the following `main.py`

```python title="main.py"
from requests.adapters import HTTPAdapter, Retry
import os
import requests
import sys


prodia_token = os.getenv('PRODIA_TOKEN')
prodia_url = 'https://inference.prodia.com/v2/job'

session = requests.Session()
retries = Retry(allowed_methods=None, status_forcelist=Retry.RETRY_AFTER_STATUS_CODES)
session.mount('http://', HTTPAdapter(max_retries=retries))
session.mount('https://', HTTPAdapter(max_retries=retries))
session.headers.update({'Authorization': f"Bearer {prodia_token}"})
```

### curl

```bash title="main.sh"
set -euo pipefail
```

You're now ready to make some API calls!

### Generate an image (inside a workflow)

### prodia-js

```javascript
const { createProdia } = require("prodia/v2");
const fs = require("node:fs/promises"); // add this to imports at the top

const prodia = createProdia({
  token: process.env.PRODIA_TOKEN, // get it from environment
});

(async () => {
  const job = await prodia.job({
    type: "workflow.serial.v1",
    config: {
      jobs: [
        {
          type: "inference.flux-fast.schnell.txt2img.v2",
          config: {
            prompt: "puppies in a cloud, 4k",
          },
        },
      ],
    },
  });

  const image = await job.arrayBuffer();

  await fs.writeFile("puppies.jpg", new Uint8Array(image));
  // open puppies.jpg
})();
```

```bash
node main.js
```

### requests

```python
from requests.adapters import HTTPAdapter, Retry
import os
import requests
import sys


prodia_token = os.getenv('PRODIA_TOKEN')
prodia_url = 'https://inference.prodia.com/v2/job'

session = requests.Session()
retries = Retry(allowed_methods=None, status_forcelist=Retry.RETRY_AFTER_STATUS_CODES)
session.mount('http://', HTTPAdapter(max_retries=retries))
session.mount('https://', HTTPAdapter(max_retries=retries))
session.headers.update({'Authorization': f"Bearer {prodia_token}"})

headers = {
    'Accept': 'image/png',
}

job = {
    'type': 'workflow.serial.v1',
    'config': {
        'jobs': [
            {
                'type': 'inference.flux-fast.schnell.txt2img.v2',
                'config': {
                    'prompt': 'puppies in a cloud, 4k',
                },
            },
        ],
    },
}

res = session.post(prodia_url, headers=headers, json=job)
print(f"Request ID: {res.headers['x-request-id']}")
print(f"Status: {res.status_code}")

if res.status_code != 200:
    print(res.text)
    sys.exit(1)

with open('puppies.jpg', 'wb') as f:
    f.write(res.content)
```

```bash
python main.py
```

### curl

```bash
set -euo pipefail

job=$(cat <<EOF
{
  "type": "workflow.serial.v1",
  "config": {
    "jobs": [
      {
        "type": "inference.flux-fast.schnell.txt2img.v2",
        "config": {
          "prompt": "puppies in a cloud, 4k"
        }
      }
    ]
  }
}
EOF
)

curl -sSf \
  -H "Authorization: Bearer $PRODIA_TOKEN" \
  -H 'Accept: image/jpeg' \
  --json "$job" \
  --output puppies.jpg \
  --retry 3 \
  https://inference.prodia.com/v2/job
```

```bash
bash main.sh
```

### macOS

```bash
open puppies.jpg
```

### Linux

```bash
xdg-open puppies.jpg
```

### Windows

```bash
start puppies.jpg
```

### Adding content moderation

### prodia-js

```javascript
const { createProdia } = require("prodia/v2");
const fs = require("node:fs/promises"); // add this to imports at the top

const prodia = createProdia({
  token: process.env.PRODIA_TOKEN, // get it from environment
});

(async () => {
  const job = await prodia.job({
    type: "workflow.serial.v1",
    config: {
      jobs: [
        {
          type: "inference.flux-fast.schnell.txt2img.v2",
          config: {
            prompt: "puppies in a cloud, 4k",
          },
        },
        {
          type: "workflow.moderate.v1",
          config: {
            threshold: 0.95,
          },
        },
      ],
    },
  });

  const image = await job.arrayBuffer();

  await fs.writeFile("puppies.jpg", new Uint8Array(image));
  // open puppies.jpg
})();
```

```bash
node main.js
```

### requests

```python
from requests.adapters import HTTPAdapter, Retry
import os
import requests
import sys


prodia_token = os.getenv('PRODIA_TOKEN')
prodia_url = 'https://inference.prodia.com/v2/job'

session = requests.Session()
retries = Retry(allowed_methods=None, status_forcelist=Retry.RETRY_AFTER_STATUS_CODES)
session.mount('http://', HTTPAdapter(max_retries=retries))
session.mount('https://', HTTPAdapter(max_retries=retries))
session.headers.update({'Authorization': f"Bearer {prodia_token}"})

headers = {
    'Accept': 'image/png',
}

job = {
    'type': 'workflow.serial.v1',
    'config': {
        'jobs': [
            {
                'type': 'inference.flux-fast.schnell.txt2img.v2',
                'config': {
                    'prompt': 'puppies in a cloud, 4k',
                },
            },
            {
                'type': 'workflow.moderate.v1',
                'config': {
                    'threshold': 0.95,
                },
            },
        ],
    },
}

res = session.post(prodia_url, headers=headers, json=job)
print(f"Request ID: {res.headers['x-request-id']}")
print(f"Status: {res.status_code}")

if res.status_code != 200:
    print(res.text)
    sys.exit(1)

with open('puppies.jpg', 'wb') as f:
    f.write(res.content)
```

```bash
python main.py
```

### curl

```bash
set -euo pipefail

job=$(cat <<EOF
{
  "type": "workflow.serial.v1",
  "config": {
    "jobs": [
      {
        "type": "inference.flux-fast.schnell.txt2img.v2",
        "config": {
          "prompt": "puppies in a cloud, 4k"
        }
      },
      {
        "type": "workflow.moderate.v1",
        "config": {
          "threshold": 0.95
        }
      }
    ]
  }
}
EOF
)

curl -sSf \
  -H "Authorization: Bearer $PRODIA_TOKEN" \
  -H 'Accept: image/jpeg' \
  --json "$job" \
  --output puppies.jpg \
  --retry 3 \
  https://inference.prodia.com/v2/job
```

```bash
bash main.sh
```

### macOS

```bash
open puppies.jpg
```

### Linux

```bash
xdg-open puppies.jpg
```

### Windows

```bash
start puppies.jpg
```

### Generate video

### prodia-js

```javascript
const { createProdia } = require("prodia/v2");
const fs = require("node:fs/promises"); // add this to imports at the top

const prodia = createProdia({
  token: process.env.PRODIA_TOKEN, // get it from environment
});

(async () => {
  const job = await prodia.job({
    type: "workflow.serial.v1",
    config: {
      jobs: [
        {
          type: "inference.flux-fast.schnell.txt2img.v2",
          config: {
            prompt: "puppies in a cloud, 4k",
          },
        },
        {
          type: "workflow.moderate.v1",
          config: {
            threshold: 0.95,
          },
        },
        {
          type: "inference.seedance.pro.img2vid.v1",
          config: {
            prompt: "puppies playing in the clouds",
          },
        },
      ],
    },
  });

  const video = await job.arrayBuffer();

  await fs.writeFile("puppies.mp4", new Uint8Array(video));
  // open puppies.mp4
})();
```

```bash
node main.js
```

### requests

```python
from requests.adapters import HTTPAdapter, Retry
import os
import requests
import sys


prodia_token = os.getenv('PRODIA_TOKEN')
prodia_url = 'https://inference.prodia.com/v2/job'

session = requests.Session()
retries = Retry(allowed_methods=None, status_forcelist=Retry.RETRY_AFTER_STATUS_CODES)
session.mount('http://', HTTPAdapter(max_retries=retries))
session.mount('https://', HTTPAdapter(max_retries=retries))
session.headers.update({'Authorization': f"Bearer {prodia_token}"})

headers = {
    'Accept': 'video/mp4',
}

job = {
    'type': 'workflow.serial.v1',
    'config': {
        'jobs': [
            {
                'type': 'inference.flux-fast.schnell.txt2img.v2',
                'config': {
                    'prompt': 'puppies in a cloud, 4k',
                },
            },
            {
                'type': 'workflow.moderate.v1',
                'config': {
                    'threshold': 0.95,
                },
            },
            {
                'type': 'inference.seedance.pro.img2vid.v1',
                'config': {
                    'prompt': 'puppies playing in the clouds',
                },
            },
        ],
    },
}

res = session.post(prodia_url, headers=headers, json=job)
print(f"Request ID: {res.headers['x-request-id']}")
print(f"Status: {res.status_code}")

if res.status_code != 200:
    print(res.text)
    sys.exit(1)

with open('puppies.mp4', 'wb') as f:
    f.write(res.content)
```

```bash
python main.py
```

### curl

```bash
set -euo pipefail

job=$(cat <<EOF
{
  "type": "workflow.serial.v1",
  "config": {
    "jobs": [
      {
        "type": "inference.flux-fast.schnell.txt2img.v2",
        "config": {
          "prompt": "puppies in a cloud, 4k"
        }
      },
      {
        "type": "workflow.moderate.v1",
        "config": {
          "threshold": 0.95
        }
      },
      {
        "type": "inference.seedance.pro.img2vid.v1",
        "config": {
          "prompt": "puppies playing in the clouds"
        }
      }
    ]
  }
}
EOF
)

curl -sSf \
  -H "Authorization: Bearer $PRODIA_TOKEN" \
  -H 'Accept: video/mp4' \
  --json "$job" \
  --output puppies.mp4 \
  --retry 3 \
  https://inference.prodia.com/v2/job
```

```bash
bash main.sh
```

### macOS

```bash
open puppies.mp4
```

### Linux

```bash
xdg-open puppies.mp4
```

### Windows

```bash
start puppies.mp4
```
