Skip to content

Commit 503906c

Browse files
authored
Added Clarity Upscaler example (#83)
* added clarity upscalar exmaple * fixed folder
1 parent 592007a commit 503906c

3 files changed

Lines changed: 209 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Generated by Beam SDK
2+
.beamignore
3+
pyproject.toml
4+
.git
5+
.idea
6+
.python-version
7+
.vscode
8+
.venv
9+
venv
10+
__pycache__
11+
.DS_Store
12+
.config
13+
drive/MyDrive
14+
.coverage
15+
.pytest_cache
16+
.ipynb
17+
.ruff_cache
18+
.dockerignore
19+
.ipynb_checkpoints
20+
.env.local
21+
.envrc
22+
**/__pycache__/
23+
**/.pytest_cache/
24+
**/node_modules/
25+
**/.venv/
26+
*.pyc
27+
.next/
28+
.circleci
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from beam import Pod, Image
2+
3+
image = (
4+
Image(python_version="python3.11")
5+
.add_commands(
6+
[
7+
"apt update && apt install -y git libgl1-mesa-glx libglib2.0-0",
8+
]
9+
)
10+
.add_python_packages(
11+
[
12+
"torch==2.4.1",
13+
]
14+
)
15+
.add_python_packages(
16+
[
17+
"torchvision",
18+
"xformers",
19+
"tensorboard",
20+
"gfpgan",
21+
"lpips",
22+
"realesrgan",
23+
"gdown",
24+
"mediapipe",
25+
"pytorch_lightning",
26+
"git+https://github.com/huggingface/transformers",
27+
]
28+
)
29+
.add_commands(
30+
[
31+
"git clone https://github.com/philz1337x/clarity-upscaler.git /clarity-upscaler",
32+
"cd /clarity-upscaler && python download_weights.py",
33+
"pip install --upgrade setuptools",
34+
]
35+
)
36+
).build_with_gpu("A10G")
37+
38+
clarity_upscaler_server = Pod(
39+
image=image,
40+
ports=[7861],
41+
cpu=14,
42+
memory="32Gi",
43+
gpu="A10G",
44+
entrypoint=[
45+
"python",
46+
"/clarity-upscaler/webui.py",
47+
"--listen",
48+
"--vae-path",
49+
"/clarity-upscaler/models/VAE/vae-ft-mse-840000-ema-pruned.safetensors",
50+
],
51+
)
52+
53+
res = clarity_upscaler_server.create()
54+
print("Clarity Upscaler server created:", res.url)
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import requests
2+
import base64
3+
from io import BytesIO
4+
from PIL import Image
5+
6+
BEAM_URL = "YOUR_BEAM_URL" # Replace with your actual Beam Pod URL
7+
API_ENDPOINT = f"{BEAM_URL}/sdapi/v1/img2img"
8+
9+
IMAGE_URL = "https://i.pinimg.com/736x/1a/70/1d/1a701d550cc1bc1d088a7add6cc91278.jpg"
10+
OUTPUT_FILENAME = "upscaled_result.png"
11+
12+
params = {
13+
"seed": 1337,
14+
"prompt": "masterpiece, best quality, highres, <lora:more_details:0.5> <lora:SDXLrender_v2.0:1>",
15+
"dynamic": 6,
16+
"handfix": "disabled",
17+
"pattern": False,
18+
"sharpen": 0,
19+
"sd_model": "juggernaut_reborn.safetensors",
20+
"scheduler": "DPM++ 3M SDE Karras",
21+
"creativity": 0.35,
22+
"lora_links": "",
23+
"downscaling": False,
24+
"resemblance": 0.6,
25+
"scale_factor": 2,
26+
"tiling_width": 112,
27+
"output_format": "png",
28+
"tiling_height": 144,
29+
"negative_prompt": "(worst quality, low quality, normal quality:2) JuggernautNegative-neg",
30+
"num_inference_steps": 18,
31+
"downscaling_resolution": 768,
32+
}
33+
34+
try:
35+
image_response = requests.get(IMAGE_URL)
36+
image = Image.open(BytesIO(image_response.content))
37+
image = image.convert("RGB")
38+
39+
buffered = BytesIO()
40+
image.save(buffered, format="PNG")
41+
img_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
42+
43+
payload = {
44+
"override_settings": {
45+
"sd_model_checkpoint": params["sd_model"],
46+
"sd_vae": "vae-ft-mse-840000-ema-pruned.safetensors",
47+
"CLIP_stop_at_last_layers": 1,
48+
},
49+
"override_settings_restore_afterwards": False,
50+
"init_images": [img_base64],
51+
"prompt": params["prompt"],
52+
"negative_prompt": params["negative_prompt"],
53+
"steps": params["num_inference_steps"],
54+
"cfg_scale": params["dynamic"],
55+
"seed": params["seed"],
56+
"do_not_save_samples": True,
57+
"sampler_name": params["scheduler"],
58+
"denoising_strength": params["creativity"],
59+
"alwayson_scripts": {
60+
"Tiled Diffusion": {
61+
"args": [
62+
True,
63+
"MultiDiffusion",
64+
True,
65+
True,
66+
1,
67+
1,
68+
params["tiling_width"],
69+
params["tiling_height"],
70+
4,
71+
8,
72+
"4x-UltraSharp",
73+
params["scale_factor"],
74+
False,
75+
0,
76+
0.0,
77+
3,
78+
]
79+
},
80+
"Tiled VAE": {
81+
"args": [
82+
True,
83+
2048,
84+
128,
85+
True,
86+
True,
87+
True,
88+
True,
89+
]
90+
},
91+
"controlnet": {
92+
"args": [
93+
{
94+
"enabled": True,
95+
"module": "tile_resample",
96+
"model": "control_v11f1e_sd15_tile",
97+
"weight": params["resemblance"],
98+
"image": img_base64,
99+
"resize_mode": 1,
100+
"lowvram": False,
101+
"downsample": 1.0,
102+
"guidance_start": 0.0,
103+
"guidance_end": 1.0,
104+
"control_mode": 1,
105+
"pixel_perfect": True,
106+
"threshold_a": 1,
107+
"threshold_b": 1,
108+
"save_detected_map": False,
109+
"processor_res": 512,
110+
}
111+
]
112+
},
113+
},
114+
}
115+
116+
response = requests.post(API_ENDPOINT, json=payload)
117+
response.raise_for_status()
118+
result = response.json()
119+
120+
upscaled_img_data = base64.b64decode(result["images"][0])
121+
with open(OUTPUT_FILENAME, "wb") as f:
122+
f.write(upscaled_img_data)
123+
124+
print(f"Image saved as {OUTPUT_FILENAME}")
125+
126+
except Exception as e:
127+
print(f"An error occurred: {e}")

0 commit comments

Comments
 (0)