新增图片分析功能,接收图片URL并生成描述,同时更新角色信息上传逻辑以支持图片分析,优化角色选择和更新流程。

This commit is contained in:
海龙 2025-08-12 20:06:55 +08:00
parent 7112de9ac1
commit e17a451e6e
3 changed files with 83 additions and 12 deletions

View File

@ -1037,4 +1037,28 @@ export const saveRegeneratedCharacter = async (request: {
return post("/character/save_regenerated_character", request);
};
/**
*
* @description URLAI服务分析图片并生成详细描述
* @param request -
* @returns Promise<ApiResponse<图片描述结果>>
*/
export const analyzeImageDescription = async (request: {
/** 图片的URL地址必须是有效的HTTP/HTTPS链接 */
image_url: string;
}): Promise<ApiResponse<{
/** 图片描述文本 */
description: string;
/** 图片URL */
image_url: string;
/** 高亮关键词列表 */
highlights: string[];
}>> => {
return post<ApiResponse<{
description: string;
image_url: string;
highlights: string[];
}>>("/character/analyze_image_description", request);
};

View File

@ -2,6 +2,7 @@ import { useState, useCallback, useMemo } from 'react';
import { RoleEntity, AITextEntity } from '../domain/Entities';
import { RoleEditUseCase } from '../usecase/RoleEditUseCase';
import { getUploadToken, uploadToQiniu } from '@/api/common';
import { analyzeImageDescription } from '@/api/video_flow';
/**
* Hook返回值接口
@ -33,8 +34,8 @@ interface UseRoleService {
fetchUserRoleLibrary: () => Promise<void>;
/** 替换角色 */
replaceRoleWithLibrary: (replaceRoleId: string) => Promise<void>;
/** 上传图片到七牛云 */
uploadImageToQiniu: (file: File) => Promise<void>;
/** 上传图片并更新角色信息 */
uploadImageAndUpdateRole: (file: File) => Promise<void>;
/** 保存重新生成的角色到角色库 */
saveRoleToLibrary: () => Promise<void>;
}
@ -314,21 +315,67 @@ export const useRoleServiceHook = (): UseRoleService => {
}, [selectedRole, roleEditUseCase, userRoleLibrary, selectRole]);
/**
*
* @description
*
* @description AI接口分析图片生成描述和高亮标签
* @param file
* @returns {Promise<string>} URL
* @returns {Promise<void>} Promise
*/
const uploadImageToQiniu = useCallback(async (file: File) => {
const uploadImageAndUpdateRole = useCallback(async (file: File) => {
if (!selectedRole) {
throw new Error('请先选择要更新的角色');
}
try {
// 1. 上传图片到七牛云
const { token } = await getUploadToken();
const url = await uploadToQiniu(file, token);
const roleEntity = await roleEditUseCase?.getRoleByImage(url);
const imageUrl = await uploadToQiniu(file, token);
// 2. 调用图片分析接口获取描述
const result = await analyzeImageDescription({
image_url: imageUrl
});
if (!result.successful) {
throw new Error(`图片分析失败: ${result.message}`);
}
const { description, highlights } = result.data;
// 3. 更新当前选中角色的图片、描述和标签
const updatedRole = {
...selectedRole,
imageUrl: imageUrl,
generateText: description,
tags: highlights.map((highlight: string, index: number) => ({
id: `tag_${Date.now()}_${index}`,
/** 名称 */
name: highlight,
/** 内容 */
content: highlight,
loadingProgress: 100,
disableEdit: false
}))
};
// 更新选中的角色
selectRole(updatedRole);
// 更新角色列表中的对应角色
setRoleList(prev =>
prev.map(role =>
role.id === selectedRole.id
? updatedRole
: role
)
);
console.log('角色图片和描述更新成功:', updatedRole);
} catch (error) {
console.error('上传图片到七牛云失败:', error);
console.error('上传图片并分析失败:', error);
throw error;
}
}, [roleEditUseCase]);
}, [selectedRole, roleEditUseCase, selectRole]);
/**
*
@ -388,7 +435,7 @@ export const useRoleServiceHook = (): UseRoleService => {
regenerateRole,
fetchUserRoleLibrary,
replaceRoleWithLibrary,
uploadImageToQiniu,
uploadImageAndUpdateRole,
saveRoleToLibrary
};
};

View File

@ -178,7 +178,7 @@ export function CharacterTabContent({
setIgnoreReplace(false);
setIsRegenerate(false);
selectRole(roleData[index].id);
selectRole(roleData[index]);
};
// 从角色库中选择角色