snackshell commited on
Commit
4103aa4
·
verified ·
1 Parent(s): 06b93b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -12
app.py CHANGED
@@ -4,29 +4,35 @@ import random
4
  from PIL import Image, ImageDraw, ImageFont
5
  import torch
6
  from diffusers import DiffusionPipeline
 
7
 
8
  # ===== CONFIG =====
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
  torch_dtype = torch.float16 if device == "cuda" else torch.float32
11
  model_repo_id = "stabilityai/sdxl-turbo"
12
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype, variant="fp16" if device == "cuda" else None)
 
 
 
 
13
  pipe.to(device)
14
 
15
  MAX_SEED = np.iinfo(np.int32).max
16
- IMAGE_SIZE = 1024
 
17
  WATERMARK_TEXT = "SelamGPT"
18
 
19
  # ===== WATERMARK FUNCTION =====
20
  def add_watermark(image):
21
  draw = ImageDraw.Draw(image)
22
- font_size = 24
23
  try:
24
  font = ImageFont.truetype("Roboto-Bold.ttf", font_size)
25
  except:
26
  font = ImageFont.load_default()
27
  text_width = draw.textlength(WATERMARK_TEXT, font=font)
28
- x = image.width - text_width - 10
29
- y = image.height - 34
30
  draw.text((x+1, y+1), WATERMARK_TEXT, font=font, fill=(0, 0, 0, 128))
31
  draw.text((x, y), WATERMARK_TEXT, font=font, fill=(255, 255, 255))
32
  return image
@@ -51,15 +57,18 @@ def generate(
51
  result = pipe(
52
  prompt=prompt,
53
  negative_prompt=negative_prompt,
54
- width=IMAGE_SIZE,
55
- height=IMAGE_SIZE,
56
  guidance_scale=guidance_scale,
57
  num_inference_steps=num_inference_steps,
58
  generator=generator,
59
  ).images[0]
60
 
61
- image = add_watermark(result)
62
- return image, seed
 
 
 
63
 
64
  # ===== EXAMPLES =====
65
  examples = [
@@ -85,7 +94,7 @@ with gr.Blocks(css=css, title="SelamGPT Turbo Generator") as demo:
85
  )
86
  generate_btn = gr.Button("Generate", variant="primary")
87
 
88
- output_image = gr.Image(label="Generated Image", type="pil", format="png")
89
  seed_display = gr.Textbox(label="Seed Used", interactive=False)
90
 
91
  with gr.Accordion("⚙️ Advanced Settings", open=False):
@@ -94,7 +103,7 @@ with gr.Blocks(css=css, title="SelamGPT Turbo Generator") as demo:
94
  seed = gr.Slider(0, MAX_SEED, step=1, label="Seed", value=0)
95
 
96
  guidance_scale = gr.Slider(0.0, 10.0, step=0.1, label="Guidance Scale", value=0.0)
97
- num_inference_steps = gr.Slider(1, 50, step=1, label="Inference Steps", value=2)
98
 
99
  gr.Examples(examples=examples, inputs=[prompt])
100
 
@@ -112,4 +121,4 @@ with gr.Blocks(css=css, title="SelamGPT Turbo Generator") as demo:
112
  )
113
 
114
  if __name__ == "__main__":
115
- demo.launch()
 
4
  from PIL import Image, ImageDraw, ImageFont
5
  import torch
6
  from diffusers import DiffusionPipeline
7
+ import io
8
 
9
  # ===== CONFIG =====
10
  device = "cuda" if torch.cuda.is_available() else "cpu"
11
  torch_dtype = torch.float16 if device == "cuda" else torch.float32
12
  model_repo_id = "stabilityai/sdxl-turbo"
13
+ pipe = DiffusionPipeline.from_pretrained(
14
+ model_repo_id,
15
+ torch_dtype=torch_dtype,
16
+ variant="fp16" if device == "cuda" else None
17
+ )
18
  pipe.to(device)
19
 
20
  MAX_SEED = np.iinfo(np.int32).max
21
+ IMAGE_WIDTH = 768
22
+ IMAGE_HEIGHT = 768
23
  WATERMARK_TEXT = "SelamGPT"
24
 
25
  # ===== WATERMARK FUNCTION =====
26
  def add_watermark(image):
27
  draw = ImageDraw.Draw(image)
28
+ font_size = int(image.width * 0.03)
29
  try:
30
  font = ImageFont.truetype("Roboto-Bold.ttf", font_size)
31
  except:
32
  font = ImageFont.load_default()
33
  text_width = draw.textlength(WATERMARK_TEXT, font=font)
34
+ x = image.width - text_width - 12
35
+ y = image.height - font_size - 10
36
  draw.text((x+1, y+1), WATERMARK_TEXT, font=font, fill=(0, 0, 0, 128))
37
  draw.text((x, y), WATERMARK_TEXT, font=font, fill=(255, 255, 255))
38
  return image
 
57
  result = pipe(
58
  prompt=prompt,
59
  negative_prompt=negative_prompt,
60
+ width=IMAGE_WIDTH,
61
+ height=IMAGE_HEIGHT,
62
  guidance_scale=guidance_scale,
63
  num_inference_steps=num_inference_steps,
64
  generator=generator,
65
  ).images[0]
66
 
67
+ watermarked = add_watermark(result)
68
+ buffer = io.BytesIO()
69
+ watermarked.convert("RGB").save(buffer, format="JPEG", quality=70)
70
+ buffer.seek(0)
71
+ return Image.open(buffer), seed
72
 
73
  # ===== EXAMPLES =====
74
  examples = [
 
94
  )
95
  generate_btn = gr.Button("Generate", variant="primary")
96
 
97
+ output_image = gr.Image(label="Generated Image", type="pil", format="jpeg")
98
  seed_display = gr.Textbox(label="Seed Used", interactive=False)
99
 
100
  with gr.Accordion("⚙️ Advanced Settings", open=False):
 
103
  seed = gr.Slider(0, MAX_SEED, step=1, label="Seed", value=0)
104
 
105
  guidance_scale = gr.Slider(0.0, 10.0, step=0.1, label="Guidance Scale", value=0.0)
106
+ num_inference_steps = gr.Slider(1, 10, step=1, label="Inference Steps", value=2)
107
 
108
  gr.Examples(examples=examples, inputs=[prompt])
109
 
 
121
  )
122
 
123
  if __name__ == "__main__":
124
+ demo.launch()