from auth_token import auth_token
from fastapi import FastAPI,Response ,Request
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)
print(torch.cuda.get_device_properties(0).total_memory)
@app.get("/")
def generate(prompt : str) :
with autocast(device):
pipe.enable_sequential_cpu_offload()
pipe.enable_attention_slicing(1)
image = pipe(prompt,guidance_scale=8.5).images[0]
image.save("testimage.png")
return {"out":"hello World"}
This is the code , where I tried to fetch the image on the basis of provided prompt by the user .
Terminal output :
But when I gave prompt in the browser It didn't generated the image :
Here's the Browser output :
This is the error I got in the browser
Failed to fetch. Possible Reasons:
CORS
Network Failure
URL scheme must be "http" or "https" for CORS request.
and didn't got any image