Aarifkhan commited on
Commit
2d69d95
·
verified ·
1 Parent(s): 6432a5a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -33
app.py CHANGED
@@ -4,37 +4,67 @@ import random
4
  import spaces
5
  import torch
6
  from diffusers import DiffusionPipeline
 
7
 
8
  dtype = torch.bfloat16
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
 
11
- pipe = DiffusionPipeline.from_pretrained("UnfilteredAI/NSFW-Flux-v1", torch_dtype=dtype).to(device)
 
 
 
 
 
 
12
 
13
  MAX_SEED = np.iinfo(np.int32).max
14
  MAX_IMAGE_SIZE = 2048
 
 
 
 
 
 
15
 
16
  @spaces.GPU()
17
- def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
 
 
 
 
 
 
 
 
 
 
 
18
  if randomize_seed:
19
  seed = random.randint(0, MAX_SEED)
 
20
  generator = torch.Generator().manual_seed(seed)
21
- image = pipe(
22
- prompt = prompt,
23
- width = width,
24
- height = height,
25
- num_inference_steps = num_inference_steps,
26
- generator = generator,
 
 
27
  guidance_scale=0.0
28
- ).images[0]
29
- return image, seed
30
-
 
 
 
31
  examples = [
32
  "a tiny astronaut hatching from an egg on the moon",
33
  "a cat holding a sign that says hello world",
34
  "an anime illustration of a wiener schnitzel",
35
  ]
36
 
37
- css="""
38
  #col-container {
39
  margin: 0 auto;
40
  max-width: 520px;
@@ -42,13 +72,18 @@ css="""
42
  """
43
 
44
  with gr.Blocks(css=css) as demo:
45
-
46
  with gr.Column(elem_id="col-container"):
47
- gr.Markdown(f"""NSFW-Flux-v1 is a 12 billion parameter rectified flow transformer capable of generating images from text descriptions. Finetuned by UnfilteredAI, this model is designed to produce a wide range of images, including explicit and NSFW (Not Safe For Work) images from textual inputs
 
 
 
 
 
 
 
48
  """)
49
 
50
  with gr.Row():
51
-
52
  prompt = gr.Text(
53
  label="Prompt",
54
  show_label=False,
@@ -56,13 +91,11 @@ with gr.Blocks(css=css) as demo:
56
  placeholder="Enter your prompt",
57
  container=False,
58
  )
59
-
60
  run_button = gr.Button("Run", scale=0)
61
 
62
  result = gr.Image(label="Result", show_label=False)
63
 
64
  with gr.Accordion("Advanced Settings", open=False):
65
-
66
  seed = gr.Slider(
67
  label="Seed",
68
  minimum=0,
@@ -70,11 +103,9 @@ with gr.Blocks(css=css) as demo:
70
  step=1,
71
  value=0,
72
  )
73
-
74
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
75
 
76
  with gr.Row():
77
-
78
  width = gr.Slider(
79
  label="Width",
80
  minimum=256,
@@ -82,7 +113,6 @@ with gr.Blocks(css=css) as demo:
82
  step=32,
83
  value=1024,
84
  )
85
-
86
  height = gr.Slider(
87
  label="Height",
88
  minimum=256,
@@ -92,8 +122,6 @@ with gr.Blocks(css=css) as demo:
92
  )
93
 
94
  with gr.Row():
95
-
96
-
97
  num_inference_steps = gr.Slider(
98
  label="Number of inference steps",
99
  minimum=1,
@@ -103,18 +131,25 @@ with gr.Blocks(css=css) as demo:
103
  )
104
 
105
  gr.Examples(
106
- examples = examples,
107
- fn = infer,
108
- inputs = [prompt],
109
- outputs = [result, seed],
110
  cache_examples="lazy"
111
  )
112
-
113
- gr.on(
114
- triggers=[run_button.click, prompt.submit],
115
- fn = infer,
116
- inputs = [prompt, seed, randomize_seed, width, height, num_inference_steps],
117
- outputs = [result, seed]
118
- )
 
 
 
 
 
 
 
119
 
120
  demo.launch()
 
4
  import spaces
5
  import torch
6
  from diffusers import DiffusionPipeline
7
+ from transformers import CLIPTokenizer
8
 
9
  dtype = torch.bfloat16
10
  device = "cuda" if torch.cuda.is_available() else "cpu"
11
 
12
+ # Initialize CLIP tokenizer for prompt length checking
13
+ tokenizer = CLIPTokenizer.from_pretrained("openai/clip-vit-base-patch32")
14
+
15
+ pipe = DiffusionPipeline.from_pretrained(
16
+ "UnfilteredAI/NSFW-Flux-v1",
17
+ torch_dtype=dtype
18
+ ).to(device)
19
 
20
  MAX_SEED = np.iinfo(np.int32).max
21
  MAX_IMAGE_SIZE = 2048
22
+ MAX_TOKENS = 77 # CLIP's maximum token length
23
+
24
+ def truncate_prompt(prompt):
25
+ """Truncate the prompt to fit within CLIP's token limit"""
26
+ tokens = tokenizer.encode(prompt, truncation=True, max_length=MAX_TOKENS)
27
+ return tokenizer.decode(tokens)
28
 
29
  @spaces.GPU()
30
+ def infer(
31
+ prompt,
32
+ seed=42,
33
+ randomize_seed=False,
34
+ width=1024,
35
+ height=1024,
36
+ num_inference_steps=4,
37
+ progress=gr.Progress(track_tqdm=True)
38
+ ):
39
+ # Truncate prompt if necessary
40
+ truncated_prompt = truncate_prompt(prompt)
41
+
42
  if randomize_seed:
43
  seed = random.randint(0, MAX_SEED)
44
+
45
  generator = torch.Generator().manual_seed(seed)
46
+
47
+ try:
48
+ image = pipe(
49
+ prompt=truncated_prompt,
50
+ width=width,
51
+ height=height,
52
+ num_inference_steps=num_inference_steps,
53
+ generator=generator,
54
  guidance_scale=0.0
55
+ ).images[0]
56
+
57
+ return image, seed
58
+ except Exception as e:
59
+ raise gr.Error(f"Error generating image: {str(e)}")
60
+
61
  examples = [
62
  "a tiny astronaut hatching from an egg on the moon",
63
  "a cat holding a sign that says hello world",
64
  "an anime illustration of a wiener schnitzel",
65
  ]
66
 
67
+ css = """
68
  #col-container {
69
  margin: 0 auto;
70
  max-width: 520px;
 
72
  """
73
 
74
  with gr.Blocks(css=css) as demo:
 
75
  with gr.Column(elem_id="col-container"):
76
+ gr.Markdown("""
77
+ NSFW-Flux-v1 is a 12 billion parameter rectified flow transformer
78
+ capable of generating images from text descriptions.
79
+ Finetuned by UnfilteredAI, this model is designed to produce
80
+ a wide range of images, including explicit and NSFW
81
+ (Not Safe For Work) images from textual inputs.
82
+
83
+ Note: Long prompts will be automatically truncated to fit the model's requirements.
84
  """)
85
 
86
  with gr.Row():
 
87
  prompt = gr.Text(
88
  label="Prompt",
89
  show_label=False,
 
91
  placeholder="Enter your prompt",
92
  container=False,
93
  )
 
94
  run_button = gr.Button("Run", scale=0)
95
 
96
  result = gr.Image(label="Result", show_label=False)
97
 
98
  with gr.Accordion("Advanced Settings", open=False):
 
99
  seed = gr.Slider(
100
  label="Seed",
101
  minimum=0,
 
103
  step=1,
104
  value=0,
105
  )
 
106
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
107
 
108
  with gr.Row():
 
109
  width = gr.Slider(
110
  label="Width",
111
  minimum=256,
 
113
  step=32,
114
  value=1024,
115
  )
 
116
  height = gr.Slider(
117
  label="Height",
118
  minimum=256,
 
122
  )
123
 
124
  with gr.Row():
 
 
125
  num_inference_steps = gr.Slider(
126
  label="Number of inference steps",
127
  minimum=1,
 
131
  )
132
 
133
  gr.Examples(
134
+ examples=examples,
135
+ fn=infer,
136
+ inputs=[prompt],
137
+ outputs=[result, seed],
138
  cache_examples="lazy"
139
  )
140
+
141
+ gr.on(
142
+ triggers=[run_button.click, prompt.submit],
143
+ fn=infer,
144
+ inputs=[
145
+ prompt,
146
+ seed,
147
+ randomize_seed,
148
+ width,
149
+ height,
150
+ num_inference_steps
151
+ ],
152
+ outputs=[result, seed]
153
+ )
154
 
155
  demo.launch()