Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,6 @@ import datetime
|
|
| 12 |
from pydrive2.auth import GoogleAuth
|
| 13 |
from pydrive2.drive import GoogleDrive
|
| 14 |
from oauth2client.service_account import ServiceAccountCredentials
|
| 15 |
-
import os
|
| 16 |
import base64
|
| 17 |
from io import BytesIO
|
| 18 |
|
|
@@ -180,28 +179,25 @@ def add_alpha(image, alpha_value=255):
|
|
| 180 |
return image
|
| 181 |
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
def generate_image(prompt):
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
# ⚠️ rembg 결과는 bytes일 수 있으니 PIL로 복구
|
| 194 |
-
out_bytes = remove(image) # -> bytes
|
| 195 |
-
image_no_bg = Image.open(io.BytesIO(out_bytes)).convert("RGBA")
|
| 196 |
-
image_no_bg.load() # lazy 로딩 방지
|
| 197 |
-
|
| 198 |
-
print("*****generate_image*****")
|
| 199 |
-
print(type(image_no_bg)) # <class 'PIL.Image.Image'>
|
| 200 |
-
return image_no_bg
|
| 201 |
-
|
| 202 |
|
| 203 |
-
except Exception as e:
|
| 204 |
-
return f"이미지 생성 중 오류 발생: {str(e)}"
|
| 205 |
|
| 206 |
def jsonl_to_description(json_data):
|
| 207 |
"""
|
|
@@ -228,13 +224,15 @@ def jsonl_to_description(json_data):
|
|
| 228 |
|
| 229 |
# 최종 결과물 생성
|
| 230 |
def generate_final_output(text_result, image_result, inputText):
|
|
|
|
|
|
|
| 231 |
name = text_result.get("Name", "Unknown name")
|
| 232 |
description = text_result.get("Description", "Unknown description")
|
| 233 |
magic = text_result.get("Magic", "Unknown name")
|
| 234 |
keysentence = text_result.get("KeySentence", "Unknown name")
|
| 235 |
|
| 236 |
base_image = Image.open("temu_esports.png").convert("RGBA")
|
| 237 |
-
image_result = image_result.resize((400, 400))
|
| 238 |
base_image.paste(image_result, (70, 373), mask=image_result)
|
| 239 |
|
| 240 |
# 이미지 위에 그리기
|
|
|
|
| 12 |
from pydrive2.auth import GoogleAuth
|
| 13 |
from pydrive2.drive import GoogleDrive
|
| 14 |
from oauth2client.service_account import ServiceAccountCredentials
|
|
|
|
| 15 |
import base64
|
| 16 |
from io import BytesIO
|
| 17 |
|
|
|
|
| 179 |
return image
|
| 180 |
|
| 181 |
|
| 182 |
+
# 추가: 안전한 배경 제거 함수 (PIL <-> bytes 변환 포함)
|
| 183 |
+
def remove_background(image_pil: Image.Image) -> Image.Image:
|
| 184 |
+
img = image_pil.convert("RGBA")
|
| 185 |
+
out_bytes = remove(img) # rembg는 보통 bytes 반환
|
| 186 |
+
out_img = Image.open(BytesIO(out_bytes)).convert("RGBA")
|
| 187 |
+
out_img.load() # lazy-loading 방지
|
| 188 |
+
return out_img
|
| 189 |
+
|
| 190 |
def generate_image(prompt):
|
| 191 |
+
# ❗에러 시 문자열 반환 금지 (타이핑 에러 유발)
|
| 192 |
+
image = pipe(
|
| 193 |
+
prompt,
|
| 194 |
+
num_inference_steps=32,
|
| 195 |
+
guidance_scale=10
|
| 196 |
+
).images[0].convert("RGBA")
|
| 197 |
+
|
| 198 |
+
image_no_bg = remove_background(image) # 항상 PIL 반환
|
| 199 |
+
return image_no_bg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
|
|
|
|
|
|
| 201 |
|
| 202 |
def jsonl_to_description(json_data):
|
| 203 |
"""
|
|
|
|
| 224 |
|
| 225 |
# 최종 결과물 생성
|
| 226 |
def generate_final_output(text_result, image_result, inputText):
|
| 227 |
+
image_result = ensure_pil(image_result)
|
| 228 |
+
|
| 229 |
name = text_result.get("Name", "Unknown name")
|
| 230 |
description = text_result.get("Description", "Unknown description")
|
| 231 |
magic = text_result.get("Magic", "Unknown name")
|
| 232 |
keysentence = text_result.get("KeySentence", "Unknown name")
|
| 233 |
|
| 234 |
base_image = Image.open("temu_esports.png").convert("RGBA")
|
| 235 |
+
image_result = image_result.resize((400, 400), Image.LANCZOS)
|
| 236 |
base_image.paste(image_result, (70, 373), mask=image_result)
|
| 237 |
|
| 238 |
# 이미지 위에 그리기
|