Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -85,9 +85,95 @@ def sd_recall(fetch_result):
|
|
85 |
return image_link
|
86 |
|
87 |
|
|
|
|
|
88 |
|
89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
with gr.Blocks(theme=gr.themes.Default()) as app:
|
92 |
with gr.Tab("์ฝํ ์์ฑ"):
|
93 |
with gr.Row():
|
|
|
85 |
return image_link
|
86 |
|
87 |
|
88 |
+
def edit_load_img(img_url):
|
89 |
+
return img_url
|
90 |
|
91 |
|
92 |
+
import base64
|
93 |
+
def image_to_base64(image_path):
|
94 |
+
with open(image_path, "rb") as image_file:
|
95 |
+
encoded_image = base64.b64encode(image_file.read())
|
96 |
+
encoded_string = encoded_image.decode('utf-8')
|
97 |
+
return encoded_string
|
98 |
|
99 |
+
|
100 |
+
from io import BytesIO
|
101 |
+
from PIL import Image
|
102 |
+
def save_base64_image_from_url(url):
|
103 |
+
file_path = "edit_img.png"
|
104 |
+
response = requests.get(url)
|
105 |
+
base64_string = response.text
|
106 |
+
image_data = base64.b64decode(base64_string)
|
107 |
+
image = Image.open(BytesIO(image_data))
|
108 |
+
image.save(file_path)
|
109 |
+
return file_path
|
110 |
+
|
111 |
+
|
112 |
+
|
113 |
+
def edit_img_generator(input_img, prompt):
|
114 |
+
# ์ด๋ฏธ์ง ์ ์ฅ
|
115 |
+
background_img = input_img['background']
|
116 |
+
background_img = Image.fromarray(background_img)
|
117 |
+
background_img_path = "background_img.png"
|
118 |
+
background_img.save(background_img_path)
|
119 |
+
mask_img = input_img['layers'][0]
|
120 |
+
mask_img_arr = Image.fromarray(mask_img)
|
121 |
+
mask_img_arr_path = "masked_img.png"
|
122 |
+
mask_img_arr.save(mask_img_arr_path)
|
123 |
+
mask_img_arr = Image.fromarray(mask_img)
|
124 |
+
mask_img_arr_path = "masked_img.png"
|
125 |
+
mask_img_arr.save(mask_img_arr_path)
|
126 |
+
background_img_base64 = image_to_base64(background_img_path)
|
127 |
+
mask_img_base64 = image_to_base64(mask_img_arr_path)
|
128 |
+
|
129 |
+
# inpaint ์์ฒญ ํํธ
|
130 |
+
url = "https://stablediffusionapi.com/api/v3/inpaint"
|
131 |
+
payload = json.dumps({
|
132 |
+
"key": SD_API_KEY,
|
133 |
+
"prompt": prompt,
|
134 |
+
"negative_prompt": None,
|
135 |
+
"init_image": background_img_base64,
|
136 |
+
"mask_image": mask_img_base64,
|
137 |
+
"width": "512",
|
138 |
+
"height": "512",
|
139 |
+
"samples": "1",
|
140 |
+
"num_inference_steps": "30",
|
141 |
+
"safety_checker": "no",
|
142 |
+
"enhance_prompt": "yes",
|
143 |
+
"guidance_scale": 7.5,
|
144 |
+
"strength": 0.7,
|
145 |
+
"base64": "yes",
|
146 |
+
"seed": None,
|
147 |
+
"webhook": None,
|
148 |
+
"track_id": None
|
149 |
+
})
|
150 |
+
headers = {
|
151 |
+
'Content-Type': 'application/json'
|
152 |
+
}
|
153 |
+
|
154 |
+
response = requests.request("POST", url, headers=headers, data=payload)
|
155 |
+
response_data = json.loads(response.text)
|
156 |
+
|
157 |
+
if response_data["status"] == "success":
|
158 |
+
image_link = save_base64_image_from_url(response_data["output"][0])
|
159 |
+
fetch_result = ""
|
160 |
+
elif response_data["status"] == "processing":
|
161 |
+
image_link = None
|
162 |
+
fetch_result = response_data["fetch_result"]
|
163 |
+
else:
|
164 |
+
image_link = None
|
165 |
+
fetch_result = "์คํจ์
๋๋ค ๋ค์ ์คํํด ์ฃผ์ธ์"
|
166 |
+
print("๊ฒฐ๊ณผ์
๋๋ค", response_data)
|
167 |
+
return image_link, fetch_result
|
168 |
+
|
169 |
+
# edit ์ด๋ฏธ์ง ๋ค์ ๋ถ๋ฌ์ค๊ธฐ
|
170 |
+
def sd_edit_recall(fetch_result):
|
171 |
+
edit_url = sd_recall(fetch_result)
|
172 |
+
image_link = save_base64_image_from_url(edit_url)
|
173 |
+
return image_link
|
174 |
+
|
175 |
+
|
176 |
+
|
177 |
with gr.Blocks(theme=gr.themes.Default()) as app:
|
178 |
with gr.Tab("์ฝํ ์์ฑ"):
|
179 |
with gr.Row():
|