Spaces:
vilarin
/
Running on Zero

vilarin commited on
Commit
c3bdaa9
1 Parent(s): 3958ec9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -14
app.py CHANGED
@@ -3,8 +3,9 @@ import gradio as gr
3
  import torch
4
  import numpy as np
5
  import random
6
- from diffusers import StableDiffusion3Pipeline, AutoencoderKL, SD3Transformer2DModel, FlowMatchEulerDiscreteScheduler
7
  import spaces
 
8
  from PIL import Image
9
  import requests
10
  import transformers
@@ -63,8 +64,10 @@ tokenizer_3 = AutoTokenizer.from_pretrained(
63
  # Ensure model and scheduler are initialized in GPU-enabled function
64
  if torch.cuda.is_available():
65
  pipe = StableDiffusion3Pipeline.from_pretrained(repo, vae=vae, transformer=transformer, tokenizer_3=tokenizer_3, text_encoder_3=text_encoder_3, torch_dtype=torch.float16).to("cuda")
 
66
 
67
  pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_config(pipe.scheduler.config)
 
68
 
69
  # Function
70
  @spaces.GPU()
@@ -86,23 +89,34 @@ def generate_image(
86
 
87
 
88
  if prompt['files']:
89
- images = Image.open(prompt['files'][-1]).convert('RGB')
 
90
  else:
91
- images = None
92
  generator = torch.Generator().manual_seed(seed)
93
 
94
 
95
-
96
- image = pipe(
97
- text,
98
- image=images,
99
- negative_prompt=negative,
100
- width=width,
101
- height=height,
102
- guidance_scale=scale,
103
- num_inference_steps=steps,
104
- generator = generator,
105
- )
 
 
 
 
 
 
 
 
 
 
106
  return image.images[0]
107
 
108
 
 
3
  import torch
4
  import numpy as np
5
  import random
6
+ from diffusers import StableDiffusion3Pipeline, AutoencoderKL, SD3Transformer2DModel, FlowMatchEulerDiscreteScheduler, StableDiffusion3Img2ImgPipeline
7
  import spaces
8
+ from diffusers.utils import load_image
9
  from PIL import Image
10
  import requests
11
  import transformers
 
64
  # Ensure model and scheduler are initialized in GPU-enabled function
65
  if torch.cuda.is_available():
66
  pipe = StableDiffusion3Pipeline.from_pretrained(repo, vae=vae, transformer=transformer, tokenizer_3=tokenizer_3, text_encoder_3=text_encoder_3, torch_dtype=torch.float16).to("cuda")
67
+ pipe2 = StableDiffusion3Img2ImgPipeline.from_pretrained(repo, vae=vae, transformer=transformer, tokenizer_3=tokenizer_3, text_encoder_3=text_encoder_3, torch_dtype=torch.float16).to("cuda")
68
 
69
  pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_config(pipe.scheduler.config)
70
+ pipe2.scheduler = FlowMatchEulerDiscreteScheduler.from_config(pipe.scheduler.config)
71
 
72
  # Function
73
  @spaces.GPU()
 
89
 
90
 
91
  if prompt['files']:
92
+ #images = Image.open(prompt['files'][-1]).convert('RGB')
93
+ init_image = load_image(prompt['files'][-1]).resize((512, 512))
94
  else:
95
+ init_image = None
96
  generator = torch.Generator().manual_seed(seed)
97
 
98
 
99
+ if init_image:
100
+ image = pipe2(
101
+ text,
102
+ image=init_image,
103
+ negative_prompt=negative,
104
+ width=width,
105
+ height=height,
106
+ guidance_scale=scale,
107
+ num_inference_steps=steps,
108
+ generator = generator,
109
+ )
110
+ else:
111
+ image = pipe(
112
+ text,
113
+ negative_prompt=negative,
114
+ width=width,
115
+ height=height,
116
+ guidance_scale=scale,
117
+ num_inference_steps=steps,
118
+ generator = generator,
119
+ )
120
  return image.images[0]
121
 
122