mrhan1993 / Fooocus-API

FastAPI powered API for Fooocus
GNU General Public License v3.0
567 stars 152 forks source link

[BUG] Error Saving Data in SQLite3: Field 'image_prompts' is Not Serializable #389

Closed BaiMoHan closed 1 month ago

BaiMoHan commented 1 month ago

Steps to Reproduce

  1. Request the API path /v2/generation/image-prompt and provide a valid value for the field "image_prompts," such as:
    {
    "prompt": "one girl",
    "negative_prompt": "unrealistic, saturated, high contrast, big nose, painting, drawing, sketch, cartoon, anime, manga, render, CG, 3d, watermark, signature, label",
    "style_selections": [
        "Fooocus V2",
        "Fooocus Enhance",
        "Fooocus Photograph"
    ],
    "performance_selection": "Speed",
    "aspect_ratios_selection": "768*1280",
    "image_number": 2,
    "image_seed": 5787836363000738913,
    "sharpness": 2,
    "guidance_scale": 3,
    "base_model_name": "realisticStockPhoto_v20.safetensors",
    "refiner_model_name": "",
    "refiner_switch": 0.5,
    "loras": [
        {
            "enabled": true,
            "model_name": "SDXL_FILM_PHOTOGRAPHY_STYLE_V1.safetensors",
            "weight": 0.25
        }
    ],
    "advanced_params": {
        "disable_preview": true,
        "disable_intermediate_results": false,
        "disable_seed_increment": false,
        "adm_scaler_positive": 1.5,
        "adm_scaler_negative": 0.8,
        "adm_scaler_end": 0.3,
        "adaptive_cfg": 7,
        "clip_skip": 2,
        "sampler_name": "dpmpp_2m_sde_gpu",
        "scheduler_name": "karras",
        "overwrite_step": 30,
        "overwrite_switch": -1,
        "overwrite_width": -1,
        "overwrite_height": -1,
        "overwrite_vary_strength": -1,
        "overwrite_upscale_strength": -1,
        "mixing_image_prompt_and_vary_upscale": false,
        "mixing_image_prompt_and_inpaint": false,
        "debugging_cn_preprocessor": false,
        "skipping_cn_preprocessor": false,
        "canny_low_threshold": 64,
        "canny_high_threshold": 128,
        "refiner_swap_method": "joint",
        "controlnet_softness": 0.25,
        "freeu_enabled": false,
        "freeu_b1": 1.01,
        "freeu_b2": 1.02,
        "freeu_s1": 0.99,
        "freeu_s2": 0.95,
        "debugging_inpaint_preprocessor": false,
        "inpaint_disable_initial_latent": false,
        "inpaint_engine": "v2.6",
        "inpaint_strength": 1,
        "inpaint_respective_field": 0.618,
        "inpaint_mask_upload_checkbox": false,
        "invert_mask_checkbox": false,
        "inpaint_erode_or_dilate": 0,
        "black_out_nsfw": false,
        "vae_name": "Default (model)"
    },
    "save_meta": true,
    "meta_scheme": "fooocus",
    "save_extension": "webp",
    "save_name": "",
    "read_wildcards_in_order": false,
    "require_base64": false,
    "async_process": true,
    "input_image": "",
    "outpaint_selections": [],
    "image_prompts": [
        {
            "cn_img": "https://localhost:7878/background/4901393759684530177",
            "cn_stop": 0.9,
            "cn_weight": 0.75,
            "cn_type": "FaceSwap"
        }
    ]
    }
  2. Check the error logs and pay attention to the error logs of sql_client.py.
    [2024-08-05 02:51:32] ERROR    [Fooocus] Worker error: (builtins.TypeError) Object of type ndarray is not JSON serializable

    So you need to preprocess the ndarray type field. It is the field image_prompts. Its item is a tuple, and the first item of the tuple is an ndarray type field.

    Description

    One easy fix likes below:

    # fooocusapi.sql_client.MySQLAlchemy.store_history.line 1
    serialized_image_prompts = [
            (arr.tolist(), float_val1, float_val2, str_val)
            for arr, float_val1, float_val2, str_val in record['image_prompts']
        ]
    record['image_prompts'] = serialized_image_prompts

    However, you will notice that the database.db stores one large ndarray value. I believe we should ignore storing the list of ndarrays and only store the weights and method of image_prompts.

    image

Environment

Linux 129-153-16-96 6.2.0-37-generic #38~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Nov  2 18:01:13 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

conda pips freeze

accelerate==0.21.0
aiohappyeyeballs==2.3.2
aiohttp==3.10.0
aiosignal==1.3.1
aiosqlite==0.19.0
annotated-types==0.6.0
antlr4-python3-runtime==4.9.3
anyio==4.1.0
arrow==1.3.0
async-lru==2.0.4
async-timeout==4.0.3
attrs==23.1.0
Babel==2.13.1
boltons==24.0.0
certifi==2024.7.4
cffi==1.16.0
chardet==5.2.0
charset-normalizer==3.3.2
click==8.1.7
coloredlogs==15.0.1
colorlog==6.8.2
comm==0.2.0
dacite==1.8.1
debugpy==1.8.0
einops==0.4.1
exceptiongroup==1.2.0
fastapi==0.110.3
fastjsonschema==2.19.0
filelock==3.15.4
flatbuffers==24.3.25
fqdn==1.5.1
frozenlist==1.4.1
fsspec==2024.6.1
greenlet==3.0.3
h11==0.14.0
htmlmin==0.1.12
httptools==0.6.1
huggingface-hub==0.24.3
humanfriendly==10.0
idna==3.7
ImageHash==4.3.1
ipywidgets==8.1.1
isoduration==20.11.0
Jinja2==3.1.4
json5==0.9.14
jsonschema==4.20.0
jsonschema-specifications==2023.11.2
jupyter-events==0.9.0
jupyter-lsp==2.2.1
jupyter-ydoc==1.1.1
jupyter_client==8.6.0
jupyter_collaboration==1.2.0
jupyter_core==5.5.0
jupyter_server==2.12.0
jupyter_server_fileid==0.9.0
jupyter_server_terminals==0.4.4
jupyterlab==4.0.9
jupyterlab-widgets==3.0.9
jupyterlab_server==2.25.2
lightning-utilities==0.11.6
llvmlite==0.41.1
markdown-it-py==3.0.0
MarkupSafe==2.1.5
mdurl==0.1.2
mistune==3.0.2
mpmath==1.3.0
multidict==6.0.5
multimethod==1.10
nbconvert==7.12.0
nbformat==5.9.2
networkx==3.3
notebook_shim==0.2.3
numba==0.58.1
numpy==1.25.2
nvidia-cublas-cu12==12.1.3.1
nvidia-cuda-cupti-cu12==12.1.105
nvidia-cuda-nvrtc-cu12==12.1.105
nvidia-cuda-runtime-cu12==12.1.105
nvidia-cudnn-cu12==9.1.0.70
nvidia-cufft-cu12==11.0.2.54
nvidia-curand-cu12==10.3.2.106
nvidia-cusolver-cu12==11.4.5.107
nvidia-cusparse-cu12==12.1.0.106
nvidia-nccl-cu12==2.20.5
nvidia-nvjitlink-cu12==12.5.82
nvidia-nvtx-cu12==12.1.105
omegaconf==2.2.3
onnxruntime==1.16.3
opencv-contrib-python==4.8.0.74
overrides==7.4.0
packaging @ file:///croot/packaging_1720101850331/work
pandas-profiling==3.6.6
patsy==0.5.4
phik==0.12.3
Pillow==9.4.0
protobuf==5.27.2
psutil==5.9.5
pycparser==2.22
pydantic==2.4.2
pydantic_core==2.10.1
pygit2==1.12.2
Pygments==2.18.0
python-dateutil==2.8.2
python-dotenv==1.0.1
python-json-logger==2.0.7
python-multipart==0.0.6
pytorch-lightning==1.9.4
PyWavelets==1.5.0
PyYAML==6.0
pyzmq==25.1.2
referencing==0.31.1
regex==2024.7.24
requests==2.32.3
rfc3339-validator==0.1.4
rfc3986-validator==0.1.1
rich==13.7.1
rpds-py==0.13.2
safetensors==0.3.1
scipy==1.9.3
seaborn==0.12.2
Send2Trash==1.8.2
sniffio==1.3.0
SQLAlchemy==2.0.31
starlette==0.37.2
statsmodels==0.14.0
sympy==1.13.1
tangled-up-in-unicode==0.2.0
timm==0.9.2
tinycss2==1.2.1
tokenizers==0.13.3
tomli==2.0.1
torch==2.4.0
torchmetrics==1.4.0.post0
torchsde==0.2.5
torchvision==0.19.0
tornado==6.4
tqdm==4.64.1
traitlets==5.14.0
trampoline==0.1.2
transformers==4.30.2
triton==3.0.0
typeguard==4.1.5
types-python-dateutil==2.8.19.14
typing_extensions==4.12.2
uri-template==1.3.0
urllib3==2.2.2
uvicorn==0.23.2
uvloop==0.19.0
visions==0.7.5
watchfiles==0.22.0
webcolors==1.13
websockets==12.0
widgetsnbextension==4.0.9
wordcloud==1.9.2
y-py==0.6.2
yarl==1.9.4
ydata-profiling==4.6.3
ypy-websocket==0.12.4
BaiMoHan commented 1 month ago

I have not stored the list of ndarrays. If you agree with this change, I would like to submit a pull request (PR).

mrhan1993 commented 1 month ago

of cause, thx for your work