Update app.py
Browse files
app.py
CHANGED
@@ -18,58 +18,11 @@ load_dotenv()
|
|
18 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
19 |
logger = logging.getLogger(__name__)
|
20 |
|
21 |
-
# LLM ์ค์ ๋ฐ ๋ฒ์ญ ํจ์
|
22 |
-
def get_translation(korean_text):
|
23 |
-
"""
|
24 |
-
ํ๊ตญ์ด ํ
์คํธ๋ฅผ ์์ด๋ก ๋ฒ์ญ
|
25 |
-
"""
|
26 |
-
try:
|
27 |
-
api_key = os.environ.get("GEMINI_API_KEY")
|
28 |
-
if not api_key:
|
29 |
-
logger.error("GEMINI_API_KEY๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
30 |
-
return korean_text
|
31 |
-
|
32 |
-
# ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
33 |
-
client = genai.GenerativeModel(
|
34 |
-
model_name="gemini-2.0-flash",
|
35 |
-
generation_config={
|
36 |
-
"temperature": 0.2,
|
37 |
-
"max_output_tokens": 1024,
|
38 |
-
"top_p": 0.9,
|
39 |
-
},
|
40 |
-
system_instruction="You are a professional translator who translates Korean to English accurately.",
|
41 |
-
api_key=api_key
|
42 |
-
)
|
43 |
-
|
44 |
-
# ๋ฒ์ญ ํ๋กฌํํธ
|
45 |
-
translation_prompt = f"""
|
46 |
-
Translate the following Korean text to English accurately:
|
47 |
-
|
48 |
-
{korean_text}
|
49 |
-
|
50 |
-
Provide only the translation, no explanations.
|
51 |
-
"""
|
52 |
-
|
53 |
-
# ๋ฒ์ญ ์์ฒญ
|
54 |
-
response = client.generate_content(translation_prompt)
|
55 |
-
|
56 |
-
# ์๋ต์์ ํ
์คํธ ์ถ์ถ
|
57 |
-
if hasattr(response, 'text'):
|
58 |
-
english_text = response.text.strip()
|
59 |
-
logger.info(f"๋ฒ์ญ ๊ฒฐ๊ณผ: {english_text}")
|
60 |
-
return english_text
|
61 |
-
else:
|
62 |
-
logger.warning("๋ฒ์ญ ์๋ต์ text ์์ฑ์ด ์์ต๋๋ค.")
|
63 |
-
return korean_text
|
64 |
-
except Exception as e:
|
65 |
-
logger.exception(f"๋ฒ์ญ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
66 |
-
return korean_text # ์ค๋ฅ ๋ฐ์ ์ ์๋ณธ ํ๊ตญ์ด ํ๋กฌํํธ ๋ฐํ
|
67 |
-
|
68 |
def save_binary_file(file_name, data):
|
69 |
with open(file_name, "wb") as f:
|
70 |
f.write(data)
|
71 |
|
72 |
-
def preprocess_prompt(prompt, image1
|
73 |
"""
|
74 |
ํ๋กฌํํธ๋ฅผ ์ฒ๋ฆฌํ๊ณ ๊ธฐ๋ฅ ๋ช
๋ น์ ํด์
|
75 |
"""
|
@@ -96,9 +49,11 @@ def preprocess_prompt(prompt, image1=None, image2=None, image3=None):
|
|
96 |
|
97 |
# ๊ธฐ๋ฅ ๋ช
๋ น ํด์
|
98 |
if "1. ์ด๋ฏธ์ง ๋ณ๊ฒฝ" in prompt:
|
|
|
99 |
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ์ฐฝ์์ ์ผ๋ก ๋ณํํด์ฃผ์ธ์. ๋ ์์ํ๊ณ ์์ ์ ์ธ ๋ฒ์ ์ผ๋ก ๋ง๋ค์ด์ฃผ์ธ์."
|
100 |
|
101 |
elif "2. ๊ธ์์ง์ฐ๊ธฐ" in prompt:
|
|
|
102 |
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์์ ๋ชจ๋ ํ
์คํธ๋ฅผ ์ฐพ์ ์์ฐ์ค๋ฝ๊ฒ ์ ๊ฑฐํด์ฃผ์ธ์. ๊น๋ํ ์ด๋ฏธ์ง๋ก ๋ง๋ค์ด์ฃผ์ธ์."
|
103 |
|
104 |
elif "3. ์ผ๊ตด๋ฐ๊พธ๊ธฐ" in prompt:
|
@@ -133,13 +88,13 @@ def preprocess_prompt(prompt, image1=None, image2=None, image3=None):
|
|
133 |
|
134 |
return prompt
|
135 |
|
136 |
-
def generate_with_images(prompt, images):
|
137 |
"""
|
138 |
๊ณต์ ๋ฌธ์์ ๊ธฐ๋ฐํ ์ฌ๋ฐ๋ฅธ API ํธ์ถ ๋ฐฉ์ ๊ตฌํ
|
139 |
-
์ฌ์๋
|
140 |
"""
|
141 |
-
max_retries = 2
|
142 |
retries = 0
|
|
|
143 |
|
144 |
while retries <= max_retries:
|
145 |
try:
|
@@ -151,18 +106,15 @@ def generate_with_images(prompt, images):
|
|
151 |
# Gemini ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
152 |
client = genai.Client(api_key=api_key)
|
153 |
|
154 |
-
|
155 |
-
english_prompt = get_translation(prompt)
|
156 |
-
logger.info(f"์๋ณธ ํ๋กฌํํธ: {prompt}")
|
157 |
-
logger.info(f"๋ฒ์ญ๋ ํ๋กฌํํธ: {english_prompt}")
|
158 |
|
159 |
# ์ปจํ
์ธ ์ค๋น
|
160 |
contents = []
|
161 |
|
162 |
# ํ
์คํธ ํ๋กฌํํธ ์ถ๊ฐ
|
163 |
-
contents.append(
|
164 |
|
165 |
-
# ์ด๋ฏธ์ง ์ถ๊ฐ
|
166 |
for idx, img in enumerate(images, 1):
|
167 |
if img is not None:
|
168 |
contents.append(img)
|
@@ -201,10 +153,9 @@ def generate_with_images(prompt, images):
|
|
201 |
if not image_found:
|
202 |
if retries < max_retries:
|
203 |
retries += 1
|
204 |
-
logger.warning(f"
|
205 |
continue
|
206 |
-
|
207 |
-
return None, f"API์์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ์๋ต ํ
์คํธ: {result_text}"
|
208 |
|
209 |
# ๊ฒฐ๊ณผ ์ด๋ฏธ์ง ๋ฐํ
|
210 |
result_img = Image.open(temp_path)
|
@@ -214,31 +165,37 @@ def generate_with_images(prompt, images):
|
|
214 |
return result_img, f"์ด๋ฏธ์ง๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ๋์์ต๋๋ค. {result_text}"
|
215 |
|
216 |
except Exception as e:
|
|
|
|
|
217 |
if retries < max_retries:
|
218 |
retries += 1
|
219 |
-
logger.
|
220 |
-
|
221 |
-
|
222 |
-
|
|
|
223 |
|
224 |
def process_images_with_prompt(image1, image2, image3, prompt):
|
225 |
"""
|
226 |
3๊ฐ์ ์ด๋ฏธ์ง์ ํ๋กฌํํธ๋ฅผ ์ฒ๋ฆฌํ๋ ํจ์
|
|
|
227 |
"""
|
228 |
try:
|
229 |
# ์ด๋ฏธ์ง ๊ฐ์ ํ์ธ
|
230 |
-
images = [
|
|
|
231 |
|
232 |
# ํ๋กฌํํธ ์ฒ๋ฆฌ
|
233 |
if not prompt or not prompt.strip():
|
234 |
-
# ํ๋กฌํํธ๊ฐ
|
235 |
-
if
|
236 |
-
|
237 |
-
|
238 |
-
|
|
|
239 |
prompt = "์ด ์ด๋ฏธ์ง๋ฅผ ์ฐฝ์์ ์ผ๋ก ๋ณํํด์ฃผ์ธ์. ๋ ์์ํ๊ณ ์์ ์ ์ธ ๋ฒ์ ์ผ๋ก ๋ง๋ค์ด์ฃผ์ธ์."
|
240 |
logger.info("๋จ์ผ ์ด๋ฏธ์ง ํ๋กฌํํธ ์๋ ์์ฑ")
|
241 |
-
elif len(
|
242 |
prompt = "์ด ๋ ์ด๋ฏธ์ง๋ฅผ ์์ฐ์ค๋ฝ๊ฒ ํฉ์ฑํด์ฃผ์ธ์. ๋ ์ด๋ฏธ์ง์ ์์๋ฅผ ์กฐํ๋กญ๊ฒ ํตํฉํ์ฌ ํ๋์ ์ด๋ฏธ์ง๋ก ๋ง๋ค์ด์ฃผ์ธ์."
|
243 |
logger.info("๋ ์ด๋ฏธ์ง ํฉ์ฑ ํ๋กฌํํธ ์๋ ์์ฑ")
|
244 |
else:
|
@@ -248,18 +205,18 @@ def process_images_with_prompt(image1, image2, image3, prompt):
|
|
248 |
# ํ๋กฌํํธ ์ ์ฒ๋ฆฌ ๋ฐ ๊ธฐ๋ฅ ๋ช
๋ น ํด์
|
249 |
prompt = preprocess_prompt(prompt, image1, image2, image3)
|
250 |
|
251 |
-
#
|
252 |
-
return generate_with_images(prompt,
|
253 |
|
254 |
except Exception as e:
|
255 |
logger.exception("์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์:")
|
256 |
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
257 |
|
258 |
-
# ๊ธฐ๋ฅ ์ ํ ์ฝ๋ฐฑ (
|
259 |
def update_prompt_from_function(function_choice):
|
260 |
function_templates = {
|
261 |
-
"1. ์ด๋ฏธ์ง ๋ณ๊ฒฝ": "#1์ ์ฐฝ์์ ์ผ๋ก
|
262 |
-
"2. ๊ธ์์ง์ฐ๊ธฐ": "#1์์ ํ
์คํธ๋ฅผ
|
263 |
"3. ์ผ๊ตด๋ฐ๊พธ๊ธฐ": "#1์ ์ธ๋ฌผ์ #2์ ์ผ๊ตด๋ก ๋ฐ๊ฟ๋ผ",
|
264 |
"4. ์ท๋ฐ๊พธ๊ธฐ": "#1์ ์ธ๋ฌผ์ #2 ๋๋ #3์ ์ท์ผ๋ก ๋ณ๊ฒฝํ๋ผ",
|
265 |
"5. ๋ฐฐ๊ฒฝ๋ฐ๊พธ๊ธฐ": "#1์ ์ด๋ฏธ์ง์ #2์ ๋ฐฐ๊ฒฝ์ผ๋ก ์์ฐ์ค๋ฝ๊ฒ ๋ฐ๊ฟ๋ผ",
|
@@ -269,104 +226,116 @@ def update_prompt_from_function(function_choice):
|
|
269 |
|
270 |
return function_templates.get(function_choice, "")
|
271 |
|
272 |
-
# Gradio ์ธํฐํ์ด์ค (
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
<
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
with gr.
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
image3_input = gr.Image(type="pil", label="#3 (์ ํ์ฌํญ)", image_mode="RGB")
|
291 |
-
|
292 |
-
# ๊ธฐ๋ฅ ์ ํ ๋๋กญ๋ค์ด
|
293 |
-
with gr.Row():
|
294 |
-
function_dropdown = gr.Dropdown(
|
295 |
-
choices=[
|
296 |
-
"1. ์ด๋ฏธ์ง ๋ณ๊ฒฝ",
|
297 |
-
"2. ๊ธ์์ง์ฐ๊ธฐ",
|
298 |
-
"3. ์ผ๊ตด๋ฐ๊พธ๊ธฐ",
|
299 |
-
"4. ์ท๋ฐ๊พธ๊ธฐ",
|
300 |
-
"5. ๋ฐฐ๊ฒฝ๋ฐ๊พธ๊ธฐ",
|
301 |
-
"6. ์ด๋ฏธ์ง ํฉ์ฑ(์ํํฌํจ)",
|
302 |
-
"7. ์ด๋ฏธ์ง ํฉ์ฑ(์คํ์ผ์ ์ฉ)"
|
303 |
-
],
|
304 |
-
label="๊ธฐ๋ฅ ์ ํ (์ ํํ๋ฉด ์๋์ผ๋ก ํ๋กฌํํธ๊ฐ ๋ณ๊ฒฝ๋ฉ๋๋ค)",
|
305 |
-
value=None
|
306 |
-
)
|
307 |
-
|
308 |
-
# ํ๋กฌํํธ ์
๋ ฅ
|
309 |
-
prompt_input = gr.Textbox(
|
310 |
-
lines=3,
|
311 |
-
placeholder="ํ๋กฌํํธ๋ฅผ ์
๋ ฅํ๊ฑฐ๋ ๋น์๋๋ฉด ์๋ ์์ฑ๋ฉ๋๋ค. ํ๊ตญ์ด๋ก ์
๋ ฅํ์ธ์.",
|
312 |
-
label="ํ๋กฌํํธ"
|
313 |
-
)
|
314 |
-
|
315 |
-
# ์์ฑ ๋ฒํผ
|
316 |
-
submit_btn = gr.Button("์ด๋ฏธ์ง ์์ฑ", variant="primary")
|
317 |
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
332 |
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
346 |
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
356 |
|
357 |
-
|
358 |
-
|
359 |
-
3. **๊ธฐ๋ฅ ์ฌ์ฉ**: ๋๋กญ๋ค์ด์์ ์ํ๋ ๊ธฐ๋ฅ์ ์ ํํ๋ฉด ์๋์ผ๋ก ํ๋กฌํํธ๊ฐ ๋ณ๊ฒฝ๋ฉ๋๋ค
|
360 |
-
4. **์ด๋ฏธ์ง ์ฐธ์กฐ**: #1, #2, #3์ผ๋ก ๊ฐ ์ด๋ฏธ์ง๋ฅผ ์ฐธ์กฐํ ์ ์์ต๋๋ค
|
361 |
-
5. **์ผ๋ถ ์ด๋ฏธ์ง๋ง**: ํ์ํ ์ด๋ฏธ์ง๋ง ์
๋ก๋ํด๋ ๊ธฐ๋ฅ ์คํ์ด ๊ฐ๋ฅํฉ๋๋ค
|
362 |
|
363 |
-
|
364 |
-
|
365 |
-
|
|
|
366 |
|
367 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
368 |
|
369 |
# ์ ํ๋ฆฌ์ผ์ด์
์คํ
|
370 |
if __name__ == "__main__":
|
371 |
-
demo = create_interface()
|
372 |
demo.launch(share=True)
|
|
|
18 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
19 |
logger = logging.getLogger(__name__)
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
def save_binary_file(file_name, data):
|
22 |
with open(file_name, "wb") as f:
|
23 |
f.write(data)
|
24 |
|
25 |
+
def preprocess_prompt(prompt, image1, image2, image3):
|
26 |
"""
|
27 |
ํ๋กฌํํธ๋ฅผ ์ฒ๋ฆฌํ๊ณ ๊ธฐ๋ฅ ๋ช
๋ น์ ํด์
|
28 |
"""
|
|
|
49 |
|
50 |
# ๊ธฐ๋ฅ ๋ช
๋ น ํด์
|
51 |
if "1. ์ด๋ฏธ์ง ๋ณ๊ฒฝ" in prompt:
|
52 |
+
# ๊ธฐ๋ณธ ํ๋กฌํํธ ์ ๊ณต
|
53 |
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ์ฐฝ์์ ์ผ๋ก ๋ณํํด์ฃผ์ธ์. ๋ ์์ํ๊ณ ์์ ์ ์ธ ๋ฒ์ ์ผ๋ก ๋ง๋ค์ด์ฃผ์ธ์."
|
54 |
|
55 |
elif "2. ๊ธ์์ง์ฐ๊ธฐ" in prompt:
|
56 |
+
# ๊ธฐ๋ณธ ํ๋กฌํํธ ์ ๊ณต
|
57 |
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์์ ๋ชจ๋ ํ
์คํธ๋ฅผ ์ฐพ์ ์์ฐ์ค๋ฝ๊ฒ ์ ๊ฑฐํด์ฃผ์ธ์. ๊น๋ํ ์ด๋ฏธ์ง๋ก ๋ง๋ค์ด์ฃผ์ธ์."
|
58 |
|
59 |
elif "3. ์ผ๊ตด๋ฐ๊พธ๊ธฐ" in prompt:
|
|
|
88 |
|
89 |
return prompt
|
90 |
|
91 |
+
def generate_with_images(prompt, images, max_retries=2):
|
92 |
"""
|
93 |
๊ณต์ ๋ฌธ์์ ๊ธฐ๋ฐํ ์ฌ๋ฐ๋ฅธ API ํธ์ถ ๋ฐฉ์ ๊ตฌํ
|
94 |
+
์คํจ ์ ์ฌ์๋ ๊ธฐ๋ฅ ์ถ๊ฐ
|
95 |
"""
|
|
|
96 |
retries = 0
|
97 |
+
last_error = None
|
98 |
|
99 |
while retries <= max_retries:
|
100 |
try:
|
|
|
106 |
# Gemini ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
107 |
client = genai.Client(api_key=api_key)
|
108 |
|
109 |
+
logger.info(f"Gemini API ์์ฒญ ์์ - ํ๋กฌํํธ: {prompt} (์๋: {retries+1}/{max_retries+1})")
|
|
|
|
|
|
|
110 |
|
111 |
# ์ปจํ
์ธ ์ค๋น
|
112 |
contents = []
|
113 |
|
114 |
# ํ
์คํธ ํ๋กฌํํธ ์ถ๊ฐ
|
115 |
+
contents.append(prompt)
|
116 |
|
117 |
+
# ์ด๋ฏธ์ง ์ถ๊ฐ (์ด๋ฏธ์ง๊ฐ ์๋ ๊ฒฝ์ฐ๋ง)
|
118 |
for idx, img in enumerate(images, 1):
|
119 |
if img is not None:
|
120 |
contents.append(img)
|
|
|
153 |
if not image_found:
|
154 |
if retries < max_retries:
|
155 |
retries += 1
|
156 |
+
logger.warning(f"API์์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ์ฌ์๋ ์ค... ({retries}/{max_retries})")
|
157 |
continue
|
158 |
+
return None, f"API์์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ์๋ต ํ
์คํธ: {result_text}"
|
|
|
159 |
|
160 |
# ๊ฒฐ๊ณผ ์ด๋ฏธ์ง ๋ฐํ
|
161 |
result_img = Image.open(temp_path)
|
|
|
165 |
return result_img, f"์ด๋ฏธ์ง๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ๋์์ต๋๋ค. {result_text}"
|
166 |
|
167 |
except Exception as e:
|
168 |
+
last_error = str(e)
|
169 |
+
logger.exception(f"์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์ (์๋: {retries+1}/{max_retries+1}):")
|
170 |
if retries < max_retries:
|
171 |
retries += 1
|
172 |
+
logger.info(f"์ฌ์๋ ์ค... ({retries}/{max_retries})")
|
173 |
+
continue
|
174 |
+
return None, f"์ค๋ฅ ๋ฐ์: {last_error}"
|
175 |
+
|
176 |
+
return None, f"์ต๋ ์ฌ์๋ ํ์ ์ด๊ณผ. ๋ง์ง๋ง ์ค๋ฅ: {last_error}"
|
177 |
|
178 |
def process_images_with_prompt(image1, image2, image3, prompt):
|
179 |
"""
|
180 |
3๊ฐ์ ์ด๋ฏธ์ง์ ํ๋กฌํํธ๋ฅผ ์ฒ๋ฆฌํ๋ ํจ์
|
181 |
+
์ด๋ฏธ์ง ์์ด ํ๋กฌํํธ๋ง์ผ๋ก๋ ๋์
|
182 |
"""
|
183 |
try:
|
184 |
# ์ด๋ฏธ์ง ๊ฐ์ ํ์ธ
|
185 |
+
images = [image1, image2, image3]
|
186 |
+
valid_images = [img for img in images if img is not None]
|
187 |
|
188 |
# ํ๋กฌํํธ ์ฒ๋ฆฌ
|
189 |
if not prompt or not prompt.strip():
|
190 |
+
# ํ๋กฌํํธ๊ฐ ์๋ ๊ฒฝ์ฐ
|
191 |
+
if not valid_images:
|
192 |
+
# ์ด๋ฏธ์ง๋ ์๊ณ ํ๋กฌํํธ๋ ์๋ ๊ฒฝ์ฐ ๊ธฐ๋ณธ ํ๋กฌํํธ ์ฌ์ฉ
|
193 |
+
prompt = "์๋ฆ๋ค์ด ํ๊ฒฝ ์ด๋ฏธ์ง๋ฅผ ์์ฑํด์ฃผ์ธ์. ํธ๋ฅธ ํ๋, ์ด๋ก์ ์ธ๋, ๊ทธ๋ฆฌ๊ณ ๋ง์ ํธ์๊ฐ ์๋ ํํ๋ก์ด ์์ฐ ํ๊ฒฝ์ ๋ง๋ค์ด์ฃผ์ธ์."
|
194 |
+
logger.info("์ด๋ฏธ์ง์ ํ๋กฌํํธ ๋ชจ๋ ์์. ๊ธฐ๋ณธ ํ๋กฌํํธ ์์ฑ")
|
195 |
+
elif len(valid_images) == 1:
|
196 |
prompt = "์ด ์ด๋ฏธ์ง๋ฅผ ์ฐฝ์์ ์ผ๋ก ๋ณํํด์ฃผ์ธ์. ๋ ์์ํ๊ณ ์์ ์ ์ธ ๋ฒ์ ์ผ๋ก ๋ง๋ค์ด์ฃผ์ธ์."
|
197 |
logger.info("๋จ์ผ ์ด๋ฏธ์ง ํ๋กฌํํธ ์๋ ์์ฑ")
|
198 |
+
elif len(valid_images) == 2:
|
199 |
prompt = "์ด ๋ ์ด๋ฏธ์ง๋ฅผ ์์ฐ์ค๋ฝ๊ฒ ํฉ์ฑํด์ฃผ์ธ์. ๋ ์ด๋ฏธ์ง์ ์์๋ฅผ ์กฐํ๋กญ๊ฒ ํตํฉํ์ฌ ํ๋์ ์ด๋ฏธ์ง๋ก ๋ง๋ค์ด์ฃผ์ธ์."
|
200 |
logger.info("๋ ์ด๋ฏธ์ง ํฉ์ฑ ํ๋กฌํํธ ์๋ ์์ฑ")
|
201 |
else:
|
|
|
205 |
# ํ๋กฌํํธ ์ ์ฒ๋ฆฌ ๋ฐ ๊ธฐ๋ฅ ๋ช
๋ น ํด์
|
206 |
prompt = preprocess_prompt(prompt, image1, image2, image3)
|
207 |
|
208 |
+
# ์๋ก์ด API ํธ์ถ ๋ฐฉ์ ์ฌ์ฉ (์ฌ์๋ ๊ธฐ๋ฅ ํฌํจ)
|
209 |
+
return generate_with_images(prompt, valid_images)
|
210 |
|
211 |
except Exception as e:
|
212 |
logger.exception("์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์:")
|
213 |
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
214 |
|
215 |
+
# ๊ธฐ๋ฅ ์ ํ ์ฝ๋ฐฑ (์์ ๋จ)
|
216 |
def update_prompt_from_function(function_choice):
|
217 |
function_templates = {
|
218 |
+
"1. ์ด๋ฏธ์ง ๋ณ๊ฒฝ": "#1์ ์ฐฝ์์ ์ผ๋ก ๋ฐ๊ฟ๋ผ",
|
219 |
+
"2. ๊ธ์์ง์ฐ๊ธฐ": "#1์์ ํ
์คํธ๋ฅผ ์ง์๋ผ",
|
220 |
"3. ์ผ๊ตด๋ฐ๊พธ๊ธฐ": "#1์ ์ธ๋ฌผ์ #2์ ์ผ๊ตด๋ก ๋ฐ๊ฟ๋ผ",
|
221 |
"4. ์ท๋ฐ๊พธ๊ธฐ": "#1์ ์ธ๋ฌผ์ #2 ๋๋ #3์ ์ท์ผ๋ก ๋ณ๊ฒฝํ๋ผ",
|
222 |
"5. ๋ฐฐ๊ฒฝ๋ฐ๊พธ๊ธฐ": "#1์ ์ด๋ฏธ์ง์ #2์ ๋ฐฐ๊ฒฝ์ผ๋ก ์์ฐ์ค๋ฝ๊ฒ ๋ฐ๊ฟ๋ผ",
|
|
|
226 |
|
227 |
return function_templates.get(function_choice, "")
|
228 |
|
229 |
+
# Gradio ์ธํฐํ์ด์ค (์์ ๋จ)
|
230 |
+
with gr.Blocks() as demo:
|
231 |
+
gr.HTML(
|
232 |
+
"""
|
233 |
+
<div style="text-align: center; margin-bottom: 1rem;">
|
234 |
+
<h1>๊ฐ๋จํ ์ด๋ฏธ์ง ์์ฑ๊ธฐ</h1>
|
235 |
+
<p>์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๊ฑฐ๋ ํ๋กฌํํธ๋ฅผ ์
๋ ฅํ์ฌ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ธ์. ๋ ๋ค ์์ด๋ ๊ธฐ๋ณธ ์ด๋ฏธ์ง๊ฐ ์์ฑ๋ฉ๋๋ค.</p>
|
236 |
+
</div>
|
237 |
+
"""
|
238 |
+
)
|
239 |
+
|
240 |
+
with gr.Row():
|
241 |
+
with gr.Column():
|
242 |
+
# 3๊ฐ์ ์ด๋ฏธ์ง ์
๋ ฅ
|
243 |
+
with gr.Row():
|
244 |
+
image1_input = gr.Image(type="pil", label="#1", image_mode="RGB")
|
245 |
+
image2_input = gr.Image(type="pil", label="#2", image_mode="RGB")
|
246 |
+
image3_input = gr.Image(type="pil", label="#3", image_mode="RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
247 |
|
248 |
+
# ๊ธฐ๋ฅ ์ ํ ๋๋กญ๋ค์ด (์ปค์คํ
ํ
์คํธ ์
๋ ฅ ์ ๊ฑฐ)
|
249 |
+
function_dropdown = gr.Dropdown(
|
250 |
+
choices=[
|
251 |
+
"1. ์ด๋ฏธ์ง ๋ณ๊ฒฝ",
|
252 |
+
"2. ๊ธ์์ง์ฐ๊ธฐ",
|
253 |
+
"3. ์ผ๊ตด๋ฐ๊พธ๊ธฐ",
|
254 |
+
"4. ์ท๋ฐ๊พธ๊ธฐ",
|
255 |
+
"5. ๋ฐฐ๊ฒฝ๋ฐ๊พธ๊ธฐ",
|
256 |
+
"6. ์ด๋ฏธ์ง ํฉ์ฑ(์ํํฌํจ)",
|
257 |
+
"7. ์ด๋ฏธ์ง ํฉ์ฑ(์คํ์ผ์ ์ฉ)"
|
258 |
+
],
|
259 |
+
label="๊ธฐ๋ฅ ์ ํ",
|
260 |
+
value=None
|
261 |
+
)
|
262 |
+
|
263 |
+
# ํ๋กฌํํธ ์
๋ ฅ (์ ํ ์ฌํญ)
|
264 |
+
prompt_input = gr.Textbox(
|
265 |
+
lines=3,
|
266 |
+
placeholder="ํ๋กฌํํธ๋ฅผ ์
๋ ฅํ๊ฑฐ๋ ๋น์๋๋ฉด ์๋ ํฉ์ฑ๋ฉ๋๋ค. ์ด๋ฏธ์ง ์์ด๋ ํ๋กฌํํธ๋ง์ผ๋ก ์์ฑ ๊ฐ๋ฅํฉ๋๋ค.",
|
267 |
+
label="ํ๋กฌํํธ (์ ํ ์ฌํญ)"
|
268 |
+
)
|
269 |
+
|
270 |
+
# ์์ฑ ๋ฒํผ
|
271 |
+
submit_btn = gr.Button("์ด๋ฏธ์ง ์์ฑ", variant="primary")
|
272 |
|
273 |
+
with gr.Column():
|
274 |
+
# ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
275 |
+
output_image = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง")
|
276 |
+
output_text = gr.Textbox(label="์ํ ๋ฉ์์ง")
|
277 |
+
|
278 |
+
# ์ฌ์ฉ๋ ํ๋กฌํํธ ํ์
|
279 |
+
prompt_display = gr.Textbox(label="์ฌ์ฉ๋ ํ๋กฌํํธ", visible=True)
|
280 |
+
|
281 |
+
# ๊ธฐ๋ฅ ์ ํ ๋๋กญ๋ค์ด ์ด๋ฒคํธ (์ฆ์ ํ๋กฌํํธ ์
๋ฐ์ดํธ)
|
282 |
+
function_dropdown.change(
|
283 |
+
fn=update_prompt_from_function,
|
284 |
+
inputs=[function_dropdown],
|
285 |
+
outputs=[prompt_input]
|
286 |
+
)
|
287 |
+
|
288 |
+
# ์ด๋ฏธ์ง ์์ฑ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
|
289 |
+
def process_and_show_prompt(image1, image2, image3, prompt):
|
290 |
+
# ์ด๋ฏธ์ง ๊ฐ์ ํ์ธ
|
291 |
+
images = [image1, image2, image3]
|
292 |
+
valid_images = [img for img in images if img is not None]
|
293 |
|
294 |
+
try:
|
295 |
+
# ์๋ ํ๋กฌํํธ ์์ฑ ๋๋ ํ๋กฌํํธ ์ ์ฒ๋ฆฌ
|
296 |
+
auto_prompt = prompt
|
297 |
+
if not prompt or not prompt.strip():
|
298 |
+
if not valid_images:
|
299 |
+
# ์ด๋ฏธ์ง๋ ์๊ณ ํ๋กฌํํธ๋ ์๋ ๊ฒฝ์ฐ
|
300 |
+
auto_prompt = "์๋ฆ๋ค์ด ํ๊ฒฝ ์ด๋ฏธ์ง๋ฅผ ์์ฑํด์ฃผ์ธ์. ํธ๋ฅธ ํ๋, ์ด๋ก์ ์ธ๋, ๊ทธ๋ฆฌ๊ณ ๋ง์ ํธ์๊ฐ ์๋ ํํ๋ก์ด ์์ฐ ํ๊ฒฝ์ ๋ง๋ค์ด์ฃผ์ธ์."
|
301 |
+
elif len(valid_images) == 1:
|
302 |
+
auto_prompt = "์ด ์ด๋ฏธ์ง๋ฅผ ์ฐฝ์์ ์ผ๋ก ๋ณํํด์ฃผ์ธ์. ๋ ์์ํ๊ณ ์์ ์ ์ธ ๋ฒ์ ์ผ๋ก ๋ง๋ค์ด์ฃผ์ธ์."
|
303 |
+
elif len(valid_images) == 2:
|
304 |
+
auto_prompt = "์ด ๋ ์ด๋ฏธ์ง๋ฅผ ์์ฐ์ค๋ฝ๊ฒ ํฉ์ฑํด์ฃผ์ธ์. ๋ ์ด๋ฏธ์ง์ ์์๋ฅผ ์กฐํ๋กญ๊ฒ ํตํฉํ์ฌ ํ๋์ ์ด๋ฏธ์ง๋ก ๋ง๋ค์ด์ฃผ์ธ์."
|
305 |
+
else:
|
306 |
+
auto_prompt = "์ด ์ธ ์ด๋ฏธ์ง๋ฅผ ์ฐฝ์์ ์ผ๋ก ํฉ์ฑํด์ฃผ์ธ์. ๋ชจ๋ ์ด๋ฏธ์ง์ ์ฃผ์ ์์๋ฅผ ํฌํจํ๋ ์์ฐ์ค๋ฝ๊ณ ์ผ๊ด๋ ํ๋์ ์ฅ๋ฉด์ผ๋ก ๋ง๋ค์ด์ฃผ์ธ์."
|
307 |
+
else:
|
308 |
+
auto_prompt = preprocess_prompt(prompt, image1, image2, image3)
|
309 |
|
310 |
+
# ์ด๋ฏธ์ง ์์ฑ ํจ์ ํธ์ถ
|
311 |
+
result_img, status = process_images_with_prompt(image1, image2, image3, prompt)
|
|
|
|
|
|
|
312 |
|
313 |
+
return result_img, status, auto_prompt
|
314 |
+
except Exception as e:
|
315 |
+
logger.exception("์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์:")
|
316 |
+
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}", prompt
|
317 |
|
318 |
+
submit_btn.click(
|
319 |
+
fn=process_and_show_prompt,
|
320 |
+
inputs=[image1_input, image2_input, image3_input, prompt_input],
|
321 |
+
outputs=[output_image, output_text, prompt_display],
|
322 |
+
)
|
323 |
+
|
324 |
+
gr.Markdown(
|
325 |
+
"""
|
326 |
+
### ์ฌ์ฉ ๋ฐฉ๋ฒ:
|
327 |
+
|
328 |
+
1. **์๋ ํฉ์ฑ**: ์ด๋ฏธ์ง๋ง ์
๋ก๋ํ๊ณ ํ๋กฌํํธ๋ฅผ ๋น์๋๋ฉด ์๋์ผ๋ก ํฉ์ฑ๋ฉ๋๋ค
|
329 |
+
2. **ํ
์คํธ๋ง์ผ๋ก ์์ฑ**: ์ด๋ฏธ์ง ์์ด ํ๋กฌํํธ๋ง ์
๋ ฅํด๋ ์ด๋ฏธ์ง๊ฐ ์์ฑ๋ฉ๋๋ค
|
330 |
+
3. **๊ธฐ๋ฅ ์ฌ์ฉ**: ๋๋กญ๋ค์ด์์ ์ํ๋ ๊ธฐ๋ฅ์ ์ ํํ๋ฉด ์๋์ผ๋ก ํ๋กฌํํธ๊ฐ ์
๋ ฅ๋ฉ๋๋ค
|
331 |
+
4. **์ด๋ฏธ์ง ์ฐธ์กฐ**: #1, #2, #3์ผ๋ก ๊ฐ ์ด๋ฏธ์ง๋ฅผ ์ฐธ์กฐํ ์ ์์ต๋๋ค
|
332 |
+
5. **์ผ๋ถ ์ด๋ฏธ์ง๋ง**: ํ์ํ ์ด๋ฏธ์ง๋ง ์
๋ก๋ํด๋ ๊ธฐ๋ฅ ์คํ์ด ๊ฐ๋ฅํฉ๋๋ค
|
333 |
+
6. **์ฌ์๋ ๊ธฐ๋ฅ**: ์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์ ์ ์๋์ผ๋ก ์ฌ์๋ํฉ๋๋ค
|
334 |
+
|
335 |
+
> **ํ**: ๊ธฐ๋ฅ์ ์ ํํ ํ์๋ ํ๋กฌํํธ๋ฅผ ์ง์ ์์ ํ ์ ์์ต๋๋ค
|
336 |
+
"""
|
337 |
+
)
|
338 |
|
339 |
# ์ ํ๋ฆฌ์ผ์ด์
์คํ
|
340 |
if __name__ == "__main__":
|
|
|
341 |
demo.launch(share=True)
|