|
| 1 | +import axios from 'axios' |
| 2 | +import { Request, Response, NextFunction } from 'express' |
| 3 | + |
| 4 | +const { NimContainerManager } = require('nim-container-manager') |
| 5 | + |
| 6 | +const getToken = async (req: Request, res: Response, next: NextFunction) => { |
| 7 | + try { |
| 8 | + const headers = { |
| 9 | + 'Content-Type': 'application/json', |
| 10 | + Accept: 'application/json' |
| 11 | + } |
| 12 | + const data = { |
| 13 | + client_id: 'Flowise', |
| 14 | + pdi: '0x1234567890abcdeg', |
| 15 | + access_policy_name: 'nim-dev' |
| 16 | + } |
| 17 | + const response = await axios.post('https://nts.ngc.nvidia.com/v1/token', data, { headers }) |
| 18 | + const responseJson = response.data |
| 19 | + return res.json(responseJson) |
| 20 | + } catch (error) { |
| 21 | + next(error) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +const preload = async (req: Request, res: Response, next: NextFunction) => { |
| 26 | + try { |
| 27 | + await NimContainerManager.preload() |
| 28 | + return res.send('Preloaded NIM') |
| 29 | + } catch (error) { |
| 30 | + next(error) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +const downloadInstaller = async (req: Request, res: Response, next: NextFunction) => { |
| 35 | + try { |
| 36 | + await NimContainerManager.downloadInstaller() |
| 37 | + return res.send('NIM Installer completed successfully!') |
| 38 | + } catch (error) { |
| 39 | + next(error) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +const pullImage = async (req: Request, res: Response, next: NextFunction) => { |
| 44 | + try { |
| 45 | + const imageTag = req.body.imageTag |
| 46 | + const apiKey = req.body.apiKey |
| 47 | + await NimContainerManager.pullImage(imageTag, apiKey) |
| 48 | + return res.send(`Pulling image ${imageTag}`) |
| 49 | + } catch (error) { |
| 50 | + next(error) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +const startContainer = async (req: Request, res: Response, next: NextFunction) => { |
| 55 | + try { |
| 56 | + const imageTag = req.body.imageTag |
| 57 | + const apiKey = req.body.apiKey |
| 58 | + await NimContainerManager.startContainer(imageTag, apiKey) |
| 59 | + return res.send(`Starting container ${imageTag}`) |
| 60 | + } catch (error) { |
| 61 | + next(error) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +const getImage = async (req: Request, res: Response, next: NextFunction) => { |
| 66 | + try { |
| 67 | + const imageTag = req.body.imageTag |
| 68 | + const images = await NimContainerManager.userImageLibrary() |
| 69 | + const image = images.find((img: any) => img.tag === imageTag) |
| 70 | + if (!image) { |
| 71 | + return res.status(404).send(`Image ${imageTag} not found`) |
| 72 | + } |
| 73 | + return res.json(image) |
| 74 | + } catch (error) { |
| 75 | + next(error) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +const getContainer = async (req: Request, res: Response, next: NextFunction) => { |
| 80 | + try { |
| 81 | + const imageTag = req.body.imageTag |
| 82 | + const images = await NimContainerManager.userImageLibrary() |
| 83 | + const image = images.find((img: any) => img.tag === imageTag) |
| 84 | + if (!image) { |
| 85 | + return res.status(404).send(`Image ${imageTag} not found`) |
| 86 | + } |
| 87 | + if (!image.container) { |
| 88 | + return res.status(404).send(`Container of ${imageTag} not found`) |
| 89 | + } |
| 90 | + const container = image.container |
| 91 | + container.image = image.name |
| 92 | + return res.json(container) |
| 93 | + } catch (error) { |
| 94 | + next(error) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +export default { |
| 99 | + preload, |
| 100 | + getToken, |
| 101 | + downloadInstaller, |
| 102 | + pullImage, |
| 103 | + startContainer, |
| 104 | + getImage, |
| 105 | + getContainer |
| 106 | +} |
0 commit comments