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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -18
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,19 +13,34 @@ pipe = DiffusionPipeline.from_pretrained(
13
  )
14
  pipe.to('cuda')
15
 
 
16
  compel = Compel(
17
- tokenizer=[pipe.tokenizer, pipe.tokenizer_2] ,
18
  text_encoder=[pipe.text_encoder, pipe.text_encoder_2],
19
  returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED,
20
  requires_pooled=[False, True]
21
  )
22
 
 
 
 
 
 
 
 
 
 
 
23
 
 
24
  @spaces.GPU(duration=120)
25
- def generate(prompt, negative_prompt, num_inference_steps, guidance_scale, width, height, num_samples):
 
26
  embeds, pooled = compel(prompt)
27
  neg_embeds, neg_pooled = compel(negative_prompt)
28
- return pipe(
 
 
29
  prompt_embeds=embeds,
30
  pooled_prompt_embeds=pooled,
31
  negative_prompt_embeds=neg_embeds,
@@ -34,20 +49,116 @@ def generate(prompt, negative_prompt, num_inference_steps, guidance_scale, width
34
  guidance_scale=guidance_scale,
35
  width=width,
36
  height=height,
37
- num_images_per_prompt=num_samples
 
38
  ).images
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
- gr.Interface(
42
- fn=generate,
43
- inputs=[
44
- gr.Text(label="Prompt"),
45
- gr.Text("(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.", label="Negative Prompt"),
46
- gr.Number(60, label="Number inference steps"),
47
- gr.Number(7, label="Guidance scale"),
48
- gr.Number(1024, label="Width"),
49
- gr.Number(1024, label="Height"),
50
- gr.Number(7, label="# images"),
51
- ],
52
- outputs=gr.Gallery(),
53
- ).launch()
 
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
  )
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],
20
  returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED,
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],
31
+ ["detailed fantasy art of magical forest with fairies, ethereal lighting, mystical atmosphere", default_negative_prompt, 60, 7.0, 1024, 768, 4],
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")
39
  embeds, pooled = compel(prompt)
40
  neg_embeds, neg_pooled = compel(negative_prompt)
41
+
42
+ progress(0.1, desc="Generating images")
43
+ images = pipe(
44
  prompt_embeds=embeds,
45
  pooled_prompt_embeds=pooled,
46
  negative_prompt_embeds=neg_embeds,
 
49
  guidance_scale=guidance_scale,
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;
62
+ box-shadow: 0 4px 8px rgba(0,0,0,0.1);
63
+ border-radius: 10px;
64
+ }
65
+ .gallery-item:hover {
66
+ transform: scale(1.03);
67
+ box-shadow: 0 8px 16px rgba(0,0,0,0.2);
68
+ }
69
+ .container {
70
+ max-width: 1200px;
71
+ margin: auto;
72
+ }
73
+ .header {
74
+ text-align: center;
75
+ margin-bottom: 2rem;
76
+ padding: 1rem;
77
+ background: linear-gradient(90deg, rgba(76,0,161,0.8) 0%, rgba(28,110,164,0.8) 100%);
78
+ border-radius: 10px;
79
+ color: white;
80
+ }
81
+ .slider-container {
82
+ background-color: #f5f5f5;
83
+ padding: 1rem;
84
+ border-radius: 10px;
85
+ margin-bottom: 1rem;
86
+ }
87
+ .prompt-container {
88
+ background-color: #f0f8ff;
89
+ padding: 1rem;
90
+ border-radius: 10px;
91
+ margin-bottom: 1rem;
92
+ border: 1px solid #d0e8ff;
93
+ }
94
+ .examples-header {
95
+ background: linear-gradient(90deg, rgba(41,128,185,0.7) 0%, rgba(142,68,173,0.7) 100%);
96
+ color: white;
97
+ padding: 0.5rem;
98
+ border-radius: 8px;
99
+ text-align: center;
100
+ margin-bottom: 0.5rem;
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
+ )
122
+
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],
145
+ outputs=output_gallery,
146
+ fn=generate,
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],
154
+ outputs=output_gallery
155
+ )
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
 
164
+ demo.launch()