Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,182 +5,158 @@ import requests
|
|
5 |
import uuid
|
6 |
import io
|
7 |
import base64
|
8 |
-
import
|
9 |
-
|
|
|
10 |
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
print (loaded_model)
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
def run_dif_color(out_prompt,model_drop,cnt,color,tint):
|
24 |
-
h=color.lstrip('#')
|
25 |
-
#h = input('Enter hex: ').lstrip('#')
|
26 |
-
#print('RGB =', tuple(int(h[i:i+2], 16) for i in (0, 2, 4)))
|
27 |
-
color=tuple(int(h[i:i+2], 16) for i in (0, 2, 4))
|
28 |
-
|
29 |
-
print (color)
|
30 |
-
p_seed=""
|
31 |
-
out_box=[]
|
32 |
-
out_html=""
|
33 |
-
#for i,ea in enumerate(loaded_model):
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
for i in range(int(cnt)):
|
38 |
-
rand=random.randint(1,500)
|
39 |
-
for i in range(rand):
|
40 |
-
p_seed+=" "
|
41 |
-
try:
|
42 |
-
#model=gr.load(f'models/{model[int(model_drop)]}')
|
43 |
-
model=loaded_model[int(model_drop)]
|
44 |
-
out_img=model(out_prompt+p_seed)
|
45 |
-
print(out_img)
|
46 |
-
|
47 |
-
raw=Image.open(out_img)
|
48 |
-
raw=raw.convert('RGB')
|
49 |
-
|
50 |
-
colorize = RGBTransform().mix_with(color,factor=float(tint)).applied_to(raw)
|
51 |
-
|
52 |
-
out_box.append(colorize)
|
53 |
-
except Exception as e:
|
54 |
-
print(e)
|
55 |
-
out_html=str(e)
|
56 |
-
pass
|
57 |
-
|
58 |
-
yield out_box,out_html
|
59 |
-
|
60 |
-
|
61 |
-
def run_dif(out_prompt,model_drop,cnt):
|
62 |
-
p_seed=""
|
63 |
-
out_box=[]
|
64 |
-
out_html=""
|
65 |
-
#for i,ea in enumerate(loaded_model):
|
66 |
-
for i in range(int(cnt)):
|
67 |
-
p_seed+=" "
|
68 |
try:
|
69 |
-
model
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
except Exception as e:
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
def run_dif_og(out_prompt,model_drop,cnt):
|
80 |
-
out_box=[]
|
81 |
-
out_html=""
|
82 |
-
#for i,ea in enumerate(loaded_model):
|
83 |
-
for i in range(cnt):
|
84 |
-
try:
|
85 |
-
#print (ea)
|
86 |
-
model=loaded_model[int(model_drop)]
|
87 |
-
out_img=model(out_prompt)
|
88 |
-
print(out_img)
|
89 |
-
url=f'https://omnibus-top-20.hf.space/file={out_img}'
|
90 |
-
print(url)
|
91 |
-
uid = uuid.uuid4()
|
92 |
-
#urllib.request.urlretrieve(image, 'tmp.png')
|
93 |
-
#out=Image.open('tmp.png')
|
94 |
-
r = requests.get(url, stream=True)
|
95 |
-
|
96 |
-
if r.status_code == 200:
|
97 |
-
img_buffer = io.BytesIO(r.content)
|
98 |
-
print (f'bytes:: {io.BytesIO(r.content)}')
|
99 |
-
str_equivalent_image = base64.b64encode(img_buffer.getvalue()).decode()
|
100 |
-
img_tag = "<img src='data:image/png;base64," + str_equivalent_image + "'/>"
|
101 |
-
out_html+=f"<div class='img_class'><a href='https://huggingface.co/models/{models[i]}'>{models[i]}</a><br>"+img_tag+"</div>"
|
102 |
-
out = Image.open(io.BytesIO(r.content))
|
103 |
-
out_box.append(out)
|
104 |
-
html_out = "<div class='grid_class'>"+out_html+"</div>"
|
105 |
-
yield out_box,html_out
|
106 |
-
except Exception as e:
|
107 |
-
out_html+=str(e)
|
108 |
-
html_out = "<div class='grid_class'>"+out_html+"</div>"
|
109 |
-
|
110 |
-
yield out_box,html_out
|
111 |
|
112 |
-
def
|
113 |
-
|
114 |
-
|
115 |
-
|
|
|
116 |
try:
|
117 |
-
|
118 |
-
model
|
119 |
-
out_img=model(out_prompt)
|
120 |
-
print(out_img)
|
121 |
-
url=f'https://omnibus-top-20.hf.space/file={out_img}'
|
122 |
-
print(url)
|
123 |
-
uid = uuid.uuid4()
|
124 |
-
#urllib.request.urlretrieve(image, 'tmp.png')
|
125 |
-
#out=Image.open('tmp.png')
|
126 |
-
r = requests.get(url, stream=True)
|
127 |
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
out = Image.open(io.BytesIO(r.content))
|
136 |
-
out_box.append(out)
|
137 |
else:
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
|
|
|
|
145 |
|
146 |
-
|
|
|
147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
print (t2)
|
157 |
-
a1,a2=t1.result()
|
158 |
-
b1,b2=t2.result()
|
159 |
-
return a1,a2
|
160 |
-
|
161 |
-
css="""
|
162 |
-
.grid_class{
|
163 |
-
display:flex;
|
164 |
-
height:100%;
|
165 |
}
|
166 |
-
.
|
167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
}
|
169 |
-
|
170 |
"""
|
|
|
171 |
with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as app:
|
172 |
with gr.Row():
|
173 |
with gr.Column():
|
174 |
-
inp=gr.Textbox(label="Prompt")
|
175 |
-
btn=gr.Button()
|
176 |
with gr.Column():
|
177 |
-
col = gr.ColorPicker(label="Color Tint")
|
178 |
tint = gr.Slider(label="Tint Strength", minimum=0, maximum=1, step=0.01, value=0.30)
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
app.launch()
|
|
|
5 |
import uuid
|
6 |
import io
|
7 |
import base64
|
8 |
+
from transforms import RGBTransform
|
9 |
+
import concurrent.futures
|
10 |
+
import time
|
11 |
|
12 |
+
# Dictionary to track model availability status
|
13 |
+
model_status = {}
|
14 |
|
15 |
+
def load_models():
|
16 |
+
"""
|
17 |
+
Attempts to load all models and tracks their availability status
|
18 |
+
Returns a list of successfully loaded models
|
19 |
+
"""
|
20 |
+
loaded_models = []
|
21 |
+
for model in models:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
try:
|
23 |
+
# Attempt to load the model
|
24 |
+
loaded_model = gr.load(f'models/{model}')
|
25 |
+
loaded_models.append(loaded_model)
|
26 |
+
model_status[model] = {'status': 'available', 'error': None}
|
27 |
except Exception as e:
|
28 |
+
# Track failed model loads
|
29 |
+
model_status[model] = {'status': 'unavailable', 'error': str(e)}
|
30 |
+
print(f"Failed to load {model}: {e}")
|
31 |
+
return loaded_models
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
def generate_single_image(model_name, model, prompt, color=None, tint_strength=0.3):
|
34 |
+
"""
|
35 |
+
Generates a single image from a specific model with optional color tinting
|
36 |
+
Returns tuple of (image, error_message, model_name)
|
37 |
+
"""
|
38 |
try:
|
39 |
+
# Generate image
|
40 |
+
out_img = model(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
# Process the image
|
43 |
+
if isinstance(out_img, str): # If URL is returned
|
44 |
+
r = requests.get(f'https://omnibus-top-20.hf.space/file={out_img}', stream=True)
|
45 |
+
if r.status_code != 200:
|
46 |
+
return None, f"HTTP Error: {r.status_code}", model_name
|
47 |
+
|
48 |
+
img = Image.open(io.BytesIO(r.content)).convert('RGB')
|
|
|
|
|
49 |
else:
|
50 |
+
img = Image.open(out_img).convert('RGB')
|
51 |
+
|
52 |
+
# Apply color tinting if specified
|
53 |
+
if color is not None:
|
54 |
+
h = color.lstrip('#')
|
55 |
+
rgb_color = tuple(int(h[i:i+2], 16) for i in (0, 2, 4))
|
56 |
+
img = RGBTransform().mix_with(rgb_color, factor=float(tint_strength)).applied_to(img)
|
57 |
+
|
58 |
+
return img, None, model_name
|
59 |
|
60 |
+
except Exception as e:
|
61 |
+
return None, str(e), model_name
|
62 |
|
63 |
+
def run_all_models(prompt, color=None, tint_strength=0.3):
|
64 |
+
"""
|
65 |
+
Generates images from all available models in parallel
|
66 |
+
"""
|
67 |
+
results = []
|
68 |
+
errors = []
|
69 |
+
|
70 |
+
# Load models if not already loaded
|
71 |
+
loaded_models = load_models()
|
72 |
+
|
73 |
+
# Use ThreadPoolExecutor for parallel execution
|
74 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
|
75 |
+
future_to_model = {
|
76 |
+
executor.submit(
|
77 |
+
generate_single_image,
|
78 |
+
model_name,
|
79 |
+
model,
|
80 |
+
prompt,
|
81 |
+
color,
|
82 |
+
tint_strength
|
83 |
+
): model_name
|
84 |
+
for model_name, model in zip(models, loaded_models)
|
85 |
+
}
|
86 |
+
|
87 |
+
for future in concurrent.futures.as_completed(future_to_model):
|
88 |
+
img, error, model_name = future.result()
|
89 |
+
if error:
|
90 |
+
errors.append(f"{model_name}: {error}")
|
91 |
+
model_status[model_name]['status'] = 'failed'
|
92 |
+
model_status[model_name]['error'] = error
|
93 |
+
if img:
|
94 |
+
results.append((img, model_name))
|
95 |
+
|
96 |
+
# Generate HTML report
|
97 |
+
html_report = "<div class='results-grid'>"
|
98 |
+
for model in models:
|
99 |
+
status = model_status[model]
|
100 |
+
status_color = {
|
101 |
+
'available': 'green',
|
102 |
+
'unavailable': 'red',
|
103 |
+
'failed': 'orange'
|
104 |
+
}.get(status['status'], 'gray')
|
105 |
+
|
106 |
+
html_report += f"""
|
107 |
+
<div class='model-status'>
|
108 |
+
<h3>{model}</h3>
|
109 |
+
<p style='color: {status_color}'>Status: {status['status']}</p>
|
110 |
+
{f"<p class='error'>Error: {status['error']}</p>" if status['error'] else ""}
|
111 |
+
</div>
|
112 |
+
"""
|
113 |
+
html_report += "</div>"
|
114 |
+
|
115 |
+
return results, html_report
|
116 |
|
117 |
+
# Gradio interface
|
118 |
+
css = """
|
119 |
+
.results-grid {
|
120 |
+
display: grid;
|
121 |
+
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
|
122 |
+
gap: 1rem;
|
123 |
+
padding: 1rem;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
}
|
125 |
+
.model-status {
|
126 |
+
border: 1px solid #ddd;
|
127 |
+
padding: 1rem;
|
128 |
+
border-radius: 4px;
|
129 |
+
}
|
130 |
+
.error {
|
131 |
+
color: red;
|
132 |
+
font-size: 0.9em;
|
133 |
+
word-break: break-word;
|
134 |
}
|
|
|
135 |
"""
|
136 |
+
|
137 |
with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as app:
|
138 |
with gr.Row():
|
139 |
with gr.Column():
|
140 |
+
inp = gr.Textbox(label="Prompt")
|
141 |
+
btn = gr.Button("Generate from All Models")
|
142 |
with gr.Column():
|
143 |
+
col = gr.ColorPicker(label="Color Tint (Optional)")
|
144 |
tint = gr.Slider(label="Tint Strength", minimum=0, maximum=1, step=0.01, value=0.30)
|
145 |
+
|
146 |
+
status_html = gr.HTML(label="Model Status")
|
147 |
+
gallery = gr.Gallery()
|
148 |
+
|
149 |
+
def process_and_display(prompt, color, tint_strength):
|
150 |
+
results, html_report = run_all_models(prompt, color, tint_strength)
|
151 |
+
return (
|
152 |
+
[img for img, _ in results],
|
153 |
+
html_report
|
154 |
+
)
|
155 |
+
|
156 |
+
btn.click(
|
157 |
+
process_and_display,
|
158 |
+
inputs=[inp, col, tint],
|
159 |
+
outputs=[gallery, status_html]
|
160 |
+
)
|
161 |
+
|
162 |
app.launch()
|