# frontend.py import gradio as gr import sys import os import spaces # 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 the prompt enhancer function from flux_app.enhance import generate as enhance_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)): seed = randomize_seed_if_needed(randomize_seed, seed, MAX_SEED) # Prepare the initial prompt (using LoRA info if needed) prompt_mash = prepare_prompt(prompt, selected_index, self.loras) enhanced_text = "" # If prompt enhancer is enabled, first run it to improve the prompt. if use_enhancer: # Stream the enhanced prompt (this will update the enhanced prompt textbox) for enhanced_chunk in enhance_generate(prompt_mash): enhanced_text = enhanced_chunk # Yield an update with no image yet and the current enhanced prompt. yield None, seed, gr.update(visible=False), enhanced_text # Use the final enhanced prompt as the prompt for image generation. prompt_mash = enhanced_text else: # Ensure the enhanced prompt textbox remains cleared. enhanced_text = "" # Continue with the image generation process. 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_text 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'