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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: p5.js Starting Your Critical AI Kit
|
| 3 |
+
author: Sarah Ciston
|
| 4 |
+
editors:
|
| 5 |
+
- Emily Martinez
|
| 6 |
+
- Minne Atairu
|
| 7 |
+
category: critical-ai
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
<!-- Play with different models: https://huggingface.co/chat/ -->
|
| 14 |
+
|
| 15 |
+
### X. [TO-DO] Get to know the terms and tools.
|
| 16 |
+
|
| 17 |
+
API:
|
| 18 |
+
Model:
|
| 19 |
+
Dataset:
|
| 20 |
+
[TO-DO]:
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
### X. [TO-DO] Create HuggingFace account.
|
| 24 |
+
|
| 25 |
+
[TO-DO][XXX]
|
| 26 |
+
|
| 27 |
+
### X. Create a Hugging Face Space.
|
| 28 |
+
|
| 29 |
+
A Hugging Face Space is just like a GitHub repo with GitHub Pages, except it's hosted by Hugging Face and already attached to its datasets, models, and API.
|
| 30 |
+
|
| 31 |
+
Visit `https://huggingface.co/new-space?`.
|
| 32 |
+
|
| 33 |
+
Name your Space. Maybe something like `p5jsCriticalAIKit` or `criticalAITutorial1`
|
| 34 |
+
|
| 35 |
+
Select `static` and then select a `Blank` template. Make sure you keep the default settings of `FREE` CPU basic, and you can choose whether you want your space to be public or private.
|
| 36 |
+
|
| 37 |
+
Your new Space should load on the `App` page, which is its web page. It should say `Running` at the top in green if it worked. Click on the drop down menu next to Running. Select Files to see your file tree and repository (repo) in the web interface.
|
| 38 |
+
|
| 39 |
+
![screenshot of new space app page]()
|
| 40 |
+
![screenshot of new space app page with file menu]()
|
| 41 |
+
|
| 42 |
+
Click "Add File" to make a new `sketch.js` file. Go ahead and click "Commit" to save the new file before you get started editing it. You can hit "Edit" to make changes to the file in your browser.
|
| 43 |
+
|
| 44 |
+
Alternately, you can clone the whole repository to work from your desktop. Refer to [p5.js Tutorial on Setup]() or [Dan Shiffman’s Hosting a p5.js Sketch with GitHub Pages](https://youtu.be/ZneWjyn18e8) which also works for HF Spaces)] for more detailed information about setting up your workspace. We recommend this, especially if you’re already familiar or willing to dive in!
|
| 45 |
+
|
| 46 |
+
### X. Add p5.js to the Space
|
| 47 |
+
|
| 48 |
+
Edit `index.html` to include the p5.js library by including this line inside the `<head></head> tags:
|
| 49 |
+
|
| 50 |
+
`<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.10.0/p5.min.js"></script>`
|
| 51 |
+
|
| 52 |
+
If you like, you can change the title of your page to your preference. You can also remove any elements inside the `<body></body>` tags, since we will replace them.
|
| 53 |
+
|
| 54 |
+
Next, update `index.html` to reference the `sketch.js` file we created. Add this line inside the `<body></body>` tags:
|
| 55 |
+
|
| 56 |
+
`<script type="module" src="sketch.js"></script>` // make sure it has the type attribute "module"
|
| 57 |
+
|
| 58 |
+
The script element importing the sketch file may be familiar to you. Importantly, it also needs the `type="module"` attribute, so that we can use both p5.js and other libraries in the file. Let's set that up next...
|
| 59 |
+
|
| 60 |
+
### X. Create a class instance of p5 in `sketch.js`.
|
| 61 |
+
|
| 62 |
+
Our p5.js Instance is basically a wrapper that allows us to hold all of our p5.js functions together in one place and label them, so that the program can recognize them as belonging to p5.js.
|
| 63 |
+
|
| 64 |
+
First we declare a `new p5()` class instance:
|
| 65 |
+
|
| 66 |
+
```javascript
|
| 67 |
+
new p5(function (p5) {
|
| 68 |
+
//
|
| 69 |
+
})
|
| 70 |
+
```
|
| 71 |
+
Then, all our usual p5.js coding will happen within these curly braces.
|
| 72 |
+
|
| 73 |
+
```js
|
| 74 |
+
new p5(function (p5) {
|
| 75 |
+
p5.setup = function(){
|
| 76 |
+
//
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
p5.draw = function(){
|
| 80 |
+
//
|
| 81 |
+
}
|
| 82 |
+
})
|
| 83 |
+
```
|
| 84 |
+
Important: When using any functions specific to p5.js, you will start them out with a label of whatever you called your p5.js instance. In this case we called it `p5` so our functions will be called `p5.setup()` and `p5.draw()` instead of the `setup()` and `draw()` you may recognize.
|
| 85 |
+
|
| 86 |
+
This will apply to any other function that is special to p5.js, like `p5.noCanvas`, but *not* to other functions which are standard to Javascript. Anything code written outside of the `new p5(){}` instance will not understand any p5.js syntax.
|
| 87 |
+
|
| 88 |
+
Let's add the instance mode version of `p5.noCanvas()` because we will be working directly with the DOM and don't need a canvas.
|
| 89 |
+
|
| 90 |
+
```js
|
| 91 |
+
new p5(function (p5) {
|
| 92 |
+
p5.setup = function(){
|
| 93 |
+
p5.noCanvas()
|
| 94 |
+
console.log('p5 instance loaded')
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
p5.draw = function(){
|
| 98 |
+
//
|
| 99 |
+
}
|
| 100 |
+
})
|
| 101 |
+
```
|
| 102 |
+
We can also check that the p5 instance is working correctly by adding `console.log('p5 instance loaded')` to `p5.setup()`, since you won't yet see a canvas or any DOM elements
|
| 103 |
+
|
| 104 |
+
Check that the page loaded, since we don't have a canvas. Add this outside of the p5.js instance:
|
| 105 |
+
|
| 106 |
+
```js
|
| 107 |
+
window.onload = function(){
|
| 108 |
+
console.log('DOM loaded, sketch.js loaded')
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
```
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
### X. Create a web interface and add template features.
|
| 115 |
+
|
| 116 |
+
We'll build an easy p5.js web interface so that we can interact with our Critical AI Kit. Create three new functions and run them in the In the `p5.setup()` function. Add a fourth function named `displayResults()` but don't run it in Setup. Instead it will run with a button press we make later.
|
| 117 |
+
|
| 118 |
+
```js
|
| 119 |
+
new p5(function (p5){
|
| 120 |
+
p5.setup = function(){
|
| 121 |
+
p5.noCanvas()
|
| 122 |
+
console.log('p5 instance loaded')
|
| 123 |
+
|
| 124 |
+
makeTextDisplay()
|
| 125 |
+
makeFields()
|
| 126 |
+
makeButtons()
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
function makeTextDisplay(){
|
| 130 |
+
//
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
function makeFields(){
|
| 134 |
+
//
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
function makeButtons(){
|
| 138 |
+
//
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
function displayResults(){
|
| 142 |
+
//
|
| 143 |
+
}
|
| 144 |
+
})
|
| 145 |
+
```
|
| 146 |
+
|
| 147 |
+
For a deep dive into how to use the p5.DOM features, see [DOM TUTORIAL]((XXX)). Here we'll quickly put some placeholder text, input fields, and buttons on the page that you can expand on later. First, add a title, a description, and some alt text for accessibility. Don't forget to add `p5.` in front of every function that is specific to p5.js.
|
| 148 |
+
|
| 149 |
+
```js
|
| 150 |
+
function makeTextDisplay(){
|
| 151 |
+
let title = p5.createElement('h1','p5.js Critical AI Kit')
|
| 152 |
+
let intro = p5.createP(`Description`)
|
| 153 |
+
let altText = p5.describe(p5.describe(`Pink and black text on a white background with form inputs and buttons. The text describes a p5.js tool that lets you explore machine learning interactively. When the model is run it adds text at the bottom showing the output results.`))
|
| 154 |
+
}
|
| 155 |
+
```
|
| 156 |
+
|
| 157 |
+
For now, we'll just add a single input field, for writing prompts. It won't work yet because we'll need to connect it to the rest of the form. We describe its size, give it a label, and give it the class `prompt`.
|
| 158 |
+
|
| 159 |
+
```js
|
| 160 |
+
function makeFields(){
|
| 161 |
+
let pField = p5.createInput(``)
|
| 162 |
+
pField.size(700)
|
| 163 |
+
pField.attribute('label', `Write a prompt here:`)
|
| 164 |
+
p5.createP(pField.attribute('label'))
|
| 165 |
+
pField.addClass("prompt")
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
```
|
| 169 |
+
|
| 170 |
+
We'll add one button that will let us send prompts to the model. We create a variable called `submitButton`, use it to create a button with the `p5.createButton` function, and display the text `"SUBMIT"` on the button. We also size the button and give it a class. For now it won't do anything because we haven't used its `.mousePressed()` method to call any functions, but we'll add that later.
|
| 171 |
+
|
| 172 |
+
```js
|
| 173 |
+
function makeButtons(){
|
| 174 |
+
let submitButton = p5.createButton("SUBMIT")
|
| 175 |
+
submitButton.size(170)
|
| 176 |
+
submitButton.class('submit')
|
| 177 |
+
// submitButton.mousePressed()
|
| 178 |
+
}
|
| 179 |
+
```
|
| 180 |
+
|
| 181 |
+
And how about somewhere to display the results we get from our model? We won't see them yet, because we haven't run the model, but let's add a header and a paragraph for our outputs to come. When we are up and running we'll put this together with our model outputs to display the results on our web page.
|
| 182 |
+
|
| 183 |
+
```js
|
| 184 |
+
function displayResults(){
|
| 185 |
+
let outHeader = p5.createElement('h3',"Results")
|
| 186 |
+
let outText = p5.createP('')
|
| 187 |
+
```
|
| 188 |
+
|
| 189 |
+
### X. Add CSS flair.
|
| 190 |
+
|
| 191 |
+
Create a `style.css` file and paste in this code:
|
| 192 |
+
|
| 193 |
+
[Link to raw file]([XXX)
|
| 194 |
+
|
| 195 |
+
You can also write your own or revamp this code! See [CSS Tutorial [XXX]]([XXX]) for more details on playing with styles.
|
| 196 |
+
|
| 197 |
+
### X. Add authorization to your space. [MAY NOT BE NEEDED][XXX]
|
| 198 |
+
|
| 199 |
+
We'll use some template configuration code to make sure our program talks to the Hugging Face API.
|
| 200 |
+
|
| 201 |
+
Paste this code into your `sketch.js` file:
|
| 202 |
+
|
| 203 |
+
```js
|
| 204 |
+
[XXX-TO-DO][MAY NOT NEED]
|
| 205 |
+
```
|
| 206 |
+
|
| 207 |
+
Also add this to your `README.md`:
|
| 208 |
+
|
| 209 |
+
```markdown
|
| 210 |
+
hf_oauth: true
|
| 211 |
+
hf_oauth_scopes:
|
| 212 |
+
- read-repos
|
| 213 |
+
- write-repos
|
| 214 |
+
- inference-api
|
| 215 |
+
```
|
| 216 |
+
|
| 217 |
+
When you next load your app, click `Authorize`
|
| 218 |
+
|
| 219 |
+
![screenshot of Hugging Face app authorization screen]()
|
| 220 |
+
|
| 221 |
+
To check if your authorization has worked, visit the Settings for your Hugging Face profile. Click `Connected Apps` and you should see the name of your `Space`.
|
| 222 |
+
|
| 223 |
+
![screenshot of authorized space in Hugging Face Settings interface]()
|
| 224 |
+
|
| 225 |
+
<!-- EXAMPLES: https://huggingface.co/docs/huggingface.js/main/en/index#huggingfaceinference-examples -->
|
| 226 |
+
|
| 227 |
+
To adjust the configuration of your HF space:
|
| 228 |
+
https://huggingface.co/docs/hub/spaces-config-reference
|
| 229 |
+
<!-- https://huggingface.co/docs/huggingface.js/hub/README#oauth-login -->
|
| 230 |
+
<!-- https://huggingface.co/spaces/huggingfacejs/client-side-oauth/blob/main/index.html →-->
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
**ALT if not using HF Spaces:**
|
| 234 |
+
### X. Get a HuggingFace API key.
|
| 235 |
+
https://huggingface.co/docs/hub/spaces-overview#managing-secrets
|
| 236 |
+
### X. Connect your API key to your p5.js instance.
|
| 237 |
+
|
| 238 |
+
## Reflections & Next Steps
|
| 239 |
+
|
| 240 |
+
We’ve now put together all the basic foundations of a web page ready to host some Critical AI tools. As we move on to [XXX]
|