File size: 9,318 Bytes
9eea77b c6538dc 29f2cfb c6538dc 29f2cfb c6538dc 29f2cfb c6538dc 29f2cfb c6538dc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
import os
import logging
import replicate
import gradio as gr
from PIL import Image
import tempfile
import uuid
# ๋ก๊น
์ค์
logger = logging.getLogger(__name__)
# ํ๊ฒฝ๋ณ์์์ ์ค์ ๊ฐ๋ค ๊ฐ์ ธ์ค๊ธฐ
REPLICATE_API_TOKEN = os.environ.get("REPLICATE_API_TOKEN", "")
APP_PASSWORD = os.environ.get("APP_PASSWORD", "")
REPLICATE_MODEL = os.environ.get("REPLICATE_MODEL", "")
# ํ๊ฒฝ๋ณ์ ๊ฒ์ฆ
if not REPLICATE_API_TOKEN:
logger.warning("REPLICATE_API_TOKEN์ด ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํด์ฃผ์ธ์.")
if not APP_PASSWORD:
logger.warning("APP_PASSWORD๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํด์ฃผ์ธ์.")
if not REPLICATE_MODEL:
logger.warning("REPLICATE_MODEL์ด ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํด์ฃผ์ธ์.")
# ์์ ๋๋ ํ ๋ฆฌ ์์ฑ
TEMP_DIR = os.path.join(tempfile.gettempdir(), "image_processor")
os.makedirs(TEMP_DIR, exist_ok=True)
logger.info(f"์์ ๋๋ ํ ๋ฆฌ ์์ฑ: {TEMP_DIR}")
def upscale_image(
image,
scale_factor=2,
output_format="jpg",
sd_model="juggernaut_reborn.safetensors [338b85bc4f]",
resemblance=0.6,
creativity=0.35,
prompt="masterpiece, best quality, highres, <lora:more_details:0.5> <lora:SDXLrender_v2.0:1>",
negative_prompt="(worst quality, low quality, normal quality:2) JuggernautNegative-neg",
seed=1337,
dynamic=6,
sharpen=0
):
"""
Clarity Upscaler๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฏธ์ง ํ์ง์ ๊ฐ์ ํฉ๋๋ค.
Args:
image: ์
๋ ฅ ์ด๋ฏธ์ง
scale_factor: ํ๋ ๋น์จ (๊ธฐ๋ณธ๊ฐ: 2)
output_format: ์ถ๋ ฅ ํ์ (๊ธฐ๋ณธ๊ฐ: jpg)
sd_model: ์ฌ์ฉํ SD ๋ชจ๋ธ
resemblance: ์๋ณธ๊ณผ์ ์ ์ฌ๋ (0.0-1.0)
creativity: ์ฐฝ์์ฑ ์์ค (0.0-1.0)
prompt: ์
์ค์ผ์ผ๋ง ๊ฐ์ด๋ ํ๋กฌํํธ
negative_prompt: ๋ค๊ฑฐํฐ๋ธ ํ๋กฌํํธ
seed: ๋๋ค ์๋
dynamic: ๋ค์ด๋๋ฏน ์๊ณ๊ฐ (1-10)
sharpen: ์ ๋ช
๋ (0-2)
Returns:
๊ฐ์ ๋ ์ด๋ฏธ์ง
"""
if not REPLICATE_API_TOKEN:
raise ValueError("REPLICATE_API_TOKEN์ด ์ค์ ๋์ง ์์์ต๋๋ค.")
if not REPLICATE_MODEL:
raise ValueError("REPLICATE_MODEL์ด ์ค์ ๋์ง ์์์ต๋๋ค.")
try:
# ์ด๋ฏธ์ง ์ ์ฅ
temp_input_path = os.path.join(TEMP_DIR, f"input_{uuid.uuid4()}.png")
image.save(temp_input_path)
logger.info(f"์
๋ ฅ ์ด๋ฏธ์ง ์ ์ฅ: {temp_input_path}")
# Replicate ์ค์
replicate.client.Client(api_token=REPLICATE_API_TOKEN)
# ๋ก๊ทธ ์ ๋ณด ์ถ๋ ฅ
logger.info(f"SD ๋ชจ๋ธ: {sd_model}")
logger.info(f"์ค์ผ์ผ ํฉํฐ: {scale_factor}")
logger.info(f"์ถ๋ ฅ ํ์: {output_format}")
logger.info(f"์ ์ฌ๋: {resemblance}")
logger.info(f"์ฐฝ์์ฑ: {creativity}")
logger.info(f"๋ค์ด๋๋ฏน: {dynamic}")
logger.info(f"์ ๋ช
๋: {sharpen}")
logger.info(f"์๋: {seed}")
# ํ๊ฒฝ๋ณ์์์ ๊ฐ์ ธ์จ ๋ชจ๋ธ๋ก API ํธ์ถ
output = replicate.run(
REPLICATE_MODEL,
input={
"seed": seed,
"image": open(temp_input_path, "rb"),
"prompt": prompt,
"dynamic": dynamic,
"handfix": "disabled",
"pattern": False,
"sharpen": sharpen,
"sd_model": sd_model,
"scheduler": "DPM++ 3M SDE Karras",
"creativity": creativity,
"lora_links": "",
"downscaling": False,
"resemblance": resemblance,
"scale_factor": scale_factor,
"tiling_width": 112,
"output_format": output_format,
"tiling_height": 144,
"custom_sd_model": "",
"negative_prompt": negative_prompt,
"num_inference_steps": 18,
"downscaling_resolution": 768
}
)
logger.info(f"Replicate API ์๋ต: {output}")
# ๊ฒฐ๊ณผ ์ด๋ฏธ์ง ๋ค์ด๋ก๋ ๋ฐ ๋ฐํ
if output and isinstance(output, list) and len(output) > 0:
import requests
from io import BytesIO
response = requests.get(output[0])
if response.status_code == 200:
result_image = Image.open(BytesIO(response.content))
# JPG๋ก ๋ณํ (output_format์ด jpg์ธ ๊ฒฝ์ฐ)
if output_format.lower() == "jpg":
temp_output = BytesIO()
if result_image.mode == 'RGBA':
result_image = result_image.convert('RGB')
result_image.save(temp_output, format='JPEG', quality=95)
temp_output.seek(0)
result_image = Image.open(temp_output)
logger.info("์ด๋ฏธ์ง ํ์ง ๊ฐ์ ์๋ฃ!")
return result_image
else:
logger.error(f"๊ฒฐ๊ณผ ์ด๋ฏธ์ง ๋ค์ด๋ก๋ ์คํจ: {response.status_code}")
return None
else:
logger.error("API ์๋ต์์ ์ด๋ฏธ์ง๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.")
return None
except Exception as e:
logger.error(f"ํ์ง ๊ฐ์ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
raise
finally:
# ์์ ํ์ผ ์ ๋ฆฌ
if os.path.exists(temp_input_path):
os.remove(temp_input_path)
def process_and_download(
password, image, scale_factor, output_format, sd_model,
resemblance, creativity, dynamic, sharpen, seed
):
# ํ๊ฒฝ๋ณ์์์ ๋น๋ฐ๋ฒํธ ๊ฒ์ฆ
if not APP_PASSWORD:
raise ValueError("์๋ฒ ์ค์ ์ค๋ฅ์
๋๋ค. APP_PASSWORD๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
if password != APP_PASSWORD:
raise ValueError("์๋ชป๋ ๋น๋ฐ๋ฒํธ์
๋๋ค.")
if image is None:
return None, None
# ์ด๋ฏธ์ง ์
์ค์ผ์ผ
result_img = upscale_image(
image=image,
scale_factor=scale_factor,
output_format=output_format,
sd_model=sd_model,
resemblance=resemblance,
creativity=creativity,
dynamic=dynamic,
sharpen=sharpen,
seed=int(seed)
)
if result_img is None:
return None, None
# ํ์ผ๋ก ์ ์ฅ
ext = "jpg" if output_format.lower() == "jpg" else "png"
# JPG๋ก ์ ์ฅํ ๋ RGB ๋ชจ๋๋ก ๋ณํ
if ext == "jpg" and result_img.mode == "RGBA":
result_img = result_img.convert("RGB")
filename = f"upscaled_{uuid.uuid4()}.{ext}"
filepath = os.path.join(TEMP_DIR, filename)
result_img.save(filepath, format="JPEG" if ext=="jpg" else "PNG")
# ๋ฆฌํด: (์ด๋ฏธ์ง, ํ์ผ ๊ฒฝ๋ก)
return result_img, filepath
def create_interface():
with gr.Blocks(title="Clarity Upscaler") as demo:
with gr.Row():
with gr.Column():
password_input = gr.Textbox(
label="๋น๋ฐ๋ฒํธ", type="password", placeholder="๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์"
)
input_image = gr.Image(label="์๋ณธ ์ด๋ฏธ์ง", type="pil")
with gr.Accordion("๊ณ ๊ธ ์ค์ ", open=False):
scale_factor = gr.Slider(1, 4, value=2, step=0.5, label="ํ๋ ๋น์จ")
output_format = gr.Radio(["jpg","png"], value="jpg", label="์ถ๋ ฅ ํ์")
sd_model = gr.Dropdown(
choices=[
"juggernaut_reborn.safetensors [338b85bc4f]",
"sd_xl_base_1.0.safetensors [39d4e625d]",
"sdxl_base_v0.9-9961.safetensors [3048045c]",
"realisticVisionV51_v51VAE.safetensors [5ecbfa0ac]",
],
value="juggernaut_reborn.safetensors [338b85bc4f]",
label="SD ๋ชจ๋ธ"
)
resemblance = gr.Slider(0.0,1.0, value=0.6, step=0.05, label="์๋ณธ ์ ์ฌ๋")
creativity = gr.Slider(0.0,1.0, value=0.35,step=0.05, label="์ฐฝ์์ฑ")
dynamic = gr.Slider(1,10, value=6, step=1, label="๋ค์ด๋๋ฏน ์๊ณ๊ฐ")
sharpen = gr.Slider(0,2, value=0, step=0.1, label="์ ๋ช
๋")
seed = gr.Number(value=1337, label="์๋", precision=0)
submit_btn = gr.Button("์คํ", variant="primary")
with gr.Column():
output_image = gr.Image(label="๊ฐ์ ๋ ์ด๋ฏธ์ง")
download_btn = gr.DownloadButton(label="์ด๋ฏธ์ง ๋ค์ด๋ก๋")
# ํด๋ฆญ ์ ๋ ๊ฐ์ ์ถ๋ ฅ(์ด๋ฏธ์ง, ํ์ผ ๊ฒฝ๋ก)์ ๋ฐํ
submit_btn.click(
fn=process_and_download,
inputs=[
password_input,
input_image, scale_factor, output_format, sd_model,
resemblance, creativity, dynamic, sharpen, seed
],
outputs=[output_image, download_btn]
)
return demo
if __name__ == "__main__":
demo = create_interface()
demo.launch(share=True) |