ssboost commited on
Commit
ae247dd
ยท
verified ยท
1 Parent(s): e0a6302

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -235
app.py CHANGED
@@ -1,236 +1,2 @@
1
  import os
2
- import sys
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'))