Randomdude432 commited on
Commit
d469545
·
1 Parent(s): e3b44bc

Grok Fix add @spaces

Browse files
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import gradio as gr
2
  import numpy as np
3
  import torch
@@ -5,14 +6,6 @@ from diffusers import FluxFillPipeline
5
  from PIL import Image
6
  import random
7
 
8
- # Set device (GPU if available, otherwise CPU)
9
- device = "cuda" if torch.cuda.is_available() else "cpu"
10
-
11
- # Load the pipeline
12
- pipe = FluxFillPipeline.from_pretrained(
13
- "black-forest-labs/FLUX.1-Fill-dev", torch_dtype=torch.bfloat16
14
- ).to(device)
15
-
16
 
17
  # Function to calculate optimal dimensions for the output image
18
  def calculate_optimal_dimensions(image: Image.Image):
@@ -43,10 +36,16 @@ def calculate_optimal_dimensions(image: Image.Image):
43
 
44
 
45
  # Inpainting function
 
46
  def infer(image: Image.Image, prompt: str):
47
  if image is None:
48
  raise gr.Error("Please upload an image.")
49
 
 
 
 
 
 
50
  # Calculate optimal dimensions
51
  width, height = calculate_optimal_dimensions(image)
52
 
@@ -56,7 +55,7 @@ def infer(image: Image.Image, prompt: str):
56
 
57
  # Set seed for reproducibility
58
  seed = random.randint(0, np.iinfo(np.int32).max)
59
- generator = torch.Generator(device).manual_seed(seed)
60
 
61
  try:
62
  output = pipe(
 
1
+ import spaces
2
  import gradio as gr
3
  import numpy as np
4
  import torch
 
6
  from PIL import Image
7
  import random
8
 
 
 
 
 
 
 
 
 
9
 
10
  # Function to calculate optimal dimensions for the output image
11
  def calculate_optimal_dimensions(image: Image.Image):
 
36
 
37
 
38
  # Inpainting function
39
+ @spaces.GPU
40
  def infer(image: Image.Image, prompt: str):
41
  if image is None:
42
  raise gr.Error("Please upload an image.")
43
 
44
+ # Load the pipeline inside the function on "cuda"
45
+ pipe = FluxFillPipeline.from_pretrained(
46
+ "black-forest-labs/FLUX.1-Fill-dev", torch_dtype=torch.bfloat16
47
+ ).to("cuda")
48
+
49
  # Calculate optimal dimensions
50
  width, height = calculate_optimal_dimensions(image)
51
 
 
55
 
56
  # Set seed for reproducibility
57
  seed = random.randint(0, np.iinfo(np.int32).max)
58
+ generator = torch.Generator("cuda").manual_seed(seed)
59
 
60
  try:
61
  output = pipe(