From 0d345761e8137408f3b98803fcbb6a8070df5e47 Mon Sep 17 00:00:00 2001 From: wwwwwwwww <526125649@qq.com> Date: Wed, 1 Jul 2026 11:32:39 +0800 Subject: [PATCH] 1 --- video-gen-api/app/api/v1/credits.py | 72 +++++++++-------------------- 1 file changed, 21 insertions(+), 51 deletions(-) diff --git a/video-gen-api/app/api/v1/credits.py b/video-gen-api/app/api/v1/credits.py index 97fa6426..e7afaff9 100644 --- a/video-gen-api/app/api/v1/credits.py +++ b/video-gen-api/app/api/v1/credits.py @@ -48,70 +48,40 @@ async def get_credit_ratios( current_user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db), ): - import json - - async def get_engine_with_ratios(gen_type: str, engines: list): - for engine in engines: + async def get_ratios_for_engine_type(gen_type: str, engine_ids: list): + for engine_id in engine_ids: result = await db.execute( select(CreditRatio) .where(CreditRatio.gen_type == gen_type) - .where(CreditRatio.model_config_id == engine.id) + .where(CreditRatio.model_config_id == engine_id) ) ratios = result.scalars().all() if ratios: - ratios_out = [CreditRatioOut.model_validate(r) for r in ratios] - engine_info = { - "id": engine.id, - "name": engine.name, - "provider": engine.provider, - "ratios": ratios_out, - } - if gen_type == "video": - try: - supported_ratios = json.loads(engine.supported_ratios) if engine.supported_ratios else [] - except Exception: - supported_ratios = [] - try: - supported_resolutions = json.loads(engine.supported_resolutions) if engine.supported_resolutions else [] - except Exception: - supported_resolutions = [] - try: - supported_durations = json.loads(engine.supported_durations) if engine.supported_durations else [] - except Exception: - supported_durations = [] - engine_info.update({ - "supported_ratios": supported_ratios, - "supported_resolutions": supported_resolutions, - "supported_durations": supported_durations, - "max_duration": engine.max_duration, - "max_image_count": engine.max_image_count, - "max_video_count": engine.max_video_count, - }) - return engine_info - return None - + return [CreditRatioOut.model_validate(r) for r in ratios] + return [] + video_engines_result = await db.execute( - select(VideoEngine) + select(VideoEngine.id) .where(VideoEngine.is_active == True) .order_by(VideoEngine.priority.desc()) ) - video_engines = video_engines_result.scalars().all() - + video_engine_ids = video_engines_result.scalars().all() + image_engines_result = await db.execute( - select(ImageEngine) + select(ImageEngine.id) .where(ImageEngine.is_active == True) .order_by(ImageEngine.priority.desc()) ) - image_engines = image_engines_result.scalars().all() - + image_engine_ids = image_engines_result.scalars().all() + grouped = {} - - video_data = await get_engine_with_ratios("video", video_engines) - if video_data: - grouped["video"] = video_data - - image_data = await get_engine_with_ratios("image", image_engines) - if image_data: - grouped["image"] = image_data - + + video_ratios = await get_ratios_for_engine_type("video", video_engine_ids) + if video_ratios: + grouped["video"] = video_ratios + + image_ratios = await get_ratios_for_engine_type("image", image_engine_ids) + if image_ratios: + grouped["image"] = image_ratios + return grouped