import gradio as gr from models import models from PIL import Image import requests import uuid import io import base64 from transforms import RGBTransform import concurrent.futures import time # Dictionary to track model availability status model_status = {} def load_models(): """ Attempts to load all models and tracks their availability status Returns a list of successfully loaded models """ loaded_models = [] for model in models: try: # Attempt to load the model loaded_model = gr.load(f'models/{model}') loaded_models.append(loaded_model) model_status[model] = {'status': 'available', 'error': None} except Exception as e: # Track failed model loads model_status[model] = {'status': 'unavailable', 'error': str(e)} print(f"Failed to load {model}: {e}") return loaded_models def generate_single_image(model_name, model, prompt, color=None, tint_strength=0.3): """ Generates a single image from a specific model with optional color tinting Returns tuple of (image, error_message, model_name) """ try: # Generate image out_img = model(prompt) # Process the image if isinstance(out_img, str): # If URL is returned r = requests.get(f'https://omnibus-top-20.hf.space/file={out_img}', stream=True) if r.status_code != 200: return None, f"HTTP Error: {r.status_code}", model_name img = Image.open(io.BytesIO(r.content)).convert('RGB') else: img = Image.open(out_img).convert('RGB') # Apply color tinting if specified if color is not None: h = color.lstrip('#') rgb_color = tuple(int(h[i:i+2], 16) for i in (0, 2, 4)) img = RGBTransform().mix_with(rgb_color, factor=float(tint_strength)).applied_to(img) return img, None, model_name except Exception as e: return None, str(e), model_name def run_all_models(prompt, color=None, tint_strength=0.3): """ Generates images from all available models in parallel """ results = [] errors = [] # Load models if not already loaded loaded_models = load_models() # Use ThreadPoolExecutor for parallel execution with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: future_to_model = { executor.submit( generate_single_image, model_name, model, prompt, color, tint_strength ): model_name for model_name, model in zip(models, loaded_models) } for future in concurrent.futures.as_completed(future_to_model): img, error, model_name = future.result() if error: errors.append(f"{model_name}: {error}") model_status[model_name]['status'] = 'failed' model_status[model_name]['error'] = error if img: results.append((img, model_name)) # Generate HTML report html_report = "
Status: {status['status']}
{f"Error: {status['error']}
" if status['error'] else ""}