Spaces:
Runtime error
Runtime error
File size: 6,449 Bytes
a85be17 7b52fe5 bd1d32c b4cc1c9 42c5e66 5858c5c a85be17 bd1d32c a85be17 bd1d32c 5858c5c 42c5e66 5858c5c 42c5e66 5858c5c 42c5e66 3bb9701 20cd113 3bb9701 1a42503 20cd113 f39ff8a 42c5e66 6b28599 bd1d32c b4cc1c9 e122d23 9890778 6b28599 382d70a e122d23 6b28599 e122d23 382d70a b4cc1c9 382d70a b4cc1c9 382d70a b4cc1c9 af5c7ef 382d70a b87e516 6642a1d e369512 b87e516 d3a8ff8 e122d23 d3a8ff8 e122d23 d3a8ff8 bd74b6e d3a8ff8 e122d23 d3a8ff8 bd74b6e d3a8ff8 b87e516 af5c7ef b87e516 e369512 af5c7ef e369512 b87e516 8a23d91 6c656e8 9ede6fd fd5acb8 9ede6fd fd5acb8 6c656e8 9ede6fd 6c656e8 9ede6fd 5d2cb5f 9ede6fd 6bf7a1b 5d2cb5f b87e516 e122d23 5858c5c f39ff8a 8a23d91 f39ff8a e122d23 6b28599 9890778 b4cc1c9 3bb9701 f39ff8a 5d2cb5f 3bb9701 f8d7fe9 a293cc4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
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
base_url=f'https://omnibus-top-20-img-img.hf.space/file='
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)
pipeline = AutoPipelineForImage2Image.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float32, use_safetensors=True)
def load_model(model_drop):
pipeline = AutoPipelineForImage2Image.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float32, use_safetensors=True)
def run_dif(prompt,im_path,model_drop,cnt):
out_box=[]
for i,ea in im_path:
print(f'ea:: {ea}')
print(f'impath:: {im_path[0]}')
#url = base_url+ea
#init_image = load_image(url)
init_image=load_image(ea)
#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,guidance_scale=8.0,negative_prompt=negative_prompt,num_inference_steps=50).images[0]
image = pipeline(prompt, image=init_image, strength=0.2,guidance_scale=8.0,num_inference_steps=2).images[0]
#make_image_grid([init_image, image], rows=1, cols=2)
out_box.append(image)
yield 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 = "<img src='data:image/png;base64," + str_equivalent_image + "'/>"
out_html+=f"<div class='img_class'><a href='https://huggingface.co/models/{models[i]}'>{models[i]}</a><br>"+img_tag+"</div>"
out = Image.open(io.BytesIO(r.content))
out_box.append(out)
html_out = "<div class='grid_class'>"+out_html+"</div>"
yield out_box,html_out
except Exception as e:
out_html+=str(e)
html_out = "<div class='grid_class'>"+out_html+"</div>"
yield out_box,html_out
def thread_dif(out_prompt,mod):
out_box=[]
out_html=""
#for i,ea in enumerate(loaded_model):
try:
print (ea)
model=loaded_model[int(mod)]
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 = "<img src='data:image/png;base64," + str_equivalent_image + "'/>"
#out_html+=f"<div class='img_class'><a href='https://huggingface.co/models/{models[i]}'>{models[i]}</a><br>"+img_tag+"</div>"
out = Image.open(io.BytesIO(r.content))
out_box.append(out)
else:
out_html=r.status_code
html_out = "<div class='grid_class'>"+out_html+"</div>"
return out_box,html_out
except Exception as e:
out_html=str(e)
#out_html+=str(e)
html_out = "<div class='grid_class'>"+out_html+"</div>"
return out_box,html_out
css="""
.grid_class{
display:flex;
height:100%;
}
.img_class{
min-width:200px;
}
"""
def load_im(img):
im_box=[]
im = Image.open(img)
width, height = im.size
new_w=int(width/10)
new_h=new_w
w=0
h=0
for i in range(int(height/new_h)):
print(i)
for b in range(10):
print(b)
# Setting the points for cropped image
left = w
top = h
right = left+new_w
bottom = top+new_h
# Cropped image of above dimension
# (It will not change original image)
im1 = im.crop((left, top, right, bottom))
im_box.append(im1)
w+=new_w
yield im_box,[]
h+=new_h
w=0
yield im_box,im_box
with gr.Blocks(css=css) as app:
with gr.Row():
with gr.Column():
inp=gr.Textbox(label="Prompt")
with gr.Row():
btn=gr.Button()
stop_btn=gr.Button()
with gr.Column():
inp_im=gr.Image(type='filepath')
im_btn=gr.Button("Image Grid")
with gr.Row():
model_drop=gr.Dropdown(label="Models", choices=models, type='index', value=models[0])
cnt = gr.Number(value=1)
out_html=gr.HTML()
outp=gr.Gallery(columns=10)
fingal=gr.Gallery(columns=10)
im_list=gr.Textbox()
im_btn.click(load_im,inp_im,[outp,im_list])
go_btn = btn.click(run_dif,[inp,outp,model_drop,cnt],[fingal,out_html])
stop_btn.click(None,None,None,cancels=[go_btn])
app.queue().launch() |