lllyasviel / Fooocus

Focus on prompting and generating
GNU General Public License v3.0
41.18k stars 5.8k forks source link

API with Gradio Client not working #2063

Closed mqtt-fan closed 8 months ago

mqtt-fan commented 9 months ago

Hi all,

I'm tried to bulk create several images via the python gradio client. Currently I'm running Fooocus on google colab using this notebook (https://colab.research.google.com/github/lllyasviel/Fooocus/blob/main/fooocus_colab.ipynb)

Now I want create a image via a prompt sent by gradio client from my Mac.

I used the example found in the "Issues": gen.py:

from gradio_client import Client

client = Client("https://7c3ae305b3b6380dfc.gradio.live/", serialize=False)
result = client.predict(
    "Howdy!",   # str in 'parameter_10' Textbox component
    "Howdy!",   # str in 'Negative Prompt' Textbox component
    ["Fooocus V2"], # List[str] in 'Selected Styles' Checkboxgroup component
    "Speed",    # str in 'Performance' Radio component
    "1152×896", # <span style='color: grey;'> | 1:2</span>",    # str in 'Aspect Ratios' Radio component
    1,  # int | float (numeric value between 1 and 32)in 'Image Number' Slider component
    "123",  # str in 'Seed' Textbox component
    0,  # int | float (numeric value between 0.0 and 30.0)in 'Image Sharpness' Slider component
    1,  # int | float (numeric value between 1.0 and 30.0)in 'Guidance Scale' Slider component
    "juggernautXL_version6Rundiffusion.safetensors",    # str (Option from: ['juggernautXL_version6Rundiffusion.safetensors', 'realisticStockPhoto_v10.safetensors', 'bluePencilXL_v050.safetensors', 'DreamShaper_8_pruned.safetensors'])in 'Base Model (SDXL only)' Dropdown component
    "None", # str (Option from: ['None', 'juggernautXL_version6Rundiffusion.safetensors', 'realisticStockPhoto_v10.safetensors', 'bluePencilXL_v050.safetensors', 'DreamShaper_8_pruned.safetensors'])in 'Refiner (SDXL or SD 1.5)' Dropdown component
    0.1,    # int | float (numeric value between 0.1 and 1.0)in 'Refiner Switch At' Slider component
    "None", # str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors', 'SDXL_FILM_PHOTOGRAPHY_STYLE_BetaV0.4.safetensors', 'sdxl_lcm_lora.safetensors'])in 'LoRA 1' Dropdown component
    -2, # int | float (numeric value between -2 and 2)in 'Weight' Slider component
    "None", # str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors', 'SDXL_FILM_PHOTOGRAPHY_STYLE_BetaV0.4.safetensors', 'sdxl_lcm_lora.safetensors'])in 'LoRA 2' Dropdown component
    -2, # int | float (numeric value between -2 and 2)in 'Weight' Slider component
    "None", # str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors', 'SDXL_FILM_PHOTOGRAPHY_STYLE_BetaV0.4.safetensors', 'sdxl_lcm_lora.safetensors'])in 'LoRA 3' Dropdown component
    -2, # int | float (numeric value between -2 and 2)in 'Weight' Slider component
    "None", # str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors', 'SDXL_FILM_PHOTOGRAPHY_STYLE_BetaV0.4.safetensors', 'sdxl_lcm_lora.safetensors'])in 'LoRA 4' Dropdown component
    -2, # int | float (numeric value between -2 and 2)in 'Weight' Slider component
    "None", # str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors', 'SDXL_FILM_PHOTOGRAPHY_STYLE_BetaV0.4.safetensors', 'sdxl_lcm_lora.safetensors'])in 'LoRA 5' Dropdown component
    -2, # int | float (numeric value between -2 and 2)in 'Weight' Slider component
    True,   # bool in 'Input Image' Checkbox component
    "Howdy!",   # str in 'parameter_73' Textbox component
    "Disabled", # str in 'Upscale or Variation:' Radio component
    "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==",   # str (filepath or URL to image)in 'Drag above image to here' Image component
    ["Left"],   # List[str] in 'Outpaint Direction' Checkboxgroup component
    "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==",   # str (filepath or URL to image)in 'Drag above image to here' Image component
    "Howdy!",   # str in 'Inpaint Additional Prompt' Textbox component
    "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==",   # str (filepath or URL to image)in 'Image' Image component
    0,  # int | float (numeric value between 0.0 and 1.0)in 'Stop At' Slider component
    0,  # int | float (numeric value between 0.0 and 2.0)in 'Weight' Slider component
    "ImagePrompt",  # str in 'Type' Radio component
    "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==",   # str (filepath or URL to image)in 'Image' Image component
    0,  # int | float (numeric value between 0.0 and 1.0)in 'Stop At' Slider component
    0,  # int | float (numeric value between 0.0 and 2.0)in 'Weight' Slider component
    "ImagePrompt",  # str in 'Type' Radio component
    "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==",   # str (filepath or URL to image)in 'Image' Image component
    0,  # int | float (numeric value between 0.0 and 1.0)in 'Stop At' Slider component
    0,  # int | float (numeric value between 0.0 and 2.0)in 'Weight' Slider component
    "ImagePrompt",  # str in 'Type' Radio component
    "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==",   # str (filepath or URL to image)in 'Image' Image component                                                                            
    0,  # int | float (numeric value between 0.0 and 1.0)in 'Stop At' Slider component                                                                
    0,  # int | float (numeric value between 0.0 and 2.0)in 'Weight' Slider component
    "ImagePrompt",  # str in 'Type' Radio component
    fn_index=29
)
print(result)
Bob@MacBook-Pro-von-Bob Downloads % python3 gen.py
Loaded as API: https://96162f317e640f692f.gradio.live/ ✔
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/gradio_client/client.py", line 831, in _predict
    output = result["data"]
             ~~~~~~^^^^^^^^
KeyError: 'data'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/Bob/Downloads/gen.py", line 4, in <module>
    result = client.predict(
             ^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/gradio_client/client.py", line 291, in predict
    return self.submit(*args, api_name=api_name, fn_index=fn_index).result()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/gradio_client/client.py", line 1120, in result
    return super().result(timeout=timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/concurrent/futures/_base.py", line 456, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/gradio_client/client.py", line 793, in _inner
    predictions = _predict(*data)
                  ^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/gradio_client/client.py", line 837, in _predict
    if "error" in result and "429" in result["error"] and is_public_space:
                             ^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: argument of type 'NoneType' is not iterable

Describe the problem I still got this error: TypeError: argument of type 'NoneType' is not iterable Python Package List on Mac:

pip3 freeze:

aiofiles==23.2.1
altair==5.2.0
annotated-types==0.6.0
anyio==4.2.0
attrs==18.2.0
certifi==2023.11.17
charset-normalizer==3.3.2
click==8.1.7
click-completion==0.5.2
click-didyoumean==0.3.0
click-help-colors==0.9.4
colorama==0.3.9
contourpy==1.2.0
cycler==0.12.1
fastapi==0.109.0
ffmpy==0.3.1
filelock==3.13.1
fonttools==4.47.2
fsspec==2023.12.2
gradient==0.5.2
gradio==3.41.2
gradio_client==0.5.0
h11==0.14.0
halo==0.0.31
httpcore==1.0.2
httpx==0.26.0
huggingface-hub==0.20.3
idna==3.6
importlib-resources==6.1.1
Jinja2==3.1.3
jsonschema==4.21.1
jsonschema-specifications==2023.12.1
kiwisolver==1.4.5
log-symbols==0.0.14
markdown-it-py==3.0.0
MarkupSafe==2.1.4
marshmallow==2.21.0
matplotlib==3.8.2
mdurl==0.1.2
numpy==1.26.3
orjson==3.9.12
packaging==23.2
pandas==2.2.0
pillow==10.2.0
progressbar2==4.3.2
prompt-toolkit==2.0.10
pydantic==2.5.3
pydantic_core==2.14.6
pydub==0.25.1
Pygments==2.17.2
pyparsing==3.1.1
python-dateutil==2.8.2
python-multipart==0.0.6
python-utils==3.8.2
pytz==2023.3.post1
PyYAML==6.0.1
referencing==0.32.1
requests==2.31.0
requests-toolbelt==1.0.0
rich==13.7.0
rpds-py==0.17.1
ruff==0.1.14
scipy==1.12.0
semantic-version==2.10.0
shellingham==1.5.4
six==1.16.0
sniffio==1.3.0
spinners==0.0.24
starlette==0.35.1
termcolor==2.4.0
terminaltables==3.1.10
tomlkit==0.12.0
toolz==0.12.1
tqdm==4.66.1
typer==0.9.0
typing_extensions==4.9.0
tzdata==2023.4
urllib3==2.1.0
uvicorn==0.27.0
wcwidth==0.2.13
websockets==11.0.3

Full Console Log

[Fooocus Model Management] Moving model(s) has taken 0.75 seconds
App started successful. Use the app with http://127.0.0.1:7865/ or 127.0.0.1:7865 or https://96162f317e640f692f.gradio.live/
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/gradio/routes.py", line 488, in run_predict
    output = await app.get_blocks().process_api(
  File "/usr/local/lib/python3.10/dist-packages/gradio/blocks.py", line 1431, in process_api
    result = await self.call_function(
  File "/usr/local/lib/python3.10/dist-packages/gradio/blocks.py", line 1103, in call_function
    prediction = await anyio.to_thread.run_sync(
  File "/usr/local/lib/python3.10/dist-packages/anyio/to_thread.py", line 33, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "/usr/local/lib/python3.10/dist-packages/anyio/_backends/_asyncio.py", line 877, in run_sync_in_worker_thread
    return await future
  File "/usr/local/lib/python3.10/dist-packages/anyio/_backends/_asyncio.py", line 807, in run
    result = context.run(func, *args)
  File "/usr/local/lib/python3.10/dist-packages/gradio/utils.py", line 707, in wrapper
    response = f(*args, **kwargs)
  File "/content/Fooocus/modules/meta_parser.py", line 7, in load_parameter_button_click
    loaded_parameter_dict = json.loads(raw_prompt_txt)
  File "/usr/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.10/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Can someone send me the correct call to generate a picture with positive and negative prompt?

mashb1t commented 9 months ago

ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000)

This indicates that the certificate is not regarded as valid by your local gradio client + the following errors show that the response is empty, which is why the array key check fails. Please check if the certificate used by gradio.live is valid if you open it in browser and if your network firewall/router/adblock filters gradio.live domains and let me know the results.

mqtt-fan commented 9 months ago

Hi @mashb1t

I'm sorry, I copied an old try instead of the script. Now I edited the issue. (I used this script: https://github.com/lllyasviel/Fooocus/issues/1293#issuecomment-1848712861)

Can you take a look at the script, why it says: TypeError: argument of type 'NoneType' is not iterable

The fooocus logs are telling this: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) So I thing the request from the python script must be wrong

mashb1t commented 9 months ago

The Fooocus interface has changed a bit since the other issue has been opened. Please find the current request example in the API definition in the Footer of Fooocus, fn_index: 32 or 33 (currently not certain).

mqtt-fan commented 9 months ago

Thanks for the hint, it is getting stranger and stranger.

If I used 33:

from gradio_client import Client

client = Client("https://7c3ae305b3b6380dfc.gradio.live/")
result = client.predict(
                "Howdy!",   # str in 'parameter_10' Textbox component
                "Howdy!",   # str in 'Negative Prompt' Textbox component
                ["Fooocus V2"], # List[str] in 'Selected Styles' Checkboxgroup component
                "Speed",    # str in 'Performance' Radio component
                "704×1408 <span style="color: grey;"> ∣ 1:2</span>",   
                1,  # int | float (numeric value between 1 and 32)
                "Howdy!",   # str in 'Seed' Textbox component
                0,  # int | float (numeric value between 0.0 and 30.0)
                1,  # int | float (numeric value between 1.0 and 30.0)
                "juggernautXL_version6Rundiffusion.safetensors",    # str (Option from: ['juggernautXL_version6Rundiffusion.safetensors', 'realism_engine_sdxl.safetensors'])
                "None", # str (Option from: ['None', 'juggernautXL_version6Rundiffusion.safetensors', 'realism_engine_sdxl.safetensors'])
                0.1,    # int | float (numeric value between 0.1 and 1.0)
                "None", # str (Option from: ['None'])
                -2, # int | float (numeric value between -2 and 2)
                "None", # str (Option from: ['None'])
                -2, # int | float (numeric value between -2 and 2)
                "None", # str (Option from: ['None'])
                -2, # int | float (numeric value between -2 and 2)
                "None", # str (Option from: ['None'])
                -2, # int | float (numeric value between -2 and 2)
                "None", # str (Option from: ['None'])
                -2, # int | float (numeric value between -2 and 2)
                True,   # bool in 'Input Image' Checkbox component
                "Howdy!",   # str in 'parameter_85' Textbox component
                "Disabled", # str in 'Upscale or Variation:' Radio component
                "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png", # str (filepath or URL to image)
                ["Left"],   # List[str] in 'Outpaint Direction' Checkboxgroup component
                "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png", # str (filepath or URL to image)
                "Howdy!",   # str in 'Inpaint Additional Prompt' Textbox component
                "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png", # str (filepath or URL to image)
                "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png", # str (filepath or URL to image)
                0,  # int | float (numeric value between 0.0 and 1.0)
                0,  # int | float (numeric value between 0.0 and 2.0)
                "ImagePrompt",  # str in 'Type' Radio component
                "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png", # str (filepath or URL to image)
                0,  # int | float (numeric value between 0.0 and 1.0)
                0,  # int | float (numeric value between 0.0 and 2.0)
                "ImagePrompt",  # str in 'Type' Radio component
                "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png", # str (filepath or URL to image)
                0,  # int | float (numeric value between 0.0 and 1.0)
                0,  # int | float (numeric value between 0.0 and 2.0)
                "ImagePrompt",  # str in 'Type' Radio component
                "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png", # str (filepath or URL to image)
                0,  # int | float (numeric value between 0.0 and 1.0)
                0,  # int | float (numeric value between 0.0 and 2.0)
                "ImagePrompt",  # str in 'Type' Radio component
                fn_index=33
)
print(result)

I got:

Bob@MacBook-Pro-von-Bob Downloads % python3 gen.py
  File "/Users/Bib/Downloads/gen.py", line 9
    "704×1408 <span style="color: grey;"> ∣ 1:2</span>",   
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?

after change it to

result = client.predict(
                "Howdy!",   # str in 'parameter_10' Textbox component
                "Howdy!",   # str in 'Negative Prompt' Textbox component
                ["Fooocus V2"], # List[str] in 'Selected Styles' Checkboxgroup component
                "Speed",    # str in 'Performance' Radio component
                "704×1408",   ...

I got the error: ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000)

But the url works fine in my browser and the is no issue at all

mqtt-fan commented 9 months ago

Looks like a issue with gradio and colab. Maybe someone can take a deeper look into this?

mqtt-fan commented 9 months ago

Can maybe someone reproduce to check if it is not an issue on my side?

sumeetweb commented 9 months ago

Gradio v3.9 isn't working properly. It's throwing websocket 1006 error in Windows. Fooocus could try updating Gradio to latest v4.

https://github.com/gradio-app/gradio/issues/6591

mashb1t commented 9 months ago

Just an addition: Fooocus isn't using gradio 3.9 but 3.41.2. Upgrading to 4.x would be a major effort, which is not planned in the near future as far as i know.

sumeetweb commented 9 months ago

Thanks for response. I will try spinning it in Ubuntu, sometime later and check if it fixes the issue or not.

mashb1t commented 8 months ago

@sumeetweb what's the result of your testing?

mashb1t commented 8 months ago

Closing as stale. One more hint: disable all VPNs before using Fooocus might help.