DonImages commited on
Commit
37c7828
·
verified ·
1 Parent(s): 7c5089e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -9
app.py CHANGED
@@ -1,7 +1,8 @@
1
- from diffusers import StableDiffusionPipeline
2
  import torch
3
- import os
4
  from huggingface_hub import login
 
 
5
 
6
  # Retrieve the token from the environment variable
7
  token = os.getenv("HF_TOKEN") # Hugging Face token from the secret
@@ -12,20 +13,33 @@ else:
12
 
13
  # Load the Stable Diffusion 3.5 model
14
  model_id = "stabilityai/stable-diffusion-3.5-large"
15
- pipe = StableDiffusion3Pipeline.from_pretrained(model_id, torch_dtype=torch.float16)
16
  pipe.to("cuda")
17
 
18
- # Define the path to the LoRA model (since it's in the main directory)
19
- lora_model_path = "lora_model.pth" # Path to the uploaded LoRA model
20
 
21
- # Load the LoRA model weights into the pipeline
22
- pipe.load_lora_model(lora_model_path) # Integrate the LoRA weights
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  # Function to generate an image from a text prompt
25
  def generate_image(prompt):
26
  image = pipe(prompt).images[0]
27
  return image
28
 
29
- import gradio as gr
30
  iface = gr.Interface(fn=generate_image, inputs="text", outputs="image")
31
- iface.launch()
 
 
1
  import torch
2
+ from diffusers import StableDiffusionPipeline
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
 
13
 
14
  # Load the Stable Diffusion 3.5 model
15
  model_id = "stabilityai/stable-diffusion-3.5-large"
16
+ pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
17
  pipe.to("cuda")
18
 
19
+ # Define the path to the LoRA model
20
+ lora_model_path = "https://huggingface.co/spaces/DonImages/Testing2/resolve/main/lora_model.pth" # LoRA model path
21
 
22
+ # Custom method to load and apply LoRA weights to the Stable Diffusion pipeline
23
+ def load_lora_model(pipe, lora_model_path):
24
+ # Load the LoRA weights (assuming it's a PyTorch .pth file)
25
+ lora_weights = torch.load(lora_model_path, map_location="cuda")
26
+
27
+ # Modify this section based on how LoRA is intended to interact with your Stable Diffusion model
28
+ # Here, we just load the weights into the model's parameters (this is a conceptual approach)
29
+ for name, param in pipe.named_parameters():
30
+ if name in lora_weights:
31
+ param.data += lora_weights[name] # Apply LoRA weights to the parameters
32
+
33
+ return pipe # Return the updated model
34
+
35
+ # Load and apply the LoRA model weights
36
+ pipe = load_lora_model(pipe, lora_model_path)
37
 
38
  # Function to generate an image from a text prompt
39
  def generate_image(prompt):
40
  image = pipe(prompt).images[0]
41
  return image
42
 
43
+ # Gradio interface
44
  iface = gr.Interface(fn=generate_image, inputs="text", outputs="image")
45
+ iface.launch()