stable-diffusion-webui icon indicating copy to clipboard operation
stable-diffusion-webui copied to clipboard

[Bug]: fastapi 0.91 app.add_middleware

Open JustFrederik opened this issue 2 years ago • 48 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues and checked the recent builds/commits

What happened?

I tried to run the project. It failed to attach the middleware in 'webui.py' line 232, because the server was already running. I downgraded fastapi to 0.90 and it works fine.

Steps to reproduce the problem

  1. clone project
  2. run webui.sh

What should have happened?

The the should have started

Commit where the problem happens

ea9bd9fc7409109adcd61b897abc2c8881161256

What platforms do you use to access the UI ?

Linux

What browsers do you use to access the UI ?

Mozilla Firefox

Command Line Arguments

No

List of extensions

No

Console logs

File "/home/user/code/sdw/webui.py", line 232, in webui
app.add_middleware(GZipMiddleware, minimum_size=1000)
File "/home/user/code/sdw/venv/lib/python3.10/site-packages/starletee/application.py", line 135, in add_middleware raise RuntimeError("Cannot add middleware after an application has started")

Additional information

Fastapi version causes problem no changes to project using arch

JustFrederik avatar Feb 10 '23 15:02 JustFrederik

can confirm. i just installed webui two times and that happens too. Mine on windows

venv "I:\stable-diffusion-webui\venv\Scripts\Python.exe"
Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug  1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)]
Commit hash: ea9bd9fc7409109adcd61b897abc2c8881161256
Installing torch and torchvision
Looking in indexes: https://pypi.org/simple, https://download.pytorch.org/whl/cu117
Collecting torch==1.13.1+cu117
  Using cached https://download.pytorch.org/whl/cu117/torch-1.13.1%2Bcu117-cp310-cp310-win_amd64.whl (2255.4 MB)
Collecting torchvision==0.14.1+cu117
  Using cached https://download.pytorch.org/whl/cu117/torchvision-0.14.1%2Bcu117-cp310-cp310-win_amd64.whl (4.8 MB)
Collecting typing-extensions
  Using cached typing_extensions-4.4.0-py3-none-any.whl (26 kB)
Collecting pillow!=8.3.*,>=5.3.0
  Using cached Pillow-9.4.0-cp310-cp310-win_amd64.whl (2.5 MB)
Collecting numpy
  Using cached numpy-1.24.2-cp310-cp310-win_amd64.whl (14.8 MB)
Collecting requests
  Using cached requests-2.28.2-py3-none-any.whl (62 kB)
Collecting certifi>=2017.4.17
  Using cached certifi-2022.12.7-py3-none-any.whl (155 kB)
Collecting urllib3<1.27,>=1.21.1
  Using cached urllib3-1.26.14-py2.py3-none-any.whl (140 kB)
Collecting charset-normalizer<4,>=2
  Using cached charset_normalizer-3.0.1-cp310-cp310-win_amd64.whl (96 kB)
Collecting idna<4,>=2.5
  Using cached idna-3.4-py3-none-any.whl (61 kB)
Installing collected packages: charset-normalizer, urllib3, typing-extensions, pillow, numpy, idna, certifi, torch, requests, torchvision
Successfully installed certifi-2022.12.7 charset-normalizer-3.0.1 idna-3.4 numpy-1.24.2 pillow-9.4.0 requests-2.28.2 torch-1.13.1+cu117 torchvision-0.14.1+cu117 typing-extensions-4.4.0 urllib3-1.26.14

[notice] A new release of pip available: 22.2.1 -> 23.0
[notice] To update, run: I:\stable-diffusion-webui\venv\Scripts\python.exe -m pip install --upgrade pip
Installing gfpgan
Installing clip
Installing open_clip
Cloning Stable Diffusion into repositories\stable-diffusion-stability-ai...
Cloning Taming Transformers into repositories\taming-transformers...
Cloning K-diffusion into repositories\k-diffusion...
Cloning CodeFormer into repositories\CodeFormer...
Cloning BLIP into repositories\BLIP...
Installing requirements for CodeFormer
Installing requirements for Web UI
Launching Web UI with arguments:
No module 'xformers'. Proceeding without it.
Moving kl-f8-anime2.ckpt from I:\stable-diffusion-webui\models to I:\stable-diffusion-webui\models\Stable-diffusion.
Calculating sha256 for I:\stable-diffusion-webui\models\Stable-diffusion\kl-f8-anime2.ckpt: df3c506e51b7ee1d7b5a6a2bb7142d47d488743c96aa778afb0f53a2cdc2d38d
Loading weights [df3c506e51] from I:\stable-diffusion-webui\models\Stable-diffusion\kl-f8-anime2.ckpt
Creating model from config: I:\stable-diffusion-webui\configs\v1-inference.yaml
LatentDiffusion: Running in eps-prediction mode
DiffusionWrapper has 859.52 M params.
Applying cross attention optimization (Doggettx).
Textual inversion embeddings loaded(0):
Model loaded in 27.0s (calculate hash: 2.1s, load weights from disk: 0.3s, create model: 12.9s, apply half(): 1.4s, move model to device: 1.3s, load textual inversion embeddings: 8.9s).
Running on local URL:  http://127.0.0.1:7860

To create a public link, set `share=True` in `launch()`.
Traceback (most recent call last):
  File "I:\stable-diffusion-webui\launch.py", line 361, in <module>
    start()
  File "I:\stable-diffusion-webui\launch.py", line 356, in start
    webui.webui()
  File "I:\stable-diffusion-webui\webui.py", line 232, in webui
    app.add_middleware(GZipMiddleware, minimum_size=1000)
  File "I:\stable-diffusion-webui\venv\lib\site-packages\starlette\applications.py", line 135, in add_middleware
    raise RuntimeError("Cannot add middleware after an application has started")
RuntimeError: Cannot add middleware after an application has started
Press any key to continue . . .

etherealxx avatar Feb 10 '23 16:02 etherealxx

I am having the same issue as the above

Traceback (most recent call last):
  File "D:\stable-diffusion-webui-master\launch.py", line 361, in <module>
    start()
  File "D:\stable-diffusion-webui-master\launch.py", line 356, in start
    webui.webui()
  File "D:\stable-diffusion-webui-master\webui.py", line 232, in webui
    app.add_middleware(GZipMiddleware, minimum_size=1000)
  File "D:\stable-diffusion-webui-master\venv\lib\site-packages\starlette\applications.py", line 135, in add_middleware
    raise RuntimeError("Cannot add middleware after an application has started")
RuntimeError: Cannot add middleware after an application has started
Press any key to continue . . .

GingerSkippy avatar Feb 10 '23 16:02 GingerSkippy

Happens as well on the colab version

Loading weights [eb172d270d] from /content/drive/MyDrive/AI/models/HassanBlend.ckpt Creating model from config: /content/stable-diffusion-webui/configs/v1-inference.yaml LatentDiffusion: Running in eps-prediction mode DiffusionWrapper has 859.52 M params. Applying xformers cross attention optimization. Textual inversion embeddings loaded(0): Model loaded in 39.2s (load weights from disk: 27.6s, create model: 3.5s, apply weights to model: 5.3s, apply half(): 1.6s, load VAE: 0.3s, move model to device: 0.8s). Running on local URL: http://127.0.0.1:7860/ Running on public URL: https://d0865c11-9b56-488f.gradio.live/

This share link expires in 72 hours. For free permanent hosting and GPU upgrades (NEW!), check out Spaces: https://huggingface.co/spaces Traceback (most recent call last): File "/content/stable-diffusion-webui/launch.py", line 361, in start() File "/content/stable-diffusion-webui/launch.py", line 356, in start webui.webui() File "/content/stable-diffusion-webui/webui.py", line 232, in webui app.add_middleware(GZipMiddleware, minimum_size=1000) File "/usr/local/envs/automatic/lib/python3.10/site-packages/starlette/applications.py", line 135, in add_middleware raise RuntimeError("Cannot add middleware after an application has started") RuntimeError: Cannot add middleware after an application has started Killing tunnel 127.0.0.1:7860 <> https://d0865c11-9b56-488f.gradio.live/

Malegiraldo22 avatar Feb 10 '23 16:02 Malegiraldo22

I'm having the same problem. Perhaps the problem is due to fastapi I have temporarily resolved by downgrade to 0.90.0

panta5 avatar Feb 10 '23 16:02 panta5

downgrade fastapi to version 0.89.1 should solve the issue

NCTUyoung avatar Feb 10 '23 16:02 NCTUyoung

Is there a "guide" for how to do that on Windows?

mews-se avatar Feb 10 '23 16:02 mews-se

0.90 is working tools and this is how u downgrade: https://stackoverflow.com/questions/5226311/installing-specific-package-version-with-pip#5226504

JustFrederik avatar Feb 10 '23 16:02 JustFrederik

pip install fastapi==0.90 or 0.89.1 didn't help for me.

martinjonesbe avatar Feb 10 '23 16:02 martinjonesbe

pip install fastapi==0.90 or 0.89.1 didn't help for me.

you can try with --upgrade flag.

panta5 avatar Feb 10 '23 16:02 panta5

my bad version 0.90. doesn’t exist. It would be 0.90.1

JustFrederik avatar Feb 10 '23 16:02 JustFrederik

Broken for me on Windows and Ubuntu.

How to fix:

  1. Navigate to your Stable Diffusion folder, for me it is: ~/artificial-intelligence-tools/stable-diffusion-webui
  2. Activate your Stable Diffusion environment: source ./venv/bin/activate
  3. Downgrade fastapi: pip install fastapi==0.90.1
  4. Exit the Stable Diffusion environment: deactivate
  5. Start Stable Diffusion how you normally do.
  6. Generate away!

rodrigo-barraza avatar Feb 10 '23 16:02 rodrigo-barraza

it's this change: https://github.com/encode/starlette/commit/51c1de1839ee187bb20de5748fca23baa37369c6 they add some kind of check

kamilkrzywda avatar Feb 10 '23 16:02 kamilkrzywda

Broken for me on Windows and Ubuntu.

How to fix:

  1. Navigate to your Stable Diffusion folder, for me it is: ~/artificial-intelligence-tools/stable-diffusion-webui
  2. Activate your Stable Diffusion environment: source ./venv/bin/activate
  3. Downgrade fastapi: pip install fastapi==0.90.1
  4. Exit the Stable Diffusion environment: deactivate
  5. Start Stable Diffusion how you normally do.
  6. Generate away!

It helps me to fix. thanks

konstdea avatar Feb 10 '23 16:02 konstdea

Broken for me on Windows and Ubuntu.

How to fix:

1. Navigate to your Stable Diffusion folder, for me it is: `~/artificial-intelligence-tools/stable-diffusion-webui`

2. Activate your Stable Diffusion environment: `source ./venv/bin/activate`

3. Downgrade fastapi: `pip install fastapi==0.90.1`

4. Exit the Stable Diffusion environment: `deactivate`

5. Start Stable Diffusion how you normally do.

6. Generate away!

What to do when one doesn't even have /bin/ yet if I might ask?

DrChristophFH avatar Feb 10 '23 16:02 DrChristophFH

!sed -i '$a fastapi==0.90.0' requirements_versions.txt for colab add this line fastapi==0.90.0 in requirements_versions.txt for pc linux unix macos samsung refrigerators ...

camenduru avatar Feb 10 '23 16:02 camenduru

For Windows I started a terminal in my stable diffusion folder and ran

.\venv\Scripts\python.exe -m pip install --upgrade fastapi==0.90.1

That solved my problem

mews-se avatar Feb 10 '23 16:02 mews-se

soo is it 0.90.1 or 0.90.0?

etherealxx avatar Feb 10 '23 16:02 etherealxx

What to do when one doesn't even have /bin/ yet if I might ask?

Start your environment first to get everything where it needs to be: bash <(wget -qO- https://raw.githubusercontent.com/AUTOMATIC1111/stable-diffusion-webui/master/webui.sh) or if you're more familiar with this, run the webui.sh or webui-user.sh files. Then proceed with the instructions above.

rodrigo-barraza avatar Feb 10 '23 16:02 rodrigo-barraza

soo is it 0.90.1 or 0.90.0?

Either or should work I believe, but I had success with 0.90.1.

rodrigo-barraza avatar Feb 10 '23 16:02 rodrigo-barraza

Downgrading to 0.90.0 or 0.90.1 or 0.89.0 did not do the trick for me i am receiveing the same error.

Blood4u avatar Feb 10 '23 16:02 Blood4u

@mews-se thanks - that worked great for me (Windows 11), v0.90.1 seems to be just fine.

photonstorm avatar Feb 10 '23 17:02 photonstorm

!sed -i '$a fastapi==0.90.0' requirements_versions.txt for colab add this line fastapi==0.90.0 in requirements_versions.txt for pc linux unix macos samsung refrigerators ...

the colab line didn't work for me /bin/bash: /usr/local/envs/automatic/lib/libtinfo.so.6: no version information available (required by /bin/bash) sed: can't read requirements_versions.txt: No such file or directory

I guess that it happens because all the environment configuration ends inside a .tar.zst file but I don't know. I'm totally noob on this

Malegiraldo22 avatar Feb 10 '23 17:02 Malegiraldo22

I had a similar issue. I've no idea if this is useful for anyone, just putting this out here:

I have 2 users on Linux Mint 20.3. User 1 installed it fine, no problem. But User 2 install script fail right after starting the server, and quits with the error message:

Traceback (most recent call last): File "launch.py", line 361, in start() File "launch.py", line 356, in start webui.webui() File "/home/USER2/stable-diffusion-webui/webui.py", line 232, in webui app.add_middleware(GZipMiddleware, minimum_size=1000) File "/home/USER2/stable-diffusion-webui/venv/lib/python3.8/site-packages/starlette/applications.py", line 135, in add_middleware raise RuntimeError("Cannot add middleware after an application has started") RuntimeError: Cannot add middleware after an application has started

Both users installed it correctly afaik (pre-requisites etc). Simply deleting the stable-diffusion-webui folder and re-install resulted in the same error. One thing is that User 2 had Anaconda installed prior (but deleted), for another version of stable diffusion (also deleted) (I think it was from this how-to: https://www.howtogeek.com/830179/how-to-run-stable-diffusion-on-your-pc-to-generate-ai-images/ )

I resorted to simply copy the install folder from user 1 and overwrite into user 2's install, and take ownership of the files. That worked!

Apparently not everything got installed the for User 2, I've no idea why.

rar0n avatar Feb 10 '23 17:02 rar0n

@Malegiraldo22 please try this https://github.com/camenduru/stable-diffusion-webui-colab

camenduru avatar Feb 10 '23 17:02 camenduru

I am running in paperspace, what should I do? If anyone has the same environment, please advise.

gihepa988 avatar Feb 10 '23 18:02 gihepa988

@gihepa988 requirements_versions.txt doesn't seem reliable. Replacing in requirements.txt works for me instead.

!sed -i 's/fastapi/fastapi==0.90.1/' requirements.txt

GoAwayNow avatar Feb 10 '23 19:02 GoAwayNow

For Windows I started a terminal in my stable diffusion folder and ran

.\venv\Scripts\python.exe -m pip install --upgrade fastapi==0.90.1

That solved my problem

Thank you!

snatvb avatar Feb 10 '23 19:02 snatvb

I am running in paperspace, what should I do? If anyone has the same environment, please advise.

if its like other systems, pip uninstall fastapi then do pip install fastapi==0.90.1 from your venv.

the fastapi package wasn't locked to a specific version in webui, and when they released a new version, anyone installing fresh will download the newest version

ClashSAN avatar Feb 10 '23 20:02 ClashSAN

I can confirm both the problem

webui-docker-auto-1 | To create a public link, setshare=Trueinlaunch(). webui-docker-auto-1 | Traceback (most recent call last): webui-docker-auto-1 | File "/stable-diffusion-webui/webui.py", line 194, in <module> webui-docker-auto-1 | webui() webui-docker-auto-1 | File "/stable-diffusion-webui/webui.py", line 162, in webui webui-docker-auto-1 | app.add_middleware(GZipMiddleware, minimum_size=1000) webui-docker-auto-1 | File "/usr/local/lib/python3.10/site-packages/starlette/applications.py", line 135, in add_middleware webui-docker-auto-1 | raise RuntimeError("Cannot add middleware after an application has started") webui-docker-auto-1 | RuntimeError: Cannot add middleware after an application has started

and the solution fastapi==0.90.1 works.

Because the requirements.txt and requiremenst_version.txt were being overridden, I added the pip command to the Dockerfile;

74 ARG SHA=44c46f0ed395967cd3830dd481a2db759fda5b3b 75 RUN <<EOF 76 cd stable-diffusion-webui 77 git fetch 78 git reset --hard ${SHA} 79 pip install -r requirements_versions.txt 80 EOF 81 82 RUN pip install opencv-python-headless transformers==4.24.0 83 RUN pip install --upgrade fastapi==0.90.1 84 85 COPY . /docker 86 87 RUN <<EOF 88 python3 /docker/info.py ${ROOT}/modules/ui.py 89 mv ${ROOT}/style.css ${ROOT}/user.css 90 sed -i 's/os.rename(tmpdir, target_dir)/shutil.move(tmpdir,target_dir)/' ${ROOT}/modules/ui_extensions.py 91 EOF 92 93 WORKDIR ${ROOT} 94 ENV CLI_ARGS="" 95 EXPOSE 7860 96 ENTRYPOINT ["/docker/entrypoint.sh"] 97 CMD python3 -u webui.py --listen --port 7860 ${CLI_ARGS}

This will obviously need to be undone when a proper fix is put in place. But my next git pull will take care of that for me.

shanec42 avatar Feb 10 '23 21:02 shanec42

Anyone know how to do this in Colab using "fast_stable_diffusion_AUTOMATIC1111"? I tried !pip uninstall fastapi and then !pip install fastapi==0.90.1 and it just threw a ton of errors.

jameslanman avatar Feb 10 '23 22:02 jameslanman