Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,20 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
import spaces
|
| 7 |
from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, StableDiffusionImg2ImgPipeline
|
| 8 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
model_repo_id = "stabilityai/stable-diffusion-3.5-medium"
|
|
@@ -16,16 +26,31 @@ else:
|
|
| 16 |
torch_dtype = torch.float32
|
| 17 |
|
| 18 |
# For text-to-image
|
| 19 |
-
pipe = DiffusionPipeline.from_pretrained(
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
pipe = pipe.to(device)
|
| 22 |
|
| 23 |
# For image-to-image
|
| 24 |
img2img_pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 25 |
model_repo_id,
|
| 26 |
-
torch_dtype=torch_dtype
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
)
|
| 28 |
-
img2img_pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained(model_repo_id, subfolder="scheduler", shift=5)
|
| 29 |
img2img_pipe = img2img_pipe.to(device)
|
| 30 |
|
| 31 |
MAX_SEED = np.iinfo(np.int32).max
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
from PIL import Image
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
import spaces
|
| 8 |
from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, StableDiffusionImg2ImgPipeline
|
| 9 |
import torch
|
| 10 |
+
from huggingface_hub import login
|
| 11 |
+
|
| 12 |
+
# Get token from Hugging Face Spaces secrets
|
| 13 |
+
# This will use the environment variable HF_ACCESS_TOKEN which is the standard in HF Spaces
|
| 14 |
+
hf_token = os.environ.get("HF_ACCESS_TOKEN")
|
| 15 |
+
if hf_token:
|
| 16 |
+
login(hf_token)
|
| 17 |
+
else:
|
| 18 |
+
print("Warning: HF_ACCESS_TOKEN not found in environment. Authentication may fail.")
|
| 19 |
|
| 20 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 21 |
model_repo_id = "stabilityai/stable-diffusion-3.5-medium"
|
|
|
|
| 26 |
torch_dtype = torch.float32
|
| 27 |
|
| 28 |
# For text-to-image
|
| 29 |
+
pipe = DiffusionPipeline.from_pretrained(
|
| 30 |
+
model_repo_id,
|
| 31 |
+
torch_dtype=torch_dtype,
|
| 32 |
+
use_auth_token=True # This will use the token from login()
|
| 33 |
+
)
|
| 34 |
+
pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained(
|
| 35 |
+
model_repo_id,
|
| 36 |
+
subfolder="scheduler",
|
| 37 |
+
shift=5,
|
| 38 |
+
use_auth_token=True
|
| 39 |
+
)
|
| 40 |
pipe = pipe.to(device)
|
| 41 |
|
| 42 |
# For image-to-image
|
| 43 |
img2img_pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 44 |
model_repo_id,
|
| 45 |
+
torch_dtype=torch_dtype,
|
| 46 |
+
use_auth_token=True
|
| 47 |
+
)
|
| 48 |
+
img2img_pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained(
|
| 49 |
+
model_repo_id,
|
| 50 |
+
subfolder="scheduler",
|
| 51 |
+
shift=5,
|
| 52 |
+
use_auth_token=True
|
| 53 |
)
|
|
|
|
| 54 |
img2img_pipe = img2img_pipe.to(device)
|
| 55 |
|
| 56 |
MAX_SEED = np.iinfo(np.int32).max
|