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
pip uninstall torch torchvision
then, install torch compiled with cuda, like:
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
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