-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ai demo and jsx support for script tags in markdown
SQUASHED: AUTO-COMMIT-demos-openai-image.md,AUTO-COMMIT-src-external-code-mirror-mode-htmlmixed-htmlmixed.js,
- Loading branch information
1 parent
3df5ad4
commit 6d98f15
Showing
2 changed files
with
52 additions
and
46 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 |
---|---|---|
@@ -1,49 +1,55 @@ | ||
# Image Generation | ||
|
||
```javascript | ||
import OpenAI from "demos/openai/openai.js" | ||
|
||
|
||
let prompt = { | ||
"model": "dall-e-3", | ||
"prompt": "a white siamese cat", | ||
"n": 1, | ||
"size": "1024x1024" | ||
} | ||
|
||
|
||
async function imageGeneration(prompt) { | ||
const apiKey = await OpenAI.ensureSubscriptionKey(); | ||
const url = "https://api.openai.com/v1/images/generations"; | ||
|
||
const requestOptions = { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"Authorization": `Bearer ${apiKey}` | ||
}, | ||
body: JSON.stringify(prompt) | ||
}; | ||
return fetch(url, requestOptions); | ||
} | ||
|
||
|
||
let response = await imageGeneration(prompt) | ||
|
||
|
||
reso | ||
|
||
let json = await response.json() | ||
|
||
|
||
json.data[0].url | ||
|
||
``` | ||
|
||
<script> | ||
|
||
let img = <img src="https://oaidalleapiprodscus.blob.core.windows.net/private/org-MPQoSTR6cnoOYKKoexRQqilJ/user-jSdR6BwdMFDIgdYmJiwmPPyY/img-5tGP5Ewc7Ro54jL8nqRNwhHC.png?st=2024-05-14T15%3A39%3A06Z&se=2024-05-14T17%3A39%3A06Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-05-14T15%3A40%3A46Z&ske=2024-05-15T15%3A40%3A46Z&sks=b&skv=2021-08-06&sig=VyLOR1dYBJBs0z7KzG/n/XXIk2lXWQ%2BJ4B29xSB5HZQ%3D"></img> | ||
import OpenAI from "demos/openai/openai.js" | ||
|
||
|
||
img | ||
async function generate() { | ||
let prompt = { | ||
"model": "dall-e-3", | ||
"prompt": input.value, | ||
"n": 1, | ||
"size": "1024x1024" | ||
} | ||
|
||
|
||
async function imageGeneration(prompt) { | ||
const apiKey = await OpenAI.ensureSubscriptionKey(); | ||
const url = "https://api.openai.com/v1/images/generations"; | ||
|
||
const requestOptions = { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"Authorization": `Bearer ${apiKey}` | ||
}, | ||
body: JSON.stringify(prompt) | ||
}; | ||
return fetch(url, requestOptions); | ||
} | ||
|
||
let start = performance.now() | ||
let response = await imageGeneration(prompt) | ||
|
||
let json = await response.json() | ||
result.innerHTML = "" | ||
result.appendChild(<div>time {performance.now() - start}ms</div>) | ||
result.appendChild(<div>{json.data[0].revised_prompt}</div>) | ||
img.src = json.data[0].url | ||
|
||
} | ||
|
||
let img = <img></img> | ||
let input = <input value="a white siamese cat"></input> | ||
let result = <div></div> | ||
|
||
let pane = <div> | ||
{input} | ||
<button click={() => generate()}>generate</button> | ||
{result} | ||
{img} | ||
</div> | ||
|
||
pane | ||
</script> | ||
|
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