import gradio as gr from random import randint from all_models import models from externalmod import gr_Interface_load, randomize_seed import asyncio import os from threading import RLock # Create a lock to ensure thread safety when accessing shared resources lock = RLock() # Load Hugging Face token from environment variable HF_TOKEN = os.environ.get("HF_TOKEN") # Function to load models def load_fn(models): global models_load models_load = {} for model in models: if model not in models_load: try: print(f"Attempting to load model: {model}") m = gr_Interface_load(f'models/{model}', hf_token=HF_TOKEN) print(f"Successfully loaded model: {model}") except Exception as error: print(f"Error loading model {model}: {error}") m = gr.Interface(lambda: None, ['text'], ['image']) models_load[model] = m # Load the models print("Loading models...") load_fn(models) print("Models loaded successfully.") num_models = 6 default_models = models[:num_models] inference_timeout = 600 MAX_SEED = 3999999999 starting_seed = randint(1941, 2024) print(f"Starting seed: {starting_seed}") def extend_choices(choices): return choices[:num_models] + ['NA'] * (num_models - len(choices)) # Asynchronous function for inference async def infer(model_str, prompt, seed=1, timeout=inference_timeout): if model_str == 'NA': return None print(f"Starting inference for model: {model_str} with prompt: '{prompt}' and seed: {seed}") try: result = await asyncio.to_thread(models_load[model_str].fn, prompt, seed=seed, token=HF_TOKEN) if result: return result except (Exception, asyncio.TimeoutError) as e: print(f"Error during inference for model {model_str}: {e}") return None def gen_fnseed(model_str, prompt, seed=1): if model_str == 'NA': return None loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) try: result = loop.run_until_complete(infer(model_str, prompt, seed)) except Exception as e: print(f"Error during generation for model {model_str}: {e}") result = None finally: loop.close() return result # Creating the Gradio UI print("Creating Gradio interface...") with gr.Blocks(theme="Nymbo/Nymbo_Theme") as demo: gr.HTML("