John6666 commited on
Commit
9feb242
Β·
verified Β·
1 Parent(s): 534e702

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +13 -12
  2. app.py +57 -0
  3. requirements.txt +10 -0
README.md CHANGED
@@ -1,12 +1,13 @@
1
- ---
2
- title: Diffusers Lora Error Test1
3
- emoji: 🏒
4
- colorFrom: blue
5
- colorTo: gray
6
- sdk: gradio
7
- sdk_version: 5.9.1
8
- app_file: app.py
9
- pinned: false
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
1
+ ---
2
+ title: Diffusers LoRA test with quantization
3
+ emoji: πŸ™„
4
+ colorFrom: indigo
5
+ colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 4.44.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import gradio as gr
3
+ import torch
4
+ from huggingface_hub import hf_hub_download
5
+ from diffusers import FluxPipeline, FluxTransformer2DModel, GGUFQuantizationConfig, BitsAndBytesConfig
6
+ import os
7
+ import subprocess
8
+ #subprocess.run("pip list", shell=True)
9
+ #subprocess.run("diffusers-cli env", shell=True)
10
+ #from optimum.quanto import freeze, qfloat8, quantize
11
+
12
+ HF_TOKEN = os.getenv("HF_TOKEN", "")
13
+ device = "cuda" if torch.cuda.is_available() else "cpu"
14
+ flux_repo = "multimodalart/FLUX.1-dev2pro-full"
15
+ ckpt_path = "https://huggingface.co/city96/FLUX.1-dev-gguf/blob/main/flux1-dev-Q2_K.gguf"
16
+ transformer_gguf = FluxTransformer2DModel.from_single_file(ckpt_path, subfolder="transformer", quantization_config=GGUFQuantizationConfig(compute_dtype=torch.bfloat16),
17
+ torch_dtype=torch.bfloat16, config=flux_repo, token=HF_TOKEN)
18
+ transformer = FluxTransformer2DModel.from_pretrained(flux_repo, subfolder="transformer", torch_dtype=torch.bfloat16, token=HF_TOKEN)
19
+ nf4_quantization_config = BitsAndBytesConfig(load_in_4bit=True)
20
+ transformer_nf4 = FluxTransformer2DModel.from_pretrained(flux_repo, subfolder="transformer", quantization_config=nf4_quantization_config,
21
+ torch_dtype=torch.bfloat16, token=HF_TOKEN)
22
+ pipe = FluxPipeline.from_pretrained(flux_repo, transformer=transformer, torch_dtype=torch.bfloat16, token=HF_TOKEN)
23
+ hyper_sd_lora = hf_hub_download("ByteDance/Hyper-SD", "Hyper-FLUX.1-dev-8steps-lora.safetensors")
24
+
25
+ @spaces.GPU(duration=70)
26
+ def infer(prompt: str, mode: str, is_lora: bool, progress=gr.Progress(track_tqdm=True)):
27
+ global pipe
28
+ try:
29
+ pipe.unload_lora_weights()
30
+ if mode == "Default": pipe.transformer = transformer
31
+ elif mode == "GGUF": pipe.transformer = transformer_gguf
32
+ elif mode == "NF4": pipe.transformer = transformer_nf4
33
+ if is_lora:
34
+ pipe.load_lora_weights(hyper_sd_lora, adapter_name="hyper-sd")
35
+ pipe.set_adapters(["hyper-sd"], adapter_weights=[0.125])
36
+ steps = 8
37
+ else: steps = 28
38
+ pipe.to(device)
39
+ image = pipe(prompt, generator=torch.manual_seed(0), num_inference_steps=steps).images[0]
40
+ pipe.to("cpu")
41
+ return image
42
+ except Exception as e:
43
+ raise gr.Error(e)
44
+
45
+ with gr.Blocks() as demo:
46
+ with gr.Row():
47
+ with gr.Column():
48
+ prompt = gr.Textbox(label="Prompt", value="A cat holding a sign that says hello world", lines=1)
49
+ mode = gr.Radio(label="Mode", choices=["Default", "GGUF", "NF4"], value="Default")
50
+ is_lora = gr.Checkbox(label="Enable LoRA", value=True)
51
+ gen_btn = gr.Button("Generate Image")
52
+ with gr.Column():
53
+ result = gr.Image(label="Result Image")
54
+
55
+ gen_btn.click(infer, [prompt, mode, is_lora], [result])
56
+
57
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ huggingface_hub
2
+ torch
3
+ diffusers
4
+ peft
5
+ transformers
6
+ accelerate
7
+ numpy<2
8
+ gguf
9
+ bitsandbytes
10
+ optimum-quanto