File size: 1,915 Bytes
ed4033a
 
1ca8738
ed4033a
 
 
 
721df59
e06d213
721df59
 
 
e06d213
 
ed4033a
721df59
1ca8738
721df59
 
e06d213
721df59
 
 
e06d213
ed4033a
e06d213
 
ed4033a
e06d213
ed4033a
721df59
1ca8738
721df59
 
e06d213
721df59
 
 
e06d213
ed4033a
e06d213
 
 
 
ed4033a
 
 
e06d213
ed4033a
e06d213
ed4033a
 
e06d213
ed4033a
e06d213
 
ed4033a
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import spaces
import torch
import os
from diffusers import DiffusionPipeline

MODEL_ID = 'black-forest-labs/FLUX.1-dev'

# Set custom cache directory to avoid filling Hugging Face storage limit
CUSTOM_CACHE_DIR = './flux_cache'
os.environ['HF_HOME'] = CUSTOM_CACHE_DIR
os.environ['TRANSFORMERS_CACHE'] = CUSTOM_CACHE_DIR

# Compile the model ahead-of-time for optimal performance (CPU version)
@spaces.GPU(duration=1500)  # Note: This might not work on CPU-only, but keeping for compatibility
def compile_transformer():
    # Load model with HF token if available and custom cache dir
    token = os.getenv('HF_TOKEN')
    pipe = DiffusionPipeline.from_pretrained(
        MODEL_ID, 
        torch_dtype=torch.float32,  # Use float32 for CPU
        token=token,
        cache_dir=CUSTOM_CACHE_DIR
    )
    # Note: No .to('cuda') since CUDA is not available
    
    # Skip AoT compilation for CPU - it's not supported well
    return None

# Load the model (CPU version)
def load_model():
    # Load model with HF token if available and custom cache dir
    token = os.getenv('HF_TOKEN')
    pipe = DiffusionPipeline.from_pretrained(
        MODEL_ID, 
        torch_dtype=torch.float32,  # Use float32 for CPU compatibility
        token=token,
        cache_dir=CUSTOM_CACHE_DIR
    )
    # Note: No .to('cuda') - running on CPU
    
    # Skip AoT compilation for CPU
    # compiled_transformer = compile_transformer()
    # if compiled_transformer:
    #     spaces.aoti_apply(compiled_transformer, pipe.transformer)
    
    return pipe

# Note: Removed @spaces.GPU since CUDA is not available
def generate_image(pipe, prompt):
    # Generate image with optimized settings for CPU
    image = pipe(
        prompt,
        num_inference_steps=10,  # Even fewer steps for CPU speed
        guidance_scale=3.5,
        height=256,  # Smaller size for CPU
        width=256
    ).images[0]
    
    return image