Kabilash10 commited on
Commit
62e0b36
·
verified ·
1 Parent(s): 7fcc0c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -57
app.py CHANGED
@@ -1,51 +1,54 @@
1
  import gradio as gr
2
  import numpy as np
3
  import random
4
- #import spaces #[uncomment to use ZeroGPU]
5
  from diffusers import DiffusionPipeline
6
  import torch
7
 
8
- device = "cuda" if torch.cuda.is_available() else "cpu"
9
- model_repo_id = "stabilityai/sdxl-turbo" #Replace to the model you would like to use
10
 
11
- if torch.cuda.is_available():
12
- torch_dtype = torch.float16
13
- else:
14
- torch_dtype = torch.float32
15
 
16
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
17
- pipe = pipe.to(device)
18
 
 
 
 
 
19
  MAX_SEED = np.iinfo(np.int32).max
20
- MAX_IMAGE_SIZE = 1024
21
 
22
- #@spaces.GPU #[uncomment to use ZeroGPU]
23
  def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
24
 
25
  if randomize_seed:
26
  seed = random.randint(0, MAX_SEED)
27
 
28
- generator = torch.Generator().manual_seed(seed)
29
 
 
30
  image = pipe(
31
- prompt = prompt,
32
- negative_prompt = negative_prompt,
33
- guidance_scale = guidance_scale,
34
- num_inference_steps = num_inference_steps,
35
- width = width,
36
- height = height,
37
- generator = generator
38
  ).images[0]
39
 
40
  return image, seed
41
 
 
42
  examples = [
43
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
44
- "An astronaut riding a green horse",
45
- "A delicious ceviche cheesecake slice",
46
  ]
47
 
48
- css="""
 
49
  #col-container {
50
  margin: 0 auto;
51
  max-width: 640px;
@@ -55,31 +58,27 @@ css="""
55
  with gr.Blocks(css=css) as demo:
56
 
57
  with gr.Column(elem_id="col-container"):
58
- gr.Markdown(f"""
59
- # Text-to-Image Gradio Template
60
- """)
61
-
62
  with gr.Row():
63
-
64
- prompt = gr.Text(
65
  label="Prompt",
66
  show_label=False,
67
  max_lines=1,
68
- placeholder="Enter your prompt",
69
  container=False,
70
  )
71
 
72
- run_button = gr.Button("Run", scale=0)
73
-
74
- result = gr.Image(label="Result", show_label=False)
75
 
76
  with gr.Accordion("Advanced Settings", open=False):
77
-
78
- negative_prompt = gr.Text(
79
- label="Negative prompt",
80
  max_lines=1,
81
- placeholder="Enter a negative prompt",
82
- visible=False,
83
  )
84
 
85
  seed = gr.Slider(
@@ -90,53 +89,51 @@ with gr.Blocks(css=css) as demo:
90
  value=0,
91
  )
92
 
93
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
94
-
95
  with gr.Row():
96
-
97
  width = gr.Slider(
98
  label="Width",
99
  minimum=256,
100
  maximum=MAX_IMAGE_SIZE,
101
  step=32,
102
- value=1024, #Replace with defaults that work for your model
103
  )
104
-
105
  height = gr.Slider(
106
  label="Height",
107
  minimum=256,
108
  maximum=MAX_IMAGE_SIZE,
109
  step=32,
110
- value=1024, #Replace with defaults that work for your model
111
  )
112
 
113
  with gr.Row():
114
-
115
  guidance_scale = gr.Slider(
116
- label="Guidance scale",
117
  minimum=0.0,
118
  maximum=10.0,
119
- step=0.1,
120
- value=0.0, #Replace with defaults that work for your model
121
  )
122
 
123
  num_inference_steps = gr.Slider(
124
- label="Number of inference steps",
125
  minimum=1,
126
- maximum=50,
127
  step=1,
128
- value=2, #Replace with defaults that work for your model
129
  )
130
 
131
  gr.Examples(
132
- examples = examples,
133
- inputs = [prompt]
134
  )
 
135
  gr.on(
136
  triggers=[run_button.click, prompt.submit],
137
- fn = infer,
138
- inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
139
- outputs = [result, seed]
140
  )
141
 
142
- demo.queue().launch()
 
1
  import gradio as gr
2
  import numpy as np
3
  import random
 
4
  from diffusers import DiffusionPipeline
5
  import torch
6
 
7
+ # Device setup - CPU for now
8
+ device = "cpu"
9
 
10
+ # Model setup (replace with the model you want to use)
11
+ model_repo_id = "KingNish/Realtime-FLUX"
 
 
12
 
13
+ # CPU runs with float32 as float16 is typically for GPU
14
+ torch_dtype = torch.float32
15
 
16
+ # Load the model on CPU
17
+ pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype).to(device)
18
+
19
+ # Constants
20
  MAX_SEED = np.iinfo(np.int32).max
21
+ MAX_IMAGE_SIZE = 512 # Reduce size for CPU performance
22
 
 
23
  def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
24
 
25
  if randomize_seed:
26
  seed = random.randint(0, MAX_SEED)
27
 
28
+ generator = torch.manual_seed(seed)
29
 
30
+ # Generate image using the pipeline
31
  image = pipe(
32
+ prompt=prompt,
33
+ negative_prompt=negative_prompt,
34
+ guidance_scale=guidance_scale,
35
+ num_inference_steps=num_inference_steps,
36
+ width=width,
37
+ height=height,
38
+ generator=generator
39
  ).images[0]
40
 
41
  return image, seed
42
 
43
+ # Sample prompts
44
  examples = [
45
+ "Astronaut in a jungle, cold color palette, detailed, 8k",
46
+ "A robot playing a guitar in a futuristic city",
47
+ "A glowing blue cat sitting on a floating island"
48
  ]
49
 
50
+ # Gradio Interface
51
+ css = """
52
  #col-container {
53
  margin: 0 auto;
54
  max-width: 640px;
 
58
  with gr.Blocks(css=css) as demo:
59
 
60
  with gr.Column(elem_id="col-container"):
61
+ gr.Markdown("# Realtime-FLUX Text-to-Image Generator")
62
+
 
 
63
  with gr.Row():
64
+ prompt = gr.Textbox(
 
65
  label="Prompt",
66
  show_label=False,
67
  max_lines=1,
68
+ placeholder="Describe the image you want to generate...",
69
  container=False,
70
  )
71
 
72
+ run_button = gr.Button("Generate")
73
+
74
+ result = gr.Image(label="Generated Image", show_label=False)
75
 
76
  with gr.Accordion("Advanced Settings", open=False):
77
+ negative_prompt = gr.Textbox(
78
+ label="Negative Prompt",
 
79
  max_lines=1,
80
+ placeholder="Enter a negative prompt (optional)",
81
+ visible=True,
82
  )
83
 
84
  seed = gr.Slider(
 
89
  value=0,
90
  )
91
 
92
+ randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
93
+
94
  with gr.Row():
 
95
  width = gr.Slider(
96
  label="Width",
97
  minimum=256,
98
  maximum=MAX_IMAGE_SIZE,
99
  step=32,
100
+ value=384, # Optimal for CPU
101
  )
 
102
  height = gr.Slider(
103
  label="Height",
104
  minimum=256,
105
  maximum=MAX_IMAGE_SIZE,
106
  step=32,
107
+ value=384, # Optimal for CPU
108
  )
109
 
110
  with gr.Row():
 
111
  guidance_scale = gr.Slider(
112
+ label="Guidance Scale",
113
  minimum=0.0,
114
  maximum=10.0,
115
+ step=0.5,
116
+ value=7.5,
117
  )
118
 
119
  num_inference_steps = gr.Slider(
120
+ label="Inference Steps",
121
  minimum=1,
122
+ maximum=25, # Keep lower for CPU performance
123
  step=1,
124
+ value=15, # Reasonable default for CPU
125
  )
126
 
127
  gr.Examples(
128
+ examples=examples,
129
+ inputs=[prompt]
130
  )
131
+
132
  gr.on(
133
  triggers=[run_button.click, prompt.submit],
134
+ fn=infer,
135
+ inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
136
+ outputs=[result, seed]
137
  )
138
 
139
+ demo.queue().launch()