Update app.py
Browse files
app.py
CHANGED
@@ -1,182 +1,38 @@
|
|
1 |
import os
|
2 |
import tempfile
|
3 |
-
from PIL import Image
|
4 |
-
import gradio as gr
|
5 |
import logging
|
6 |
import re
|
7 |
import time
|
8 |
import json
|
9 |
-
|
|
|
10 |
from google import genai
|
11 |
from google.genai import types
|
|
|
12 |
from dotenv import load_dotenv
|
13 |
|
14 |
load_dotenv()
|
15 |
|
16 |
-
#
|
17 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
18 |
logger = logging.getLogger(__name__)
|
19 |
|
20 |
-
#
|
21 |
-
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", "")
|
22 |
-
|
23 |
-
# Gemini API ์ด๊ธฐํ
|
24 |
-
genai_client = genai.Client(api_key=GEMINI_API_KEY)
|
25 |
-
|
26 |
-
# ๋ฐฐ๊ฒฝ JSON ํ์ผ ๊ฒฝ๋ก ์ค์ - ์๋ ๊ฒฝ๋ก ์ฌ์ฉ
|
27 |
-
BACKGROUNDS_DIR = "./background"
|
28 |
-
|
29 |
-
# ๋๋ฒ๊น
์ ์ํ ์ ๋ณด ์ถ๋ ฅ
|
30 |
-
print(f"ํ์ฌ ์์
๋๋ ํ ๋ฆฌ: {os.getcwd()}")
|
31 |
-
print(f"์ฌ์ฉ ์ค์ธ ๋ฐฐ๊ฒฝ ๋๋ ํ ๋ฆฌ ๊ฒฝ๋ก: {BACKGROUNDS_DIR}")
|
32 |
-
|
33 |
-
# JSON ํ์ผ์ด ์กด์ฌํ์ง ์์ ๊ฒฝ์ฐ ๋๋ ํ ๋ฆฌ ์์ฑ
|
34 |
-
if not os.path.exists(BACKGROUNDS_DIR):
|
35 |
-
os.makedirs(BACKGROUNDS_DIR)
|
36 |
-
print(f"๋ฐฐ๊ฒฝ ๋๋ ํ ๋ฆฌ๋ฅผ ์์ฑํ์ต๋๋ค: {BACKGROUNDS_DIR}")
|
37 |
-
else:
|
38 |
-
print(f"๋ฐฐ๊ฒฝ ๋๋ ํ ๋ฆฌ๊ฐ ์ด๋ฏธ ์กด์ฌํฉ๋๋ค: {BACKGROUNDS_DIR}")
|
39 |
-
try:
|
40 |
-
for file in os.listdir(BACKGROUNDS_DIR):
|
41 |
-
print(f"๋ฐ๊ฒฌ๋ ํ์ผ: {file}")
|
42 |
-
except Exception as e:
|
43 |
-
print(f"๋๋ ํ ๋ฆฌ ๋ด์ฉ์ ๋์ดํ๋ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
44 |
-
|
45 |
-
# JSON ํ์ผ ๋ก๋ ํจ์
|
46 |
-
def load_background_json(filename):
|
47 |
-
file_path = os.path.join(BACKGROUNDS_DIR, filename)
|
48 |
-
try:
|
49 |
-
with open(file_path, 'r', encoding='utf-8') as f:
|
50 |
-
data = json.load(f)
|
51 |
-
print(f"{filename} ํ์ผ์ ์ฑ๊ณต์ ์ผ๋ก ๋ก๋ํ์ต๋๋ค. {len(data)} ํญ๋ชฉ ํฌํจ.")
|
52 |
-
return data
|
53 |
-
except FileNotFoundError:
|
54 |
-
print(f"๊ฒฝ๊ณ : {filename} ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค. ๊ธฐ๋ณธ๊ฐ์ ์ฌ์ฉํฉ๋๋ค.")
|
55 |
-
return {}
|
56 |
-
except json.JSONDecodeError:
|
57 |
-
print(f"๊ฒฝ๊ณ : {filename} ํ์ผ์ JSON ํ์์ด ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค. ๊ธฐ๋ณธ๊ฐ์ ์ฌ์ฉํฉ๋๋ค.")
|
58 |
-
return {}
|
59 |
-
except Exception as e:
|
60 |
-
print(f"๊ฒฝ๊ณ : {filename} ํ์ผ ๋ก๋ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}. ๊ธฐ๋ณธ๊ฐ์ ์ฌ์ฉํฉ๋๋ค.")
|
61 |
-
return {}
|
62 |
-
|
63 |
-
# ๋ฐฐ๊ฒฝ ๋ฐ์ดํฐ ๋ก๋
|
64 |
-
SIMPLE_BACKGROUNDS = load_background_json("simple_backgrounds.json")
|
65 |
-
STUDIO_BACKGROUNDS = load_background_json("studio_backgrounds.json")
|
66 |
-
NATURE_BACKGROUNDS = load_background_json("nature_backgrounds.json")
|
67 |
-
INDOOR_BACKGROUNDS = load_background_json("indoor_backgrounds.json")
|
68 |
-
ABSTRACT_BACKGROUNDS = load_background_json("abstract_backgrounds.json")
|
69 |
-
|
70 |
-
# ๋ฐฐ๊ฒฝ์ด ๋ก๋๋์ง ์์ ๊ฒฝ์ฐ ๊ธฐ๋ณธ๊ฐ ์ค์
|
71 |
-
if not SIMPLE_BACKGROUNDS:
|
72 |
-
SIMPLE_BACKGROUNDS = {"ํ์ดํธ ๋ฐฐ๊ฒฝ": "white background"}
|
73 |
-
if not STUDIO_BACKGROUNDS:
|
74 |
-
STUDIO_BACKGROUNDS = {"์ ํ ์ฌ์ง ์คํ๋์ค": "product photography studio"}
|
75 |
-
if not NATURE_BACKGROUNDS:
|
76 |
-
NATURE_BACKGROUNDS = {"์ด๋ ํด๋ณ": "tropical beach"}
|
77 |
-
if not INDOOR_BACKGROUNDS:
|
78 |
-
INDOOR_BACKGROUNDS = {"๋ชจ๋ ๋ฆฌ๋น๋ฃธ": "modern living room"}
|
79 |
-
if not ABSTRACT_BACKGROUNDS:
|
80 |
-
ABSTRACT_BACKGROUNDS = {"๋ค์จ ์กฐ๋ช
": "neon lights"}
|
81 |
-
|
82 |
def save_binary_file(file_name, data):
|
83 |
with open(file_name, "wb") as f:
|
84 |
f.write(data)
|
85 |
|
86 |
-
def generate_system_instruction():
|
87 |
-
return """๋น์ ์ ์ํ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ ๋ณ๊ฒฝํ๊ธฐ ์ํ ๊ณ ํ์ง ํ๋กฌํํธ๋ฅผ ์์ฑํ๋ ์ ๋ฌธ๊ฐ์
๋๋ค.
|
88 |
-
์ฌ์ฉ์๊ฐ ์ ๊ณตํ๋ ์ํ๋ช
, ๋ฐฐ๊ฒฝ ์ ํ, ์ถ๊ฐ ์์ฒญ์ฌํญ์ ๋ฐํ์ผ๋ก ๋ฏธ๋์ ๋(Midjourney)์ ์ฌ์ฉํ ์ ์๋
|
89 |
-
์์ธํ๊ณ ์ ๋ฌธ์ ์ธ ํ๋กฌํํธ๋ฅผ ์์ด๋ก ์์ฑํด์ฃผ์ธ์.
|
90 |
-
๋ค์ ๊ฐ์ด๋๋ผ์ธ์ ๋ฐ๋์ ๋ฐ๋ผ์ผ ํฉ๋๋ค:
|
91 |
-
1. ์ํ์ "#1"๋ก ์ง์ ํ์ฌ ์ฐธ์กฐํฉ๋๋ค. (์: "skincare tube (#1)")
|
92 |
-
2. *** ๋งค์ฐ ์ค์: ์ํ์ ์๋ ํน์ฑ(๋์์ธ, ์์, ํํ, ๋ก๊ณ , ํจํค์ง ๋ฑ)์ ์ด๋ค ์ํฉ์์๋ ์ ๋ ๋ณ๊ฒฝํ์ง ์์ต๋๋ค. ***
|
93 |
-
3. *** ์ํ์ ๋ณธ์ง์ ํน์ฑ์ ์ ์งํ๋, ์์ฐ์ค๋ฌ์ด ํ๊ฒฝ ํตํฉ์ ์ํ ์กฐ๋ช
๊ณผ ๊ทธ๋ฆผ์๋ ํ์ฉํฉ๋๋ค: ***
|
94 |
-
- ์ํ ์์ฒด์ ์์, ๋์์ธ, ํํ, ํ
์ค์ฒ๋ ์ ๋ ์์ ํ์ง ์์ต๋๋ค.
|
95 |
-
- ํ๊ฒฝ๊ณผ ์์ฐ์ค๋ฝ๊ฒ ์ด์ธ๋ฆฌ๋ ๊ทธ๋ฆผ์, ์ฃผ๋ณ ์กฐ๋ช
ํจ๊ณผ๋ ํ์ฉ๋ฉ๋๋ค.
|
96 |
-
- ์ํ์ ๋ฌผ๋ฐฉ์ธ, ์์ถ, ๊ธ, ์๊ณผ ๊ฐ์ ์ถ๊ฐ ์์๋ ๋ฌผ๋ฆฌ์ ํจ๊ณผ๋ ์ ์ฉํ์ง ์์ต๋๋ค.
|
97 |
-
- ํ๊ฒฝ์ ์ด์ธ๋ฆฌ๋ ์์ฐ์ค๋ฌ์ด ๋น ๋ฐ์ฌ, ์ฃผ๋ณ ์กฐ๋ช
, ๊ทธ๋ฆผ์๋ ์ฌ์ค์ ํตํฉ๊ฐ์ ์ํด ์ ์ฉํ ์ ์์ต๋๋ค.
|
98 |
-
4. ์ด๋ฏธ์ง ๋น์จ์ ์ ํํ 1:1(์ ์ฌ๊ฐํ) ํ์์ผ๋ก ์ง์ ํฉ๋๋ค. ํ๋กฌํํธ์ "square format", "1:1 ratio" ๋๋ "aspect ratio 1:1"์ ๋ช
์์ ์ผ๋ก ํฌํจํฉ๋๋ค.
|
99 |
-
5. ์ํ์ ๋ฐ๋์ ์ ์ฌ๊ฐํ ๊ตฌ๋์ ์ ์ค์์ ๋ฐฐ์น๋์ด์ผ ํฉ๋๋ค.
|
100 |
-
6. ์ํ์ ์ด๋ฏธ์ง์ ์ฃผ์ ์ด์ ์ผ๋ก ๋ถ๊ฐ์ํค๊ณ , ์ํ์ ๋น์จ์ด ์ ์ฒด ์ด๋ฏธ์ง์์ ํฌ๊ฒ ์ฐจ์งํ๋๋ก ํฉ๋๋ค.
|
101 |
-
7. ์ํ ์ด๋ฏธ์ง ์ปท์์(#1)์ ๊ธฐ๋ณธ ํํ์ ์์์ ์ ์งํ๋ฉด์, ์ ํํ ํ๊ฒฝ์ ์์ฐ์ค๋ฝ๊ฒ ํตํฉ๋๋๋ก ํฉ๋๋ค.
|
102 |
-
8. ๊ณ ๊ธ์ค๋ฌ์ด ์์
์ ์ด๋ฏธ์ง๋ฅผ ์ํ ๋ค์ ํ๊ฒฝ ์์๋ค์ ํฌํจํ์ธ์:
|
103 |
-
- ์ํ๊ณผ ์ด์ธ๋ฆฌ๋ ์ฃผ๋ณ ํ๊ฒฝ/๋ฐฐ๊ฒฝ ์์๋ฅผ ์ถ๊ฐํฉ๋๋ค. ์๋ฅผ ๋ค์ด, ํ์ฅํ ์ฃผ๋ณ์ ๊ฝ์ด๋ ํ๋ธ, ์๋ฃ ์ ํ ์์ ๊ณผ์ผ, ์ ์์ ํ ๊ทผ์ฒ์ ํ๋์ ์ํ ๋ฑ.
|
104 |
-
- ํ๊ฒฝ์ ์กฐ๋ช
ํจ๊ณผ(๋ฆผ ๋ผ์ดํธ, ๋ฐฑ๋ผ์ดํธ, ์ํํธ๋ฐ์ค ๋ฑ)๋ฅผ ์ค๋ช
ํฉ๋๋ค.
|
105 |
-
- ์ํ์ด ํ๊ฒฝ์ ์์ฐ์ค๋ฝ๊ฒ ์กด์ฌํ๋ ๊ฒ์ฒ๋ผ ๋ณด์ด๋๋ก ์ ์ ํ ๊ทธ๋ฆผ์์ ๋น ํํ์ ํฌํจํฉ๋๋ค.
|
106 |
-
- ์ํ์ ์ฉ๋๋ ์ฅ์ ์ ๊ฐ์ ์ ์ผ๋ก ์์ํ๋ ๋ฐฐ๊ฒฝ ์์๋ฅผ ํฌํจํฉ๋๋ค.
|
107 |
-
- ํ๋กํ์
๋ํ ์์
์ฌ์ง ํจ๊ณผ(์ ํ์ ํผ์ฌ๊ณ ์ฌ๋, ์ํํธ ํฌ์ปค์ค, ์คํ๋์ค ์กฐ๋ช
๋ฑ)๋ฅผ ๋ช
์ํฉ๋๋ค.
|
108 |
-
9. ํ๋กฌํํธ์ ๋ค์ ์์๋ค์ ๋ช
์์ ์ผ๋ก ํฌํจํ์ธ์:
|
109 |
-
- "highly detailed commercial photography"
|
110 |
-
- "award-winning product photography"
|
111 |
-
- "professional advertising imagery"
|
112 |
-
- "studio quality"
|
113 |
-
- "magazine advertisement quality"
|
114 |
-
10. ๋ฐฐ๊ฒฝ ํ๊ฒฝ ์์๋ฅผ ์ํ ์นดํ
๊ณ ๋ฆฌ์ ๋ง๊ฒ ์ ํํฉ๋๋ค:
|
115 |
-
- ์คํจ์ผ์ด ์ ํ: ๊นจ๋ํ ์์ค ์ ๋ฐ, ์ฐ์ํ ํ์ฅ๋, ์คํ ๊ฐ์ ํ๊ฒฝ ๋ฑ
|
116 |
-
- ์๋ฃ ์ ํ: ์ธ๋ จ๋ ํ
์ด๋ธ, ํํฐ ํ๊ฒฝ, ์ผ์ธ ํผํฌ๋ ์ฅ๋ฉด ๋ฑ
|
117 |
-
- ์ ์ ์ ํ: ์ธ๋ จ๋ ์์
๊ณต๊ฐ, ํ๋์ ์ธ ๊ฑฐ์ค, ๋ฏธ๋๋ฉํ ์ฑ
์ ๋ฑ
|
118 |
-
- ํจ์
/์๋ฅ: ์ธ๋ จ๋ ์ผ๋ฃธ, ๋์ ๊ฑฐ๋ฆฌ, ์๋ ๊ฐ์คํ ๋ผ์ดํ์คํ์ผ ํ๊ฒฝ ๋ฑ
|
119 |
-
- ์ํ ์ ํ: ๊น๋ํ ์ฃผ๋ฐฉ, ์ํ, ์๋ฆฌ ํ๊ฒฝ ๋ฑ
|
120 |
-
11. ์ฌ์ฉ์๊ฐ ์ ๊ณตํ ๊ตฌ์ฒด์ ์ธ ๋ฐฐ๊ฒฝ๊ณผ ์ถ๊ฐ ์์ฒญ์ฌํญ์ ์ ํํ ๋ฐ์ํฉ๋๋ค.
|
121 |
-
12. ํ๋กฌํํธ๋ ๋ฏธ๋์ ๋ AI์ ์ต์ ํ๋์ด์ผ ํฉ๋๋ค.
|
122 |
-
13. ํ๋กฌํํธ ๋์ "--ar 1:1 --s 750 --q 2" ํ๋ผ๋ฏธํฐ๋ฅผ ์ถ๊ฐํ์ฌ ๋ฏธ๋์ ๋์์ ๊ณ ํ์ง ์ ์ฌ๊ฐํ ๋น์จ์ ๊ฐ์ ํฉ๋๋ค.
|
123 |
-
์ถ๋ ฅ ํ์์ ์์ด๋ก ๋ ๋จ์ผ ๋จ๋ฝ์ ์์ธํ ํ๋กฌํํธ์ฌ์ผ ํ๋ฉฐ, ๋์ ๋ฏธ๋์ ๋ ํ๋ผ๋ฏธํฐ๊ฐ ํฌํจ๋์ด์ผ ํฉ๋๋ค.
|
124 |
-
"""
|
125 |
-
|
126 |
-
def generate_prompt_with_gemini(product_name, background_info, additional_info=""):
|
127 |
-
if not GEMINI_API_KEY:
|
128 |
-
return "Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ ๋ณ์ GEMINI_API_KEY๋ฅผ ์ค์ ํ๊ฑฐ๋ ์ฝ๋์ ์ง์ ์
๋ ฅํ์ธ์."
|
129 |
-
try:
|
130 |
-
prompt_request = f"""
|
131 |
-
์ํ๋ช
: {product_name}
|
132 |
-
๋ฐฐ๊ฒฝ ์ ํ: {background_info.get('english', 'studio')}
|
133 |
-
๋ฐฐ๊ฒฝ ์นดํ
๊ณ ๋ฆฌ: {background_info.get('category', '')}
|
134 |
-
๋ฐฐ๊ฒฝ ์ด๋ฆ: {background_info.get('name', '')}
|
135 |
-
์ถ๊ฐ ์์ฒญ์ฌํญ: {additional_info}
|
136 |
-
์ค์ ์๊ตฌ์ฌํญ:
|
137 |
-
1. ์ํ์ด ํฌ๊ฒ ๋ถ๊ฐ๋๊ณ ์ด๋ฏธ์ง์์ ์ค์ฌ์ ์ธ ์์น๋ฅผ ์ฐจ์งํ๋๋ก ํ๋กฌํํธ๋ฅผ ์์ฑํด์ฃผ์ธ์.
|
138 |
-
2. ์ด๋ฏธ์ง๋ ์ ํํ 1:1 ๋น์จ(์ ์ฌ๊ฐํ)์ด์ด์ผ ํฉ๋๋ค.
|
139 |
-
3. ์ํ์ ์ ์ฌ๊ฐํ ํ๋ ์์ ์ ์ค์์ ์์นํด์ผ ํฉ๋๋ค.
|
140 |
-
4. ์ํ์ ๋์์ธ, ์์, ํํ, ๋ก๊ณ ๋ฑ ๋ณธ์ง์ ํน์ฑ์ ์ ๋ ์์ ํ์ง ๋ง์ธ์.
|
141 |
-
5. ํ๊ฒฝ๊ณผ์ ์์ฐ์ค๋ฌ์ด ํตํฉ์ ์ํ ์กฐ๋ช
ํจ๊ณผ์ ๊ทธ๋ฆผ์๋ ํฌํจํด์ฃผ์ธ์.
|
142 |
-
6. ์ํ์ ๋ ๋๋ณด์ด๊ฒ ํ๋ ๋ฐฐ๊ฒฝ ํ๊ฒฝ์ ์ค๋ช
ํด์ฃผ์ธ์.
|
143 |
-
7. ๊ณ ๊ธ์ค๋ฌ์ด ์์
๊ด๊ณ ํ์ง์ ์ด๋ฏธ์ง๊ฐ ๋๋๋ก ํ๊ฒฝ ์ค๋ช
์ ํด์ฃผ์ธ์.
|
144 |
-
8. ํ๋กฌํํธ ๋์ ๋ฏธ๋์ ๋ ํ๋ผ๋ฏธํฐ "--ar 1:1 --s 750 --q 2"๋ฅผ ์ถ๊ฐํด์ฃผ์ธ์.
|
145 |
-
ํ๊ตญ์ด ์
๋ ฅ ๋ด์ฉ์ ์์ด๋ก ์ ์ ํ ๋ฒ์ญํ์ฌ ๋ฐ์ํด์ฃผ์ธ์.
|
146 |
-
"""
|
147 |
-
response = genai_client.models.generate_content(
|
148 |
-
model="gemini-2.0-flash",
|
149 |
-
contents=[
|
150 |
-
{
|
151 |
-
"role": "user",
|
152 |
-
"parts": [
|
153 |
-
{"text": generate_system_instruction()},
|
154 |
-
{"text": prompt_request}
|
155 |
-
]
|
156 |
-
}
|
157 |
-
],
|
158 |
-
generation_config=types.GenerationConfig(
|
159 |
-
temperature=0.7,
|
160 |
-
top_p=0.95,
|
161 |
-
top_k=64,
|
162 |
-
max_output_tokens=1024,
|
163 |
-
)
|
164 |
-
)
|
165 |
-
|
166 |
-
response_text = response.candidates[0].content.parts[0].text.strip()
|
167 |
-
if "--ar 1:1" not in response_text:
|
168 |
-
response_text = response_text.rstrip(".") + ". --ar 1:1 --s 750 --q 2"
|
169 |
-
return response_text
|
170 |
-
except Exception as e:
|
171 |
-
return f"ํ๋กฌํํธ ์์ฑ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
172 |
-
|
173 |
def translate_prompt_to_english(prompt):
|
174 |
if not re.search("[๊ฐ-ํฃ]", prompt):
|
175 |
return prompt
|
176 |
-
|
177 |
prompt = prompt.replace("#1", "IMAGE_TAG_ONE")
|
178 |
-
|
179 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
translation_prompt = f"""
|
181 |
Translate the following Korean text to English:
|
182 |
|
@@ -185,21 +41,22 @@ def translate_prompt_to_english(prompt):
|
|
185 |
IMPORTANT: The token IMAGE_TAG_ONE is a special tag
|
186 |
and must be preserved exactly as is in your translation. Do not translate this token.
|
187 |
"""
|
188 |
-
|
189 |
logger.info(f"Translation prompt: {translation_prompt}")
|
190 |
-
response =
|
191 |
model="gemini-2.0-flash",
|
192 |
contents=[translation_prompt],
|
193 |
-
|
|
|
194 |
temperature=0.2,
|
195 |
top_p=0.95,
|
196 |
top_k=40,
|
197 |
max_output_tokens=512
|
198 |
)
|
199 |
)
|
200 |
-
|
201 |
-
|
202 |
-
|
|
|
203 |
if translated_text.strip():
|
204 |
translated_text = translated_text.replace("IMAGE_TAG_ONE", "#1")
|
205 |
logger.info(f"Translated text: {translated_text.strip()}")
|
@@ -213,49 +70,52 @@ def translate_prompt_to_english(prompt):
|
|
213 |
prompt = prompt.replace("IMAGE_TAG_ONE", "#1")
|
214 |
return prompt
|
215 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
def generate_with_images(prompt, images, variation_index=0):
|
217 |
try:
|
218 |
-
|
|
|
219 |
return None, "API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ๋ณ์๋ฅผ ํ์ธํด์ฃผ์ธ์."
|
220 |
-
|
221 |
logger.info(f"Gemini API ์์ฒญ ์์ - ํ๋กฌํํธ: {prompt}, ๋ณํ ์ธ๋ฑ์ค: {variation_index}")
|
222 |
-
|
223 |
variation_suffixes = [
|
224 |
" Create this as the first variation. Do not add any text, watermarks, or labels to the image.",
|
225 |
" Create this as the second variation with more vivid colors. Do not add any text, watermarks, or labels to the image.",
|
226 |
" Create this as the third variation with a more creative style. Do not add any text, watermarks, or labels to the image.",
|
227 |
" Create this as the fourth variation with enhanced details. Do not add any text, watermarks, or labels to the image."
|
228 |
]
|
229 |
-
|
230 |
if variation_index < len(variation_suffixes):
|
231 |
prompt = prompt + variation_suffixes[variation_index]
|
232 |
else:
|
233 |
prompt = prompt + " Do not add any text, watermarks, or labels to the image."
|
234 |
-
|
235 |
contents = [prompt]
|
236 |
for idx, img in enumerate(images, 1):
|
237 |
if img is not None:
|
238 |
-
# PIL ์ด๋ฏธ์ง๋ฅผ ํํธ๋ก ๋ณํ
|
239 |
contents.append(img)
|
240 |
logger.info(f"์ด๋ฏธ์ง #{idx} ์ถ๊ฐ๋จ")
|
241 |
-
|
242 |
-
response = genai_client.models.generate_content(
|
243 |
model="gemini-2.0-flash-exp-image-generation",
|
244 |
contents=contents,
|
245 |
-
|
|
|
246 |
temperature=1,
|
247 |
top_p=0.95,
|
248 |
top_k=40,
|
249 |
max_output_tokens=8192
|
250 |
)
|
251 |
)
|
252 |
-
|
253 |
-
# ์์ ํ์ผ์ ํญ์ JPG ํ์ฅ์๋ก ์์ฑ
|
254 |
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as tmp:
|
255 |
temp_path = tmp.name
|
256 |
result_text = ""
|
257 |
image_found = False
|
258 |
-
|
259 |
for part in response.candidates[0].content.parts:
|
260 |
if hasattr(part, 'text') and part.text:
|
261 |
result_text += part.text
|
@@ -264,18 +124,12 @@ def generate_with_images(prompt, images, variation_index=0):
|
|
264 |
save_binary_file(temp_path, part.inline_data.data)
|
265 |
image_found = True
|
266 |
logger.info("์๋ต์์ ์ด๋ฏธ์ง ์ถ์ถ ์ฑ๊ณต")
|
267 |
-
|
268 |
if not image_found:
|
269 |
return None, f"API์์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ์๋ต ํ
์คํธ: {result_text}"
|
270 |
-
|
271 |
result_img = Image.open(temp_path)
|
272 |
if result_img.mode == "RGBA":
|
273 |
-
result_img = result_img.convert("RGB")
|
274 |
-
|
275 |
-
# ๋ณํ๋ ์ด๋ฏธ์ง๋ฅผ JPG๋ก ์ ์ฅ
|
276 |
result_img.save(temp_path, format="JPEG", quality=95)
|
277 |
-
|
278 |
-
# ํ์ผ ๊ฒฝ๋ก ๋ฐํ
|
279 |
return temp_path, f"์ด๋ฏธ์ง๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ๋์์ต๋๋ค. {result_text}"
|
280 |
except Exception as e:
|
281 |
logger.exception("์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:")
|
@@ -284,21 +138,21 @@ def generate_with_images(prompt, images, variation_index=0):
|
|
284 |
def process_images_with_prompt(image1, prompt, variation_index=0, max_retries=3):
|
285 |
retry_count = 0
|
286 |
last_error = None
|
287 |
-
|
288 |
while retry_count < max_retries:
|
289 |
try:
|
290 |
images = [image1]
|
291 |
valid_images = [img for img in images if img is not None]
|
292 |
if not valid_images:
|
293 |
return None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.", ""
|
294 |
-
|
295 |
if prompt and prompt.strip():
|
296 |
-
|
297 |
-
|
|
|
|
|
|
|
298 |
else:
|
299 |
final_prompt = "Please creatively transform this image into a more vivid and artistic version. Do not include any text or watermarks in the generated image."
|
300 |
logger.info("Default prompt generated for single image")
|
301 |
-
|
302 |
result_img, status = generate_with_images(final_prompt, valid_images, variation_index)
|
303 |
if result_img is not None:
|
304 |
return result_img, status, final_prompt
|
@@ -312,23 +166,18 @@ def process_images_with_prompt(image1, prompt, variation_index=0, max_retries=3)
|
|
312 |
retry_count += 1
|
313 |
logger.exception(f"์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์, ์ฌ์๋ {retry_count}/{max_retries}:")
|
314 |
time.sleep(1)
|
315 |
-
|
316 |
return None, f"์ต๋ ์ฌ์๋ ํ์({max_retries}ํ) ์ด๊ณผ ํ ์คํจ: {last_error}", prompt
|
317 |
|
318 |
def generate_multiple_images(image1, prompt, progress=gr.Progress()):
|
319 |
results = []
|
320 |
statuses = []
|
321 |
prompts = []
|
322 |
-
|
323 |
num_images = 4
|
324 |
max_retries = 3
|
325 |
-
|
326 |
progress(0, desc="์ด๋ฏธ์ง ์์ฑ ์ค๋น ์ค...")
|
327 |
-
|
328 |
for i in range(num_images):
|
329 |
progress((i / num_images), desc=f"{i+1}/{num_images} ์ด๋ฏธ์ง ์์ฑ ์ค...")
|
330 |
result_img, status, final_prompt = process_images_with_prompt(image1, prompt, i, max_retries)
|
331 |
-
|
332 |
if result_img is not None:
|
333 |
results.append(result_img)
|
334 |
statuses.append(f"์ด๋ฏธ์ง #{i+1}: {status}")
|
@@ -337,20 +186,136 @@ def generate_multiple_images(image1, prompt, progress=gr.Progress()):
|
|
337 |
results.append(None)
|
338 |
statuses.append(f"์ด๋ฏธ์ง #{i+1} ์์ฑ ์คํจ: {status}")
|
339 |
prompts.append(f"์ด๋ฏธ์ง #{i+1}: {final_prompt}")
|
340 |
-
|
341 |
time.sleep(1)
|
342 |
-
|
343 |
progress(1.0, desc="์ด๋ฏธ์ง ์์ฑ ์๋ฃ!")
|
344 |
-
|
345 |
while len(results) < 4:
|
346 |
results.append(None)
|
347 |
-
|
348 |
combined_status = "\n".join(statuses)
|
349 |
combined_prompts = "\n".join(prompts)
|
350 |
-
|
351 |
return results[0], results[1], results[2], results[3], combined_status, combined_prompts
|
352 |
|
353 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
354 |
def get_selected_background_info(bg_type, simple, studio, nature, indoor, abstract):
|
355 |
if bg_type == "์ฌํ ๋ฐฐ๊ฒฝ":
|
356 |
return {
|
@@ -389,175 +354,185 @@ def get_selected_background_info(bg_type, simple, studio, nature, indoor, abstra
|
|
389 |
"english": "white background"
|
390 |
}
|
391 |
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
# ์คํ๋์ค ๋ฐฐ๊ฒฝ ์ ํ ๋๋กญ๋ค์ด
|
425 |
-
studio_dropdown = gr.Dropdown(
|
426 |
-
choices=list(STUDIO_BACKGROUNDS.keys()),
|
427 |
-
value=list(STUDIO_BACKGROUNDS.keys())[0] if STUDIO_BACKGROUNDS else None,
|
428 |
-
label="์คํ๋์ค ๋ฐฐ๊ฒฝ ์ ํ",
|
429 |
-
visible=False,
|
430 |
-
interactive=True
|
431 |
-
)
|
432 |
-
|
433 |
-
# ์์ฐ ํ๊ฒฝ ์ ํ ๋๋กญ๋ค์ด
|
434 |
-
nature_dropdown = gr.Dropdown(
|
435 |
-
choices=list(NATURE_BACKGROUNDS.keys()),
|
436 |
-
value=list(NATURE_BACKGROUNDS.keys())[0] if NATURE_BACKGROUNDS else None,
|
437 |
-
label="์์ฐ ํ๊ฒฝ ์ ํ",
|
438 |
-
visible=False,
|
439 |
-
interactive=True
|
440 |
-
)
|
441 |
-
|
442 |
-
# ์ค๋ด ํ๊ฒฝ ์ ํ ๋๋กญ๋ค์ด
|
443 |
-
indoor_dropdown = gr.Dropdown(
|
444 |
-
choices=list(INDOOR_BACKGROUNDS.keys()),
|
445 |
-
value=list(INDOOR_BACKGROUNDS.keys())[0] if INDOOR_BACKGROUNDS else None,
|
446 |
-
label="์ค๋ด ํ๊ฒฝ ์ ํ",
|
447 |
-
visible=False,
|
448 |
-
interactive=True
|
449 |
-
)
|
450 |
-
|
451 |
-
# ์ถ์/ํน์ ๋ฐฐ๊ฒฝ ์ ํ ๋๋กญ๋ค์ด
|
452 |
-
abstract_dropdown = gr.Dropdown(
|
453 |
-
choices=list(ABSTRACT_BACKGROUNDS.keys()),
|
454 |
-
value=list(ABSTRACT_BACKGROUNDS.keys())[0] if ABSTRACT_BACKGROUNDS else None,
|
455 |
-
label="์ถ์/ํน์ ๋ฐฐ๊ฒฝ ์ ํ",
|
456 |
-
visible=False,
|
457 |
-
interactive=True
|
458 |
-
)
|
459 |
-
|
460 |
-
# ์ถ๊ฐ ์์ฒญ์ฌํญ
|
461 |
-
additional_info = gr.Textbox(
|
462 |
-
label="์ถ๊ฐ ์์ฒญ์ฌํญ (์ ํ์ฌํญ)",
|
463 |
-
placeholder="์: ๊ณ ๊ธ์ค๋ฌ์ด ๋๋, ๋ฐ์ ์กฐ๋ช
, ์์ฐ์ค๋ฌ์ด ๋ณด์กฐ์ ์ธ ๊ฐ์ฒด๋ฅผ ์ถ๊ฐํด์ฃผ์ธ์ ๋ฑ",
|
464 |
-
lines=2,
|
465 |
-
interactive=True
|
466 |
-
)
|
467 |
|
468 |
-
|
469 |
-
|
470 |
-
|
471 |
-
|
472 |
-
|
473 |
-
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
with gr.
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
-
|
492 |
-
|
493 |
-
|
494 |
-
|
495 |
-
|
496 |
-
|
497 |
-
|
498 |
-
|
499 |
-
|
500 |
-
|
501 |
-
|
502 |
-
|
503 |
-
|
504 |
-
|
505 |
-
|
506 |
-
|
507 |
-
|
508 |
-
|
509 |
-
|
510 |
-
|
511 |
-
|
512 |
-
|
513 |
-
|
514 |
-
|
515 |
-
|
516 |
-
|
517 |
-
|
518 |
-
|
519 |
-
|
520 |
-
|
521 |
-
|
522 |
-
|
523 |
-
|
524 |
-
|
525 |
-
|
526 |
-
|
527 |
-
|
528 |
-
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
|
533 |
-
|
534 |
-
|
535 |
-
|
536 |
-
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
|
542 |
-
|
543 |
-
|
544 |
-
|
545 |
-
|
546 |
-
|
547 |
-
|
548 |
-
|
549 |
-
|
550 |
-
|
551 |
-
|
552 |
-
|
553 |
-
|
554 |
-
|
555 |
-
|
556 |
-
|
557 |
-
|
558 |
-
|
559 |
-
|
560 |
-
|
561 |
-
|
562 |
-
|
563 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import tempfile
|
|
|
|
|
3 |
import logging
|
4 |
import re
|
5 |
import time
|
6 |
import json
|
7 |
+
from PIL import Image
|
8 |
+
import gradio as gr
|
9 |
from google import genai
|
10 |
from google.genai import types
|
11 |
+
import google.generativeai as genai_generative
|
12 |
from dotenv import load_dotenv
|
13 |
|
14 |
load_dotenv()
|
15 |
|
16 |
+
# ------------------- ๋ก๊น
์ค์ -------------------
|
17 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
18 |
logger = logging.getLogger(__name__)
|
19 |
|
20 |
+
# ------------------- ๊ธฐ๋ณธ ์ด๋ฏธ์ง ์์ฑ ๊ด๋ จ ํจ์ (๊ธฐ๋ณธ์ฝ๋) -------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
def save_binary_file(file_name, data):
|
22 |
with open(file_name, "wb") as f:
|
23 |
f.write(data)
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
def translate_prompt_to_english(prompt):
|
26 |
if not re.search("[๊ฐ-ํฃ]", prompt):
|
27 |
return prompt
|
|
|
28 |
prompt = prompt.replace("#1", "IMAGE_TAG_ONE")
|
|
|
29 |
try:
|
30 |
+
api_key = os.environ.get("GEMINI_API_KEY")
|
31 |
+
if not api_key:
|
32 |
+
logger.error("Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
33 |
+
prompt = prompt.replace("IMAGE_TAG_ONE", "#1")
|
34 |
+
return prompt
|
35 |
+
client = genai.Client(api_key=api_key)
|
36 |
translation_prompt = f"""
|
37 |
Translate the following Korean text to English:
|
38 |
|
|
|
41 |
IMPORTANT: The token IMAGE_TAG_ONE is a special tag
|
42 |
and must be preserved exactly as is in your translation. Do not translate this token.
|
43 |
"""
|
|
|
44 |
logger.info(f"Translation prompt: {translation_prompt}")
|
45 |
+
response = client.models.generate_content(
|
46 |
model="gemini-2.0-flash",
|
47 |
contents=[translation_prompt],
|
48 |
+
config=types.GenerateContentConfig(
|
49 |
+
response_modalities=['Text'],
|
50 |
temperature=0.2,
|
51 |
top_p=0.95,
|
52 |
top_k=40,
|
53 |
max_output_tokens=512
|
54 |
)
|
55 |
)
|
56 |
+
translated_text = ""
|
57 |
+
for part in response.candidates[0].content.parts:
|
58 |
+
if hasattr(part, 'text') and part.text:
|
59 |
+
translated_text += part.text
|
60 |
if translated_text.strip():
|
61 |
translated_text = translated_text.replace("IMAGE_TAG_ONE", "#1")
|
62 |
logger.info(f"Translated text: {translated_text.strip()}")
|
|
|
70 |
prompt = prompt.replace("IMAGE_TAG_ONE", "#1")
|
71 |
return prompt
|
72 |
|
73 |
+
def preprocess_prompt(prompt, image1):
|
74 |
+
has_img1 = image1 is not None
|
75 |
+
if "#1" in prompt and not has_img1:
|
76 |
+
prompt = prompt.replace("#1", "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง(์์)")
|
77 |
+
else:
|
78 |
+
prompt = prompt.replace("#1", "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง")
|
79 |
+
prompt += " ์ด๋ฏธ์ง๋ฅผ ์์ฑํด์ฃผ์ธ์. ์ด๋ฏธ์ง์ ํ
์คํธ๋ ๊ธ์๋ฅผ ํฌํจํ์ง ๋ง์ธ์."
|
80 |
+
return prompt
|
81 |
+
|
82 |
def generate_with_images(prompt, images, variation_index=0):
|
83 |
try:
|
84 |
+
api_key = os.environ.get("GEMINI_API_KEY")
|
85 |
+
if not api_key:
|
86 |
return None, "API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ๋ณ์๋ฅผ ํ์ธํด์ฃผ์ธ์."
|
87 |
+
client = genai.Client(api_key=api_key)
|
88 |
logger.info(f"Gemini API ์์ฒญ ์์ - ํ๋กฌํํธ: {prompt}, ๋ณํ ์ธ๋ฑ์ค: {variation_index}")
|
|
|
89 |
variation_suffixes = [
|
90 |
" Create this as the first variation. Do not add any text, watermarks, or labels to the image.",
|
91 |
" Create this as the second variation with more vivid colors. Do not add any text, watermarks, or labels to the image.",
|
92 |
" Create this as the third variation with a more creative style. Do not add any text, watermarks, or labels to the image.",
|
93 |
" Create this as the fourth variation with enhanced details. Do not add any text, watermarks, or labels to the image."
|
94 |
]
|
|
|
95 |
if variation_index < len(variation_suffixes):
|
96 |
prompt = prompt + variation_suffixes[variation_index]
|
97 |
else:
|
98 |
prompt = prompt + " Do not add any text, watermarks, or labels to the image."
|
|
|
99 |
contents = [prompt]
|
100 |
for idx, img in enumerate(images, 1):
|
101 |
if img is not None:
|
|
|
102 |
contents.append(img)
|
103 |
logger.info(f"์ด๋ฏธ์ง #{idx} ์ถ๊ฐ๋จ")
|
104 |
+
response = client.models.generate_content(
|
|
|
105 |
model="gemini-2.0-flash-exp-image-generation",
|
106 |
contents=contents,
|
107 |
+
config=types.GenerateContentConfig(
|
108 |
+
response_modalities=['Text', 'Image'],
|
109 |
temperature=1,
|
110 |
top_p=0.95,
|
111 |
top_k=40,
|
112 |
max_output_tokens=8192
|
113 |
)
|
114 |
)
|
|
|
|
|
115 |
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as tmp:
|
116 |
temp_path = tmp.name
|
117 |
result_text = ""
|
118 |
image_found = False
|
|
|
119 |
for part in response.candidates[0].content.parts:
|
120 |
if hasattr(part, 'text') and part.text:
|
121 |
result_text += part.text
|
|
|
124 |
save_binary_file(temp_path, part.inline_data.data)
|
125 |
image_found = True
|
126 |
logger.info("์๋ต์์ ์ด๋ฏธ์ง ์ถ์ถ ์ฑ๊ณต")
|
|
|
127 |
if not image_found:
|
128 |
return None, f"API์์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ์๋ต ํ
์คํธ: {result_text}"
|
|
|
129 |
result_img = Image.open(temp_path)
|
130 |
if result_img.mode == "RGBA":
|
131 |
+
result_img = result_img.convert("RGB")
|
|
|
|
|
132 |
result_img.save(temp_path, format="JPEG", quality=95)
|
|
|
|
|
133 |
return temp_path, f"์ด๋ฏธ์ง๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ๋์์ต๋๋ค. {result_text}"
|
134 |
except Exception as e:
|
135 |
logger.exception("์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:")
|
|
|
138 |
def process_images_with_prompt(image1, prompt, variation_index=0, max_retries=3):
|
139 |
retry_count = 0
|
140 |
last_error = None
|
|
|
141 |
while retry_count < max_retries:
|
142 |
try:
|
143 |
images = [image1]
|
144 |
valid_images = [img for img in images if img is not None]
|
145 |
if not valid_images:
|
146 |
return None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.", ""
|
|
|
147 |
if prompt and prompt.strip():
|
148 |
+
processed_prompt = preprocess_prompt(prompt, image1)
|
149 |
+
if re.search("[๊ฐ-ํฃ]", processed_prompt):
|
150 |
+
final_prompt = translate_prompt_to_english(processed_prompt)
|
151 |
+
else:
|
152 |
+
final_prompt = processed_prompt
|
153 |
else:
|
154 |
final_prompt = "Please creatively transform this image into a more vivid and artistic version. Do not include any text or watermarks in the generated image."
|
155 |
logger.info("Default prompt generated for single image")
|
|
|
156 |
result_img, status = generate_with_images(final_prompt, valid_images, variation_index)
|
157 |
if result_img is not None:
|
158 |
return result_img, status, final_prompt
|
|
|
166 |
retry_count += 1
|
167 |
logger.exception(f"์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์, ์ฌ์๋ {retry_count}/{max_retries}:")
|
168 |
time.sleep(1)
|
|
|
169 |
return None, f"์ต๋ ์ฌ์๋ ํ์({max_retries}ํ) ์ด๊ณผ ํ ์คํจ: {last_error}", prompt
|
170 |
|
171 |
def generate_multiple_images(image1, prompt, progress=gr.Progress()):
|
172 |
results = []
|
173 |
statuses = []
|
174 |
prompts = []
|
|
|
175 |
num_images = 4
|
176 |
max_retries = 3
|
|
|
177 |
progress(0, desc="์ด๋ฏธ์ง ์์ฑ ์ค๋น ์ค...")
|
|
|
178 |
for i in range(num_images):
|
179 |
progress((i / num_images), desc=f"{i+1}/{num_images} ์ด๋ฏธ์ง ์์ฑ ์ค...")
|
180 |
result_img, status, final_prompt = process_images_with_prompt(image1, prompt, i, max_retries)
|
|
|
181 |
if result_img is not None:
|
182 |
results.append(result_img)
|
183 |
statuses.append(f"์ด๋ฏธ์ง #{i+1}: {status}")
|
|
|
186 |
results.append(None)
|
187 |
statuses.append(f"์ด๋ฏธ์ง #{i+1} ์์ฑ ์คํจ: {status}")
|
188 |
prompts.append(f"์ด๋ฏธ์ง #{i+1}: {final_prompt}")
|
|
|
189 |
time.sleep(1)
|
|
|
190 |
progress(1.0, desc="์ด๋ฏธ์ง ์์ฑ ์๋ฃ!")
|
|
|
191 |
while len(results) < 4:
|
192 |
results.append(None)
|
|
|
193 |
combined_status = "\n".join(statuses)
|
194 |
combined_prompts = "\n".join(prompts)
|
|
|
195 |
return results[0], results[1], results[2], results[3], combined_status, combined_prompts
|
196 |
|
197 |
+
# ------------------- ์ํ ํ๋กฌํํธ ์์ฑ ๊ด๋ จ ํจ์ (์ฐธ์กฐ์ฝ๋) -------------------
|
198 |
+
|
199 |
+
# ๋ฐฐ๊ฒฝ JSON ํ์ผ ๊ฒฝ๋ก ์ค์
|
200 |
+
BACKGROUNDS_DIR = "./background"
|
201 |
+
if not os.path.exists(BACKGROUNDS_DIR):
|
202 |
+
os.makedirs(BACKGROUNDS_DIR)
|
203 |
+
logger.info(f"๋ฐฐ๊ฒฝ ๋๋ ํ ๋ฆฌ๋ฅผ ์์ฑํ์ต๋๋ค: {BACKGROUNDS_DIR}")
|
204 |
+
else:
|
205 |
+
logger.info(f"๋ฐฐ๊ฒฝ ๋๋ ํ ๋ฆฌ๊ฐ ์ด๋ฏธ ์กด์ฌํฉ๋๋ค: {BACKGROUNDS_DIR}")
|
206 |
+
|
207 |
+
def load_background_json(filename):
|
208 |
+
file_path = os.path.join(BACKGROUNDS_DIR, filename)
|
209 |
+
try:
|
210 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
211 |
+
data = json.load(f)
|
212 |
+
logger.info(f"{filename} ํ์ผ์ ์ฑ๊ณต์ ์ผ๋ก ๋ก๋ํ์ต๋๋ค. {len(data)} ํญ๋ชฉ ํฌํจ.")
|
213 |
+
return data
|
214 |
+
except Exception as e:
|
215 |
+
logger.warning(f"{filename} ํ์ผ ๋ก๋ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}. ๊ธฐ๋ณธ๊ฐ์ ์ฌ์ฉํฉ๋๋ค.")
|
216 |
+
return {}
|
217 |
+
|
218 |
+
SIMPLE_BACKGROUNDS = load_background_json("simple_backgrounds.json")
|
219 |
+
STUDIO_BACKGROUNDS = load_background_json("studio_backgrounds.json")
|
220 |
+
NATURE_BACKGROUNDS = load_background_json("nature_backgrounds.json")
|
221 |
+
INDOOR_BACKGROUNDS = load_background_json("indoor_backgrounds.json")
|
222 |
+
ABSTRACT_BACKGROUNDS = load_background_json("abstract_backgrounds.json")
|
223 |
+
|
224 |
+
# ๋ฐฐ๊ฒฝ์ด ๋ก๋๋์ง ์์ ๊ฒฝ์ฐ ๊ธฐ๋ณธ๊ฐ ์ค์
|
225 |
+
if not SIMPLE_BACKGROUNDS:
|
226 |
+
SIMPLE_BACKGROUNDS = {"ํ์ดํธ ๋ฐฐ๊ฒฝ": "white background"}
|
227 |
+
if not STUDIO_BACKGROUNDS:
|
228 |
+
STUDIO_BACKGROUNDS = {"์ ํ ์ฌ์ง ์คํ๋์ค": "product photography studio"}
|
229 |
+
if not NATURE_BACKGROUNDS:
|
230 |
+
NATURE_BACKGROUNDS = {"์ด๋ ํด๋ณ": "tropical beach"}
|
231 |
+
if not INDOOR_BACKGROUNDS:
|
232 |
+
INDOOR_BACKGROUNDS = {"๋ชจ๋ ๋ฆฌ๋น๋ฃธ": "modern living room"}
|
233 |
+
if not ABSTRACT_BACKGROUNDS:
|
234 |
+
ABSTRACT_BACKGROUNDS = {"๋ค์จ ์กฐ๋ช
": "neon lights"}
|
235 |
+
|
236 |
+
def generate_system_instruction():
|
237 |
+
return """๋น์ ์ ์ํ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ ๋ณ๊ฒฝํ๊ธฐ ์ํ ๊ณ ํ์ง ํ๋กฌํํธ๋ฅผ ์์ฑํ๋ ์ ๋ฌธ๊ฐ์
๋๋ค.
|
238 |
+
์ฌ์ฉ์๊ฐ ์ ๊ณตํ๋ ์ํ๋ช
, ๋ฐฐ๊ฒฝ ์ ํ, ์ถ๊ฐ ์์ฒญ์ฌํญ์ ๋ฐํ์ผ๋ก ๋ฏธ๋์ ๋(Midjourney)์ ์ฌ์ฉํ ์ ์๋
|
239 |
+
์์ธํ๊ณ ์ ๋ฌธ์ ์ธ ํ๋กฌํํธ๋ฅผ ์์ด๋ก ์์ฑํด์ฃผ์ธ์.
|
240 |
+
๋ค์ ๊ฐ์ด๋๋ผ์ธ์ ๋ฐ๋์ ๋ฐ๋ผ์ผ ํฉ๋๋ค:
|
241 |
+
1. ์ํ์ "#1"๋ก ์ง์ ํ์ฌ ์ฐธ์กฐํฉ๋๋ค. (์: "skincare tube (#1)")
|
242 |
+
2. *** ๋งค์ฐ ์ค์: ์ํ์ ์๋ ํน์ฑ(๋์์ธ, ์์, ํํ, ๋ก๊ณ , ํจํค์ง ๋ฑ)์ ์ด๋ค ์ํฉ์์๋ ์ ๋ ๋ณ๊ฒฝํ์ง ์์ต๋๋ค. ***
|
243 |
+
3. *** ์ํ์ ๋ณธ์ง์ ํน์ฑ์ ์ ์งํ๋, ์์ฐ์ค๋ฌ์ด ํ๊ฒฝ ํตํฉ์ ์ํ ์กฐ๋ช
๊ณผ ๊ทธ๋ฆผ์๋ ํ์ฉํฉ๋๋ค: ***
|
244 |
+
- ์ํ ์์ฒด์ ์์, ๋์์ธ, ํํ, ํ
์ค์ฒ๋ ์ ๋ ์์ ํ์ง ์์ต๋๋ค.
|
245 |
+
- ํ๊ฒฝ๊ณผ ์์ฐ์ค๋ฝ๊ฒ ์ด์ธ๋ฆฌ๋ ๊ทธ๋ฆผ์, ์ฃผ๋ณ ์กฐ๋ช
ํจ๊ณผ๋ ํ์ฉ๋ฉ๋๋ค.
|
246 |
+
- ์ํ์ ๋ฌผ๋ฐฉ์ธ, ์์ถ, ๊ธ, ์๊ณผ ๊ฐ์ ์ถ๊ฐ ์์๋ ๋ฌผ๋ฆฌ์ ํจ๊ณผ๋ ์ ์ฉํ์ง ์์ต๋๋ค.
|
247 |
+
- ํ๊ฒฝ์ ์ด์ธ๋ฆฌ๋ ์์ฐ์ค๋ฌ์ด ๋น ๋ฐ์ฌ, ์ฃผ๋ณ ์กฐ๋ช
, ๊ทธ๋ฆผ์๋ ์ฌ์ค์ ํตํฉ๊ฐ์ ์ํด ์ ์ฉํ ์ ์์ต๋๋ค.
|
248 |
+
4. ์ด๋ฏธ์ง ๋น์จ์ ์ ํํ 1:1(์ ์ฌ๊ฐํ) ํ์์ผ๋ก ์ง์ ํฉ๋๋ค. ํ๋กฌํํธ์ "square format", "1:1 ratio" ๋๋ "aspect ratio 1:1"์ ๋ช
์์ ์ผ๋ก ํฌํจํฉ๋๋ค.
|
249 |
+
5. ์ํ์ ๋ฐ๋์ ์ ์ฌ๊ฐํ ๊ตฌ๋์ ์ ์ค์์ ๋ฐฐ์น๋์ด์ผ ํฉ๋๋ค.
|
250 |
+
6. ์ํ์ ์ด๋ฏธ์ง์ ์ฃผ์ ์ด์ ์ผ๋ก ๋ถ๊ฐ์ํค๊ณ , ์ํ์ ๋น์จ์ด ์ ์ฒด ์ด๋ฏธ์ง์์ ํฌ๊ฒ ์ฐจ์งํ๋๋ก ํฉ๋๋ค.
|
251 |
+
7. ์ํ ์ด๋ฏธ์ง ์ปท์์(#1)์ ๊ธฐ๋ณธ ํํ์ ์์์ ์ ์งํ๋ฉด์, ์ ํํ ํ๊ฒฝ์ ์์ฐ์ค๋ฝ๊ฒ ํตํฉ๋๋๋ก ํฉ๋๋ค.
|
252 |
+
8. ๊ณ ๊ธ์ค๋ฌ์ด ์์
์ ์ด๋ฏธ์ง๋ฅผ ์ํ ๋ค์ ํ๊ฒฝ ์์๋ค์ ํฌํจํ์ธ์:
|
253 |
+
- ์ํ๊ณผ ์ด์ธ๋ฆฌ๋ ์ฃผ๋ณ ํ๊ฒฝ/๋ฐฐ๊ฒฝ ์์๋ฅผ ์ถ๊ฐํฉ๋๋ค. ์๋ฅผ ๋ค์ด, ํ์ฅํ ์ฃผ๋ณ์ ๊ฝ์ด๋ ํ๋ธ, ์๋ฃ ์ ํ ์์ ๊ณผ์ผ, ์ ์์ ํ ๊ทผ์ฒ์ ํ๋์ ์ํ ๋ฑ.
|
254 |
+
- ํ๊ฒฝ์ ์กฐ๋ช
ํจ๊ณผ(๋ฆผ ๋ผ์ดํธ, ๋ฐฑ๋ผ์ดํธ, ์ํํธ๋ฐ์ค ๋ฑ)๋ฅผ ์ค๋ช
ํฉ๋๋ค.
|
255 |
+
- ์ํ์ด ํ๊ฒฝ์ ์์ฐ์ค๋ฝ๊ฒ ์กด์ฌํ๋ ๊ฒ์ฒ๋ผ ๋ณด์ด๋๋ก ์ ์ ํ ๊ทธ๋ฆผ์์ ๋น ํํ์ ํฌํจํฉ๋๋ค.
|
256 |
+
- ์ํ์ ์ฉ๋๋ ์ฅ์ ์ ๊ฐ์ ์ ์ผ๋ก ์์ํ๋ ๋ฐฐ๊ฒฝ ์์๋ฅผ ํฌํจํฉ๋๋ค.
|
257 |
+
- ํ๋กํ์
๋ํ ์์
์ฌ์ง ํจ๊ณผ(์ ํ์ ํผ์ฌ๊ณ ์ฌ๋, ์ํํธ ํฌ์ปค์ค, ์คํ๋์ค ์กฐ๋ช
๋ฑ)๋ฅผ ๋ช
์ํฉ๋๋ค.
|
258 |
+
9. ํ๋กฌํํธ์ ๋ค์ ์์๋ค์ ๋ช
์์ ์ผ๋ก ํฌํจํ์ธ์:
|
259 |
+
- "highly detailed commercial photography"
|
260 |
+
- "award-winning product photography"
|
261 |
+
- "professional advertising imagery"
|
262 |
+
- "studio quality"
|
263 |
+
- "magazine advertisement quality"
|
264 |
+
10. ๋ฐฐ๊ฒฝ ํ๊ฒฝ ์์๋ฅผ ์ํ ์นดํ
๊ณ ๋ฆฌ์ ๋ง๊ฒ ์ ํํฉ๋๋ค:
|
265 |
+
- ์คํจ์ผ์ด ์ ํ: ๊นจ๋ํ ์์ค ์ ๋ฐ, ์ฐ์ํ ํ์ฅ๋, ์คํ ๊ฐ์ ํ๊ฒฝ ๋ฑ
|
266 |
+
- ์๋ฃ ์ ํ: ์ธ๋ จ๋ ํ
์ด๋ธ, ํํฐ ํ๊ฒฝ, ์ผ์ธ ํผํฌ๋ ์ฅ๋ฉด ๋ฑ
|
267 |
+
- ์ ์ ์ ํ: ์ธ๋ จ๋ ์์
๊ณต๊ฐ, ํ๋์ ์ธ ๊ฑฐ์ค, ๋ฏธ๋๋ฉํ ์ฑ
์ ๋ฑ
|
268 |
+
- ํจ์
/์๋ฅ: ์ธ๋ จ๋ ์ผ๋ฃธ, ๋์ ๊ฑฐ๋ฆฌ, ์๋ ๊ฐ์คํ ๋ผ์ดํ์คํ์ผ ํ๊ฒฝ ๋ฑ
|
269 |
+
- ์ํ ์ ํ: ๊น๋ํ ์ฃผ๋ฐฉ, ์ํ, ์๋ฆฌ ํ๊ฒฝ ๋ฑ
|
270 |
+
11. ์ฌ์ฉ์๊ฐ ์ ๊ณตํ ๊ตฌ์ฒด์ ์ธ ๋ฐฐ๊ฒฝ๊ณผ ์ถ๊ฐ ์์ฒญ์ฌํญ์ ์ ํํ ๋ฐ์ํฉ๋๋ค.
|
271 |
+
12. ํ๋กฌํํธ๋ ๋ฏธ๋์ ๋ AI์ ์ต์ ํ๋์ด์ผ ํฉ๋๋ค.
|
272 |
+
13. ํ๋กฌํํธ ๋์ "--ar 1:1 --s 750 --q 2" ํ๋ผ๋ฏธํฐ๋ฅผ ์ถ๊ฐํ์ฌ ๋ฏธ๋์ ๋์์ ๊ณ ํ์ง ์ ์ฌ๊ฐํ ๋น์จ์ ๊ฐ์ ํฉ๋๋ค.
|
273 |
+
์ถ๋ ฅ ํ์์ ์์ด๋ก ๋ ๋จ์ผ ๋จ๋ฝ์ ์์ธํ ํ๋กฌํํธ์ฌ์ผ ํ๋ฉฐ, ๋์ ๋ฏธ๋์ ๋ ํ๋ผ๋ฏธํฐ๊ฐ ํฌํจ๋์ด์ผ ํฉ๋๋ค.
|
274 |
+
"""
|
275 |
+
|
276 |
+
def generate_prompt_with_gemini(product_name, background_info, additional_info=""):
|
277 |
+
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", "")
|
278 |
+
if not GEMINI_API_KEY:
|
279 |
+
return "Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ ๋ณ์ GEMINI_API_KEY๋ฅผ ์ค์ ํ๊ฑฐ๋ ์ฝ๋์ ์ง์ ์
๋ ฅํ์ธ์."
|
280 |
+
try:
|
281 |
+
genai_generative.configure(api_key=GEMINI_API_KEY)
|
282 |
+
prompt_request = f"""
|
283 |
+
์ํ๋ช
: {product_name}
|
284 |
+
๋ฐฐ๊ฒฝ ์ ํ: {background_info.get('english', 'studio')}
|
285 |
+
๋ฐฐ๊ฒฝ ์นดํ
๊ณ ๋ฆฌ: {background_info.get('category', '')}
|
286 |
+
๋ฐฐ๊ฒฝ ์ด๋ฆ: {background_info.get('name', '')}
|
287 |
+
์ถ๊ฐ ์์ฒญ์ฌํญ: {additional_info}
|
288 |
+
์ค์ ์๊ตฌ์ฌํญ:
|
289 |
+
1. ์ํ์ด ํฌ๊ฒ ๋ถ๊ฐ๋๊ณ ์ด๋ฏธ์ง์์ ์ค์ฌ์ ์ธ ์์น๋ฅผ ์ฐจ์งํ๋๋ก ํ๋กฌํํธ๋ฅผ ์์ฑํด์ฃผ์ธ์.
|
290 |
+
2. ์ด๋ฏธ์ง๋ ์ ํํ 1:1 ๋น์จ(์ ์ฌ๊ฐํ)์ด์ด์ผ ํฉ๋๋ค.
|
291 |
+
3. ์ํ์ ์ ์ฌ๊ฐํ ํ๋ ์์ ์ ์ค์์ ์์นํด์ผ ํฉ๋๋ค.
|
292 |
+
4. ์ํ์ ๋์์ธ, ์์, ํํ, ๋ก๊ณ ๋ฑ ๋ณธ์ง์ ํน์ฑ์ ์ ๋ ์์ ํ์ง ๋ง์ธ์.
|
293 |
+
5. ํ๊ฒฝ๊ณผ์ ์์ฐ์ค๋ฌ์ด ํตํฉ์ ์ํ ์กฐ๋ช
ํจ๊ณผ์ ๊ทธ๋ฆผ์๋ ํฌํจํด์ฃผ์ธ์.
|
294 |
+
6. ์ํ์ ๋ ๋๋ณด์ด๊ฒ ํ๋ ๋ฐฐ๊ฒฝ ํ๊ฒฝ์ ์ค๋ช
ํด์ฃผ์ธ์.
|
295 |
+
7. ๊ณ ๊ธ์ค๋ฌ์ด ์์
๊ด๊ณ ํ์ง์ ์ด๋ฏธ๏ฟฝ๏ฟฝ๏ฟฝ๊ฐ ๋๋๋ก ํ๊ฒฝ ์ค๋ช
์ ํด์ฃผ์ธ์.
|
296 |
+
8. ํ๋กฌํํธ ๋์ ๋ฏธ๋์ ๋ ํ๋ผ๋ฏธํฐ "--ar 1:1 --s 750 --q 2"๋ฅผ ์ถ๊ฐํด์ฃผ์ธ์.
|
297 |
+
ํ๊ตญ์ด ์
๋ ฅ ๋ด์ฉ์ ์์ด๋ก ์ ์ ํ ๋ฒ์ญํ์ฌ ๋ฐ์ํด์ฃผ์ธ์.
|
298 |
+
"""
|
299 |
+
model = genai_generative.GenerativeModel(
|
300 |
+
'gemini-2.0-flash',
|
301 |
+
system_instruction=generate_system_instruction()
|
302 |
+
)
|
303 |
+
response = model.generate_content(
|
304 |
+
prompt_request,
|
305 |
+
generation_config=genai_generative.types.GenerationConfig(
|
306 |
+
temperature=0.7,
|
307 |
+
top_p=0.95,
|
308 |
+
top_k=64,
|
309 |
+
max_output_tokens=1024,
|
310 |
+
)
|
311 |
+
)
|
312 |
+
response_text = response.text.strip()
|
313 |
+
if "--ar 1:1" not in response_text:
|
314 |
+
response_text = response_text.rstrip(".") + ". --ar 1:1 --s 750 --q 2"
|
315 |
+
return response_text
|
316 |
+
except Exception as e:
|
317 |
+
return f"ํ๋กฌํํธ ์์ฑ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
318 |
+
|
319 |
def get_selected_background_info(bg_type, simple, studio, nature, indoor, abstract):
|
320 |
if bg_type == "์ฌํ ๋ฐฐ๊ฒฝ":
|
321 |
return {
|
|
|
354 |
"english": "white background"
|
355 |
}
|
356 |
|
357 |
+
def generate_product_prompt_output(image, bg_type, simple, studio, nature, indoor, abstract, product_name, additional_info):
|
358 |
+
if image is None:
|
359 |
+
gr.Warning("์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.")
|
360 |
+
return "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.", None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ ํ ํ๋กฌํํธ๋ฅผ ์์ฑํด์ฃผ์ธ์."
|
361 |
+
product_name = product_name.strip() or "์ ํ"
|
362 |
+
background_info = get_selected_background_info(bg_type, simple, studio, nature, indoor, abstract)
|
363 |
+
try:
|
364 |
+
prompt = generate_prompt_with_gemini(product_name, background_info, additional_info)
|
365 |
+
if "Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค" in prompt:
|
366 |
+
gr.Warning("Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํค๋ฅผ ๋ฐ๊ธ๋ฐ์ ์ค์ ํด์ฃผ์ธ์.")
|
367 |
+
prompt = """
|
368 |
+
[Gemini API ํค ๋๋ฝ]
|
369 |
+
API ํค ์ค์ ๋ฐฉ๋ฒ:
|
370 |
+
1. ํ๊ฒฝ ๋ณ์: export GEMINI_API_KEY="your-api-key"
|
371 |
+
2. ์ฝ๋ ๋ด ์ง์ ์
๋ ฅ: GEMINI_API_KEY = "your-api-key"
|
372 |
+
ํค ๋ฐ๊ธ: https://makersuite.google.com/
|
373 |
+
"""
|
374 |
+
return prompt, image, "API ํค๋ฅผ ์ค์ ํด์ผ ํฉ๋๋ค."
|
375 |
+
preview = f"""
|
376 |
+
<div style="padding:10px; border:1px solid #ddd; border-radius:8px; margin-top:10px;">
|
377 |
+
<h3>ํ๋กฌํํธ ์์ฝ</h3>
|
378 |
+
<p><strong>์ด ๊ธธ์ด:</strong> {len(prompt)} ๊ธ์</p>
|
379 |
+
<p><strong>๋ฐฐ๊ฒฝ:</strong> {background_info['category']} > {background_info['name']}</p>
|
380 |
+
<p><strong>๋ฏธ๋์ ๋ ํ๋ผ๋ฏธํฐ:</strong> {" ".join([param for param in ["--ar 1:1", "--s 750", "--q 2"] if param in prompt])}</p>
|
381 |
+
</div>
|
382 |
+
"""
|
383 |
+
return prompt, image, preview
|
384 |
+
except Exception as e:
|
385 |
+
error_msg = f"ํ๋กฌํํธ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
386 |
+
gr.Error(error_msg)
|
387 |
+
return error_msg, image, "์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
388 |
|
389 |
+
# ------------------- Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ -------------------
|
390 |
+
|
391 |
+
def create_basic_app():
|
392 |
+
with gr.Blocks() as basic_app:
|
393 |
+
with gr.Column():
|
394 |
+
with gr.Group():
|
395 |
+
with gr.Column():
|
396 |
+
with gr.Row():
|
397 |
+
image1_input = gr.Image(type="pil", label="์ด๋ฏธ์ง ์
๋ก๋", image_mode="RGB")
|
398 |
+
prompt_input = gr.Textbox(
|
399 |
+
lines=3,
|
400 |
+
placeholder="ํ๋กฌํํธ๋ฅผ ์
๋ ฅํ์ธ์. '#1'์ผ๋ก ์
๋ก๋ํ ์ด๋ฏธ์ง๋ฅผ ์ฐธ์กฐํ ์ ์์ต๋๋ค.",
|
401 |
+
label="ํ๋กฌํํธ ์
๋ ฅ"
|
402 |
+
)
|
403 |
+
with gr.Row():
|
404 |
+
submit_single_btn = gr.Button('์ด๋ฏธ์ง ์์ฑ (1์ฅ)')
|
405 |
+
submit_btn = gr.Button('์ด๋ฏธ์ง ์์ฑ (4์ฅ)')
|
406 |
+
with gr.Column():
|
407 |
+
with gr.Row():
|
408 |
+
with gr.Column():
|
409 |
+
output_image1 = gr.Image(label="์ด๋ฏธ์ง #1", type="filepath")
|
410 |
+
output_image3 = gr.Image(label="์ด๋ฏธ์ง #3", type="filepath")
|
411 |
+
with gr.Column():
|
412 |
+
output_image2 = gr.Image(label="์ด๋ฏธ์ง #2", type="filepath")
|
413 |
+
output_image4 = gr.Image(label="์ด๋ฏธ์ง #4", type="filepath")
|
414 |
+
output_text = gr.Textbox(label="๊ฒฐ๊ณผ ์ ๋ณด", lines=2)
|
415 |
+
prompt_display = gr.Textbox(label="์ฌ์ฉ๋ ํ๋กฌํํธ (์์ด)", lines=2)
|
416 |
+
submit_single_btn.click(
|
417 |
+
fn=lambda image1, prompt: process_images_with_prompt(image1, prompt, 0),
|
418 |
+
inputs=[image1_input, prompt_input],
|
419 |
+
outputs=[output_image1, output_text, prompt_display],
|
420 |
+
)
|
421 |
+
submit_btn.click(
|
422 |
+
fn=generate_multiple_images,
|
423 |
+
inputs=[image1_input, prompt_input],
|
424 |
+
outputs=[output_image1, output_image2, output_image3, output_image4, output_text, prompt_display],
|
425 |
+
)
|
426 |
+
return basic_app
|
427 |
+
|
428 |
+
def create_product_prompt_app():
|
429 |
+
with gr.Blocks(title="๊ณ ๊ธ ์ํ ์ด๋ฏธ์ง ๋ฐฐ๊ฒฝ ํ๋กฌํํธ ์์ฑ๊ธฐ") as product_app:
|
430 |
+
gr.Markdown("# ๊ณ ๊ธ ์ํ ์ด๋ฏธ์ง ๋ฐฐ๊ฒฝ ํ๋กฌํํธ ์์ฑ๊ธฐ")
|
431 |
+
gr.Markdown("์ํ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๊ณ ์ต์
์ ์ ํํ๋ฉด ๊ณ ํ์ง ์์
์ฉ ํ๋กฌํํธ๊ฐ ์์ฑ๋ฉ๋๋ค.")
|
432 |
+
with gr.Row():
|
433 |
+
product_name = gr.Textbox(label="์ํ๋ช
(ํ๊ตญ์ด ์
๋ ฅ)", placeholder="์: ์คํจ์ผ์ด ํ๋ธ, ํ
๋ธ๋ฌ ๋ฑ", interactive=True)
|
434 |
+
background_type = gr.Radio(
|
435 |
+
choices=["์ฌํ ๋ฐฐ๊ฒฝ", "์คํ๋์ค ๋ฐฐ๊ฒฝ", "์์ฐ ํ๊ฒฝ", "์ค๋ด ํ๊ฒฝ", "์ถ์/ํน์ ๋ฐฐ๊ฒฝ"],
|
436 |
+
label="๋ฐฐ๊ฒฝ ์ ํ",
|
437 |
+
value="์ฌํ ๋ฐฐ๊ฒฝ"
|
438 |
+
)
|
439 |
+
with gr.Row():
|
440 |
+
with gr.Column(scale=1):
|
441 |
+
image_input = gr.Image(label="์ํ ์ด๋ฏธ์ง ์
๋ก๋", type="pil")
|
442 |
+
simple_dropdown = gr.Dropdown(
|
443 |
+
choices=list(SIMPLE_BACKGROUNDS.keys()),
|
444 |
+
value=list(SIMPLE_BACKGROUNDS.keys())[0] if SIMPLE_BACKGROUNDS else None,
|
445 |
+
label="์ฌํ ๋ฐฐ๊ฒฝ ์ ํ",
|
446 |
+
visible=True,
|
447 |
+
interactive=True
|
448 |
+
)
|
449 |
+
studio_dropdown = gr.Dropdown(
|
450 |
+
choices=list(STUDIO_BACKGROUNDS.keys()),
|
451 |
+
value=list(STUDIO_BACKGROUNDS.keys())[0] if STUDIO_BACKGROUNDS else None,
|
452 |
+
label="์คํ๋์ค ๋ฐฐ๊ฒฝ ์ ํ",
|
453 |
+
visible=False,
|
454 |
+
interactive=True
|
455 |
+
)
|
456 |
+
nature_dropdown = gr.Dropdown(
|
457 |
+
choices=list(NATURE_BACKGROUNDS.keys()),
|
458 |
+
value=list(NATURE_BACKGROUNDS.keys())[0] if NATURE_BACKGROUNDS else None,
|
459 |
+
label="์๏ฟฝ๏ฟฝ๏ฟฝ ํ๊ฒฝ ์ ํ",
|
460 |
+
visible=False,
|
461 |
+
interactive=True
|
462 |
+
)
|
463 |
+
indoor_dropdown = gr.Dropdown(
|
464 |
+
choices=list(INDOOR_BACKGROUNDS.keys()),
|
465 |
+
value=list(INDOOR_BACKGROUNDS.keys())[0] if INDOOR_BACKGROUNDS else None,
|
466 |
+
label="์ค๋ด ํ๊ฒฝ ์ ํ",
|
467 |
+
visible=False,
|
468 |
+
interactive=True
|
469 |
+
)
|
470 |
+
abstract_dropdown = gr.Dropdown(
|
471 |
+
choices=list(ABSTRACT_BACKGROUNDS.keys()),
|
472 |
+
value=list(ABSTRACT_BACKGROUNDS.keys())[0] if ABSTRACT_BACKGROUNDS else None,
|
473 |
+
label="์ถ์/ํน์ ๋ฐฐ๊ฒฝ ์ ํ",
|
474 |
+
visible=False,
|
475 |
+
interactive=True
|
476 |
+
)
|
477 |
+
additional_info = gr.Textbox(
|
478 |
+
label="์ถ๊ฐ ์์ฒญ์ฌํญ (์ ํ์ฌํญ)",
|
479 |
+
placeholder="์: ๊ณ ๊ธ์ค๋ฌ์ด ๋๋, ๋ฐ์ ์กฐ๋ช
, ์์ฐ์ค๋ฌ์ด ๋ณด์กฐ์ ์ธ ๊ฐ์ฒด๋ฅผ ์ถ๊ฐํด์ฃผ์ธ์ ๋ฑ",
|
480 |
+
lines=3,
|
481 |
+
interactive=True
|
482 |
+
)
|
483 |
+
submit_btn = gr.Button("ํ๋กฌํํธ ์์ฑ", variant="primary")
|
484 |
+
with gr.Column(scale=1):
|
485 |
+
prompt_output = gr.Textbox(label="์์ฑ๋ ํ๋กฌํํธ", lines=10)
|
486 |
+
image_preview = gr.Image(label="์
๋ก๋๋ ์ด๋ฏธ์ง (#1)", type="pil")
|
487 |
+
preview_html = gr.HTML("ํ๋กฌํํธ ์์ฑ ์ ์ฌ๊ธฐ์ ๋ฏธ๋ฆฌ๋ณด๊ธฐ๊ฐ ํ์๋ฉ๋๋ค.")
|
488 |
+
def update_dropdowns(bg_type):
|
489 |
+
return {
|
490 |
+
simple_dropdown: gr.update(visible=(bg_type == "์ฌํ ๋ฐฐ๊ฒฝ")),
|
491 |
+
studio_dropdown: gr.update(visible=(bg_type == "์คํ๋์ค ๋ฐฐ๊ฒฝ")),
|
492 |
+
nature_dropdown: gr.update(visible=(bg_type == "์์ฐ ํ๊ฒฝ")),
|
493 |
+
indoor_dropdown: gr.update(visible=(bg_type == "์ค๋ด ํ๊ฒฝ")),
|
494 |
+
abstract_dropdown: gr.update(visible=(bg_type == "์ถ์/ํน์ ๋ฐฐ๊ฒฝ"))
|
495 |
+
}
|
496 |
+
background_type.change(
|
497 |
+
fn=update_dropdowns,
|
498 |
+
inputs=[background_type],
|
499 |
+
outputs=[simple_dropdown, studio_dropdown, nature_dropdown, indoor_dropdown, abstract_dropdown]
|
500 |
+
)
|
501 |
+
def generate_output(image, bg_type, simple, studio, nature, indoor, abstract, product_name, additional_text):
|
502 |
+
return generate_product_prompt_output(image, bg_type, simple, studio, nature, indoor, abstract, product_name, additional_text)
|
503 |
+
submit_btn.click(
|
504 |
+
fn=generate_output,
|
505 |
+
inputs=[
|
506 |
+
image_input,
|
507 |
+
background_type,
|
508 |
+
simple_dropdown,
|
509 |
+
studio_dropdown,
|
510 |
+
nature_dropdown,
|
511 |
+
indoor_dropdown,
|
512 |
+
abstract_dropdown,
|
513 |
+
product_name,
|
514 |
+
additional_info
|
515 |
+
],
|
516 |
+
outputs=[
|
517 |
+
prompt_output,
|
518 |
+
image_preview,
|
519 |
+
preview_html
|
520 |
+
]
|
521 |
+
)
|
522 |
+
return product_app
|
523 |
+
|
524 |
+
def main():
|
525 |
+
basic_app = create_basic_app()
|
526 |
+
product_app = create_product_prompt_app()
|
527 |
+
with gr.Blocks() as demo:
|
528 |
+
gr.Markdown("# ํตํฉ ์ด๋ฏธ์ง ์์ฑ ๋ฐ ํ๋กฌํํธ ์์ฑ ์ฑ")
|
529 |
+
with gr.Tabs():
|
530 |
+
with gr.TabItem("์ผ๋ฐ ์ด๋ฏธ์ง ์์ฑ"):
|
531 |
+
basic_app.render()
|
532 |
+
with gr.TabItem("์ํ ํ๋กฌํํธ ์์ฑ ๋ฐ ์ด๋ฏธ์ง ์์ฑ"):
|
533 |
+
product_app.render()
|
534 |
+
demo.queue()
|
535 |
+
demo.launch()
|
536 |
+
|
537 |
+
if __name__ == "__main__":
|
538 |
+
main()
|