| import spaces |
| import torch |
| import gradio as gr |
| from diffusers import DiffusionPipeline |
| from huggingface_hub import HfApi |
| import os |
|
|
| |
| api = HfApi() |
| HF_USERNAME = "sanetium" |
|
|
| def get_my_loras(): |
| try: |
| |
| |
| models = api.list_models(author=HF_USERNAME) |
| return ["None"] + [m.id for m in models] |
| except Exception as e: |
| return ["None"] |
|
|
| |
| |
| pipe = DiffusionPipeline.from_pretrained( |
| "black-forest-labs/FLUX.2-klein-9b-kv", |
| torch_dtype=torch.bfloat16 |
| ) |
|
|
| current_lora = None |
|
|
| |
| @spaces.GPU |
| def generate_image(prompt, input_image, lora_choice): |
| global current_lora |
| |
| |
| if lora_choice != "None" and lora_choice != current_lora: |
| if current_lora is not None: |
| pipe.unload_lora_weights() |
| pipe.load_lora_weights(lora_choice) |
| current_lora = lora_choice |
| elif lora_choice == "None" and current_lora is not None: |
| pipe.unload_lora_weights() |
| current_lora = None |
|
|
| |
| kwargs = { |
| "prompt": prompt, |
| "num_inference_steps": 4, |
| "guidance_scale": 1.0, |
| "height": 1024, |
| "width": 1024, |
| } |
| |
| |
| if input_image is not None: |
| kwargs["image"] = input_image |
| |
| image = pipe(**kwargs).images[0] |
| return image |
|
|
| |
| |
| theme = gr.themes.Soft( |
| primary_hue="orange", |
| secondary_hue="red" |
| ) |
|
|
| |
| with gr.Blocks() as demo: |
| gr.Markdown("# FLUX Klein 9B KV Studio") |
| |
| with gr.Row(): |
| with gr.Column(): |
| prompt = gr.Textbox( |
| label="Prompt", |
| placeholder="e.g., Change the model's outfit to a red silk summer dress, keeping the pose and face consistent..." |
| ) |
| |
| with gr.Row(): |
| lora_dropdown = gr.Dropdown( |
| choices=get_my_loras(), |
| value="None", |
| label="Select LoRA Adapter", |
| info="Dynamically loaded from your HF models" |
| ) |
| refresh_btn = gr.Button("๐ Refresh", scale=0) |
| |
| |
| input_img = gr.Image( |
| type="pil", |
| label="Reference Image (Optional - Leave empty to generate, upload model photo to edit)" |
| ) |
| |
| generate_btn = gr.Button("Generate", variant="primary") |
| |
| with gr.Column(): |
| output_img = gr.Image(label="Result") |
| |
| |
| refresh_btn.click( |
| fn=lambda: gr.update(choices=get_my_loras()), |
| outputs=lora_dropdown |
| ) |
| |
| generate_btn.click( |
| fn=generate_image, |
| inputs=[prompt, input_img, lora_dropdown], |
| outputs=[output_img] |
| ) |
|
|
| |
| demo.launch(theme=theme) |