/** * @license * Copyright Big Vision Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @fileoverview Image and text prompts. */ import {html, LitElement} from 'lit'; import * as naughtyWords from 'naughty-words'; import {app} from '../lit_demo/app'; import {getBackend} from '../lit_demo/compute'; import {getImageUrl} from '../lit_demo/constants'; import {getUrl} from '../lit_demo/url_utils'; import {MessageList} from './message-list'; import {customElement, query} from 'lit/decorators.js'; import styles from './image-prompts.scss'; const setHref = (anchorEl: HTMLAnchorElement, href:string) => { anchorEl. href = href; }; const HTML_TEMPLATE = ` We cannot include the word "{word}" as it is found on the list naughty-words/{lang}. We understand blocklists are an imperfect solution but we believe it's important to ensure these models are not misused, and hope that in this instance it does not serve to marginalise anybody. If you don't agree, please reach out via form link. `; /** * Shows image and text prompts, and computes similarities. * * Also dispatches some events like `'duplicate'` to the parent. */ @customElement('image-prompts') export class ImagePrompts extends LitElement { static override styles = [styles]; @query('message-list') messageList!: MessageList; @query('.animation') animation!: HTMLElement; @query('.bottom') bottom!: HTMLElement; lastPrompts?: string[]; constructor(private readonly imageId: string) { super(); } override firstUpdated() { if (getBackend() !== 'webgl') { this.messageList.warning( 'Please activate WebGL. Running ML demos on ' + 'CPU will drain your battery in no time...'); } } onDuplicate() { this.dispatchEvent(new Event('duplicate')); } onRemove() { this.remove(); } onClear() { this.shadowRoot!.querySelectorAll('.prompt').forEach((input: Element) => { (input as HTMLInputElement).value = ''; }); (this.shadowRoot!.querySelector('.prompt') as HTMLInputElement).focus(); } onKeyup(event: KeyboardEvent) { if (event.key === 'Enter') { this.onCompute(); } } async setPrompts(prompts: string[]) { await this.updateComplete; this.shadowRoot!.querySelectorAll('.prompt').forEach((input: Element, idx: number) => { (input as HTMLInputElement).value = prompts[idx] || ''; }); } getPrompts(): string[] { return [...this.shadowRoot!.querySelectorAll('.prompt')].map((input: Element) => (input as HTMLInputElement).value ); } override render() { const row = app.imageData.get(this.imageId); const inputs = row.prompts.split(',').map((prompt: string, idx: number) => { return html`