File size: 3,547 Bytes
a85be17 7b52fe5 bd1d32c b4cc1c9 d3a8ff8 bd1d32c a85be17 bd1d32c a85be17 bd1d32c d3a8ff8 bd1d32c b4cc1c9 bd1d32c 382d70a b4cc1c9 382d70a b4cc1c9 382d70a b4cc1c9 af5c7ef 382d70a b87e516 6642a1d e369512 b87e516 d3a8ff8 bd74b6e d3a8ff8 bd74b6e d3a8ff8 75eccb4 bd74b6e d3a8ff8 b87e516 af5c7ef b87e516 e369512 af5c7ef e369512 b87e516 bd1d32c b4cc1c9 bd1d32c d3a8ff8 bd1d32c |
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 |
import gradio as gr
from models import models
from PIL import Image
import requests
import uuid
import io
import base64
import threading
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,mod):
out_box=[]
out_html=""
for i,ea in enumerate(loaded_model):
try:
print (ea)
model=loaded_model[int(i)]
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)
html_out = "<div class='grid_class'>"+out_html+"</div>"
return out_box,html_out
except Exception as 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) as app:
inp=gr.Textbox()
out_html=gr.HTML()
outp=gr.Gallery()
btn=gr.Button()
btn.click(start_threads,inp,[outp,out_html])
app.launch() |