This commit is contained in:
ct
2025-07-04 03:10:37 +08:00
parent 239b74fd5a
commit 21b56b6cf0
6 changed files with 411 additions and 141 deletions

View File

@@ -108,24 +108,58 @@ public static function generateMemeByCategory(Category $category)
return $meme;
}
public static function generateMemeOutputByCategory(Category $category)
public static function generateMemeByKeyword($keyword)
{
$meme_output = self::generateMemeOutputByKeyword($keyword);
$meme = null;
if ($meme_output->success) {
$meme = Meme::create([
'type' => self::TYPE_SINGLE_CAPTION_MEME_BACKGROUND,
'prompt' => $meme_output->prompt,
'category_id' => null,
'caption' => $meme_output->caption,
'meme_keywords' => $meme_output->keywords,
'background' => $meme_output->background,
'keywords' => $meme_output->keywords,
'is_system' => true,
'status' => self::STATUS_PENDING,
'primary_keyword_type' => $meme_output->primary_keyword_type,
'action_keywords' => $meme_output->action_keywords,
'emotion_keywords' => $meme_output->emotion_keywords,
'misc_keywords' => $meme_output->misc_keywords,
]);
$meme->attachTags($meme_output->keywords, 'meme');
}
if (! is_null($meme) && $meme->status == self::STATUS_PENDING) {
// populate meme_media_id
$meme->meme_media_id = self::getSuitableMemeMedia($meme)->id;
$meme->background_media_id = self::generateBackgroundMediaWithRunware($meme_output->background)->id;
if (
// !is_null($meme->meme_media_id) &&
! is_null($meme->background_media_id)
) {
$meme->status = self::STATUS_COMPLETED;
}
$meme->save();
}
return $meme;
}
public static function generateMemeOutputByKeyword($keyword, $category = null)
{
$retries = 3;
$attempt = 0;
$random_keyword = Str::lower($category->name);
if (! is_null($category->parent_id)) {
$random_keyword = $category->parent->name.' - '.$random_keyword;
}
if (! is_null($category->meme_angles)) {
$random_keyword .= ' - '.collect($category->meme_angles)->random();
} elseif (! is_null($category->keywords)) {
$random_keyword .= ' - '.collect($category->keywords)->random();
}
$prompt = "Write me 1 meme about {$random_keyword}";
$prompt = "Write me 1 meme about {$keyword}";
// RETRY MECHANISM START
do {
@@ -169,13 +203,31 @@ public static function generateMemeOutputByCategory(Category $category)
$meme_output = (object) [
'success' => false,
'attempts' => $attempt, // Optional: track how many attempts were made
'error' => 'Failed to generate valid meme after '.$retries.' attempts',
'error' => 'Failed to generate valid meme after ' . $retries . ' attempts',
];
}
return $meme_output;
}
public static function generateMemeOutputByCategory(Category $category)
{
$random_keyword = Str::lower($category->name);
if (! is_null($category->parent_id)) {
$random_keyword = $category->parent->name . ' - ' . $random_keyword;
}
if (! is_null($category->meme_angles)) {
$random_keyword .= ' - ' . collect($category->meme_angles)->random();
} elseif (! is_null($category->keywords)) {
$random_keyword .= ' - ' . collect($category->keywords)->random();
}
return self::generateMemeOutputByKeyword($random_keyword, $category);
}
public static function generateBackgroundMediaWithRunware($prompt)
{
$media_width = 1024;