0

This is the code:

from auth_token import auth_token
from fastapi import FastAPI, Response
from fastapi.middleware.cors import CORSMiddleware
import torch
from torch import autocast
from diffusers import StableDiffusionPipeline
from io import BytesIO
import base64 


app = FastAPI()

app.add_middleware(
    CORSMiddleware, 
    allow_credentials=True, 
    allow_origins=["*"], 
    allow_methods=["*"], 
    allow_headers=["*"]
)

device = "cuda"
model_id = "CompVis/stable-diffusion-v1-4"
pipe = StableDiffusionPipeline.from_pretrained(model_id, revision="fp16", torch_dtype=torch.float16, use_auth_token=auth_token)
pipe.to(device)

@app.get("/")
def generate(prompt: str): 
    with autocast(device): 
        image = pipe(prompt, guidance_scale=8.5).images[0]

    image.save("testimage.png")
    buffer = BytesIO()
    image.save(buffer, format="PNG")
    imgstr = base64.b64encode(buffer.getvalue())

    return Response(content=imgstr, media_type="image/png")

And the error is:

safety_checker\model.safetensors not found
Loading pipeline components...:  71%|█████████████████████████████████████████████████████████████████████████████▊                               | 5/7 [00:01<00:00,  3.89it/s]C:\Users\mouba\AppData\Roaming\Python\Python311\site-packages\transformers\models\clip\feature_extraction_clip.py:28: FutureWarning: The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please use CLIPImageProcessor instead.
  warnings.warn(
`text_config_dict` is provided which will be used to initialize `CLIPTextConfig`. The value `text_config["id2label"]` will be overriden.
`text_config_dict` is provided which will be used to initialize `CLIPTextConfig`. The value `text_config["bos_token_id"]` will be overriden.
`text_config_dict` is provided which will be used to initialize `CLIPTextConfig`. The value `text_config["eos_token_id"]` will be overriden.
Loading pipeline components...: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████| 7/7 [00:01<00:00,  3.63it/s]
Traceback (most recent call last):
  File "g:\Projects\Code-That-ReactStableDiffusion-main\api\api.py", line 23, in <module>
    pipe.to(device)
  File "C:\Users\mouba\AppData\Roaming\Python\Python311\site-packages\diffusers\pipelines\pipeline_utils.py", line 727, in to
    module.to(torch_device, torch_dtype)
  File "C:\Users\mouba\AppData\Roaming\Python\Python311\site-packages\torch\nn\modules\module.py", line 1145, in to
    return self._apply(convert)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\mouba\AppData\Roaming\Python\Python311\site-packages\torch\nn\modules\module.py", line 797, in _apply
    module._apply(fn)
  File "C:\Users\mouba\AppData\Roaming\Python\Python311\site-packages\torch\nn\modules\module.py", line 797, in _apply
    module._apply(fn)
  File "C:\Users\mouba\AppData\Roaming\Python\Python311\site-packages\torch\nn\modules\module.py", line 820, in _apply
    param_applied = fn(param)
                    ^^^^^^^^^
  File "C:\Users\mouba\AppData\Roaming\Python\Python311\site-packages\torch\nn\modules\module.py", line 1143, in convert
    return t.to(device, dtype if t.is_floating_point() or t.is_complex() else None, non_blocking)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\mouba\AppData\Roaming\Python\Python311\site-packages\torch\cuda\__init__.py", line 239, in _lazy_init
    raise AssertionError("Torch not compiled with CUDA enabled")
AssertionError: Torch not compiled with CUDA enabled
James
  • 32,991
  • 4
  • 47
  • 70
  • Try searching for the error online first. It appears that you installed pytorch without CUDA support. https://pytorch.org/ has instructions for how to do that – Suraj Shourie Sep 01 '23 at 19:26

1 Answers1

0

pip uninstall torch torchvision

then, install torch compiled with cuda, like:

CUDA 11.6

pip install torch==1.13.1+cu116 torchvision==0.14.1+cu116 torchaudio==0.13.1 --extra-index-url https://download.pytorch.org/whl/cu116

you may install cpu only version, like

cpuonly

pip install torch==1.13.1+cpu torchvision==0.14.1+cpu torchaudio==0.13.1 --extra-index-url https://download.pytorch.org/whl/cpu

https://pytorch.org/get-started/previous-versions/

Jiu_Zou
  • 463
  • 1
  • 4
  • I can not install because of an error ERROR: Could not find a version that satisfies the requirement torch==1.13.1+cu116 (from versions: 2.0.0, 2.0.1) ERROR: No matching distribution found for torch==1.13.1+cu116 – 131 Mohammad Ubaida Sep 02 '23 at 10:42
  • you can download it manually by yourself. Searching it in the webpage [https://download.pytorch.org/whl/cu116](https://download.pytorch.org/whl/cu116) – Jiu_Zou Sep 02 '23 at 10:48