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