Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,15 @@
|
|
1 |
-
import spaces # Import this first to avoid CUDA initialization issues
|
2 |
-
import os
|
3 |
import gradio as gr
|
4 |
import numpy as np
|
5 |
-
import
|
6 |
import torch
|
7 |
import random
|
8 |
import time
|
9 |
from PIL import Image
|
10 |
from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, FluxTransformer2DModel
|
11 |
from transformers import CLIPTextModel, CLIPTokenizer, T5EncoderModel, T5TokenizerFast
|
|
|
12 |
from gradio_client import Client, handle_file
|
|
|
13 |
|
14 |
dtype = torch.bfloat16
|
15 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
@@ -21,22 +21,45 @@ hf_token = os.getenv('waffles')
|
|
21 |
if not hf_token:
|
22 |
raise ValueError("Hugging Face API token not found. Please set the 'waffles' environment variable.")
|
23 |
|
|
|
|
|
24 |
|
25 |
-
pipe = DiffusionPipeline.from_pretrained("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
@spaces.GPU()
|
28 |
-
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
|
29 |
if randomize_seed:
|
30 |
seed = random.randint(0, MAX_SEED)
|
31 |
generator = torch.Generator().manual_seed(seed)
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
return image, seed
|
41 |
|
42 |
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
+
import spaces
|
4 |
import torch
|
5 |
import random
|
6 |
import time
|
7 |
from PIL import Image
|
8 |
from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, FluxTransformer2DModel
|
9 |
from transformers import CLIPTextModel, CLIPTokenizer, T5EncoderModel, T5TokenizerFast
|
10 |
+
from huggingface_hub import hf_hub_download
|
11 |
from gradio_client import Client, handle_file
|
12 |
+
import os
|
13 |
|
14 |
dtype = torch.bfloat16
|
15 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
21 |
if not hf_token:
|
22 |
raise ValueError("Hugging Face API token not found. Please set the 'waffles' environment variable.")
|
23 |
|
24 |
+
MAX_SEED = np.iinfo(np.int32).max
|
25 |
+
MAX_IMAGE_SIZE = 2048
|
26 |
|
27 |
+
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=torch.bfloat16, token=hf_token).to(device)
|
28 |
+
|
29 |
+
@spaces.GPU(duration=300)
|
30 |
+
def infer(prompt, seed=0, randomize_seed=True, width=640, height=1024, guidance_scale=0.0, num_inference_steps=5, lora_model="AlekseyCalvin/RCA_Agitprop_Manufactory", progress=gr.Progress(track_tqdm=True)):
|
31 |
+
global pipe
|
32 |
+
|
33 |
+
# Load LoRA if specified
|
34 |
+
if lora_model:
|
35 |
+
try:
|
36 |
+
pipe.load_lora_weights(lora_model)
|
37 |
+
except Exception as e:
|
38 |
+
return None, seed, f"Failed to load LoRA model: {str(e)}"
|
39 |
|
|
|
|
|
40 |
if randomize_seed:
|
41 |
seed = random.randint(0, MAX_SEED)
|
42 |
generator = torch.Generator().manual_seed(seed)
|
43 |
+
|
44 |
+
try:
|
45 |
+
image = pipe(
|
46 |
+
prompt=prompt,
|
47 |
+
width=width,
|
48 |
+
height=height,
|
49 |
+
num_inference_steps=num_inference_steps,
|
50 |
+
generator=generator,
|
51 |
+
guidance_scale=guidance_scale
|
52 |
+
).images[0]
|
53 |
+
|
54 |
+
# Unload LoRA weights after generation
|
55 |
+
if lora_model:
|
56 |
+
pipe.unload_lora_weights()
|
57 |
+
|
58 |
+
return image, seed, "Image generated successfully."
|
59 |
+
except Exception as e:
|
60 |
+
return None, seed, f"Error during image generation: {str(e)}"
|
61 |
+
|
62 |
+
|
63 |
return image, seed
|
64 |
|
65 |
|