From affd4db6f0e8fc8fac8b42732b265237cbfbdfdc Mon Sep 17 00:00:00 2001 From: zcr Date: Wed, 18 Mar 2026 12:21:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Eflux2=20klein=20=E5=9B=BE?= =?UTF-8?q?=E5=83=8F=E7=94=9F=E6=88=90=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 2 + src/core/config.py | 1 + src/routers/flux2_gen_img.py | 72 ++++++++++++++++++++++++++++++++++++ src/schemas/flux2_gen_img.py | 14 +++++++ 4 files changed, 89 insertions(+) create mode 100644 src/routers/flux2_gen_img.py create mode 100644 src/schemas/flux2_gen_img.py diff --git a/main.py b/main.py index 578c3d5..ee49aa8 100644 --- a/main.py +++ b/main.py @@ -7,6 +7,7 @@ from fastapi.middleware.cors import CORSMiddleware from logging_env import LOGGER_CONFIG_DICT from src.routers import chat, deep_agent_chat from src.routers import generate_3D +from src.routers import flux2_gen_img logging.config.dictConfig(LOGGER_CONFIG_DICT) @@ -28,6 +29,7 @@ app_server.add_middleware( app_server.include_router(chat.router) app_server.include_router(deep_agent_chat.router) app_server.include_router(generate_3D.router) +app_server.include_router(flux2_gen_img.router) @app_server.get("/") diff --git a/src/core/config.py b/src/core/config.py index 38415fb..2394b06 100644 --- a/src/core/config.py +++ b/src/core/config.py @@ -34,6 +34,7 @@ class Settings(BaseSettings): # --- 本地服务器配置信息 --- IMAGE_TO_3D_MODEL_URL: str = Field(default='', description="") + FLUX2_GEN_IMG_MODEL_URL: str = Field(default='', description="") # --- 外部工具api配置信息 --- TAVILY_API_KEY: str = Field(default="", description="") diff --git a/src/routers/flux2_gen_img.py b/src/routers/flux2_gen_img.py new file mode 100644 index 0000000..27004a1 --- /dev/null +++ b/src/routers/flux2_gen_img.py @@ -0,0 +1,72 @@ +import json +import logging +import httpx +from fastapi import APIRouter + +from src.core.config import settings +from src.schemas.flux2_gen_img import Flux2_Gen_Img_Model +from src.schemas.response_template import ResponseModel + +logger = logging.getLogger(__name__) +router = APIRouter(prefix="/canvas", tags=["Furniture Canvas"]) + + +@router.post("/flux2_gen_img") +async def flux2_gen_img(request_data: Flux2_Gen_Img_Model): + """ + ### 参数说明: + + - **bucket_name**: OSS桶名 (必填) + - **object_name**: OSS对象名(文件路径)(必填) + + - **input_image_paths**: 输入图片路径列表 (非必填,默认[]) + - **width**: 图片宽度,默认512像素 (非必填,默认512) + - **height**: 图片高度,默认512像素 (非必填,默认512) + - **prompt**: 文本提示词,用于模型推理等场景 (非必填,默认"") + - **steps**: 推理步数,控制模型生成过程的迭代次数 (非必填,默认4) + - **guidance**: 引导系数,调节提示词对生成结果的影响程度 (非必填,默认 4.0 ) + + ### 请求体示例: + ``` + { + "input_image_paths": ["test/typical_building_space_station.png","test/typical_creature_dragon.png"], + "width": 512, + "height": 512, + "bucket_name": "my-oss-bucket", + "object_name": "generated_images/result.jpg", + "prompt": "a beautiful landscape with mountains and rivers", + "steps": 4, + "guidance": 4.0 + } + ```` + + ### 输出示例: + ``` + { + "code": 200, + "msg": "OK!", + "data": { + "output_path": "test/generated_images/result.jpg" + } + } + ``` + """ + try: + logger.info( + f"flux2_gen_img request: {json.dumps(request_data.model_dump(), indent=4)}" + ) + + async with httpx.AsyncClient(timeout=120) as client: + resp = await client.post( + f"http://{settings.FLUX2_GEN_IMG_MODEL_URL}/predict", + json=request_data.model_dump(), + ) + + result = resp.json() + + logger.info(f"flux2_gen_img response: {json.dumps(result, indent=4)}") + + return ResponseModel(data=result) + + except Exception as e: + logger.warning(f"img_to_3D Run Exception: {e}") diff --git a/src/schemas/flux2_gen_img.py b/src/schemas/flux2_gen_img.py new file mode 100644 index 0000000..884142b --- /dev/null +++ b/src/schemas/flux2_gen_img.py @@ -0,0 +1,14 @@ +from typing import List, Optional + +from pydantic import BaseModel, Field + + +class Flux2_Gen_Img_Model(BaseModel): + input_image_paths: Optional[List[str]] = Field(..., description="输入图片路径列表") + width: Optional[int] = Field(default=512, description="图片宽度,默认512像素") + height: Optional[int] = Field(default=512, description="图片高度,默认512像素") + bucket_name: Optional[str] = Field(default="", description="OSS桶名,不传则为None") + object_name: Optional[str] = Field(default="", description="OSS对象名(文件路径),不传则为None") + prompt: Optional[str] = Field(default="", description="文本提示词,用于模型推理等场景") + steps: Optional[int] = Field(default=4, description="推理步数,控制模型生成过程的迭代次数") + guidance: Optional[float] = Field(default=4.0, description="引导系数,调节提示词对生成结果的影响程度")