appdev007 commited on
Commit
fa8c0a0
·
verified ·
1 Parent(s): b8ad25c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +187 -73
app.py CHANGED
@@ -1,92 +1,218 @@
 
1
  import gradio as gr
2
  import numpy as np
 
 
3
  import random
4
-
5
- # import spaces #[uncomment to use ZeroGPU]
6
- from diffusers import DiffusionPipeline
7
  import torch
 
8
 
9
- device = "cuda" if torch.cuda.is_available() else "cpu"
10
- model_repo_id = "stabilityai/sdxl-turbo" # Replace to the model you would like to use
11
 
12
- if torch.cuda.is_available():
13
- torch_dtype = torch.float16
14
- else:
15
- torch_dtype = torch.float32
 
 
 
16
 
17
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
18
- pipe = pipe.to(device)
19
 
20
- MAX_SEED = np.iinfo(np.int32).max
21
- MAX_IMAGE_SIZE = 1024
22
-
23
-
24
- # @spaces.GPU #[uncomment to use ZeroGPU]
25
- def infer(
26
- prompt,
27
- negative_prompt,
28
- seed,
29
- randomize_seed,
30
- width,
31
- height,
32
- guidance_scale,
33
- num_inference_steps,
34
- progress=gr.Progress(track_tqdm=True),
35
- ):
36
- if randomize_seed:
37
- seed = random.randint(0, MAX_SEED)
38
 
39
- generator = torch.Generator().manual_seed(seed)
 
 
 
 
 
 
 
40
 
41
- image = pipe(
42
- prompt=prompt,
43
- negative_prompt=negative_prompt,
44
- guidance_scale=guidance_scale,
45
- num_inference_steps=num_inference_steps,
46
- width=width,
47
- height=height,
48
- generator=generator,
49
- ).images[0]
50
 
51
- return image, seed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
- examples = [
55
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
56
- "An astronaut riding a green horse",
57
- "A delicious ceviche cheesecake slice",
58
- ]
59
 
60
  css = """
 
61
  #col-container {
62
  margin: 0 auto;
63
- max-width: 640px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  }
65
  """
66
 
67
  with gr.Blocks(css=css) as demo:
 
68
  with gr.Column(elem_id="col-container"):
69
- gr.Markdown(" # Text-to-Image Gradio Template")
 
 
 
 
 
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  with gr.Row():
72
  prompt = gr.Text(
73
  label="Prompt",
74
  show_label=False,
75
  max_lines=1,
76
- placeholder="Enter your prompt",
77
  container=False,
 
78
  )
79
 
80
- run_button = gr.Button("Run", scale=0, variant="primary")
81
 
82
  result = gr.Image(label="Result", show_label=False)
83
-
84
  with gr.Accordion("Advanced Settings", open=False):
 
85
  negative_prompt = gr.Text(
86
  label="Negative prompt",
87
  max_lines=1,
88
  placeholder="Enter a negative prompt",
89
- visible=False,
90
  )
91
 
92
  seed = gr.Slider(
@@ -105,7 +231,7 @@ with gr.Blocks(css=css) as demo:
105
  minimum=256,
106
  maximum=MAX_IMAGE_SIZE,
107
  step=32,
108
- value=1024, # Replace with defaults that work for your model
109
  )
110
 
111
  height = gr.Slider(
@@ -113,42 +239,30 @@ with gr.Blocks(css=css) as demo:
113
  minimum=256,
114
  maximum=MAX_IMAGE_SIZE,
115
  step=32,
116
- value=1024, # Replace with defaults that work for your model
117
  )
118
 
119
  with gr.Row():
120
  guidance_scale = gr.Slider(
121
  label="Guidance scale",
122
  minimum=0.0,
123
- maximum=10.0,
124
  step=0.1,
125
- value=0.0, # Replace with defaults that work for your model
126
  )
127
 
128
  num_inference_steps = gr.Slider(
129
  label="Number of inference steps",
130
  minimum=1,
131
- maximum=50,
132
  step=1,
133
- value=2, # Replace with defaults that work for your model
134
  )
135
 
136
- gr.Examples(examples=examples, inputs=[prompt])
137
- gr.on(
138
- triggers=[run_button.click, prompt.submit],
139
  fn=infer,
140
- inputs=[
141
- prompt,
142
- negative_prompt,
143
- seed,
144
- randomize_seed,
145
- width,
146
- height,
147
- guidance_scale,
148
- num_inference_steps,
149
- ],
150
- outputs=[result, seed],
151
  )
152
 
153
- if __name__ == "__main__":
154
- demo.launch()
 
1
+ import spaces
2
  import gradio as gr
3
  import numpy as np
4
+ import PIL.Image
5
+ from PIL import Image
6
  import random
7
+ from diffusers import StableDiffusionXLPipeline
8
+ from diffusers import EulerAncestralDiscreteScheduler
 
9
  import torch
10
+ from compel import Compel, ReturnedEmbeddingsType
11
 
12
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
13
 
14
+ # Make sure to use torch.float16 consistently throughout the pipeline
15
+ pipe = StableDiffusionXLPipeline.from_pretrained(
16
+ "votepurchase/pornmasterPro_noobV3VAE",
17
+ torch_dtype=torch.float16,
18
+ variant="fp16", # Explicitly use fp16 variant
19
+ use_safetensors=True # Use safetensors if available
20
+ )
21
 
22
+ pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
23
+ pipe.to(device)
24
 
25
+ # Force all components to use the same dtype
26
+ pipe.text_encoder.to(torch.float16)
27
+ pipe.text_encoder_2.to(torch.float16)
28
+ pipe.vae.to(torch.float16)
29
+ pipe.unet.to(torch.float16)
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
+ # 追加: Initialize Compel for long prompt processing
32
+ compel = Compel(
33
+ tokenizer=[pipe.tokenizer, pipe.tokenizer_2],
34
+ text_encoder=[pipe.text_encoder, pipe.text_encoder_2],
35
+ returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED,
36
+ requires_pooled=[False, True],
37
+ truncate_long_prompts=False
38
+ )
39
 
40
+ MAX_SEED = np.iinfo(np.int32).max
41
+ MAX_IMAGE_SIZE = 1216
42
+
43
+ # Default prompt
44
+ DEFAULT_PROMPT = "Detailed illustration, realistic style, portrait of a beautiful Japanese woman, wearing an elegant traditional Japanese uniform, neatly tailored with intricate patterns and subtle textures, serene expression, soft natural lighting, standing gracefully in a traditional Japanese garden with cherry blossom petals gently falling in the background, cinematic quality, ultra-detailed, high-resolution, warm tones"
 
 
 
 
45
 
46
+ # 追加: Simple long prompt processing function
47
+ def process_long_prompt(prompt, negative_prompt=""):
48
+ """Simple long prompt processing using Compel"""
49
+ try:
50
+ conditioning, pooled = compel([prompt, negative_prompt])
51
+ return conditioning, pooled
52
+ except Exception as e:
53
+ print(f"Long prompt processing failed: {e}, falling back to standard processing")
54
+ return None, None
55
+
56
+ @spaces.GPU
57
+ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
58
+ # 変更: Remove the 60-word limit warning and add long prompt check
59
+ use_long_prompt = len(prompt.split()) > 60 or len(prompt) > 300
60
+
61
+ if randomize_seed:
62
+ seed = random.randint(0, MAX_SEED)
63
 
64
+ generator = torch.Generator(device=device).manual_seed(seed)
65
+
66
+ try:
67
+ # 追加: Try long prompt processing first if prompt is long
68
+ if use_long_prompt:
69
+ print("Using long prompt processing...")
70
+ conditioning, pooled = process_long_prompt(prompt, negative_prompt)
71
+
72
+ if conditioning is not None:
73
+ output_image = pipe(
74
+ prompt_embeds=conditioning[0:1],
75
+ pooled_prompt_embeds=pooled[0:1],
76
+ negative_prompt_embeds=conditioning[1:2],
77
+ negative_pooled_prompt_embeds=pooled[1:2],
78
+ guidance_scale=guidance_scale,
79
+ num_inference_steps=num_inference_steps,
80
+ width=width,
81
+ height=height,
82
+ generator=generator
83
+ ).images[0]
84
+ return output_image
85
+
86
+ # Fall back to standard processing
87
+ output_image = pipe(
88
+ prompt=prompt,
89
+ negative_prompt=negative_prompt,
90
+ guidance_scale=guidance_scale,
91
+ num_inference_steps=num_inference_steps,
92
+ width=width,
93
+ height=height,
94
+ generator=generator
95
+ ).images[0]
96
+
97
+ return output_image
98
+ except RuntimeError as e:
99
+ print(f"Error during generation: {e}")
100
+ # Return a blank image with error message
101
+ error_img = Image.new('RGB', (width, height), color=(0, 0, 0))
102
+ return error_img
103
 
 
 
 
 
 
104
 
105
  css = """
106
+ /* Main container styling */
107
  #col-container {
108
  margin: 0 auto;
109
+ max-width: 520px;
110
+ }
111
+
112
+ /* Gradient background for the entire app */
113
+ .gradio-container {
114
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 25%, #f093fb 50%, #f5576c 75%, #ffc947 100%);
115
+ min-height: 100vh;
116
+ }
117
+
118
+ /* Main block styling with semi-transparent background */
119
+ .contain {
120
+ background: rgba(255, 255, 255, 0.95);
121
+ border-radius: 20px;
122
+ padding: 20px;
123
+ box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
124
+ backdrop-filter: blur(4px);
125
+ border: 1px solid rgba(255, 255, 255, 0.18);
126
+ }
127
+
128
+ /* Input field styling */
129
+ .gr-text-input {
130
+ background: rgba(255, 255, 255, 0.9) !important;
131
+ border: 2px solid rgba(102, 126, 234, 0.3) !important;
132
+ border-radius: 10px !important;
133
+ }
134
+
135
+ /* Button styling */
136
+ .gr-button {
137
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
138
+ border: none !important;
139
+ color: white !important;
140
+ font-weight: bold !important;
141
+ transition: all 0.3s ease !important;
142
+ }
143
+
144
+ .gr-button:hover {
145
+ transform: translateY(-2px);
146
+ box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
147
+ }
148
+
149
+ /* Accordion styling */
150
+ .gr-accordion {
151
+ background: rgba(255, 255, 255, 0.8) !important;
152
+ border-radius: 10px !important;
153
+ margin-top: 10px !important;
154
+ }
155
+
156
+ /* Result image container */
157
+ .gr-image {
158
+ border-radius: 15px !important;
159
+ box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1) !important;
160
+ }
161
+
162
+ /* Slider styling */
163
+ .gr-slider {
164
+ background: rgba(255, 255, 255, 0.8) !important;
165
  }
166
  """
167
 
168
  with gr.Blocks(css=css) as demo:
169
+
170
  with gr.Column(elem_id="col-container"):
171
+ gr.Markdown(
172
+ """
173
+ # 🎨 Stable Diffusion XL Image Generator
174
+ ### Create beautiful images with AI
175
+ """
176
+ )
177
 
178
+ # Badge section
179
+ gr.HTML(
180
+ """
181
+ <div style="display: flex; justify-content: center; align-items: center; gap: 20px; margin: 20px 0;">
182
+ <a href="https://huggingface.co/spaces/Heartsync/Wan-2.2-ADULT" target="_blank">
183
+ <img src="https://img.shields.io/static/v1?label=T2I%20%26%20TI2V&message=Wan-2.2-ADULT&color=%230000ff&labelColor=%23800080&logo=huggingface&logoColor=white&style=for-the-badge" alt="badge">
184
+ </a>
185
+ <a href="https://huggingface.co/spaces/Heartsync/PornHUB" target="_blank">
186
+ <img src="https://img.shields.io/static/v1?label=T2I%20&message=PornHUB&color=%230000ff&labelColor=%23800080&logo=huggingface&logoColor=white&style=for-the-badge" alt="badge">
187
+ </a>
188
+ <a href="https://huggingface.co/spaces/Heartsync/Hentai-Adult" target="_blank">
189
+ <img src="https://img.shields.io/static/v1?label=T2I%20&message=Hentai-Adult&color=%230000ff&labelColor=%23800080&logo=huggingface&logoColor=white&style=for-the-badge" alt="badge">
190
+ </a>
191
+ </div>
192
+ """
193
+ )
194
+
195
  with gr.Row():
196
  prompt = gr.Text(
197
  label="Prompt",
198
  show_label=False,
199
  max_lines=1,
200
+ placeholder="Enter your prompt (long prompts are automatically supported)",
201
  container=False,
202
+ value=DEFAULT_PROMPT # Set default prompt
203
  )
204
 
205
+ run_button = gr.Button("Run", scale=0)
206
 
207
  result = gr.Image(label="Result", show_label=False)
208
+
209
  with gr.Accordion("Advanced Settings", open=False):
210
+
211
  negative_prompt = gr.Text(
212
  label="Negative prompt",
213
  max_lines=1,
214
  placeholder="Enter a negative prompt",
215
+ value="nsfw, (low quality, worst quality:1.2), very displeasing, 3d, watermark, signature, ugly, poorly drawn"
216
  )
217
 
218
  seed = gr.Slider(
 
231
  minimum=256,
232
  maximum=MAX_IMAGE_SIZE,
233
  step=32,
234
+ value=1024,
235
  )
236
 
237
  height = gr.Slider(
 
239
  minimum=256,
240
  maximum=MAX_IMAGE_SIZE,
241
  step=32,
242
+ value=1024,
243
  )
244
 
245
  with gr.Row():
246
  guidance_scale = gr.Slider(
247
  label="Guidance scale",
248
  minimum=0.0,
249
+ maximum=20.0,
250
  step=0.1,
251
+ value=7,
252
  )
253
 
254
  num_inference_steps = gr.Slider(
255
  label="Number of inference steps",
256
  minimum=1,
257
+ maximum=28,
258
  step=1,
259
+ value=28,
260
  )
261
 
262
+ run_button.click(
 
 
263
  fn=infer,
264
+ inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
265
+ outputs=[result]
 
 
 
 
 
 
 
 
 
266
  )
267
 
268
+ demo.queue().launch()