From bf0700f3eb24ce060e52d08c16585891ed33fd15 Mon Sep 17 00:00:00 2001 From: Julian Bilcke Date: Thu, 22 Aug 2024 15:31:27 +0200 Subject: [PATCH] investigating hotshot --- .../providers/hotshot/createHotshotVideo.ts | 47 +++++++++++++++++++ .../api/resolve/providers/hotshot/index.ts | 2 + .../api/resolve/providers/hotshot/types.ts | 29 ++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 packages/app/src/app/api/resolve/providers/hotshot/createHotshotVideo.ts create mode 100644 packages/app/src/app/api/resolve/providers/hotshot/types.ts diff --git a/packages/app/src/app/api/resolve/providers/hotshot/createHotshotVideo.ts b/packages/app/src/app/api/resolve/providers/hotshot/createHotshotVideo.ts new file mode 100644 index 00000000..488bf770 --- /dev/null +++ b/packages/app/src/app/api/resolve/providers/hotshot/createHotshotVideo.ts @@ -0,0 +1,47 @@ +import { + HotshotVideoGenerationParams, + HotshotVideoGenerationResponse, +} from './types' + +/** + * Asynchronous function to initiate video generation using the Hotshot API. + * @param apiKey - The API key for authentication. + * @param params - The parameters for video generation. + * @returns A promise that resolves to the video generation response. + */ +export async function createHotshotVideo( + apiKey: string, + params: HotshotVideoGenerationParams +): Promise { + const baseUrl = 'https://hotshot.co' + const headers = { + Accept: 'application/json', + 'Content-Type': 'application/json', + + // I think right now it's using cookies + // 'X-API-Key': apiKey, + } + + try { + const response = await fetch(`${baseUrl}/api/v1/text-to-video/generate`, { + method: 'POST', + headers: headers, + body: JSON.stringify(params), + }) + + // note: at this stage normally we should connect to the queue using websockets + // however since we are an API context and Hotshot is slow to generate, + // a simply polling is probably more than enough + // eg initial wait time of 20 seconds + check every 5 sec + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`) + } + + const data: HotshotVideoGenerationResponse = await response.json() + return data + } catch (error) { + console.error('Error in video generation:', error) + throw error + } +} diff --git a/packages/app/src/app/api/resolve/providers/hotshot/index.ts b/packages/app/src/app/api/resolve/providers/hotshot/index.ts index 47822f87..4ad8e7ff 100644 --- a/packages/app/src/app/api/resolve/providers/hotshot/index.ts +++ b/packages/app/src/app/api/resolve/providers/hotshot/index.ts @@ -13,6 +13,8 @@ export async function resolveSegment( // TODO: implement the Hotshot API // (current status: waiting to hear back) + // createHotshotVideo(...) + throw new Error( `Clapper doesn't support ${request.segment.category} generation for provider "Hotshot". Please open a pull request with (working code) to solve this!` ) diff --git a/packages/app/src/app/api/resolve/providers/hotshot/types.ts b/packages/app/src/app/api/resolve/providers/hotshot/types.ts new file mode 100644 index 00000000..eabd96a1 --- /dev/null +++ b/packages/app/src/app/api/resolve/providers/hotshot/types.ts @@ -0,0 +1,29 @@ +/** + * Represents the parameters for the Hotshot video generation request. + */ +export type HotshotVideoGenerationParams = { + prompt: string + prompt_options: string // " --generation-type generate" + + // I think this is just a randomly generated UUID + request_id: string // eg. "6bf37b2d-576b-4ae0-93d4-9e7980fa3ce3" + + generation_type: string // "generate" + + files: Array // for future use? +} + +/** + * Represents the response from the Hotshot video generation request. + */ +export type HotshotVideoGenerationResponse = { + // will be something like "https://dvfx9cgvtgnyd.cloudfront.net", + cdnHost: string + + // will be something like "6bf37b2d-576b-4ae0-93d4-9e7980fa3ce3", + request_id: string + + responses: Array<{ + message: string // "submitted to ['eC4XDlU_d72nqFG5AAAn']" + }> +}