Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,17 @@
|
|
1 |
import gradio as gr
|
2 |
from random import randint
|
3 |
from all_models import models
|
|
|
4 |
from externalmod import gr_Interface_load, randomize_seed
|
|
|
5 |
import asyncio
|
6 |
import os
|
7 |
from threading import RLock
|
8 |
|
9 |
-
# Create a lock
|
10 |
lock = RLock()
|
11 |
|
12 |
-
# Load Hugging Face token from environment
|
13 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
14 |
|
15 |
# Function to load models
|
@@ -19,79 +21,87 @@ def load_fn(models):
|
|
19 |
for model in models:
|
20 |
if model not in models_load:
|
21 |
try:
|
22 |
-
print(f"
|
23 |
m = gr_Interface_load(f'models/{model}', hf_token=HF_TOKEN)
|
24 |
-
print(f"Successfully loaded: {model}")
|
25 |
except Exception as error:
|
26 |
-
print(f"Error loading {model}: {error}")
|
27 |
m = gr.Interface(lambda: None, ['text'], ['image'])
|
28 |
models_load[model] = m
|
29 |
|
30 |
-
# Load models
|
31 |
print("Loading models...")
|
32 |
load_fn(models)
|
33 |
print("Models loaded successfully.")
|
34 |
|
35 |
-
num_models =
|
|
|
|
|
|
|
|
|
36 |
starting_seed = randint(1941, 2024)
|
|
|
37 |
print(f"Starting seed: {starting_seed}")
|
38 |
|
39 |
-
# Extend choices to match num_models
|
40 |
def extend_choices(choices):
|
41 |
-
|
42 |
-
return extended
|
43 |
-
|
44 |
-
# Update image boxes based on selected models
|
45 |
-
def update_imgbox(choices):
|
46 |
-
choices_extended = extend_choices(choices)
|
47 |
-
return [gr.Image(None, label=m, visible=(m != 'NA')) for m in choices_extended]
|
48 |
|
49 |
-
# Asynchronous inference
|
50 |
-
async def infer(model_str, prompt, seed=1, timeout=
|
51 |
if model_str == 'NA':
|
52 |
return None
|
|
|
|
|
|
|
53 |
try:
|
54 |
-
|
55 |
-
task = asyncio.to_thread(models_load[model_str].fn, prompt=prompt, seed=seed, token=HF_TOKEN)
|
56 |
-
result = await asyncio.wait_for(task, timeout=timeout)
|
57 |
if result:
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
return image_path
|
62 |
-
except Exception as e:
|
63 |
-
print(f"Error in inference for {model_str}: {e}")
|
64 |
return None
|
65 |
|
66 |
-
# Wrapper function for inference
|
67 |
def gen_fnseed(model_str, prompt, seed=1):
|
68 |
if model_str == 'NA':
|
69 |
return None
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
-
#
|
73 |
print("Creating Gradio interface...")
|
74 |
with gr.Blocks(theme="Nymbo/Nymbo_Theme") as demo:
|
75 |
-
gr.HTML("<center><h1>Compare-
|
76 |
-
|
|
|
77 |
txt_input = gr.Textbox(label='Your prompt:', lines=4)
|
78 |
-
gen_button = gr.Button('Generate images')
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
81 |
seed_rand.click(randomize_seed, None, [seed])
|
82 |
|
83 |
-
|
84 |
-
|
|
|
85 |
|
86 |
for m, o in zip(current_models, output):
|
87 |
-
gen_button.click(gen_fnseed, inputs=[m, txt_input, seed], outputs=[o])
|
88 |
|
89 |
with gr.Accordion('Model selection'):
|
90 |
-
model_choice = gr.CheckboxGroup(models, label=f'Choose up to {num_models} models', value=
|
91 |
-
model_choice.change(
|
92 |
-
model_choice.change(extend_choices, model_choice, current_models)
|
93 |
|
94 |
-
# Reduce concurrency to avoid T4 overload
|
95 |
-
demo.queue(default_concurrency_limit=50, max_size=100)
|
96 |
print("Launching Gradio interface...")
|
97 |
-
demo.
|
|
|
|
1 |
import gradio as gr
|
2 |
from random import randint
|
3 |
from all_models import models
|
4 |
+
|
5 |
from externalmod import gr_Interface_load, randomize_seed
|
6 |
+
|
7 |
import asyncio
|
8 |
import os
|
9 |
from threading import RLock
|
10 |
|
11 |
+
# Create a lock to ensure thread safety when accessing shared resources
|
12 |
lock = RLock()
|
13 |
|
14 |
+
# Load Hugging Face token from environment variable
|
15 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
16 |
|
17 |
# Function to load models
|
|
|
21 |
for model in models:
|
22 |
if model not in models_load:
|
23 |
try:
|
24 |
+
print(f"Attempting to load model: {model}")
|
25 |
m = gr_Interface_load(f'models/{model}', hf_token=HF_TOKEN)
|
26 |
+
print(f"Successfully loaded model: {model}")
|
27 |
except Exception as error:
|
28 |
+
print(f"Error loading model {model}: {error}")
|
29 |
m = gr.Interface(lambda: None, ['text'], ['image'])
|
30 |
models_load[model] = m
|
31 |
|
32 |
+
# Load the models
|
33 |
print("Loading models...")
|
34 |
load_fn(models)
|
35 |
print("Models loaded successfully.")
|
36 |
|
37 |
+
num_models = 6
|
38 |
+
|
39 |
+
default_models = models[:num_models]
|
40 |
+
inference_timeout = 600
|
41 |
+
MAX_SEED = 3999999999
|
42 |
starting_seed = randint(1941, 2024)
|
43 |
+
|
44 |
print(f"Starting seed: {starting_seed}")
|
45 |
|
|
|
46 |
def extend_choices(choices):
|
47 |
+
return choices[:num_models] + ['NA'] * (num_models - len(choices))
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
# Asynchronous function for inference
|
50 |
+
async def infer(model_str, prompt, seed=1, timeout=inference_timeout):
|
51 |
if model_str == 'NA':
|
52 |
return None
|
53 |
+
|
54 |
+
print(f"Starting inference for model: {model_str} with prompt: '{prompt}' and seed: {seed}")
|
55 |
+
|
56 |
try:
|
57 |
+
result = await asyncio.to_thread(models_load[model_str].fn, prompt, seed=seed, token=HF_TOKEN)
|
|
|
|
|
58 |
if result:
|
59 |
+
return result
|
60 |
+
except (Exception, asyncio.TimeoutError) as e:
|
61 |
+
print(f"Error during inference for model {model_str}: {e}")
|
|
|
|
|
|
|
62 |
return None
|
63 |
|
|
|
64 |
def gen_fnseed(model_str, prompt, seed=1):
|
65 |
if model_str == 'NA':
|
66 |
return None
|
67 |
+
|
68 |
+
loop = asyncio.new_event_loop()
|
69 |
+
asyncio.set_event_loop(loop)
|
70 |
+
try:
|
71 |
+
result = loop.run_until_complete(infer(model_str, prompt, seed))
|
72 |
+
except Exception as e:
|
73 |
+
print(f"Error during generation for model {model_str}: {e}")
|
74 |
+
result = None
|
75 |
+
finally:
|
76 |
+
loop.close()
|
77 |
+
return result
|
78 |
|
79 |
+
# Creating the Gradio UI
|
80 |
print("Creating Gradio interface...")
|
81 |
with gr.Blocks(theme="Nymbo/Nymbo_Theme") as demo:
|
82 |
+
gr.HTML("<center><h1>Compare-6</h1></center>")
|
83 |
+
|
84 |
+
with gr.Tab('Compare-6'):
|
85 |
txt_input = gr.Textbox(label='Your prompt:', lines=4)
|
86 |
+
gen_button = gr.Button('Generate up to 6 images')
|
87 |
+
|
88 |
+
with gr.Row():
|
89 |
+
seed = gr.Slider("Seed", 0, MAX_SEED, step=1, value=starting_seed)
|
90 |
+
seed_rand = gr.Button("Randomize Seed 🎲")
|
91 |
+
|
92 |
seed_rand.click(randomize_seed, None, [seed])
|
93 |
|
94 |
+
with gr.Row():
|
95 |
+
output = [gr.Image(label=m, min_width=480) for m in default_models]
|
96 |
+
current_models = [gr.Textbox(m, visible=False) for m in default_models]
|
97 |
|
98 |
for m, o in zip(current_models, output):
|
99 |
+
gen_button.click(fn=gen_fnseed, inputs=[m, txt_input, seed], outputs=[o])
|
100 |
|
101 |
with gr.Accordion('Model selection'):
|
102 |
+
model_choice = gr.CheckboxGroup(models, label=f'Choose up to {num_models} models', value=default_models)
|
103 |
+
model_choice.change(lambda c: extend_choices(c), model_choice, current_models)
|
|
|
104 |
|
|
|
|
|
105 |
print("Launching Gradio interface...")
|
106 |
+
demo.queue(default_concurrency_limit=50, max_size=100)
|
107 |
+
demo.launch(share=True, max_threads=50)
|