aiqtech commited on
Commit
ee973ae
·
verified ·
1 Parent(s): 70f555c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -23
app.py CHANGED
@@ -5,7 +5,7 @@ import os
5
  from compel import Compel, ReturnedEmbeddingsType
6
  from diffusers import DiffusionPipeline
7
 
8
- # 모델 로드
9
  model_name = os.environ.get('MODEL_NAME', 'UnfilteredAI/NSFW-gen-v2')
10
  pipe = DiffusionPipeline.from_pretrained(
11
  model_name,
@@ -13,7 +13,7 @@ pipe = DiffusionPipeline.from_pretrained(
13
  )
14
  pipe.to('cuda')
15
 
16
- # Compel 설정
17
  compel = Compel(
18
  tokenizer=[pipe.tokenizer, pipe.tokenizer_2],
19
  text_encoder=[pipe.text_encoder, pipe.text_encoder_2],
@@ -21,10 +21,10 @@ compel = Compel(
21
  requires_pooled=[False, True]
22
  )
23
 
24
- # 기본 네거티브 프롬프트
25
  default_negative_prompt = "(low quality, worst quality:1.2), very displeasing, 3d, watermark, signature, ugly, poorly drawn, (deformed | distorted | disfigured:1.3), bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, mutated hands and fingers:1.4, disconnected limbs, blurry, amputation."
26
 
27
- # 예제 프롬프트 목록
28
  example_prompts = [
29
  ["a beautiful woman in a summer dress at the beach, golden sunset, professional photography, 8k", default_negative_prompt, 40, 7.5, 1024, 1024, 4],
30
  ["portrait of a cyberpunk character, highly detailed, neon lights, futuristic cityscape in background, 8k, ultra realistic", default_negative_prompt, 50, 8.0, 768, 1024, 4],
@@ -32,7 +32,7 @@ example_prompts = [
32
  ["photorealistic portrait of a stunning model, studio lighting, fashion photography", default_negative_prompt, 45, 7.0, 1024, 1024, 4],
33
  ]
34
 
35
- # 이미지 생성 함수
36
  @spaces.GPU(duration=120)
37
  def generate(prompt, negative_prompt, num_inference_steps, guidance_scale, width, height, num_samples, progress=gr.Progress()):
38
  progress(0, desc="Preparing")
@@ -50,12 +50,13 @@ def generate(prompt, negative_prompt, num_inference_steps, guidance_scale, width
50
  width=width,
51
  height=height,
52
  num_images_per_prompt=num_samples,
53
- callback=lambda i, t, latents: progress((i + 1) / num_inference_steps)
 
54
  ).images
55
 
56
  return images
57
 
58
- # CSS 스타일
59
  css = """
60
  .gallery-item {
61
  transition: transform 0.2s;
@@ -101,21 +102,21 @@ css = """
101
  }
102
  """
103
 
104
- # Gradio 인터페이스
105
  with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
106
  gr.HTML("""
107
  <div class="header">
108
- <h1>🎨 UnfilteredAI NSFW-gen-v2 이미지 생성기</h1>
109
- <p>창의적인 프롬프트를 입력하고 고품질 이미지를 생성해보세요.</p>
110
  </div>
111
  """)
112
 
113
  with gr.Row():
114
  with gr.Column(scale=2):
115
  with gr.Group(elem_classes="prompt-container"):
116
- prompt = gr.Textbox(label="프롬프트", placeholder="원하는 이미지를 설명해주세요...", lines=3)
117
  negative_prompt = gr.Textbox(
118
- label="네거티브 프롬프트",
119
  value=default_negative_prompt,
120
  lines=3
121
  )
@@ -123,22 +124,22 @@ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
123
  with gr.Group(elem_classes="slider-container"):
124
  with gr.Row():
125
  with gr.Column():
126
- steps = gr.Slider(minimum=20, maximum=100, value=60, step=1, label="추론 단계 (Quality)", info="높을수록 품질 향상 (시간 증가)")
127
- guidance = gr.Slider(minimum=1, maximum=15, value=7, step=0.1, label="가이던스 스케일 (Creativity)", info="낮을수록 창의적인 결과")
128
 
129
  with gr.Column():
130
  with gr.Row():
131
- width = gr.Slider(minimum=512, maximum=1536, value=1024, step=128, label="너비")
132
- height = gr.Slider(minimum=512, maximum=1536, value=1024, step=128, label="높이")
133
 
134
- num_samples = gr.Slider(minimum=1, maximum=8, value=4, step=1, label="생성할 이미지 ", info=" ��에 생성할 이미지 ")
135
 
136
- generate_btn = gr.Button("🚀 이미지 생성", variant="primary", size="lg")
137
 
138
  with gr.Column(scale=3):
139
- output_gallery = gr.Gallery(label="생성된 이미지", elem_classes="gallery-item", columns=2, object_fit="contain", height=650)
140
 
141
- gr.HTML("""<div class="examples-header"><h3>✨ 예제 프롬프트</h3></div>""")
142
  gr.Examples(
143
  examples=example_prompts,
144
  inputs=[prompt, negative_prompt, steps, guidance, width, height, num_samples],
@@ -147,7 +148,7 @@ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
147
  cache_examples=True,
148
  )
149
 
150
- # 이벤트 연결
151
  generate_btn.click(
152
  fn=generate,
153
  inputs=[prompt, negative_prompt, steps, guidance, width, height, num_samples],
@@ -156,8 +157,8 @@ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
156
 
157
  gr.HTML("""
158
  <div style="text-align: center; margin-top: 20px; padding: 10px; background-color: #f0f0f0; border-radius: 10px;">
159
- <p>💡 팁: 고품질 이미지를 위해서는 세부적인 프롬프트와 높은 추론 단계를 사용하세요.</p>
160
- <p>예) "professional photography, 8k, highly detailed, sharp focus, HDR" 등의 품질 관련 용어를 추가해보세요.</p>
161
  </div>
162
  """)
163
 
 
5
  from compel import Compel, ReturnedEmbeddingsType
6
  from diffusers import DiffusionPipeline
7
 
8
+ # Load model
9
  model_name = os.environ.get('MODEL_NAME', 'UnfilteredAI/NSFW-gen-v2')
10
  pipe = DiffusionPipeline.from_pretrained(
11
  model_name,
 
13
  )
14
  pipe.to('cuda')
15
 
16
+ # Compel setup
17
  compel = Compel(
18
  tokenizer=[pipe.tokenizer, pipe.tokenizer_2],
19
  text_encoder=[pipe.text_encoder, pipe.text_encoder_2],
 
21
  requires_pooled=[False, True]
22
  )
23
 
24
+ # Default negative prompt
25
  default_negative_prompt = "(low quality, worst quality:1.2), very displeasing, 3d, watermark, signature, ugly, poorly drawn, (deformed | distorted | disfigured:1.3), bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, mutated hands and fingers:1.4, disconnected limbs, blurry, amputation."
26
 
27
+ # Example prompts
28
  example_prompts = [
29
  ["a beautiful woman in a summer dress at the beach, golden sunset, professional photography, 8k", default_negative_prompt, 40, 7.5, 1024, 1024, 4],
30
  ["portrait of a cyberpunk character, highly detailed, neon lights, futuristic cityscape in background, 8k, ultra realistic", default_negative_prompt, 50, 8.0, 768, 1024, 4],
 
32
  ["photorealistic portrait of a stunning model, studio lighting, fashion photography", default_negative_prompt, 45, 7.0, 1024, 1024, 4],
33
  ]
34
 
35
+ # Image generation function
36
  @spaces.GPU(duration=120)
37
  def generate(prompt, negative_prompt, num_inference_steps, guidance_scale, width, height, num_samples, progress=gr.Progress()):
38
  progress(0, desc="Preparing")
 
50
  width=width,
51
  height=height,
52
  num_images_per_prompt=num_samples,
53
+ callback_steps=1, # Fixed: Added callback_steps parameter
54
+ callback_on_step_end=lambda i, t, latents: progress((i + 1) / num_inference_steps)
55
  ).images
56
 
57
  return images
58
 
59
+ # CSS styles
60
  css = """
61
  .gallery-item {
62
  transition: transform 0.2s;
 
102
  }
103
  """
104
 
105
+ # Gradio interface
106
  with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
107
  gr.HTML("""
108
  <div class="header">
109
+ <h1>🎨 UnfilteredAI NSFW-gen-v2 Image Generator</h1>
110
+ <p>Enter creative prompts and generate high-quality images.</p>
111
  </div>
112
  """)
113
 
114
  with gr.Row():
115
  with gr.Column(scale=2):
116
  with gr.Group(elem_classes="prompt-container"):
117
+ prompt = gr.Textbox(label="Prompt", placeholder="Describe your desired image...", lines=3)
118
  negative_prompt = gr.Textbox(
119
+ label="Negative Prompt",
120
  value=default_negative_prompt,
121
  lines=3
122
  )
 
124
  with gr.Group(elem_classes="slider-container"):
125
  with gr.Row():
126
  with gr.Column():
127
+ steps = gr.Slider(minimum=20, maximum=100, value=60, step=1, label="Inference Steps (Quality)", info="Higher values improve quality (longer generation time)")
128
+ guidance = gr.Slider(minimum=1, maximum=15, value=7, step=0.1, label="Guidance Scale (Creativity)", info="Lower values create more creative results")
129
 
130
  with gr.Column():
131
  with gr.Row():
132
+ width = gr.Slider(minimum=512, maximum=1536, value=1024, step=128, label="Width")
133
+ height = gr.Slider(minimum=512, maximum=1536, value=1024, step=128, label="Height")
134
 
135
+ num_samples = gr.Slider(minimum=1, maximum=8, value=4, step=1, label="Number of Images", info="Number of images to generate at once")
136
 
137
+ generate_btn = gr.Button("🚀 Generate Images", variant="primary", size="lg")
138
 
139
  with gr.Column(scale=3):
140
+ output_gallery = gr.Gallery(label="Generated Images", elem_classes="gallery-item", columns=2, object_fit="contain", height=650)
141
 
142
+ gr.HTML("""<div class="examples-header"><h3>✨ Example Prompts</h3></div>""")
143
  gr.Examples(
144
  examples=example_prompts,
145
  inputs=[prompt, negative_prompt, steps, guidance, width, height, num_samples],
 
148
  cache_examples=True,
149
  )
150
 
151
+ # Event connections
152
  generate_btn.click(
153
  fn=generate,
154
  inputs=[prompt, negative_prompt, steps, guidance, width, height, num_samples],
 
157
 
158
  gr.HTML("""
159
  <div style="text-align: center; margin-top: 20px; padding: 10px; background-color: #f0f0f0; border-radius: 10px;">
160
+ <p>💡 Tip: For high-quality images, use detailed prompts and higher inference steps.</p>
161
+ <p>Example: Add quality terms like "professional photography, 8k, highly detailed, sharp focus, HDR" to your prompts.</p>
162
  </div>
163
  """)
164