Spaces:
Sleeping
Sleeping
File size: 10,469 Bytes
820f4f3 c4911f8 e83d07f c4911f8 1891031 67c7c54 1891031 67c7c54 1891031 67c7c54 820f4f3 |
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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
import gradio as gr
import openai
from openai import OpenAI
# from google.colab import userdata
import os
openai.api_key = "sk-nF3vcQD8BwC7dt6taOyQFH9O36rUBGL5dZd1PieaplT3BlbkFJNKltEcbwN7KItynOiRmFC1VECGPjas9DV7jKcZBdYA"
# ์๋ ํ๋กฌํํธ ์์ฑ
def novel_keyword(nobel_input):
system_prompt = "๋น์ ์ ์ฃผ์ด์ง๋ ๋ด์ฉ์ ์ด์ธ๋ฆฌ๋ ์ด๋ฏธ์ง๋ฅผ ์ถ์ฒํ๋ ํฉ๋๋ค. ์ด๋ฏธ์ง๋ฅผ ๋ฌ์ฌํ๋ ํค์๋๋ฅผ ์์ด๋ก, ์ฝค๋ง๋ก ์๋ ค์ฃผ์ธ์"
client = OpenAI()
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
temperature=0,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": nobel_input}
])
return completion.choices[0].message.content
# ์คํ
์ด๋ธ ๋ํจ์ ๊ธฐ๋ณธ ์ด๋ฏธ์ง API ์์ฒญ
import requests
import json
def sd_call(prompt, width, height):
url = "https://stablediffusionapi.com/api/v3/text2img"
payload = json.dumps({
"key": "xbYWUXEs8U4iUweAQjOoH6mrasutoExqC3a2H6NtlnYAWTRXui9y0t2w1g8P",
"prompt": prompt,
"negative_prompt": "ng_deepnegative_v1_75t, (worst quality:1.4), (low quality:1.4), (normal quality:1.4), lowres, (nsfw:1.4)",
"width": width,
"height": height,
"samples": "1",
"num_inference_steps": "20",
"seed": None,
"guidance_scale": 7.5,
"safety_checker": "yes",
"multi_lingual": "no",
"panorama": "no",
"self_attention": "no",
"upscale": "no",
"embeddings_model": None,
"webhook": None,
"track_id": None
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
response_data = json.loads(response.text)
if response_data["status"] == "success":
image_link = response_data["output"][0]
fetch_result = "์ด๋ฏธ์ง ์์ฑ์ด ์๋ฃ๋์์ต๋๋ค"
elif response_data["status"] == "processing":
image_link = None
fetch_result = response_data["fetch_result"]
else:
image_link = None
fetch_result = "์คํจ์
๋๋ค ๋ค์ ์คํํด ์ฃผ์ธ์"
return image_link, fetch_result
def sd_recall(fetch_result):
url = fetch_result
payload = json.dumps({
"key": "xbYWUXEs8U4iUweAQjOoH6mrasutoExqC3a2H6NtlnYAWTRXui9y0t2w1g8P"
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
response_data = json.loads(response.text)
image_link = response_data["output"]
if not image_link:
image_link = None
else:
image_link = image_link[0]
return image_link
def edit_load_img(img_url):
return img_url
import base64
def image_to_base64(image_path):
with open(image_path, "rb") as image_file:
encoded_image = base64.b64encode(image_file.read())
encoded_string = encoded_image.decode('utf-8')
return encoded_string
from io import BytesIO
from PIL import Image
def save_base64_image_from_url(url):
file_path = "edit_img.png"
response = requests.get(url)
base64_string = response.text
image_data = base64.b64decode(base64_string)
image = Image.open(BytesIO(image_data))
image.save(file_path)
return file_path
def edit_img_generator(input_img, prompt):
# ์ด๋ฏธ์ง ์ ์ฅ
background_img = input_img['background']
background_img = Image.fromarray(background_img)
background_img_path = "background_img.png"
background_img.save(background_img_path)
mask_img = input_img['layers'][0]
mask_img_arr = Image.fromarray(mask_img)
mask_img_arr_path = "masked_img.png"
mask_img_arr.save(mask_img_arr_path)
mask_img_arr = Image.fromarray(mask_img)
mask_img_arr_path = "masked_img.png"
mask_img_arr.save(mask_img_arr_path)
background_img_base64 = image_to_base64(background_img_path)
mask_img_base64 = image_to_base64(mask_img_arr_path)
# inpaint ์์ฒญ ํํธ
url = "https://stablediffusionapi.com/api/v3/inpaint"
payload = json.dumps({
"key": SD_API_KEY,
"prompt": prompt,
"negative_prompt": None,
"init_image": background_img_base64,
"mask_image": mask_img_base64,
"width": "512",
"height": "512",
"samples": "1",
"num_inference_steps": "30",
"safety_checker": "no",
"enhance_prompt": "yes",
"guidance_scale": 7.5,
"strength": 0.7,
"base64": "yes",
"seed": None,
"webhook": None,
"track_id": None
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
response_data = json.loads(response.text)
if response_data["status"] == "success":
image_link = save_base64_image_from_url(response_data["output"][0])
fetch_result = ""
elif response_data["status"] == "processing":
image_link = None
fetch_result = response_data["fetch_result"]
else:
image_link = None
fetch_result = "์คํจ์
๋๋ค ๋ค์ ์คํํด ์ฃผ์ธ์"
print("๊ฒฐ๊ณผ์
๋๋ค", response_data)
return image_link, fetch_result
# edit ์ด๋ฏธ์ง ๋ค์ ๋ถ๋ฌ์ค๊ธฐ
def sd_edit_recall(fetch_result):
edit_url = sd_recall(fetch_result)
image_link = save_base64_image_from_url(edit_url)
return image_link
with gr.Blocks(theme=gr.themes.Default()) as app:
with gr.Tab("์ฝํ ์์ฑ"):
with gr.Row():
# 1
gr.Markdown(
value="""
# ์ฝํ ์์ฑ
์งง์ ์์ค์ ์ด์ธ๋ฆฌ๋ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.
"""
)
with gr.Row():
# column1
with gr.Column(scale=5):
# 2
pos_prompt = gr.Textbox(
label="์ด๋ฏธ์ง ์์ฑ ํ๋กฌํํธ๋ฅผ ์์ฑํด ์ฃผ์ธ์",
value="ultra realistic close up portrait ((beautiful pale cyberpunk female with heavy black eyeliner))",
lines=8,
interactive=True,
)
with gr.Row():
# 3
auto_prompt_generator = gr.Textbox(
label="์๋ ํ๋กฌํํธ ์์ฑ",
lines=6,
placeholder="์ด๋ฏธ์ง ์์ฑ์ ์ํ ์์ค ๋ด์ฉ์ ์์ฑํด ์ฃผ์ธ์.\n์๋์ผ๋ก ํ๋กฌํํธ๊ฐ ์์ฑ๋ฉ๋๋ค.",
scale=7,
)
# 4
prompt_generator_btn = gr.Button(scale=1, value="์๋\n์์ฑ")
with gr.Group():
with gr.Row():
# 5
img_width = gr.Slider(
label="=Width", maximum=1024, value=512, interactive=True
)
# 6
img_height = gr.Slider(
label="=Height", maximum=1024, value=512, interactive=True
)
# column2
with gr.Column(scale=3):
# 7
output_status = gr.Textbox(
show_label=False, lines=1, placeholder="์ด๋ฏธ์ง ์ํ๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค."
)
# 8
generator_img = gr.Image(
value="https://pub-3626123a908346a7a8be8d9295f44e26.r2.dev/generations/8c595b57-563c-4417-9bfb-96aaebbb30b3-0.png",
label="์ด๋ฏธ์ง๊ฐ ์์ฑ๋ฉ๋๋ค.",
)
# 9
generator_img_btn = gr.Button(value="์ด๋ฏธ์ง ์์ฑ")
# 10
refresh_img_btn = gr.Button(value="์ด๋ฏธ์ง ์๋ก๊ณ ์นจ")
# ์๋ ์์ฑ ๋ฒํผ ํด๋ฆญ
prompt_generator_btn.click(
fn=novel_keyword,
inputs=[auto_prompt_generator],
outputs=[pos_prompt]
) # ์ด๋ฏธ์ง ์์ฑ ๋ฒํผ ํด๋ฆญ
generator_img_btn.click(
fn=sd_call,
inputs=[pos_prompt, img_width, img_height],
outputs=[generator_img, output_status]
) # ์ด๋ฏธ์ง ์๋ก๊ณ ์นจ ๋ฒํผ ํด๋ฆญ
refresh_img_btn.click(
fn=sd_recall,
inputs=[output_status],
outputs=[generator_img]
)
with gr.Tab("์ด๋ฏธ์ง ํธ์ง") as edit_tab:
with gr.Row():
#1
gr.Markdown(
value="""
# ์ด๋ฏธ์ง ํธ์ง
์์ฑํ ์ด๋ฏธ์ง๋ฅผ ํธ์งํ ์ ์์ต๋๋ค.
""")
with gr.Row():
#2
edit_prompt = gr.Textbox(
label="์ด๋ฏธ์ง ์์ ํ๋กฌํํธ๋ฅผ ์์ฑํด ์ฃผ์ธ์",
value="black hair",
lines=5,
interactive=True,
scale=7
)
#3
edit_btn = gr.Button(
value="์ด๋ฏธ์ง ํธ์ง",
scale=1
)
with gr.Row():
#4
edit_status = gr.Textbox(
show_label=False,
lines=1,
placeholder="ํธ์ง ์ด๋ฏธ์ง ์ํ๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค.",
scale=7
)
#5
refresh_edit_btn = gr.Button(
value="์ด๋ฏธ์ง ์๋ก๊ณ ์นจ",
scale=1
)
with gr.Row():
#6
org_img = gr.ImageMask(
label="์์ ์ ์ด๋ฏธ์ง",
image_mode='RGB',
brush = gr.Brush(
default_size=20,
colors=["#FFFFFF"],
color_mode="fixed",
),
show_label=True
)
#7
edit_img = gr.Image(
label="์์ ํ ์ด๋ฏธ์ง",
)
edit_tab.select(
fn=edit_load_img,
inputs=[generator_img],
outputs=[org_img]
)
#์ด๋ฏธ์ง ํธ์ง ๋ฒํผ ํด๋ฆญ
edit_btn.click(
fn=edit_img_generator,
inputs=[org_img, edit_prompt],
outputs=[edit_img, edit_status]
)
#์ด๋ฏธ์ง
refresh_edit_btn.click(
fn=sd_edit_recall,
inputs=[edit_status],
outputs=[edit_img]
)
app.launch(debug=True) |