AiCoderv2 commited on
Commit
75a470f
·
verified ·
1 Parent(s): f9d8d84

Update Gradio app with multiple files

Browse files
Files changed (5) hide show
  1. app.py +1 -1
  2. config.py +3 -3
  3. models.py +17 -15
  4. requirements.txt +14 -7
  5. utils.py +18 -1
app.py CHANGED
@@ -7,7 +7,7 @@ with gr.Blocks(title="AI Text-to-Image Generator", theme=gr.themes.Soft()) as de
7
  # AI Text-to-Image Generator
8
  [Built with anycoder](https://huggingface.co/spaces/akhaliq/anycoder)
9
 
10
- Generate images from text prompts using Stable Diffusion.
11
  """)
12
 
13
  with gr.Row():
 
7
  # AI Text-to-Image Generator
8
  [Built with anycoder](https://huggingface.co/spaces/akhaliq/anycoder)
9
 
10
+ Generate images from text prompts using FLUX (fast generation).
11
  """)
12
 
13
  with gr.Row():
config.py CHANGED
@@ -1,7 +1,7 @@
1
  # Configuration constants
2
  DEFAULT_PROMPT = "A beautiful landscape"
3
- DEFAULT_NEGATIVE_PROMPT = "blurry, low quality"
4
  DEFAULT_STEPS = 20
5
  DEFAULT_GUIDANCE = 7.5
6
- IMAGE_HEIGHT = 512
7
- IMAGE_WIDTH = 512
 
1
  # Configuration constants
2
  DEFAULT_PROMPT = "A beautiful landscape"
3
+ DEFAULT_NEGATIVE_PROMPT = ""
4
  DEFAULT_STEPS = 20
5
  DEFAULT_GUIDANCE = 7.5
6
+ IMAGE_HEIGHT = 1024
7
+ IMAGE_WIDTH = 1024
models.py CHANGED
@@ -1,40 +1,43 @@
1
  import torch
2
- from diffusers import StableDiffusionPipeline
3
  import spaces
4
 
5
  # Configuration
6
- MODEL_ID = "CompVis/stable-diffusion-v1-4" # Can be changed to other SD models
7
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
8
 
9
- # Load pipeline
10
- pipe = StableDiffusionPipeline.from_pretrained(MODEL_ID, torch_dtype=torch.float16)
 
 
 
11
  pipe.to(DEVICE)
12
 
13
- # AoT Compilation for faster inference
14
  @spaces.GPU(duration=1500)
15
  def compile_transformer():
16
- with spaces.aoti_capture(pipe.unet):
17
- pipe("test prompt", num_inference_steps=1)
18
 
19
  exported = torch.export.export(
20
- pipe.unet,
21
  args=call.args,
22
  kwargs=call.kwargs,
23
  )
24
  return spaces.aoti_compile(exported)
25
 
26
  # Apply compiled model
27
- compiled_unet = compile_transformer()
28
- spaces.aoti_apply(compiled_unet, pipe.unet)
29
 
30
  @spaces.GPU
31
  def generate_image(prompt, negative_prompt="", num_inference_steps=20, guidance_scale=7.5):
32
  """
33
- Generate an image from text prompt using Stable Diffusion.
34
 
35
  Args:
36
  prompt (str): The text prompt for image generation.
37
- negative_prompt (str): Negative prompt to avoid certain elements.
38
  num_inference_steps (int): Number of denoising steps.
39
  guidance_scale (float): Scale for classifier-free guidance.
40
 
@@ -44,11 +47,10 @@ def generate_image(prompt, negative_prompt="", num_inference_steps=20, guidance_
44
  try:
45
  result = pipe(
46
  prompt=prompt,
47
- negative_prompt=negative_prompt if negative_prompt else None,
48
  num_inference_steps=int(num_inference_steps),
49
  guidance_scale=float(guidance_scale),
50
- height=512,
51
- width=512
52
  )
53
  return result.images[0]
54
  except Exception as e:
 
1
  import torch
2
+ from diffusers import DiffusionPipeline
3
  import spaces
4
 
5
  # Configuration
6
+ MODEL_ID = 'black-forest-labs/FLUX.1-dev'
7
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
8
 
9
+ # Set dtype based on device for compatibility
10
+ dtype = torch.bfloat16 if DEVICE == "cuda" else torch.float32
11
+
12
+ # Load pipeline with appropriate dtype for device
13
+ pipe = DiffusionPipeline.from_pretrained(MODEL_ID, dtype=dtype)
14
  pipe.to(DEVICE)
15
 
16
+ # AoT Compilation for faster inference (requires GPU)
17
  @spaces.GPU(duration=1500)
18
  def compile_transformer():
19
+ with spaces.aoti_capture(pipe.transformer) as call:
20
+ pipe("test prompt")
21
 
22
  exported = torch.export.export(
23
+ pipe.transformer,
24
  args=call.args,
25
  kwargs=call.kwargs,
26
  )
27
  return spaces.aoti_compile(exported)
28
 
29
  # Apply compiled model
30
+ compiled_transformer = compile_transformer()
31
+ spaces.aoti_apply(compiled_transformer, pipe.transformer)
32
 
33
  @spaces.GPU
34
  def generate_image(prompt, negative_prompt="", num_inference_steps=20, guidance_scale=7.5):
35
  """
36
+ Generate an image from text prompt using FLUX.
37
 
38
  Args:
39
  prompt (str): The text prompt for image generation.
40
+ negative_prompt (str): Negative prompt (not used in FLUX).
41
  num_inference_steps (int): Number of denoising steps.
42
  guidance_scale (float): Scale for classifier-free guidance.
43
 
 
47
  try:
48
  result = pipe(
49
  prompt=prompt,
 
50
  num_inference_steps=int(num_inference_steps),
51
  guidance_scale=float(guidance_scale),
52
+ height=1024,
53
+ width=1024
54
  )
55
  return result.images[0]
56
  except Exception as e:
requirements.txt CHANGED
@@ -1,7 +1,14 @@
1
- gradio>=4.0.0
2
- torch>=2.0.0
3
- diffusers>=0.25.0
4
- transformers>=4.30.0
5
- accelerate>=0.25.0
6
- safetensors>=0.4.0
7
- spaces
 
 
 
 
 
 
 
 
1
+ gradio
2
+ spaces
3
+ git+https://github.com/huggingface/diffusers
4
+ git+https://github.com/huggingface/transformers
5
+ torch
6
+ torchvision
7
+ accelerate
8
+ tokenizers
9
+ sentencepiece
10
+ Pillow
11
+ requests
12
+ numpy
13
+ scipy
14
+ matplotlib
utils.py CHANGED
@@ -13,4 +13,21 @@ def preprocess_inputs(prompt, negative_prompt, steps, guidance):
13
  negative_prompt = negative_prompt.strip() if negative_prompt else ""
14
  steps = max(1, min(50, int(steps)))
15
  guidance = max(1.0, min(20.0, float(guidance)))
16
- return prompt, negative_prompt, steps, guidance
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  negative_prompt = negative_prompt.strip() if negative_prompt else ""
14
  steps = max(1, min(50, int(steps)))
15
  guidance = max(1.0, min(20.0, float(guidance)))
16
+ return prompt, negative_prompt, steps, guidance
17
+
18
+ === gradio>=4.0.0
19
+ torch>=2.0.0
20
+ diffusers>=0.27.0
21
+ transformers>=4.36.0
22
+ accelerate>=0.25.0
23
+ safetensors>=0.4.0
24
+ spaces
25
+ torchao>=0.4.0 ===
26
+ gradio>=4.0.0
27
+ torch>=2.0.0
28
+ diffusers>=0.27.0
29
+ transformers>=4.36.0
30
+ accelerate>=0.25.0
31
+ safetensors>=0.4.0
32
+ spaces
33
+ torchao>=0.4.0