import gradio as gr from models import models from PIL import Image import requests import uuid import io import base64 import torch from diffusers import AutoPipelineForImage2Image from diffusers.utils import make_image_grid, load_image loaded_model=[] for i,model in enumerate(models): try: loaded_model.append(gr.load(f'models/{model}')) except Exception as e: print(e) pass print (loaded_model) def run_dif(out_prompt,model_drop,cnt): out_box=[] pipeline = AutoPipelineForImage2Image.from_pretrained( "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16, variant="fp16", use_safetensors=True ) pipeline.enable_model_cpu_offload() # remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed pipeline.enable_xformers_memory_efficient_attention() # prepare image url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png" init_image = load_image(url) prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" # pass prompt and image to pipeline image = pipeline(prompt, image=init_image, strength=0.8).images[0] #make_image_grid([init_image, image], rows=1, cols=2) out_box.append(image) return out_box,"" def run_dif_old(out_prompt,model_drop,cnt): p_seed="" out_box=[] out_html="" #for i,ea in enumerate(loaded_model): for i in range(int(cnt)): p_seed+=" " try: model=loaded_model[int(model_drop)] out_img=model(out_prompt+p_seed) print(out_img) out_box.append(out_img) except Exception as e: print(e) out_html=str(e) pass yield out_box,out_html def run_dif_og(out_prompt,model_drop,cnt): out_box=[] out_html="" #for i,ea in enumerate(loaded_model): for i in range(cnt): try: #print (ea) model=loaded_model[int(model_drop)] out_img=model(out_prompt) print(out_img) url=f'https://omnibus-top-20.hf.space/file={out_img}' print(url) uid = uuid.uuid4() #urllib.request.urlretrieve(image, 'tmp.png') #out=Image.open('tmp.png') r = requests.get(url, stream=True) if r.status_code == 200: img_buffer = io.BytesIO(r.content) print (f'bytes:: {io.BytesIO(r.content)}') str_equivalent_image = base64.b64encode(img_buffer.getvalue()).decode() img_tag = "" out_html+=f"