File size: 6,276 Bytes
a85be17 7b52fe5 bd1d32c b4cc1c9 0d41e1e 603b913 749bb52 1555e70 749bb52 36d1c70 bd1d32c a85be17 bd1d32c a85be17 bd1d32c 749bb52 2868311 028d3c8 2868311 749bb52 0d41e1e 749bb52 0d41e1e 749bb52 0d41e1e 749bb52 2868311 749bb52 e122d23 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 75eccb4 bd74b6e d3a8ff8 b87e516 af5c7ef b87e516 e369512 af5c7ef e369512 b87e516 1555e70 d798ebd f188212 e6317d6 |
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 196 197 198 |
import gradio as gr
from models import models
from PIL import Image
import requests
import uuid
import io
import base64
import random
import os
from transforms import RGBTransform # from source code mentioned above
from theme import theme
from fastapi import FastAPI
app = FastAPI()
HF_TOKEN = os.environ.get("HF_TOKEN") if os.environ.get("HF_TOKEN") else None # If private or gated models aren't used, ENV setting is unnecessary.
server_timeout = 100
inference_timeout = 100
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_color(out_prompt,model_drop,cnt,color,tint):
h=color.lstrip('#')
#h = input('Enter hex: ').lstrip('#')
#print('RGB =', tuple(int(h[i:i+2], 16) for i in (0, 2, 4)))
color=tuple(int(h[i:i+2], 16) for i in (0, 2, 4))
print (color)
p_seed=""
out_box=[]
out_html=""
#for i,ea in enumerate(loaded_model):
for i in range(int(cnt)):
rand=random.randint(1,500)
for i in range(rand):
p_seed+=" "
try:
#model=gr.load(f'models/{model[int(model_drop)]}')
model=loaded_model[int(model_drop)]
out_img=model(out_prompt+p_seed)
print(out_img)
raw=Image.open(out_img)
raw=raw.convert('RGB')
colorize = RGBTransform().mix_with(color,factor=float(tint)).applied_to(raw)
out_box.append(colorize)
except Exception as e:
print(e)
out_html=str(e)
pass
yield out_box,out_html
def run_dif(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
def start_threads(prompt):
t1 = threading.Thread(target=thread_dif, args=(prompt,0))
t2 = threading.Thread(target=thread_dif, args=(prompt,1))
t1.start()
t2.start()
print (t1)
print (t2)
a1,a2=t1.result()
b1,b2=t2.result()
return a1,a2
css="""
.grid_class{
display:flex;
height:100%;
}
.img_class{
min-width:200px;
}
"""
with gr.Blocks(css=css, theme=theme) as app:
with gr.Tab("Text to Image"):
with gr.Row():
with gr.Column():
inp=gr.Textbox(label="Prompt")
btn=gr.Button()
with gr.Column():
col = gr.ColorPicker(label="Color Tint")
tint = gr.Slider(label="Tint Strength", minimum=0, maximum=1, step=0.01, value=0.30)
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()
btn.click(run_dif_color,[inp,model_drop,cnt,col,tint],[outp,out_html])
app.queue(default_concurrency_limit=200, max_size=200) # <-- Sets up a queue with default parameters
if __name__ == "__main__":
timeout = 100
app.launch() |