-
-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prepare work for the improvement of face swap
- Loading branch information
1 parent
154f3b7
commit 616e708
Showing
3 changed files
with
67 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
packages/app/src/app/api/resolve/providers/falai/runImageFaceSwap.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import * as fal from '@fal-ai/serverless-client' | ||
import { TimelineSegment } from '@aitube/timeline' | ||
import { ResolveRequest } from '@aitube/clapper-services' | ||
import { FalAiImageResponse } from './types' | ||
|
||
export async function runImageFaceSwap( | ||
request: ResolveRequest | ||
): Promise<TimelineSegment> { | ||
if (!request.settings.falAiApiKey) { | ||
throw new Error(`Missing API key for "Fal.ai"`) | ||
} | ||
|
||
fal.config({ | ||
credentials: request.settings.falAiApiKey, | ||
}) | ||
|
||
const segment: TimelineSegment = request.segment | ||
|
||
const imageFaceswapWorkflowModel = | ||
request.settings.imageFaceswapWorkflow.data || '' | ||
|
||
if (imageFaceswapWorkflowModel) { | ||
try { | ||
const faceSwapResult = (await fal.run(imageFaceswapWorkflowModel, { | ||
input: { | ||
base_image_url: segment.assetUrl, | ||
swap_image_url: request.prompts.image.identity, | ||
|
||
sync_mode: true, | ||
num_images: 1, | ||
enable_safety_checker: | ||
request.settings.censorNotForAllAudiencesContent, | ||
}, | ||
})) as FalAiImageResponse | ||
|
||
// note how it is | ||
const imageResult = faceSwapResult.image?.url || '' | ||
|
||
if (!imageResult) { | ||
throw new Error(`the generate image is empty`) | ||
} | ||
|
||
if (request.settings.censorNotForAllAudiencesContent) { | ||
if ( | ||
Array.isArray(faceSwapResult.has_nsfw_concepts) && | ||
faceSwapResult.has_nsfw_concepts.includes(true) | ||
) { | ||
throw new Error( | ||
`The generated content has been filtered according to your safety settings` | ||
) | ||
} | ||
} | ||
|
||
segment.assetUrl = imageResult | ||
} catch (err) { | ||
console.error(`failed to run a face-swap using Fal.ai:`, err) | ||
} | ||
} | ||
|
||
return segment | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters