yunquan commited on
Commit
68bc1b3
·
verified ·
1 Parent(s): 53be099

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -21
app.py CHANGED
@@ -16,27 +16,21 @@ access_token = os.getenv("HF_ACCESS_TOKEN")
16
  device = "cuda" if torch.cuda.is_available() else "cpu"
17
 
18
 
19
- # Load the Stable Diffusion model
20
- if torch.cuda.is_available():
21
- torch.cuda.max_memory_allocated(device=device)
22
- pipe = StableDiffusionPipeline.from_pretrained(
23
- "stabilityai/stable-diffusion-3-medium",
24
- torch_dtype=torch.float16,
25
- use_auth_token=access_token # Use the token here
26
- )
27
- pipe.enable_xformers_memory_efficient_attention()
28
- pipe = pipe.to(device)
29
- else:
30
- pipe = StableDiffusionPipeline.from_pretrained(
31
- "stabilityai/stable-diffusion-3-medium",
32
- use_auth_token=access_token # Use the token here
33
- )
34
- pipe = pipe.to(device)
35
-
36
- logging.info("Loading the model...")
37
-
38
- # Load model
39
- logging.info("Model loaded successfully.")
40
 
41
  MAX_SEED = np.iinfo(np.int32).max
42
  MAX_IMAGE_SIZE = 1024
 
16
  device = "cuda" if torch.cuda.is_available() else "cpu"
17
 
18
 
19
+ # Global variable for the pipeline
20
+ pipe = None
21
+
22
+ def load_model():
23
+ global pipe
24
+ if pipe is None:
25
+ logging.info("Loading the Stable Diffusion model...")
26
+ pipe = StableDiffusionPipeline.from_pretrained(
27
+ "stabilityai/stable-diffusion-3-medium",
28
+ torch_dtype=torch.float16,
29
+ use_auth_token=access_token,
30
+ cache_dir="/path/to/cache" # specify cache directory
31
+ )
32
+ pipe = pipe.to(device)
33
+ logging.info("Model loaded successfully.")
 
 
 
 
 
 
34
 
35
  MAX_SEED = np.iinfo(np.int32).max
36
  MAX_IMAGE_SIZE = 1024