I am going to provide a gradio app from huggingface that allows you to choose a lora for…
- User
I am going to provide a gradio app from huggingface that allows you to choose a lora for the flux.1 model and run image generation with it. Make a new app that is not for choosing a lora, but just running image generation with a hardcoded lora. for each prompt, the app should generate an image with and without the lora and display them both. Here is the app:import gradio as gr import json import logging import torch from PIL import Image import spaces from diffusers import DiffusionPipeline import copy import random import time
Load LoRAs from JSON file
with open('loras.json', 'r') as f: loras = json.load(f)
Initialize the base model
base_model = "black-forest-labs/FLUX.1-dev" pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16)
MAX_SEED = 2**32-1
class calculateDuration: def init(self, activity_name=""): self.activity_name = activity_name
codedef __enter__(self): self.start_time = time.time() return self def __exit__(self, exc_type, exc_value, traceback): self.end_time = time.time() self.elapsed_time = self.end_time - self.start_time if self.activity_name: print(f"Elapsed time for {self.activity_name}: {self.elapsed_time:.6f} seconds") else: print(f"Elapsed time: {self.elapsed_time:.6f} seconds")def update_selection(evt: gr.SelectData, width, height): selected_lora = loras[evt.index] new_placeholder = f"Type a prompt for {selected_lora['title']}" lora_repo = selected_lora["repo"] updated_text = f"### Selected: {lora_repo} ✨" if "aspect" in selected_lora: if selected_lora["aspect"] == "portrait": width = 768 height = 1024 elif selected_lora["aspect"] == "landscape": width = 1024 height = 768 return ( gr.update(placeholder=new_placeholder), updated_text, evt.index, width, height, )
@spaces.GPU(duration=70) def generate_image(prompt, trigger_word, steps, seed, cfg_scale, width, height, lora_scale, progress): pipe.to("cuda") generator = torch.Generator(device="cuda").manual_seed(seed)
code - Assistant