Skip to content

Commit

Permalink
dont call inference API is output is available from example
Browse files Browse the repository at this point in the history
  • Loading branch information
mishig25 committed Oct 30, 2023
1 parent c20cfb1 commit e5cdf0a
Show file tree
Hide file tree
Showing 24 changed files with 181 additions and 49 deletions.
4 changes: 3 additions & 1 deletion js/src/lib/components/InferenceWidget/shared/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ModelData } from "../../../interfaces/Types";
import type { WidgetExampleOutput } from "./WidgetExample";

export interface WidgetProps {
apiToken?: string;
Expand All @@ -11,10 +12,11 @@ export interface WidgetProps {
isLoggedIn?: boolean;
}

export interface InferenceRunOpts {
export interface InferenceRunOpts<TOutput = WidgetExampleOutput> {
withModelLoading?: boolean;
isOnLoadCall?: boolean;
useCache?: boolean;
exampleOutput?: TOutput;
}

export interface ExampleRunOpts {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,17 @@
}
}
async function getOutput({ withModelLoading = false, isOnLoadCall = false }: InferenceRunOpts = {}) {
async function getOutput({
withModelLoading = false,
isOnLoadCall = false,
exampleOutput = undefined,
}: InferenceRunOpts<WidgetExampleOutputLabels> = {}) {
if (exampleOutput) {
output = exampleOutput;
outputJson = "";
return;
}
if (!file && !selectedSampleUrl) {
error = "You must select or record an audio file";
output = [];
Expand Down Expand Up @@ -136,7 +146,8 @@
}
file = null;
selectedSampleUrl = sample.src;
getOutput(opts.inferenceOpts);
const exampleOutput = sample.output;
getOutput({ ...opts.inferenceOpts, exampleOutput });
}
function validateExample(sample: WidgetExample): sample is WidgetExampleAssetInput<WidgetExampleOutputLabels> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@
}
}
async function getOutput({ withModelLoading = false, isOnLoadCall = false }: InferenceRunOpts = {}) {
async function getOutput({
withModelLoading = false,
isOnLoadCall = false,
exampleOutput = undefined,
}: InferenceRunOpts = {}) {
if (!file && !selectedSampleUrl) {
error = "You must select or record an audio file";
return;
Expand Down Expand Up @@ -134,7 +138,8 @@
}
file = null;
selectedSampleUrl = sample.src;
getOutput(opts.inferenceOpts);
const exampleOutput = sample.output;
getOutput({ ...opts.inferenceOpts, exampleOutput });
}
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,17 @@
}
}
async function getOutput({ withModelLoading = false, isOnLoadCall = false }: InferenceRunOpts = {}) {
async function getOutput({
withModelLoading = false,
isOnLoadCall = false,
exampleOutput = undefined,
}: InferenceRunOpts<WidgetExampleOutputText> = {}) {
if (exampleOutput) {
output = exampleOutput.text;
outputJson = "";
return;
}
if (!file && !selectedSampleUrl) {
error = "You must select or record an audio file";
output = "";
Expand Down Expand Up @@ -137,7 +147,8 @@
}
file = null;
selectedSampleUrl = sample.src;
getOutput(opts.inferenceOpts);
const exampleOutput = sample.output;
getOutput({ ...opts.inferenceOpts, exampleOutput });
}
function updateModelLoading(isLoading: boolean, estimatedTime: number = 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@
let outputJson: string;
let text = "";
async function getOutput({ withModelLoading = false, isOnLoadCall = false }: InferenceRunOpts = {}) {
async function getOutput({
withModelLoading = false,
isOnLoadCall = false,
exampleOutput = undefined,
}: InferenceRunOpts = {}) {
const trimmedText = text.trim();
if (!trimmedText) {
Expand Down Expand Up @@ -143,7 +147,8 @@
if (opts.isPreview) {
return;
}
getOutput(opts.inferenceOpts);
const exampleOutput = sample.output;
getOutput({ ...opts.inferenceOpts, exampleOutput });
}
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@
let outputJson: string;
let text = "";
async function getOutput({ withModelLoading = false, isOnLoadCall = false }: InferenceRunOpts = {}) {
async function getOutput({
withModelLoading = false,
isOnLoadCall = false,
exampleOutput = undefined,
}: InferenceRunOpts = {}) {
const trimmedText = text.trim();
if (!trimmedText) {
error = "You need to input some text";
output = undefined;
exampleOutput = undefined;
outputJson = "";
return;
}
Expand Down Expand Up @@ -63,7 +67,7 @@
computeTime = "";
error = "";
modelLoading = { isLoading: false, estimatedTime: 0 };
output = undefined;
exampleOutput = undefined;
outputJson = "";
if (res.status === "success") {
Expand Down Expand Up @@ -111,7 +115,8 @@
if (opts.isPreview) {
return;
}
getOutput(opts.inferenceOpts);
const exampleOutput = sample.output;
getOutput({ ...opts.inferenceOpts, exampleOutput });
}
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@
let text = "";
let setTextAreaValue: (text: string) => void;
async function getOutput({ withModelLoading = false, isOnLoadCall = false }: InferenceRunOpts = {}) {
async function getOutput({
withModelLoading = false,
isOnLoadCall = false,
exampleOutput = undefined,
}: InferenceRunOpts<WidgetExampleOutputLabels> = {}) {
if (exampleOutput) {
output = exampleOutput;
outputJson = "";
return;
}
const trimmedText = text.trim();
if (!trimmedText) {
Expand Down Expand Up @@ -112,7 +122,8 @@
}
return;
}
getOutput(opts.inferenceOpts);
const exampleOutput = sample.output;
getOutput({ ...opts.inferenceOpts, exampleOutput });
}
function validateExample(sample: WidgetExample): sample is WidgetExampleTextInput<WidgetExampleOutputLabels> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
async function getOutput(
file: File | Blob,
{ withModelLoading = false, isOnLoadCall = false }: InferenceRunOpts = {}
{ withModelLoading = false, isOnLoadCall = false, exampleOutput = undefined }: InferenceRunOpts = {}
) {
if (!file) {
return;
Expand Down Expand Up @@ -108,7 +108,8 @@
return;
}
const blob = await getBlobFromUrl(imgSrc);
getOutput(blob, opts.inferenceOpts);
const exampleOutput = sample.output;
getOutput(blob, { ...opts.inferenceOpts, exampleOutput });
}
function validateExample(sample: WidgetExample): sample is WidgetExampleAssetInput<WidgetExampleOutputLabels> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
async function getOutput(
file: File | Blob,
{ withModelLoading = false, isOnLoadCall = false }: InferenceRunOpts = {}
{ withModelLoading = false, isOnLoadCall = false, exampleOutput = undefined }: InferenceRunOpts = {}
) {
if (!file) {
return;
Expand Down Expand Up @@ -217,7 +217,8 @@
return;
}
const blob = await getBlobFromUrl(imgSrc);
getOutput(blob, opts.inferenceOpts);
const exampleOutput = sample.output;
getOutput(blob, { ...opts.inferenceOpts, exampleOutput });
}
onMount(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,15 @@
const res = await fetch(imgSrc);
const blob = await res.blob();
await updateImageBase64(blob);
getOutput(opts.inferenceOpts);
const exampleOutput = sample.output;
getOutput({ ...opts.inferenceOpts, exampleOutput });
}
async function getOutput({ withModelLoading = false, isOnLoadCall = false }: InferenceRunOpts = {}) {
async function getOutput({
withModelLoading = false,
isOnLoadCall = false,
exampleOutput = undefined,
}: InferenceRunOpts = {}) {
const trimmedPrompt = prompt.trim();
if (!imageBase64) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
async function getOutput(
file: File | Blob,
{ withModelLoading = false, isOnLoadCall = false }: InferenceRunOpts = {}
{ withModelLoading = false, isOnLoadCall = false, exampleOutput = undefined }: InferenceRunOpts = {}
) {
if (!file) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
async function getOutput(
file: File | Blob,
{ withModelLoading = false, isOnLoadCall = false }: InferenceRunOpts = {}
{ withModelLoading = false, isOnLoadCall = false, exampleOutput = undefined }: InferenceRunOpts = {}
) {
if (!file) {
return;
Expand Down Expand Up @@ -136,7 +136,8 @@
return;
}
const blob = await getBlobFromUrl(imgSrc);
getOutput(blob, opts.inferenceOpts);
const exampleOutput = sample.output;
getOutput(blob, { ...opts.inferenceOpts, exampleOutput });
}
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
let question = "";
let setTextAreaValue: (text: string) => void;
async function getOutput({ withModelLoading = false, isOnLoadCall = false }: InferenceRunOpts = {}) {
async function getOutput({
withModelLoading = false,
isOnLoadCall = false,
exampleOutput = undefined,
}: InferenceRunOpts = {}) {
const trimmedQuestion = question.trim();
const trimmedContext = context.trim();
Expand Down Expand Up @@ -113,7 +117,8 @@
if (opts.isPreview) {
return;
}
getOutput(opts.inferenceOpts);
const exampleOutput = sample.output;
getOutput({ ...opts.inferenceOpts, exampleOutput });
}
function validateExample(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
let output: Array<{ label: string; score: number }> = [];
let outputJson: string;
async function getOutput({ withModelLoading = false, isOnLoadCall = false }: InferenceRunOpts = {}) {
async function getOutput({
withModelLoading = false,
isOnLoadCall = false,
exampleOutput = undefined,
}: InferenceRunOpts = {}) {
const trimmedSourceSentence = sourceSentence.trim();
if (!trimmedSourceSentence) {
error = "You need to input some text";
Expand Down Expand Up @@ -124,7 +128,8 @@
if (opts.isPreview) {
return;
}
getOutput(opts.inferenceOpts);
const exampleOutput = sample.output;
getOutput({ ...opts.inferenceOpts, exampleOutput });
}
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
let text = "";
let setTextAreaValue: (text: string) => void;
async function getOutput({ withModelLoading = false, isOnLoadCall = false }: InferenceRunOpts = {}) {
async function getOutput({
withModelLoading = false,
isOnLoadCall = false,
exampleOutput = undefined,
}: InferenceRunOpts = {}) {
const trimmedValue = text.trim();
if (!trimmedValue) {
Expand Down Expand Up @@ -94,7 +98,8 @@
if (opts.isPreview) {
return;
}
getOutput(opts.inferenceOpts);
const exampleOutput = sample.output;
getOutput({ ...opts.inferenceOpts, exampleOutput });
}
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@
table = updatedTable;
}
async function getOutput({ withModelLoading = false, isOnLoadCall = false }: InferenceRunOpts = {}) {
async function getOutput({
withModelLoading = false,
isOnLoadCall = false,
exampleOutput = undefined,
}: InferenceRunOpts = {}) {
const trimmedQuery = query.trim();
if (!trimmedQuery) {
Expand Down Expand Up @@ -144,7 +148,8 @@
if (opts.isPreview) {
return;
}
getOutput(opts.inferenceOpts);
const exampleOutput = sample.output;
getOutput({ ...opts.inferenceOpts, exampleOutput });
}
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@
output = [];
}
async function getOutput({ withModelLoading = false, isOnLoadCall = false }: InferenceRunOpts = {}) {
async function getOutput({
withModelLoading = false,
isOnLoadCall = false,
exampleOutput = undefined,
}: InferenceRunOpts = {}) {
for (let [i, row] of table.entries()) {
for (const [j, cell] of row.entries()) {
if (!String(cell)) {
Expand Down Expand Up @@ -168,7 +172,8 @@
if (opts.isPreview) {
return;
}
getOutput(opts.inferenceOpts);
const exampleOutput = sample.output;
getOutput({ ...opts.inferenceOpts, exampleOutput });
}
</script>

Expand Down
Loading

0 comments on commit e5cdf0a

Please sign in to comment.