forked from pytorch-ignite/code-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolab.js
86 lines (74 loc) · 2.18 KB
/
colab.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { pushToGitHub, getZip_Uid } from './utils'
const repoOwner = process.env.VUE_APP_GH_USER
const repo = process.env.VUE_APP_GH_REPO
// This function is the one Netlify function runs on
// https://docs.netlify.com/functions/build-with-javascript/#synchronous-function-format
exports.handler = async function (event, _) {
// event is a JSON object
const data = JSON.parse(event.body)
const template = `ignite-${data.template}`
const nbName = `${template}.ipynb`
const { zipRes, nbUid } = await getZip_Uid(data)
const title = template
.replace('ignite-', '')
.split('-')
.map((v) => v[0].toUpperCase() + v.slice(1))
.join(' ')
// notebook cell structure
function create_nb_cell(source_array, cell_type) {
return {
cell_type: cell_type,
metadata: {},
execution_count: null,
outputs: [],
source: source_array
}
}
let specific_commands = []
if (title === 'Template Vision Segmentation') {
specific_commands.push(
'!python -c "from data import download_datasets; download_datasets(\'./\')"'
)
}
const md_cell = [
`# ${title} by PyTorch-Ignite Code-Generator\n\n`,
'Please, run the cell below to execute your code.'
]
const common_nb_commands = [
`!wget ${zipRes}\n`,
`!unzip ${template}.zip\n`,
'!pip install -r requirements.txt'
]
const execution_nb_commands = ['!python main.py config.yaml']
let nb_cells = [
create_nb_cell(md_cell, 'markdown'),
create_nb_cell(common_nb_commands, 'code')
]
if (specific_commands.length > 0) {
nb_cells.push(create_nb_cell(specific_commands, 'code'))
}
nb_cells.push(create_nb_cell(execution_nb_commands, 'code'))
const nb = {
nbformat: 4,
nbformat_minor: 0,
metadata: {
kernelspec: {
display_name: 'Python 3',
name: 'python3'
},
accelerator: 'GPU'
},
cells: nb_cells
}
// Create the notebook on GitHub
await pushToGitHub(
Buffer.from(JSON.stringify(nb)).toString('base64'),
nbName,
nbUid
)
const colabLink = `https://colab.research.google.com/github/${repoOwner}/${repo}/blob/main/nbs/${nbUid}/${nbName}`
return {
statusCode: 200,
body: colabLink
}
}