Skip to content

Commit 88b8e46

Browse files
committed
Refactor code based on the PR comments from @SBrandeis
1 parent 6182f9f commit 88b8e46

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

packages/inference/src/providers/novita.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,11 @@ export class NovitaTextToVideoTask extends TaskProviderHelper implements TextToV
5858
}
5959

6060
override makeRoute(params: UrlParams): string {
61-
if (params.authMethod !== "provider-key") {
62-
return `/v3/async/${params.model}?_subdomain=queue`;
63-
}
6461
return `/v3/async/${params.model}`;
6562
}
6663

6764
override preparePayload(params: BodyParams): Record<string, unknown> {
68-
const { num_inference_steps, ...restParameters } = params.args.parameters as Record<string, unknown>;
65+
const { num_inference_steps, ...restParameters } = (params.args.parameters as Record<string, unknown>) ?? {};
6966
return {
7067
...omit(params.args, ["inputs", "parameters"]),
7168
...restParameters,
@@ -91,11 +88,10 @@ export class NovitaTextToVideoTask extends TaskProviderHelper implements TextToV
9188
const baseUrl = `${parsedUrl.protocol}//${parsedUrl.host}${
9289
parsedUrl.host === "router.huggingface.co" ? "/novita" : ""
9390
}`;
94-
const queryParams = parsedUrl.search;
95-
const resultUrl = `${baseUrl}/v3/async/task-result${queryParams ? queryParams + '&' : '?'}task_id=${taskId}`;
91+
const resultUrl = `${baseUrl}/v3/async/task-result?task_id=${taskId}`;
9692

9793
let status = '';
98-
let taskResult = undefined;
94+
let taskResult: unknown;
9995

10096
while (status !== 'TASK_STATUS_SUCCEED' && status !== 'TASK_STATUS_FAILED') {
10197
await delay(500);
@@ -105,7 +101,19 @@ export class NovitaTextToVideoTask extends TaskProviderHelper implements TextToV
105101
}
106102
try {
107103
taskResult = await resultResponse.json();
108-
status = taskResult.task.status;
104+
if (
105+
taskResult &&
106+
typeof taskResult === "object" &&
107+
"task" in taskResult &&
108+
taskResult.task &&
109+
typeof taskResult.task === "object" &&
110+
"status" in taskResult.task &&
111+
typeof taskResult.task.status === "string"
112+
) {
113+
status = taskResult.task.status;
114+
} else {
115+
throw new InferenceOutputError("Failed to get task status");
116+
}
109117
} catch (error) {
110118
throw new InferenceOutputError("Failed to parse task result");
111119
}
@@ -115,8 +123,7 @@ export class NovitaTextToVideoTask extends TaskProviderHelper implements TextToV
115123
throw new InferenceOutputError("Task failed");
116124
}
117125

118-
// There will be at most one video in the response.
119-
const isValidOutput =
126+
if (
120127
typeof taskResult === "object" &&
121128
!!taskResult &&
122129
"videos" in taskResult &&
@@ -126,13 +133,12 @@ export class NovitaTextToVideoTask extends TaskProviderHelper implements TextToV
126133
taskResult.videos.length > 0 &&
127134
"video_url" in taskResult.videos[0] &&
128135
typeof taskResult.videos[0].video_url === "string" &&
129-
isUrl(taskResult.videos[0].video_url);
130-
131-
if (!isValidOutput) {
136+
isUrl(taskResult.videos[0].video_url)
137+
) {
138+
const urlResponse = await fetch(taskResult.videos[0].video_url);
139+
return await urlResponse.blob();
140+
} else {
132141
throw new InferenceOutputError("Expected { videos: [{ video_url: string }] }");
133142
}
134-
135-
const urlResponse = await fetch(taskResult.videos[0].video_url);
136-
return await urlResponse.blob();
137143
}
138144
}

0 commit comments

Comments
 (0)