DonImages commited on
Commit
f46d5fb
·
verified ·
1 Parent(s): 4fee12c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -25
app.py CHANGED
@@ -3,7 +3,6 @@ from diffusers import StableDiffusion3Pipeline
3
  from huggingface_hub import login
4
  import os
5
  import gradio as gr
6
- from transformers import pipeline as transformers_pipeline
7
 
8
  # Retrieve the token from the environment variable
9
  token = os.getenv("HF_TOKEN") # Hugging Face token from the secret
@@ -12,32 +11,11 @@ if token:
12
  else:
13
  raise ValueError("Hugging Face token not found. Please set it as a repository secret in the Space settings.")
14
 
15
- # Load the Stable Diffusion 3.5 model with quantization enabled for CPU
16
  model_id = "stabilityai/stable-diffusion-3.5-large"
17
- pipe = StableDiffusion3Pipeline.from_pretrained(model_id, torch_dtype=torch.float16) # Use float16 for less memory usage
18
 
19
- # Perform quantization on the model to reduce the memory footprint
20
- pipe.unet = torch.quantization.quantize_dynamic(pipe.unet, {torch.nn.Linear}, dtype=torch.qint8)
21
-
22
- pipe.to("cpu") # Ensure it runs on CPU
23
-
24
- # Define the path to the LoRA model
25
- lora_model_path = "./lora_model.pth" # Assuming the file is saved locally
26
-
27
- # Custom method to load and apply LoRA weights to the Stable Diffusion pipeline
28
- def load_lora_model(pipe, lora_model_path):
29
- # Load the LoRA weights
30
- lora_weights = torch.load(lora_model_path, map_location="cpu")
31
-
32
- # Apply weights to the UNet submodule
33
- for name, param in pipe.unet.named_parameters(): # Accessing unet parameters
34
- if name in lora_weights:
35
- param.data += lora_weights[name]
36
-
37
- return pipe
38
-
39
- # Load and apply the LoRA model weights
40
- pipe = load_lora_model(pipe, lora_model_path)
41
 
42
  # Function to generate an image from a text prompt
43
  def generate_image(prompt):
 
3
  from huggingface_hub import login
4
  import os
5
  import gradio as gr
 
6
 
7
  # Retrieve the token from the environment variable
8
  token = os.getenv("HF_TOKEN") # Hugging Face token from the secret
 
11
  else:
12
  raise ValueError("Hugging Face token not found. Please set it as a repository secret in the Space settings.")
13
 
14
+ # Load the Stable Diffusion 3.5 model
15
  model_id = "stabilityai/stable-diffusion-3.5-large"
16
+ pipe = StableDiffusion3Pipeline.from_pretrained(model_id) # No LoRA integration
17
 
18
+ pipe.to("cpu") # Ensuring it runs on CPU
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  # Function to generate an image from a text prompt
21
  def generate_image(prompt):