Skip to content

Commit

Permalink
Send a GET send_command request to server when running in internal co…
Browse files Browse the repository at this point in the history
…lab which

doesn't support POST.

Also hide the "select model" button when running in internal colab.

PiperOrigin-RevId: 679672925
  • Loading branch information
Google AI Edge authored and copybara-github committed Sep 27, 2024
1 parent 256de27 commit a2f59d6
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 20 deletions.
4 changes: 4 additions & 0 deletions src/ui/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import {

import {loadTfjsModel} from './tfjs';

/** Checks if the current page is running in internal colab. */
export const INTERNAL_COLAB =
new URLSearchParams(window.location.search).get('internal_colab') === '1';

/** Checks if the given path is a supported internal storage path. */
export function isInternalStoragePath(path: string): boolean {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,20 @@
</div>

<!-- Or -->
<div class="or-label">or</div>
@if (!internalColab) {
<div class="or-label">or</div>
}

<!-- Upload button -->
<button
mat-flat-button
class="btn-upload"
(click)="uploadInput.click()"
[disabled]="hasReachedMaxModelsCount || loading()">
Select from your computer
</button>
@if (!internalColab) {
<button
mat-flat-button
class="btn-upload"
(click)="uploadInput.click()"
[disabled]="hasReachedMaxModelsCount || loading()">
Select from your computer
</button>
}
<input class="upload-input" type="file" multiple #uploadInput
(change)="handleClickUpload(uploadInput)">
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ import {
ModelItemStatus,
ModelItemType,
} from '../../common/types';
import {getElectronApi, isInternalStoragePath} from '../../common/utils';
import {
getElectronApi,
INTERNAL_COLAB,
isInternalStoragePath,
} from '../../common/utils';
import {AdapterExtensionService} from '../../services/adapter_extension_service';
import {ModelSource, UrlService} from '../../services/url_service';
import {Bubble} from '../bubble/bubble';
Expand Down Expand Up @@ -140,6 +144,7 @@ export class ModelSourceInput {

readonly loading = signal<boolean>(false);
readonly hasUploadedModels = signal<boolean>(false);
readonly internalColab = INTERNAL_COLAB;

private portal: ComponentPortal<AdapterSelectorPanel> | null = null;

Expand Down
30 changes: 21 additions & 9 deletions src/ui/src/services/extension_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ import {Injectable, signal} from '@angular/core';

import {ExtensionCommand} from '../common/extension_command';
import {Extension} from '../common/types';
import {INTERNAL_COLAB} from '../common/utils';

const EXTERNAL_GET_EXTENSIONS_API_PATH = '/api/v1/get_extensions';
const EXTERNAL_SEND_CMD_API_PATH = '/apipost/v1/send_command';
const EXTERNAL_SEND_CMD_GET_API_PATH = '/api/v1/send_command';
const EXTERNAL_SEND_CMD_POST_API_PATH = '/apipost/v1/send_command';

/**
* Service for managing model explorer extensions.
*/
@Injectable({providedIn: 'root'})
export class ExtensionService {
readonly loading = signal<boolean>(true);
readonly internalColab = INTERNAL_COLAB;

extensions: Extension[] = [];

Expand All @@ -40,15 +43,24 @@ export class ExtensionService {
async sendCommandToExtension<T>(
cmd: ExtensionCommand,
): Promise<{cmdResp?: T; otherError?: string}> {
const requestData: RequestInit = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
};
requestData.body = JSON.stringify(cmd);
try {
const resp = await fetch(EXTERNAL_SEND_CMD_API_PATH, requestData);
let resp: Response | undefined = undefined;
// In internal colab, use GET request.
if (this.internalColab) {
const url = `${EXTERNAL_SEND_CMD_GET_API_PATH}?json=${JSON.stringify(cmd)}`;
resp = await fetch(url);
}
// In other environments, use POST request.
else {
const requestData: RequestInit = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
};
requestData.body = JSON.stringify(cmd);
resp = await fetch(EXTERNAL_SEND_CMD_POST_API_PATH, requestData);
}
if (!resp.ok) {
return {otherError: `Failed to convert model. ${resp.status}`};
}
Expand Down
6 changes: 6 additions & 0 deletions src/ui/src/services/url_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ enum QueryParamKey {
BENCHMARK = 'benchmark',
ENABLE_SUBGRAPH_SELECTION = 'ess',
ENABLE_EXPORT_TO_RESOURCE = 'eetr',
INTERNAL_COLAB = 'internal_colab',
}

declare interface OldEncodedUrlData {
Expand Down Expand Up @@ -70,6 +71,7 @@ export class UrlService {

renderer = 'webgl';
showOpenInNewTab = false;
internalColab = false;
benchmark = false;
enableSubgraphSelection = false;
enableExportToResource = false;
Expand Down Expand Up @@ -129,6 +131,9 @@ export class UrlService {
queryParams[QueryParamKey.SHOW_OPEN_IN_NEW_TAB] = this.showOpenInNewTab
? '1'
: '0';
queryParams[QueryParamKey.INTERNAL_COLAB] = this.internalColab
? '1'
: '0';
queryParams[QueryParamKey.ENABLE_SUBGRAPH_SELECTION] = this
.enableSubgraphSelection
? '1'
Expand Down Expand Up @@ -196,6 +201,7 @@ export class UrlService {
this.renderer = renderer || 'webgl';
this.showOpenInNewTab =
params.get(QueryParamKey.SHOW_OPEN_IN_NEW_TAB) === '1';
this.internalColab = params.get(QueryParamKey.INTERNAL_COLAB) === '1';
this.enableSubgraphSelection =
params.get(QueryParamKey.ENABLE_SUBGRAPH_SELECTION) === '1';
this.enableExportToResource =
Expand Down
2 changes: 0 additions & 2 deletions src/ui/src/theme/theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ $blue-palette: (
// The `mat.core` mixin applies common styles used by multiple
// components, such ripples, overlays, high contrast mode, etc.
@include mat.core();
@include mat.app-background();
@include mat.elevation-classes();

// Define the application's color palettes.
$primary: mat.m2-define-palette($blue-palette, 600);
Expand Down

0 comments on commit a2f59d6

Please sign in to comment.