Skip to content

Commit

Permalink
vision_paint open ai
Browse files Browse the repository at this point in the history
SQUASHED: AUTO-COMMIT-demos-openai-vision-paint.md,AUTO-COMMIT-doc-journal-2024-05-23.md-index.md,AUTO-COMMIT-doc-journal-2024-05-23.md-squence-A-B.png,AUTO-COMMIT-src-components-tools-lively-image-editor.html,AUTO-COMMIT-src-components-tools-lively-image-editor.js,AUTO-COMMIT-src-components-tools-openai-audio-chat.js,
  • Loading branch information
JensLincke committed May 23, 2024
1 parent 3ca078a commit 3df5ad4
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 11 deletions.
56 changes: 56 additions & 0 deletions demos/openai/vision-paint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Paint


<script>
import OpenAI from "demos/openai/openai.js"


let paint = await (<lively-image-editor id="editor"></lively-image-editor>)
paint.clear()


async function view() {
let url = paint.canvas.toDataURL()
let blob = await fetch(url).then(r => r.blob())
let image = await lively.files.readBlobAsDataURL(blob)
let prompt = {
"model": "gpt-4o",
"max_tokens": 500,
"temperature": 0.1,
"top_p": 1,
"n": 1,
"stream": false,
"stop": "VANILLA",
"messages": [
{ "role": "system", "content": "You are an AI chat bot!" },
{ "role": "user", "content": [
{
"type": "text",
"text": "What’s in this image?"
},
{
"type": "image_url",
"image_url": {
"url": image,
"detail": "low" // high
}
}
]}
]
}

let json = await OpenAI.openAIRequest(prompt).then(r => r.json())
result.textContent = json.choices[0].message.content
}

let result = <div></div>

let pane = <div>
{paint}
<button click={() => view()}>view</button>
{result}
</div>


pane
</script>
6 changes: 6 additions & 0 deletions doc/journal/2024-05-23.md/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## 2024-05-23 Vision Paint OpenAI example
*Author: @JensLincke*

<browse://demos/openai/vision-paint.md>

![](squence-A-B.png)
Binary file added doc/journal/2024-05-23.md/squence-A-B.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions src/components/tools/lively-image-editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
height: calc(100% - 35px);
}

canvas {

border: 1px solid gray;
}

</style>
<div id="controls">
<lively-crayoncolors id="penColor" title="pen"></lively-crayoncolors>
Expand Down
21 changes: 11 additions & 10 deletions src/components/tools/lively-image-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ export default class LivelyImageEditor extends Morph {
return lively.rect(lively.pt(0,0), lively.getExtent(this.canvas)).containsPoint(pos)
}

clear() {
this.ctx.fillStyle = 'white'; // Set the fill color to blue
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
}


// #important
onPointerMove(evt) {
evt.preventDefault()
Expand Down Expand Up @@ -325,16 +331,13 @@ export default class LivelyImageEditor extends Morph {
const menuElements = [
["save", () => this.onSave()],
["save as...", () => this.onSaveAs()],
["clear", () => this.clear()],
["open image", () => this.onOpen()],
];
const menu = new lively.contextmenu(this, menuElements)
menu.openIn(document.body, evt, this)
}

livelyMigrate(other) {
this.target = other.target
}


get lastSource() {
return this._lastSource
}
Expand All @@ -358,13 +361,11 @@ export default class LivelyImageEditor extends Morph {
}

livelyMigrate(other) {

this.target = other.target
this.migrateCanvas = other.canvas
}


async livelyExample() {


async livelyExample() {
this.loadImage("https://lively-kernel.org/lively4/foo/test.png")
}
}
9 changes: 8 additions & 1 deletion src/components/tools/openai-audio-chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ export default class OpenaiAudioChat extends Morph {
async initialize() {
this.windowTitle = "OpenAI Audio Chat";
this.audioRecorder = new AudioRecorder();
this.prompt = [
{
role: 'system',
content: 'Play the role of a helpfull AI chat-bot in a JavaScript, HTML, CSS Web-based development environment.'
}
]

if (!this.conversation) {
this.conversation = [
// {
Expand Down Expand Up @@ -153,7 +160,7 @@ export default class OpenaiAudioChat extends Morph {
"n": 1,
"stream": false,
"stop": "VANILLA",
"messages": this.conversation,
"messages": this.prompt.concat(this.conversation),
}

const requestOptions = {
Expand Down

0 comments on commit 3df5ad4

Please sign in to comment.