Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -20,16 +20,20 @@ class StableBuddyApp:
|
|
20 |
if not auth_token:
|
21 |
raise ValueError("AUTH_TOKEN environment variable is not set.")
|
22 |
|
23 |
-
# Use float16 and fp16 so that stable diffusion can work on 4GB VRAM
|
24 |
self.pipe = StableDiffusionPipeline.from_pretrained(
|
25 |
-
model_id, revision='fp16', torch_dtype=torch.
|
26 |
)
|
27 |
self.pipe.to(device)
|
28 |
|
29 |
def generate_image(self, prompt):
|
30 |
"""Generate an image based on the prompt."""
|
31 |
try:
|
32 |
-
|
|
|
|
|
|
|
|
|
33 |
image = self.pipe(prompt, guidance_scale=8.5).images[0]
|
34 |
|
35 |
# Save the generated image temporarily
|
@@ -40,8 +44,7 @@ class StableBuddyApp:
|
|
40 |
|
41 |
except Exception as e:
|
42 |
print(f"An error occurred: {e}")
|
43 |
-
return None
|
44 |
-
|
45 |
# Create an instance of the StableBuddyApp
|
46 |
stable_buddy_app = StableBuddyApp()
|
47 |
|
|
|
20 |
if not auth_token:
|
21 |
raise ValueError("AUTH_TOKEN environment variable is not set.")
|
22 |
|
23 |
+
# Use float16 and fp16 so that stable diffusion can work on 4GB VRAM float 32 for cpu
|
24 |
self.pipe = StableDiffusionPipeline.from_pretrained(
|
25 |
+
model_id, revision='fp16', torch_dtype=torch.float32, use_auth_token=auth_token
|
26 |
)
|
27 |
self.pipe.to(device)
|
28 |
|
29 |
def generate_image(self, prompt):
|
30 |
"""Generate an image based on the prompt."""
|
31 |
try:
|
32 |
+
# Use autocast only for GPU
|
33 |
+
if self.device == "cuda":
|
34 |
+
with autocast(self.device):
|
35 |
+
image = self.pipe(prompt, guidance_scale=8.5).images[0]
|
36 |
+
else:
|
37 |
image = self.pipe(prompt, guidance_scale=8.5).images[0]
|
38 |
|
39 |
# Save the generated image temporarily
|
|
|
44 |
|
45 |
except Exception as e:
|
46 |
print(f"An error occurred: {e}")
|
47 |
+
return None
|
|
|
48 |
# Create an instance of the StableBuddyApp
|
49 |
stable_buddy_app = StableBuddyApp()
|
50 |
|