0xadamm commited on
Commit
ba9ac78
1 Parent(s): 8dc2725

initial commit

Browse files
Files changed (2) hide show
  1. .vscode/settings.json +0 -6
  2. handler.py +0 -128
.vscode/settings.json DELETED
@@ -1,6 +0,0 @@
1
- {
2
- "[python]": {
3
- "editor.defaultFormatter": "ms-python.black-formatter"
4
- },
5
- "python.formatting.provider": "none"
6
- }
 
 
 
 
 
 
 
handler.py DELETED
@@ -1,128 +0,0 @@
1
- import torch
2
- from PIL import Image
3
- from diffusers import (
4
- StableDiffusionControlNetImg2ImgPipeline,
5
- ControlNetModel,
6
- DDIMScheduler,
7
- )
8
- from diffusers.utils import load_image
9
- import openai
10
- from io import BytesIO
11
- import base64
12
- import qrcode
13
- import random
14
-
15
- qrcode_data = "https://www.vertxdesigns.com/"
16
- prompt = "masterpiece, best quality, mecha, no humans, black armor, blue eyes, science fiction, fire, laser canon beam, war, conflict, destroyed city background"
17
- negative_prompt = "UnrealisticDream, FastNegativeEmbedding"
18
-
19
-
20
- qr = qrcode.QRCode(
21
- version=1,
22
- error_correction=qrcode.constants.ERROR_CORRECT_H,
23
- box_size=10,
24
- border=4,
25
- )
26
- qr.add_data(qrcode_data)
27
- qr.make(fit=True)
28
- img = qr.make_image(fill_color="black", back_color="white")
29
-
30
- # Resize image
31
- basewidth = 768
32
- wpercent = basewidth / float(img.size[0])
33
- hsize = int((float(img.size[1]) * float(wpercent)))
34
- qrcode_image = img.resize((basewidth, hsize), Image.LANCZOS)
35
-
36
- # Display the image
37
- qrcode_image
38
- # img.save('qrcode.png')
39
-
40
-
41
- # Initialize the control net model and pipeline.
42
- controlnet = ControlNetModel.from_pretrained(
43
- "DionTimmer/controlnet_qrcode-control_v11p_sd21", torch_dtype=torch.float16
44
- )
45
-
46
- pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
47
- "stabilityai/stable-diffusion-2-1",
48
- controlnet=controlnet,
49
- safety_checker=None,
50
- torch_dtype=torch.float16,
51
- )
52
-
53
- # Enable memory efficient attention.
54
- pipe.enable_xformers_memory_efficient_attention()
55
-
56
- # Set the scheduler for the pipeline.
57
- pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
58
-
59
- # Enable CPU offload for the model.
60
- pipe.enable_model_cpu_offload()
61
-
62
-
63
- # Resizes input_image to a specified resolution while maintaining the aspect ratio.
64
- def resize_for_condition_image(input_image: Image, resolution: int):
65
- input_image = input_image.convert("RGB")
66
- W, H = input_image.size
67
- k = float(resolution) / min(H, W)
68
- H *= k
69
- W *= k
70
- H = int(round(H / 64.0)) * 64
71
- W = int(round(W / 64.0)) * 64
72
- img = input_image.resize((W, H), resample=Image.LANCZOS)
73
- return img
74
-
75
-
76
- def get_random_seed():
77
- return random.randint(1, 1e8) # random integer between 1 and 1,000,000.
78
-
79
-
80
- # Generate and store your seed.
81
- seed = get_random_seed()
82
-
83
- # Set the seed for the random number generator.
84
- generator = torch.manual_seed(seed)
85
-
86
- # Print the seed.
87
- print(seed)
88
-
89
-
90
- openai.api_key = "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"
91
- response = openai.Image.create(prompt=prompt, n=1, size="1024x1024")
92
- image_url = response.data[0].url
93
- print(image_url)
94
-
95
-
96
- init_image = load_image(image_url)
97
-
98
- # Set the control image to the qrcode image.
99
- control_image = qrcode_image
100
-
101
- # Resize the initial image
102
- init_image = resize_for_condition_image(init_image, 768)
103
-
104
- # Run the image generation process using the pipeline.
105
- image = pipe(
106
- prompt=prompt,
107
- negative_prompt=negative_prompt,
108
- image=init_image, # The initial image, set as a QR code image
109
- control_image=control_image, # QR code image
110
- width=768,
111
- height=768,
112
- guidance_scale=7.5, # The influence of the 'prompt' 0-50
113
- controlnet_conditioning_scale=1.6, # The influence of the qr code 1-5
114
- generator=generator, # Random seed for the generation process
115
- strength=0.99, # Noise added to the QR code 0-1
116
- num_inference_steps=150, # The number of steps in the image generation process
117
- )
118
-
119
-
120
- image.images[0]
121
-
122
-
123
- pil_image = image.images[0]
124
- buffered = BytesIO()
125
- pil_image.save(buffered, format="PNG")
126
- image_base64 = base64.b64encode(buffered.getvalue()).decode()
127
- print(f"First 10 characters: {image_base64[:10]}")
128
- print(f"Length of string: {len(image_base64):,}")