# frontend.py import gradio as gr import sys import os # Add the parent directory to sys.path parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) sys.path.insert(0, parent_dir) #print(sys.path) #DEBUG from flux_app.backend import ModelManager # Absolute import from flux_app.config import MAX_SEED # Absolute import from flux_app.lora_handling import ( add_custom_lora, remove_custom_lora, prepare_prompt, unload_lora_weights, load_lora_weights_into_pipeline, update_selection ) from flux_app.utilities import randomize_seed_if_needed, calculateDuration # Absolute import import spaces # Import the prompt enhancer generate function from the new module from flux_app.enhance import generate # Dummy loras data for initial UI setup. initial_loras = [ {"image": "placeholder.jpg", "title": "Placeholder LoRA", "repo": "placeholder/repo", "weights": None, "trigger_word": ""}, ] class Frontend: def __init__(self, model_manager: ModelManager): self.model_manager = model_manager self.loras = initial_loras self.load_initial_loras() self.css = self.define_css() def define_css(self): # A cleaner, professional CSS styling. return ''' /* Title Styling */ #title { text-align: center; margin-bottom: 20px; } #title h1 { font-size: 2.5rem; margin: 0; color: #333; } /* Button and Column Styling */ #gen_btn { width: 100%; padding: 12px; font-weight: bold; border-radius: 5px; } #gen_column { display: flex; align-items: center; justify-content: center; } /* Gallery and List Styling */ #gallery .grid-wrap { margin-top: 15px; } #lora_list { background-color: #f5f5f5; padding: 10px; border-radius: 4px; font-size: 0.9rem; } .card_internal { display: flex; align-items: center; height: 100px; margin-top: 10px; } .card_internal img { margin-right: 10px; } .styler { --form-gap-width: 0px !important; } /* Progress Bar Styling */ .progress-container { width: 100%; height: 20px; background-color: #e0e0e0; border-radius: 10px; overflow: hidden; margin-bottom: 20px; } .progress-bar { height: 100%; background-color: #4f46e5; transition: width 0.3s ease-in-out; width: calc(var(--current) / var(--total) * 100%); } ''' def load_initial_loras(self): try: from flux_app.lora import loras as loras_list # Absolute import self.loras = loras_list except ImportError: print("Warning: lora.py not found, using placeholder LoRAs.") pass @spaces.GPU(duration=100) def run_lora(self, prompt, image_input, image_strength, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale, use_enhancer, progress=gr.Progress(track_tqdm=True)): # If prompt enhancer is enabled, generate the enhanced prompt. if use_enhancer: enhanced_prompt = "" # Generate the enhanced prompt (consume the generator to get the final result) for chunk in generate(prompt): enhanced_prompt = chunk prompt_used = enhanced_prompt else: enhanced_prompt = "" prompt_used = prompt seed = randomize_seed_if_needed(randomize_seed, seed, MAX_SEED) prompt_mash = prepare_prompt(prompt_used, selected_index, self.loras) selected_lora = self.loras[selected_index] unload_lora_weights(self.model_manager.pipe, self.model_manager.pipe_i2i) pipe_to_use = self.model_manager.pipe_i2i if image_input is not None else self.model_manager.pipe load_lora_weights_into_pipeline(pipe_to_use, selected_lora["repo"], selected_lora.get("weights")) if image_input is not None: final_image = self.model_manager.generate_image_to_image( prompt_mash, image_input, image_strength, steps, cfg_scale, width, height, lora_scale, seed ) yield final_image, seed, gr.update(visible=False), enhanced_prompt else: image_generator = self.model_manager.generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scale) final_image = None step_counter = 0 for image in image_generator: step_counter += 1 final_image = image progress_bar = f'