|
| 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