-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
314 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Below imports are defined in | ||
// `external_node_modules` of [functions] in netlify.toml | ||
// They are required for this function to run | ||
|
||
import { v4 as uuidv4 } from 'uuid' | ||
import { Octokit } from '@octokit/core' | ||
import JSZip from 'jszip' | ||
|
||
const nbUid = uuidv4() | ||
const repoOwner = process.env.VUE_APP_GH_USER | ||
const repo = process.env.VUE_APP_GH_REPO | ||
|
||
/** | ||
* Create a file on GitHub with Octokit. | ||
* @param {string} content | ||
* @param {string} filename | ||
* @returns download_url | ||
*/ | ||
async function pushToGitHub(content, filename) { | ||
const octokit = new Octokit({ | ||
auth: process.env.VUE_APP_GH_TOKEN | ||
}) | ||
try { | ||
const res = await octokit.request( | ||
'PUT /repos/{owner}/{repo}/contents/{path}', | ||
{ | ||
owner: repoOwner, | ||
repo: repo, | ||
path: `nbs/${nbUid}/${filename}`, | ||
message: `nb: add ${nbUid}`, | ||
content: content | ||
} | ||
) | ||
// the download url is raw url - https://raw.githubusercontent.com/... | ||
return res.data.content.download_url | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
} | ||
|
||
// This function is the one Netlify function runs on | ||
// https://docs.netlify.com/functions/build-with-javascript/#synchronous-function-format | ||
export async function handler(event, _) { | ||
// event is a JSON object | ||
const data = JSON.parse(event.body) | ||
const zip = new JSZip() | ||
const code = data.code | ||
const template = `ignite-${data.template}` | ||
const nbName = `${template}.ipynb` | ||
|
||
// As usual from Download component, | ||
// we will zip the files and | ||
// generate a base64 format for pushing to GitHub | ||
// with Octokit. | ||
for (const filename in code) { | ||
zip.file(filename, code[filename]) | ||
} | ||
const content = await zip.generateAsync({ type: 'base64' }) | ||
const zipRes = await pushToGitHub(content, `${template}.zip`) | ||
|
||
const title = template | ||
.replace('ignite-', '') | ||
.split('-') | ||
.map((v) => v[0].toUpperCase() + v.slice(1)) | ||
.join(' ') | ||
// notebook cell structure | ||
const nb = { | ||
nbformat: 4, | ||
nbformat_minor: 0, | ||
metadata: { | ||
kernelspec: { | ||
display_name: 'Python 3', | ||
name: 'python3' | ||
}, | ||
accelerator: 'GPU' | ||
}, | ||
cells: [ | ||
{ | ||
cell_type: 'markdown', | ||
metadata: {}, | ||
execution_count: null, | ||
outputs: [], | ||
source: [ | ||
`# ${title} by PyTorch-Ignite Code-Generator\n\n`, | ||
'Please, run the cell below to execute your code.' | ||
] | ||
}, | ||
{ | ||
cell_type: 'code', | ||
metadata: {}, | ||
execution_count: null, | ||
outputs: [], | ||
source: [ | ||
`!wget ${zipRes}\n`, | ||
`!unzip ${template}.zip\n`, | ||
'!pip install -r requirements.txt\n', | ||
'!python main.py\n' | ||
] | ||
} | ||
] | ||
} | ||
// Create the notebook on GitHub | ||
await pushToGitHub(Buffer.from(JSON.stringify(nb)).toString('base64'), nbName) | ||
|
||
const colabLink = `https://colab.research.google.com/github/${repoOwner}/${repo}/blob/main/nbs/${nbUid}/${nbName}` | ||
return { | ||
statusCode: 200, | ||
body: colabLink | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
[build.environment] | ||
NODE_VERSION = "14" | ||
PYTHON_VERSION = "3.7" | ||
AWS_LAMBDA_JS_RUNTIME = "nodejs14.x" | ||
|
||
[build] | ||
publish = "./dist" | ||
command = "npx pnpm i --frozen-lockfile --store=node_modules/.pnpm-store && npx pnpm run build" | ||
functions = "functions" | ||
|
||
[functions] | ||
node_bundler = "esbuild" | ||
external_node_modules = ["uuid", "jszip", "@octokit/core"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.