Skip to content

Commit

Permalink
dalle fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nbonamy committed May 25, 2024
1 parent 21e535f commit 412b462
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/components/MessageItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</div>

<!-- image for backwards compatibility -->
<MessageItemImage :image-url="imageUrl" @image-loaded="onImageLoaded(message)" v-if="message.type == 'image'" />
<MessageItemImage :url="imageUrl" @image-loaded="onImageLoaded(message)" v-if="message.type == 'image'" />

<!-- text -->
<div v-if="message.type == 'text' && message.content !== null">
Expand Down
9 changes: 6 additions & 3 deletions src/components/MessageItemBody.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div v-if="message.type == 'text'">
<div v-for="block in blocks">
<div v-if="block.type == 'text'" v-html="mdRender(block.content)" class="text"></div>
<MessageItemImage :image-url="block.content" @image-loaded="onImageLoaded(message)" v-else-if="block.type == 'image'" />
<MessageItemImage :url="block.url" :desc="block.desc" @image-loaded="onImageLoaded(message)" v-else-if="block.type == 'image'" />
</div>
</div>
</template>
Expand Down Expand Up @@ -37,16 +37,19 @@ const blocks = computed(() => {
if (!imageUrl.startsWith('http') && !imageUrl.startsWith('file://')) {
imageUrl = `file://${imageUrl}`
}
blocks.push({ type: 'image', content: imageUrl })
blocks.push({ type: 'image', url: imageUrl, desc: match[1]})
// continue
lastIndex = regex.lastIndex
}
// add last block
blocks.push({ type: 'text', content: props.message.content.substring(lastIndex) })
if (lastIndex != props.message.content.length) {
blocks.push({ type: 'text', content: props.message.content.substring(lastIndex) })
}
// done
//console.log(blocks)
return blocks
})
Expand Down
10 changes: 5 additions & 5 deletions src/components/MessageItemImage.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div class="image-container">
<img :src="imageUrl" class="image" @click="onFullscreen(imageUrl)" @load="onImageLoaded()"/>
<img :src="url" :alt="desc" class="image" @click="onFullscreen" @load="onImageLoaded"/>
<BIconDownload class="download" @click="onDownload()" />
</div>
</template>
Expand All @@ -13,7 +13,8 @@ import useEventBus from '../composables/useEventBus'
const { emitEvent } = useEventBus()
const props = defineProps({
imageUrl: String,
url: String,
desc: String
})
const emits = defineEmits(['image-loaded'])
Expand All @@ -23,13 +24,12 @@ const onImageLoaded = () => {
}
const onFullscreen = (url) => {
emitEvent('fullScreen', url)
emitEvent('fullScreen', props.url)
}
const onDownload = () => {
console.log('download', props.imageUrl)
window.api.file.download({
url: props.imageUrl,
url: props.url,
properties: {
filename: 'image.png',
}
Expand Down
73 changes: 50 additions & 23 deletions src/plugins/dalle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default class extends Plugin {
}

getDescription(): string {
return 'Generate an image based on a prompt. Returns the path of the image saved on disk.'
return 'Generate an image based on a prompt. Returns the path of the image saved on disk and a description of the image.'
}

getPreparationDescription(): string {
Expand All @@ -33,32 +33,58 @@ export default class extends Plugin {
}

getParameters(): PluginParameter[] {
return [

const parameters: PluginParameter[] = [
{
name: 'prompt',
type: 'string',
description: 'The description of the image',
required: true
},
// {
// name: 'size',
// type: 'string',
// description: 'The size of the image',
// required: false
// },
// {
// name: 'style',
// type: 'string',
// description: 'The style of the image',
// required: false
// },
// {
// name: 'n',
// type: 'number',
// description: 'Number of images to generate',
// required: false
// },
}
]

// rest depends on model
if (store.config.engines.openai.model.image === 'dall-e-2') {

parameters.push({
name: 'size',
type: 'string',
enum: [ '256x256', '512x512', '1024x1024' ],
description: 'The size of the image',
required: false
})

} else if (store.config.engines.openai.model.image === 'dall-e-3') {

parameters.push({
name: 'quality',
type: 'string',
enum: [ 'standard', 'hd' ],
description: 'The quality of the image',
required: false
})

parameters.push({
name: 'size',
type: 'string',
enum: [ '1024x1024', '1792x1024', '1024x1792' ],
description: 'The size of the image',
required: false
})

parameters.push({
name: 'style',
type: 'string',
enum: ['vivid', 'natural'],
description: 'The style of the image',
required: false
})

}

// done
return parameters

}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand All @@ -79,16 +105,17 @@ export default class extends Plugin {
response_format: 'b64_json',
size: parameters?.size,
style: parameters?.style,
quality: parameters?.quality,
n: parameters?.n || 1,
})

// save the content on disk
const filename = saveFileContents('png', response.data[0].b64_json)


// return an object
return {
path: `file://${filename}`
path: `file://${filename}`,
description: parameters?.prompt
}

}
Expand Down

0 comments on commit 412b462

Please sign in to comment.