atef91 commited on
Commit
6e0d2cb
·
verified ·
1 Parent(s): 4c69a95

Upload 3 files

Browse files
Files changed (2) hide show
  1. app.py +31 -146
  2. requirements.txt +2 -6
app.py CHANGED
@@ -6,107 +6,34 @@ import warnings
6
  warnings.filterwarnings('ignore')
7
 
8
  # Constants
9
- MODEL_ID = "CompVis/stable-diffusion-v1-4" # Using v1.4 which is publicly available
10
- DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
11
- DTYPE = torch.float32 # Using float32 for better compatibility
12
 
13
- class FooocusGenerator:
14
- def __init__(self):
15
- self.pipe = None
16
- self.load_models()
17
-
18
- def load_models(self):
19
- try:
20
- # Initialize pipeline with basic settings
21
- self.pipe = StableDiffusionPipeline.from_pretrained(
22
- MODEL_ID,
23
- torch_dtype=DTYPE,
24
- use_auth_token=False # No auth token needed for v1.4
25
- )
26
-
27
- if DEVICE == "cuda":
28
- self.pipe.enable_attention_slicing()
29
- self.pipe = self.pipe.to(DEVICE)
30
-
31
- except Exception as e:
32
- print(f"Error loading model: {str(e)}")
33
- raise e
34
-
35
- def generate_image(
36
- self,
37
- prompt,
38
- negative_prompt,
39
- style_selections,
40
- performance_selection,
41
- aspect_ratios_selection,
42
- image_number,
43
- image_seed,
44
- sharpness,
45
- guidance_scale,
46
- progress=gr.Progress()
47
- ):
48
- try:
49
- # Process style selections
50
- processed_prompt = self.process_style(prompt, style_selections)
51
-
52
- # Set seed for reproducibility
53
- if image_seed == -1:
54
- image_seed = torch.randint(0, 2147483647, (1,)).item()
55
-
56
- generator = torch.manual_seed(image_seed)
57
-
58
- # Set steps based on performance selection
59
- steps = {
60
- "Speed": 20,
61
- "Quality": 30,
62
- "Extreme Speed": 15
63
- }.get(performance_selection, 30)
64
-
65
- # Set image dimensions based on aspect ratio
66
- dimensions = {
67
- "Square": (512, 512),
68
- "Portrait": (512, 768),
69
- "Landscape": (768, 512)
70
- }.get(aspect_ratios_selection, (512, 512))
71
-
72
- # Generate image with basic settings
73
- with torch.inference_mode():
74
- image = self.pipe(
75
- prompt=processed_prompt,
76
- negative_prompt=negative_prompt,
77
- num_inference_steps=steps,
78
- guidance_scale=guidance_scale,
79
- height=dimensions[1],
80
- width=dimensions[0],
81
- generator=generator
82
- ).images[0]
83
-
84
- return image
85
-
86
- except Exception as e:
87
- print(f"Error generating image: {str(e)}")
88
- return None
89
-
90
- def process_style(self, prompt, style_selections):
91
- style_modifiers = {
92
- "Fooocus V2": ", professional, high quality, detailed",
93
- "Fooocus Enhance": ", enhanced details, perfect composition",
94
- "Fooocus Sharp": ", sharp focus, high resolution",
95
- "Fooocus Masterpiece": ", masterpiece, best quality, award winning",
96
- }
97
 
98
- selected_modifiers = [style_modifiers[style] for style in style_selections if style in style_modifiers]
99
- processed_prompt = prompt + " ".join(selected_modifiers)
 
 
 
 
 
 
100
 
101
- return processed_prompt
102
-
103
- # Initialize the generator
104
- generator = FooocusGenerator()
105
 
106
  # Create the Gradio interface
107
- with gr.Blocks(title="Fooocus Web") as demo:
108
- gr.Markdown("# 🎨 Fooocus Web")
109
- gr.Markdown("Generate high-quality images with advanced controls")
110
 
111
  with gr.Row():
112
  with gr.Column():
@@ -119,53 +46,16 @@ with gr.Blocks(title="Fooocus Web") as demo:
119
  label="Negative Prompt",
120
  placeholder="What you don't want in the image...",
121
  info="Specify unwanted elements",
122
- value="ugly, blurry, low quality, distorted, deformed"
123
  )
124
 
125
  with gr.Row():
126
- style_selections = gr.CheckboxGroup(
127
- choices=["Fooocus V2", "Fooocus Enhance", "Fooocus Sharp", "Fooocus Masterpiece"],
128
- label="Style Selections",
129
- value=["Fooocus V2"]
130
- )
131
-
132
- with gr.Row():
133
- performance_selection = gr.Radio(
134
- choices=["Speed", "Quality", "Extreme Speed"],
135
- label="Performance",
136
- value="Quality"
137
- )
138
-
139
- aspect_ratios_selection = gr.Radio(
140
- choices=["Square", "Portrait", "Landscape"],
141
- label="Aspect Ratio",
142
- value="Square"
143
- )
144
-
145
- with gr.Row():
146
- image_number = gr.Slider(
147
- minimum=1,
148
- maximum=32,
149
- value=1,
150
  step=1,
151
- label="Number of Images"
152
- )
153
-
154
- image_seed = gr.Slider(
155
- minimum=-1,
156
- maximum=2147483647,
157
- step=1,
158
- value=-1,
159
- label="Seed (-1 for random)"
160
- )
161
-
162
- with gr.Row():
163
- sharpness = gr.Slider(
164
- minimum=0.0,
165
- maximum=30.0,
166
- value=2.0,
167
- step=0.1,
168
- label="Sharpness"
169
  )
170
 
171
  guidance_scale = gr.Slider(
@@ -182,17 +72,12 @@ with gr.Blocks(title="Fooocus Web") as demo:
182
  output_image = gr.Image(label="Generated Image", type="pil")
183
 
184
  generate_btn.click(
185
- fn=generator.generate_image,
186
  inputs=[
187
  prompt,
188
  negative_prompt,
189
- style_selections,
190
- performance_selection,
191
- aspect_ratios_selection,
192
- image_number,
193
- image_seed,
194
- sharpness,
195
  guidance_scale,
 
196
  ],
197
  outputs=output_image
198
  )
 
6
  warnings.filterwarnings('ignore')
7
 
8
  # Constants
9
+ MODEL_ID = "google/ddpm-celebahq-256" # Using a simpler model
10
+ DEVICE = "cpu" # Force CPU for better compatibility
11
+ DTYPE = torch.float32
12
 
13
+ def generate_image(prompt, negative_prompt, guidance_scale=7.5, steps=30):
14
+ try:
15
+ # Initialize pipeline with basic settings
16
+ pipe = StableDiffusionPipeline.from_pretrained(MODEL_ID)
17
+ pipe = pipe.to(DEVICE)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ # Generate image
20
+ with torch.inference_mode():
21
+ image = pipe(
22
+ prompt=prompt,
23
+ negative_prompt=negative_prompt,
24
+ num_inference_steps=steps,
25
+ guidance_scale=guidance_scale,
26
+ ).images[0]
27
 
28
+ return image
29
+ except Exception as e:
30
+ print(f"Error generating image: {str(e)}")
31
+ return None
32
 
33
  # Create the Gradio interface
34
+ with gr.Blocks(title="Simple Image Generator") as demo:
35
+ gr.Markdown("# 🎨 Simple Image Generator")
36
+ gr.Markdown("Generate images with text prompts")
37
 
38
  with gr.Row():
39
  with gr.Column():
 
46
  label="Negative Prompt",
47
  placeholder="What you don't want in the image...",
48
  info="Specify unwanted elements",
49
+ value="ugly, blurry, low quality"
50
  )
51
 
52
  with gr.Row():
53
+ steps = gr.Slider(
54
+ minimum=10,
55
+ maximum=50,
56
+ value=30,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  step=1,
58
+ label="Steps"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  )
60
 
61
  guidance_scale = gr.Slider(
 
72
  output_image = gr.Image(label="Generated Image", type="pil")
73
 
74
  generate_btn.click(
75
+ fn=generate_image,
76
  inputs=[
77
  prompt,
78
  negative_prompt,
 
 
 
 
 
 
79
  guidance_scale,
80
+ steps,
81
  ],
82
  outputs=output_image
83
  )
requirements.txt CHANGED
@@ -1,12 +1,8 @@
1
  torch==1.13.1
2
  torchvision==0.14.1
3
- diffusers==0.3.0
4
- transformers==4.19.2
5
  accelerate==0.12.0
6
- safetensors==0.3.1
7
  gradio==3.16.2
8
  opencv-python==4.8.0.74
9
  einops==0.6.1
10
- pytorch-lightning==1.9.0
11
- omegaconf==2.3.0
12
- huggingface-hub>=0.19
 
1
  torch==1.13.1
2
  torchvision==0.14.1
3
+ diffusers==0.2.4
4
+ transformers==4.18.0
5
  accelerate==0.12.0
 
6
  gradio==3.16.2
7
  opencv-python==4.8.0.74
8
  einops==0.6.1