Spaces:
Sleeping
Sleeping
| import os | |
| import logging | |
| import re | |
| import json | |
| import gradio as gr | |
| from google import genai | |
| from google.genai import types | |
| import google.generativeai as genai_generative | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| # ------------------- ๋ก๊น ์ค์ ------------------- | |
| logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') | |
| logger = logging.getLogger(__name__) | |
| # ------------------- ๋ฐฐ๊ฒฝ ๋๋ ํ ๋ฆฌ ์ค์ ------------------- | |
| BACKGROUNDS_DIR = "./background" | |
| if not os.path.exists(BACKGROUNDS_DIR): | |
| os.makedirs(BACKGROUNDS_DIR) | |
| logger.info(f"๋ฐฐ๊ฒฝ ๋๋ ํ ๋ฆฌ๋ฅผ ์์ฑํ์ต๋๋ค: {BACKGROUNDS_DIR}") | |
| # ------------------- ์ ์ญ ๋ณ์ ์ค์ ------------------- | |
| SIMPLE_BACKGROUNDS = {} | |
| STUDIO_BACKGROUNDS = {} | |
| NATURE_BACKGROUNDS = {} | |
| INDOOR_BACKGROUNDS = {} | |
| TECHNOLOGY_BACKGROUNDS = {} | |
| COLORFUL_PATTERN_BACKGROUNDS = {} | |
| ABSTRACT_BACKGROUNDS = {} | |
| JEWELRY_BACKGROUNDS = {} # ์ฅฌ์ผ๋ฆฌ ๋ฐฐ๊ฒฝ ์ ์ญ ๋ณ์ ์ถ๊ฐ | |
| # ------------------- ๋ฐฐ๊ฒฝ JSON ํ์ผ ๋ก๋ ํจ์ ------------------- | |
| def load_background_json(filename): | |
| """๋ฐฐ๊ฒฝ JSON ํ์ผ ๋ก๋ ํจ์""" | |
| file_path = os.path.join(BACKGROUNDS_DIR, filename) | |
| try: | |
| with open(file_path, 'r', encoding='utf-8') as f: | |
| data = json.load(f) | |
| logger.info(f"{filename} ํ์ผ์ ์ฑ๊ณต์ ์ผ๋ก ๋ก๋ํ์ต๋๋ค. {len(data)} ํญ๋ชฉ ํฌํจ.") | |
| return data | |
| except FileNotFoundError: | |
| logger.info(f"{filename} ํ์ผ์ด ์์ต๋๋ค.") | |
| return {} | |
| except Exception as e: | |
| logger.warning(f"{filename} ํ์ผ ๋ก๋ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}.") | |
| return {} | |
| # ------------------- ๋ฐฐ๊ฒฝ ์ต์ ์ด๊ธฐํ ํจ์ ------------------- | |
| def initialize_backgrounds(): | |
| """๋ชจ๋ ๋ฐฐ๊ฒฝ ์ต์ ์ด๊ธฐํ ํจ์""" | |
| global SIMPLE_BACKGROUNDS, STUDIO_BACKGROUNDS, NATURE_BACKGROUNDS, INDOOR_BACKGROUNDS | |
| global TECHNOLOGY_BACKGROUNDS, COLORFUL_PATTERN_BACKGROUNDS, ABSTRACT_BACKGROUNDS | |
| global JEWELRY_BACKGROUNDS # ์ฅฌ์ผ๋ฆฌ ๋ฐฐ๊ฒฝ ์ถ๊ฐ | |
| # ๋๋ ํ ๋ฆฌ ๋ด ๋ชจ๋ ํ์ผ ๋ก๊น | |
| logger.info(f"Backgrounds ๋๋ ํ ๋ฆฌ ๊ฒฝ๋ก: {BACKGROUNDS_DIR}") | |
| logger.info(f"๋๋ ํ ๋ฆฌ ๋ด ํ์ผ ๋ชฉ๋ก: {os.listdir(BACKGROUNDS_DIR)}") | |
| # ๊ฐ ๋ฐฐ๊ฒฝ ํ์ผ ๋ก๋ | |
| SIMPLE_BACKGROUNDS = load_background_json("simple_backgrounds.json") | |
| STUDIO_BACKGROUNDS = load_background_json("studio_backgrounds.json") | |
| NATURE_BACKGROUNDS = load_background_json("nature_backgrounds.json") | |
| INDOOR_BACKGROUNDS = load_background_json("indoor_backgrounds.json") | |
| TECHNOLOGY_BACKGROUNDS = load_background_json("tech-backgrounds-final.json") # ํ์ผ ์ด๋ฆ ์์ | |
| COLORFUL_PATTERN_BACKGROUNDS = load_background_json("colorful-pattern-backgrounds.json") # ํ์ผ ์ด๋ฆ ์์ | |
| ABSTRACT_BACKGROUNDS = load_background_json("abstract_backgrounds.json") | |
| JEWELRY_BACKGROUNDS = load_background_json("jewelry_backgrounds.json") | |
| # ๊ธฐ๋ณธ๊ฐ ์ค์ (ํ์ผ์ด ์๊ฑฐ๋ ๋น์ด์๋ ๊ฒฝ์ฐ) | |
| if not SIMPLE_BACKGROUNDS: | |
| SIMPLE_BACKGROUNDS = {"ํด๋์ ํ์ดํธ": "clean white background with soft even lighting"} | |
| if not STUDIO_BACKGROUNDS: | |
| STUDIO_BACKGROUNDS = {"๋ฏธ๋๋ฉ ํ๋ซ๋ ์ด": "minimalist flat lay with clean white background"} | |
| if not NATURE_BACKGROUNDS: | |
| NATURE_BACKGROUNDS = {"์ด๋ ํด๋ณ": "tropical beach with crystal clear water"} | |
| if not INDOOR_BACKGROUNDS: | |
| INDOOR_BACKGROUNDS = {"๋ฏธ๋๋ฉ ์ค์นธ๋๋๋น์ ๊ฑฐ์ค": "minimalist Scandinavian living room"} | |
| if not TECHNOLOGY_BACKGROUNDS: | |
| TECHNOLOGY_BACKGROUNDS = {"๋ค์ด๋๋ฏน ์คํ๋์": "dynamic water splash interaction with product"} | |
| if not COLORFUL_PATTERN_BACKGROUNDS: | |
| COLORFUL_PATTERN_BACKGROUNDS = {"ํ๋ คํ ๊ฝ ํจํด": "vibrant floral pattern backdrop"} | |
| if not ABSTRACT_BACKGROUNDS: | |
| ABSTRACT_BACKGROUNDS = {"๋ค์จ ๋ผ์ดํธ": "neon light abstract background with vibrant glowing elements"} | |
| if not JEWELRY_BACKGROUNDS: | |
| JEWELRY_BACKGROUNDS = {"ํด๋์ ํ์ดํธ ์คํฌ": "pristine white silk fabric backdrop"} | |
| logger.info("๋ชจ๋ ๋ฐฐ๊ฒฝ ์ต์ ์ด๊ธฐํ ์๋ฃ") | |
| # ๋ฐฐ๊ฒฝ ๋๋กญ๋ค์ด ์ด๊ธฐํ๋ฅผ ์ํ ํจ์ ์ถ๊ฐ | |
| def initialize_dropdowns(): | |
| """๋๋กญ๋ค์ด ๋ฉ๋ด ์ด๊ธฐํ ํจ์""" | |
| # ๊ฐ ๋ฐฐ๊ฒฝ ์ ํ๋ณ ๋๋กญ๋ค์ด ์ ํ ๋ชฉ๋ก ์์ฑ | |
| simple_choices = list(SIMPLE_BACKGROUNDS.keys()) | |
| studio_choices = list(STUDIO_BACKGROUNDS.keys()) | |
| nature_choices = list(NATURE_BACKGROUNDS.keys()) | |
| indoor_choices = list(INDOOR_BACKGROUNDS.keys()) | |
| tech_choices = list(TECHNOLOGY_BACKGROUNDS.keys()) | |
| colorful_choices = list(COLORFUL_PATTERN_BACKGROUNDS.keys()) | |
| abstract_choices = list(ABSTRACT_BACKGROUNDS.keys()) | |
| jewelry_choices = list(JEWELRY_BACKGROUNDS.keys()) | |
| return { | |
| "simple": simple_choices, | |
| "studio": studio_choices, | |
| "nature": nature_choices, | |
| "indoor": indoor_choices, | |
| "tech": tech_choices, | |
| "colorful": colorful_choices, | |
| "abstract": abstract_choices, | |
| "jewelry": jewelry_choices, # ์๋ก ์ถ๊ฐ | |
| } | |
| # ------------------- ํ๋กฌํํธ ๊ด๋ จ ํจ์ ------------------- | |
| def filter_prompt_only(prompt): | |
| """Gemini์ ์ค๋ช ๋ฐ ๋ถํ์ํ ๋ฉ์์ง๋ฅผ ์ ๊ฑฐํ๊ณ ์ค์ ํ๋กฌํํธ๋ง ์ถ์ถํ๋ ํจ์""" | |
| # ์ฝ๋ ๋ธ๋ก ๋ด๋ถ์ ํ๋กฌํํธ ์ฐพ๊ธฐ | |
| code_block_pattern = r"```\s*(.*?)```" | |
| code_match = re.search(code_block_pattern, prompt, re.DOTALL) | |
| if code_match: | |
| return code_match.group(1).strip() | |
| # Midjourney ํ๋ผ๋ฏธํฐ๋ฅผ ํฌํจํ๋ ํ๋กฌํํธ ๋ถ๋ถ ์ฐพ๊ธฐ | |
| if "--ar 1:1" in prompt: | |
| lines = prompt.split('\n') | |
| prompt_lines = [] | |
| in_prompt = False | |
| for line in lines: | |
| # ํ๋กฌํํธ ์์ ๋ถ๋ถ ์ธ์ (์ผ๋ฐ์ ์ธ ์ ํ ์ค๋ช ์ด๋ 'Magazine-worthy' ๊ฐ์ ํค์๋๋ก ์์) | |
| if (not in_prompt and | |
| ("product" in line.lower() or | |
| "magazine" in line.lower() or | |
| "commercial" in line.lower() or | |
| "photography" in line.lower())): | |
| in_prompt = True | |
| prompt_lines.append(line) | |
| # ์ด๋ฏธ ํ๋กฌํํธ ์์ญ์ ์๋ ๊ฒฝ์ฐ ๊ณ์ ์ถ๊ฐ | |
| elif in_prompt: | |
| # ์ค๋ช ์ด๋ ๋ฉํ ํ ์คํธ๊ฐ ์์๋๋ฉด ์ค๋จ | |
| if "explanation" in line.lower() or "let me know" in line.lower(): | |
| break | |
| prompt_lines.append(line) | |
| # ํ๋กฌํํธ ๋ผ์ธ ํฉ์น๊ธฐ | |
| if prompt_lines: | |
| return '\n'.join(prompt_lines).strip() | |
| # ์ ๋ฐฉ๋ฒ์ผ๋ก ์ฐพ์ง ๋ชปํ ๊ฒฝ์ฐ ์๋ณธ ๋ฐํ | |
| return prompt.strip() | |
| def get_selected_background_info(bg_type, simple, studio, nature, indoor, tech, colorful, abstract, jewelry): | |
| """์ ํ๋ ๋ฐฐ๊ฒฝ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋ ํจ์""" | |
| if bg_type == "์ฌํ ๋ฐฐ๊ฒฝ": | |
| return { | |
| "category": "์ฌํ ๋ฐฐ๊ฒฝ", | |
| "name": simple, | |
| "english": SIMPLE_BACKGROUNDS.get(simple, "white background") | |
| } | |
| elif bg_type == "์คํ๋์ค ๋ฐฐ๊ฒฝ": | |
| return { | |
| "category": "์คํ๋์ค ๋ฐฐ๊ฒฝ", | |
| "name": studio, | |
| "english": STUDIO_BACKGROUNDS.get(studio, "product photography studio") | |
| } | |
| elif bg_type == "์์ฐ ํ๊ฒฝ": | |
| return { | |
| "category": "์์ฐ ํ๊ฒฝ", | |
| "name": nature, | |
| "english": NATURE_BACKGROUNDS.get(nature, "natural environment") | |
| } | |
| elif bg_type == "์ค๋ด ํ๊ฒฝ": | |
| return { | |
| "category": "์ค๋ด ํ๊ฒฝ", | |
| "name": indoor, | |
| "english": INDOOR_BACKGROUNDS.get(indoor, "indoor environment") | |
| } | |
| elif bg_type == "ํ ํฌ๋๋ก์ง ๋ฐฐ๊ฒฝ": | |
| return { | |
| "category": "ํ ํฌ๋๋ก์ง ๋ฐฐ๊ฒฝ", | |
| "name": tech, | |
| "english": TECHNOLOGY_BACKGROUNDS.get(tech, "technology environment") | |
| } | |
| elif bg_type == "์ปฌ๋ฌํ ํจํด ๋ฐฐ๊ฒฝ": | |
| return { | |
| "category": "์ปฌ๋ฌํ ํจํด ๋ฐฐ๊ฒฝ", | |
| "name": colorful, | |
| "english": COLORFUL_PATTERN_BACKGROUNDS.get(colorful, "colorful pattern background") | |
| } | |
| elif bg_type == "์ถ์/ํน์ ๋ฐฐ๊ฒฝ": | |
| return { | |
| "category": "์ถ์/ํน์ ๋ฐฐ๊ฒฝ", | |
| "name": abstract, | |
| "english": ABSTRACT_BACKGROUNDS.get(abstract, "abstract background") | |
| } | |
| elif bg_type == "์ฅฌ์ผ๋ฆฌ ๋ฐฐ๊ฒฝ": | |
| return { | |
| "category": "์ฅฌ์ผ๋ฆฌ ๋ฐฐ๊ฒฝ", | |
| "name": jewelry, | |
| "english": JEWELRY_BACKGROUNDS.get(jewelry, "jewelry backdrop") | |
| } | |
| else: | |
| return { | |
| "category": "๊ธฐ๋ณธ ๋ฐฐ๊ฒฝ", | |
| "name": "ํ์ดํธ ๋ฐฐ๊ฒฝ", | |
| "english": "white background" | |
| } | |
| # ------------------- ํ๋กฌํํธ ์์ฑ ํจ์ ------------------- | |
| def generate_enhanced_system_instruction(): | |
| """ํฅ์๋ ์์คํ ์ธ์คํธ๋ญ์ ์์ฑ ํจ์""" | |
| return """๋น์ ์ ์ํ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ ๋ณ๊ฒฝํ๊ธฐ ์ํ ์ต๊ณ ํ์ง์ ํ๋กฌํํธ๋ฅผ ์์ฑํ๋ ์ ๋ฌธ๊ฐ์ ๋๋ค. | |
| ์ฌ์ฉ์๊ฐ ์ ๊ณตํ๋ ์ํ๋ช , ๋ฐฐ๊ฒฝ ์ ํ, ์ถ๊ฐ ์์ฒญ์ฌํญ์ ๋ฐํ์ผ๋ก ๋ฏธ๋์ ๋(Midjourney)์ ์ฌ์ฉํ ์ ์๋ ์์ธํ๊ณ ์ ๋ฌธ์ ์ธ ํ๋กฌํํธ๋ฅผ ์์ด๋ก ์์ฑํด์ฃผ์ธ์. | |
| ๋ค์ ๊ฐ์ด๋๋ผ์ธ์ ๋ฐ๋์ ์ค์ํด์ผ ํฉ๋๋ค: | |
| 1. ์ํ์ "#1"๋ก ์ง์ ํ์ฌ ์ฐธ์กฐํฉ๋๋ค. (์: "skincare tube (#1)") | |
| 2. *** ๋งค์ฐ ์ค์: ์ํ์ ์๋ ํน์ฑ(๋์์ธ, ์์, ํํ, ๋ก๊ณ , ํจํค์ง ๋ฑ)์ ์ด๋ค ์ํฉ์์๋ ์ ๋ ๋ณ๊ฒฝํ์ง ์์ต๋๋ค. *** | |
| 3. *** ์ํ์ ๋ณธ์ง์ ํน์ฑ์ ์ ์งํ๋, ์ํ์ ํฌ์ปค์ค๋ฅผ ๋ง์ถฐ ๋ชจ๋ ์ธ๋ถ ์ฌํญ์ด ์ ๋ช ํ๊ฒ ๋๋ฌ๋๋๋ก ํ๋ฉฐ, | |
| 8K ํด์๋(8K resolution), ์ค๋ฒ์คํ๋ ์๋ ์ด๊ณ ํ์ง(ultra high definition without oversharpening)๋ก ๋ ๋๋ง๋์ด์ผ ํฉ๋๋ค. *** | |
| 4. ์ด๋ฏธ์ง ๋น์จ์ ์ ํํ 1:1(์ ์ฌ๊ฐํ) ํ์์ผ๋ก ์ง์ ํฉ๋๋ค. ํ๋กฌํํธ์ "square format", "1:1 ratio" ๋๋ "aspect ratio 1:1"์ ๋ช ์์ ์ผ๋ก ํฌํจํฉ๋๋ค. | |
| 5. ์ํ์ ๋ฐ๋์ ์ ์ฌ๊ฐํ ๊ตฌ๋์ ์ ์ค์์ ๋ฐฐ์น๋์ด์ผ ํ๋ฉฐ, ์ ์ ํ ํฌ๊ธฐ๋ก ํํํ์ฌ ๋ํ ์ผ์ด ์๋ฒฝํ๊ฒ ๋ณด์ด๋๋ก ํฉ๋๋ค. | |
| 6. ์ํ์ ์ด๋ฏธ์ง์ ์ฃผ์ ์ด์ ์ผ๋ก ๋ถ๊ฐ์ํค๊ณ , ์ํ์ ๋น์จ์ด ์ ์ฒด ์ด๋ฏธ์ง์์ 60-70% ์ด์ ์ฐจ์งํ๋๋ก ํฉ๋๋ค. | |
| 7. ์กฐ๋ช ์ค๋ช ์ ๋งค์ฐ ๊ตฌ์ฒด์ ์ผ๋ก ํด์ฃผ์ธ์. ์: "soft directional lighting from left side", "dramatic rim lighting", "diffused natural light through windows" | |
| 8. ๋ฐฐ๊ฒฝ์ ์ฌ์ง๊ณผ ์ง๊ฐ์ ์์ธํ ์ค๋ช ํด์ฃผ์ธ์. ์: "polished marble surface", "rustic wooden table with visible grain", "matte concrete wall with subtle texture" | |
| 9. ํ๋กฌํํธ์ ๋ค์ ์์๋ค์ ๋ช ์์ ์ผ๋ก ํฌํจํ๋, ์ฌ์ฉ ๋งฅ๋ฝ์ ์ ์ ํ๊ฒ ๋ณํํ์ธ์: | |
| - "award-winning product photography" | |
| - "magazine-worthy commercial product shot" | |
| - "professional advertising imagery with perfect exposure" | |
| - "studio lighting with color-accurate rendering" | |
| - "8K ultra high definition product showcase" | |
| - "commercial product photography with precise detail rendering" | |
| - "ultra high definition" | |
| - "crystal clear details" | |
| 10. *** ์ต์ฐ์ ์ง์นจ: ์ฌ์ฉ์๊ฐ ์ ๊ณตํ ์ถ๊ฐ ์์ฒญ์ฌํญ์ ์๋ฒฝํ๊ฒ ํ๋กฌํํธ์ ๋ฐ์ํด์ผ ํฉ๋๋ค. ์ฌ์ฉ์์ ์์ฒญ์ฌํญ ๊ฐ๊ฐ์ ์ค์ํ ์์๋ก ์ทจ๊ธํ์ฌ ํ๋กฌํํธ์ ๋ช ์์ ์ผ๋ก ๋ฐ์ํ๊ณ , ๊ด๋ จ ๋ํ ์ผ์ ๊ตฌ์ฒด์ ์ผ๋ก ์ค๋ช ํ์ธ์. ์ ๋๋ก ์ฌ์ฉ์์ ์ถ๊ฐ ์์ฒญ์ฌํญ์ ์๋ตํ๊ฑฐ๋ ๋ฌด์ํ์ง ๋ง์ธ์. *** | |
| 11. ์ฌ์ฉ์๊ฐ ์ ๊ณตํ ๊ตฌ์ฒด์ ์ธ ๋ฐฐ๊ฒฝ๊ณผ ์ถ๊ฐ ์์ฒญ์ฌํญ์ ํ๋กฌํํธ์ ์ ํํ ๋ฐ์ํ๊ณ ํ์ฅํฉ๋๋ค. | |
| 12. ํ๋กฌํํธ ๋์ "--ar 1:1 --s 750 --q 2 --v 5.2" ํ๋ผ๋ฏธํฐ๋ฅผ ์ถ๊ฐํ์ฌ ๋ฏธ๋์ ๋์์ ๊ณ ํ์ง ์ ์ฌ๊ฐํ ๋น์จ์ ๊ฐ์ ํฉ๋๋ค. | |
| 13. ๋งค์ฐ ์ค์: ํ๋กฌํํธ ์ธ์ ๋ค๋ฅธ ์ค๋ช ์ด๋ ๋ฉํ ํ ์คํธ๋ฅผ ํฌํจํ์ง ๋ง์ธ์. ์๋ฅผ ๋ค์ด "Here's a prompt for you" ๋๋ "Let me know if you need adjustments" ๊ฐ์ ๋ฉ์์ง๋ ์ค๋ช ์ ํฌํจํ์ง ๋ง์ธ์. ์ค์ง ํ๋กฌํํธ ์์ฒด๋ง ์ ๊ณตํ์ธ์. | |
| """ | |
| def generate_prompt_with_gemini(product_name, background_info, additional_info=""): | |
| """ํฅ์๋ ํ๋กฌํํธ ์์ฑ ํจ์""" | |
| GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", "") | |
| if not GEMINI_API_KEY: | |
| return "Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ ๋ณ์ GEMINI_API_KEY๋ฅผ ์ค์ ํ๊ฑฐ๋ ์ฝ๋์ ์ง์ ์ ๋ ฅํ์ธ์." | |
| try: | |
| genai_generative.configure(api_key=GEMINI_API_KEY) | |
| # ์ถ๊ฐ ์์ฒญ์ฌํญ์ ๋ ๊ฐ์กฐํ ํ๋กฌํํธ ์์ฒญ ํ ํ๋ฆฟ | |
| prompt_request = f""" | |
| ์ํ๋ช : {product_name} | |
| ๋ฐฐ๊ฒฝ ์ ํ: {background_info.get('english', 'studio')} | |
| ๋ฐฐ๊ฒฝ ์นดํ ๊ณ ๋ฆฌ: {background_info.get('category', '')} | |
| ๋ฐฐ๊ฒฝ ์ด๋ฆ: {background_info.get('name', '')} | |
| ์ถ๊ฐ ์์ฒญ์ฌํญ: {additional_info} | |
| ์ค์ ์๊ตฌ์ฌํญ: | |
| 1. ์ํ(#1)์ด ์ด๋ฏธ์ง ๊ตฌ๋์์ ์ค์ฌ์ ์ธ ์์น๋ฅผ ์ฐจ์งํ๋ฉฐ ์ ์ ํ ํฌ๊ธฐ(์ด๋ฏธ์ง์ 60-70%)๋ก ํํ๋๋๋ก ํ๋กฌํํธ๋ฅผ ์์ฑํด์ฃผ์ธ์. | |
| 2. ์ด๋ฏธ์ง๋ ์ ํํ 1:1 ๋น์จ(์ ์ฌ๊ฐํ)์ด์ด์ผ ํฉ๋๋ค. | |
| 3. ์ํ์ ๋์์ธ, ์์, ํํ, ๋ก๊ณ ๋ฑ ๋ณธ์ง์ ํน์ฑ์ ์ ๋ ์์ ํ์ง ๋ง์ธ์. | |
| 4. ๊ตฌ์ฒด์ ์ธ ์กฐ๋ช ๊ธฐ๋ฒ์ ์์ธํ ๋ช ์ํด์ฃผ์ธ์: | |
| - ์ ํํ ์กฐ๋ช ์์น (์: "45-degree key light from upper left") | |
| - ์กฐ๋ช ํ์ง (์: "soft diffused light", "hard directional light") | |
| - ์กฐ๋ช ๊ฐ๋์ ์์จ๋ (์: "warm tungsten key light with cool blue fill") | |
| - ๋ฐ์ฌ์ ๊ทธ๋ฆผ์ ์ฒ๋ฆฌ ๋ฐฉ์ (์: "controlled specular highlights with soft shadow transitions") | |
| 5. ์ํ์ ๋ ๋๋ณด์ด๊ฒ ํ๋ ๋ณด์กฐ ์์(props)๋ฅผ ์์ฐ์ค๋ฝ๊ฒ ํ์ฉํ๋, ์ํ์ด ํญ์ ์ฃผ์ธ๊ณต์ด์ด์ผ ํฉ๋๋ค. | |
| 6. ๋ฐฐ๊ฒฝ ์ฌ์ง๊ณผ ํ๋ฉด ์ง๊ฐ์ ๊ตฌ์ฒด์ ์ผ๋ก ์ค๋ช ํ๊ณ , ์ํ๊ณผ์ ์ํธ์์ฉ ๋ฐฉ์์ ๋ช ์ํด์ฃผ์ธ์. | |
| 7. ์์ ๊ตฌ์ฑ(color palette, color harmonies)์ ๋ช ํํ ํด์ฃผ์ธ์. | |
| 8. ๊ณ ๊ธ์ค๋ฌ์ด ์์ ๊ด๊ณ ํ์ง์ ์ด๋ฏธ์ง๊ฐ ๋๋๋ก ํ๋กฌํํธ๋ฅผ ์์ฑํด์ฃผ์ธ์. | |
| 9. ํ๋กฌํํธ ๋์ ๋ฏธ๋์ ๋ ํ๋ผ๋ฏธํฐ "--ar 1:1 --s 750 --q 2 --v 5.2"๋ฅผ ์ถ๊ฐํด์ฃผ์ธ์. | |
| 10. **๋งค์ฐ ์ค์**: ์ฌ์ฉ์๊ฐ ์ ๊ณตํ ์ถ๊ฐ ์์ฒญ์ฌํญ("{additional_info}")์ ํ๋กฌํํธ์ ์๋ฒฝํ๊ฒ ๋ฐ์ํด์ผ ํฉ๋๋ค. ๊ฐ ์์ฒญ์ฌํญ์ ํ๋กฌํํธ์ ์ฃผ์ ๋ถ๋ถ์ ๋ช ์์ ์ผ๋ก ํฌํจ์ํค๊ณ , ๊ด๋ จ ๋ํ ์ผ์ ์์ธํ ๊ธฐ์ ํด์ฃผ์ธ์. | |
| ํ๊ตญ์ด ์ ๋ ฅ ๋ด์ฉ์ ์ ๋ฌธ์ ์ธ ์์ด๋ก ๋ฒ์ญํ์ฌ ๋ฐ์ํด์ฃผ์ธ์. | |
| """ | |
| # ์ถ๊ฐ ์์ฒญ์ฌํญ์ ๊ฐ์กฐํ ์์คํ ์ธ์คํธ๋ญ์ ์์ฑ | |
| enhanced_system_instruction = generate_enhanced_system_instruction() + f""" | |
| ํน๋ณ ์ง์นจ: ์ฌ์ฉ์์ ์ถ๊ฐ ์์ฒญ์ฌํญ์ ์ต์ฐ์ ์์๋ก ์ฒ๋ฆฌํด์ผ ํฉ๋๋ค. ์ถ๊ฐ ์์ฒญ์ฌํญ("{additional_info}")์ ๊ฐ ํญ๋ชฉ์ ํ๋กฌํํธ์ ๋ช ์์ ์ผ๋ก ํฌํจ์ํค๊ณ , ํด๋น ์์ฒญ์ ๊ด๋ จ๋ ๊ตฌ์ฒด์ ์ธ ์ค๋ช ์ ์ถ๊ฐํ์ธ์. ์ฌ์ฉ์์ ์์ฒญ์ฌํญ์ ๋ฌด์ํ๊ฑฐ๋ ์๋ตํ์ง ๋ง์ธ์. | |
| """ | |
| # ๋ ์ฐฝ์์ ์ธ ๊ฒฐ๊ณผ๋ฅผ ์ํ ์์ฑ ์ค์ ์กฐ์ | |
| model = genai_generative.GenerativeModel( | |
| 'gemini-2.0-flash', | |
| system_instruction=enhanced_system_instruction | |
| ) | |
| response = model.generate_content( | |
| prompt_request, | |
| generation_config=genai_generative.types.GenerationConfig( | |
| temperature=0.7, # ์ ํ์ฑ์ ์ํด ์ฝ๊ฐ ๋ฎ์ถค | |
| top_p=0.95, | |
| top_k=40, | |
| max_output_tokens=1600, # ๋ ์์ธํ ํ๋กฌํํธ ํ์ฉ | |
| ) | |
| ) | |
| response_text = response.text.strip() | |
| # ์ถ๊ฐ ์์ฒญ์ฌํญ์ด ํ๋กฌํํธ์ ์ ๋๋ก ๋ฐ์๋์๋์ง ํ์ธํ๋ ๋ก์ง | |
| if additional_info and not any(keyword.lower() in response_text.lower() for keyword in additional_info.split(',')): | |
| # ์ถ๊ฐ ์์ฒญ์ฌํญ์ด ๋ฐ์๋์ง ์์๋ค๋ฉด ์ถ๊ฐ | |
| logger.warning("์ถ๊ฐ ์์ฒญ์ฌํญ์ด ํ๋กฌํํธ์ ๋ฐ์๋์ง ์์์ต๋๋ค. ๊ฐ์ ๋ก ์ถ๊ฐํฉ๋๋ค.") | |
| # ์์ฒญ์ฌํญ์ ์ง์ ํ๋กฌํํธ์ ์ถ๊ฐ (๋ฉํ ํ ์คํธ ์์ด) | |
| if "--ar 1:1" in response_text: | |
| # ๋ฏธ๋์ ๋ ํ๋ผ๋ฏธํฐ ์์ ์ถ๊ฐ | |
| parts = response_text.split("--ar 1:1") | |
| if len(parts) >= 2: | |
| # ์ถ๊ฐ ์์ฒญ์ฌํญ ์ง์ ์ฝ์ | |
| response_text = parts[0].rstrip(" ,") + ", " + additional_info + ". --ar 1:1" + parts[1] | |
| else: | |
| # ๋ฏธ๋์ ๋ ํ๋ผ๋ฏธํฐ๊ฐ ์๋ ๊ฒฝ์ฐ ๋์ ์ถ๊ฐ | |
| response_text = response_text.rstrip(" .") + ". " + additional_info | |
| # ๋ฏธ๋์ ๋ ํ๋ผ๋ฏธํฐ๊ฐ ์์ ๊ฒฝ์ฐ ์ถ๊ฐ | |
| if "--ar 1:1" not in response_text: | |
| response_text = response_text.rstrip(" .") + ". --ar 1:1 --s 750 --q 2 --v 5.2" | |
| return response_text | |
| except Exception as e: | |
| logger.exception("ํ๋กฌํํธ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:") | |
| return f"ํ๋กฌํํธ ์์ฑ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}" | |
| # ------------------- ํ๋กฌํํธ ์์ฑ ์ ์ฉ ํจ์ ------------------- | |
| def generate_prompt_only(image, bg_type, simple, studio, nature, indoor, tech, colorful, abstract, jewelry, product_name, additional_info): | |
| product_name = product_name.strip() or "์ ํ" | |
| background_info = get_selected_background_info(bg_type, simple, studio, nature, indoor, tech, colorful, abstract, jewelry) | |
| generated_prompt = generate_prompt_with_gemini(product_name, background_info, additional_info) | |
| if "Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค" in generated_prompt: | |
| warning_msg = ( | |
| "[Gemini API ํค ๋๋ฝ]\n" | |
| "API ํค ์ค์ ๋ฐฉ๋ฒ:\n" | |
| "1. ํ๊ฒฝ ๋ณ์: export GEMINI_API_KEY=\"your-api-key\"\n" | |
| "2. ์ฝ๋ ๋ด ์ง์ ์ ๋ ฅ: GEMINI_API_KEY = \"your-api-key\"\n" | |
| "ํค ๋ฐ๊ธ: https://makersuite.google.com/" | |
| ) | |
| return warning_msg | |
| final_prompt = filter_prompt_only(generated_prompt) | |
| return final_prompt | |
| # ------------------- Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ ------------------- | |
| def create_app(): | |
| # ๋๋กญ๋ค์ด ์ต์ ์ด๊ธฐํ | |
| dropdown_options = initialize_dropdowns() | |
| with gr.Blocks(title="๊ณ ๊ธ ์ํ ์ด๋ฏธ์ง ๋ฐฐ๊ฒฝ ํ๋กฌํํธ ์์ฑ") as demo: | |
| gr.Markdown("# ๊ณ ๊ธ ์ํ ์ด๋ฏธ์ง ๋ฐฐ๊ฒฝ ํ๋กฌํํธ ์์ฑ") | |
| gr.Markdown( | |
| "์ํ ์ด๋ฏธ์ง๋ฅผ ์ ๋ก๋ํ๊ณ , ์ ํ๋ช , ๋ฐฐ๊ฒฝ ์ต์ , ์ถ๊ฐ ์์ฒญ์ฌํญ์ ์ ๋ ฅํ๋ฉด Gemini API๋ฅผ ํตํด ์์ด ํ๋กฌํํธ๋ฅผ ์์ฑํฉ๋๋ค." | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| product_name = gr.Textbox(label="์ํ๋ช (ํ๊ตญ์ด ์ ๋ ฅ)", placeholder="์: ์คํจ์ผ์ด ํ๋ธ, ์ค๋งํธ์์น, ํฅ์, ์ด๋ํ ๋ฑ", interactive=True) | |
| # ์ํ ์ด๋ฏธ์ง ์ ๋ก๋๋ฅผ ์ํ๋ช ์๋๋ก ์ด๋ | |
| image_input = gr.Image(label="์ํ ์ด๋ฏธ์ง ์ ๋ก๋", type="pil") | |
| background_type = gr.Radio( | |
| choices=["์ฌํ ๋ฐฐ๊ฒฝ", "์คํ๋์ค ๋ฐฐ๊ฒฝ", "์์ฐ ํ๊ฒฝ", "์ค๋ด ํ๊ฒฝ", "ํ ํฌ๋๋ก์ง ๋ฐฐ๊ฒฝ", "์ปฌ๋ฌํ ํจํด ๋ฐฐ๊ฒฝ", "์ถ์/ํน์ ๋ฐฐ๊ฒฝ", "์ฅฌ์ผ๋ฆฌ ๋ฐฐ๊ฒฝ"], | |
| label="๋ฐฐ๊ฒฝ ์ ํ", | |
| value="์ฌํ ๋ฐฐ๊ฒฝ" | |
| ) | |
| simple_dropdown = gr.Dropdown( | |
| choices=dropdown_options["simple"], | |
| value=dropdown_options["simple"][0] if dropdown_options["simple"] else None, | |
| label="์ฌํ ๋ฐฐ๊ฒฝ ์ ํ", | |
| visible=True, | |
| interactive=True | |
| ) | |
| studio_dropdown = gr.Dropdown( | |
| choices=dropdown_options["studio"], | |
| value=dropdown_options["studio"][0] if dropdown_options["studio"] else None, | |
| label="์คํ๋์ค ๋ฐฐ๊ฒฝ ์ ํ", | |
| visible=False, | |
| interactive=True | |
| ) | |
| nature_dropdown = gr.Dropdown( | |
| choices=dropdown_options["nature"], | |
| value=dropdown_options["nature"][0] if dropdown_options["nature"] else None, | |
| label="์์ฐ ํ๊ฒฝ ์ ํ", | |
| visible=False, | |
| interactive=True | |
| ) | |
| indoor_dropdown = gr.Dropdown( | |
| choices=dropdown_options["indoor"], | |
| value=dropdown_options["indoor"][0] if dropdown_options["indoor"] else None, | |
| label="์ค๋ด ํ๊ฒฝ ์ ํ", | |
| visible=False, | |
| interactive=True | |
| ) | |
| tech_dropdown = gr.Dropdown( | |
| choices=dropdown_options["tech"], | |
| value=dropdown_options["tech"][0] if dropdown_options["tech"] else None, | |
| label="ํ ํฌ๋๋ก์ง ๋ฐฐ๊ฒฝ ์ ํ", | |
| visible=False, | |
| interactive=True | |
| ) | |
| colorful_dropdown = gr.Dropdown( | |
| choices=dropdown_options["colorful"], | |
| value=dropdown_options["colorful"][0] if dropdown_options["colorful"] else None, | |
| label="์ปฌ๋ฌํ ํจํด ๋ฐฐ๊ฒฝ ์ ํ", | |
| visible=False, | |
| interactive=True | |
| ) | |
| abstract_dropdown = gr.Dropdown( | |
| choices=dropdown_options["abstract"], | |
| value=dropdown_options["abstract"][0] if dropdown_options["abstract"] else None, | |
| label="์ถ์/ํน์ ๋ฐฐ๊ฒฝ ์ ํ", | |
| visible=False, | |
| interactive=True | |
| ) | |
| jewelry_dropdown = gr.Dropdown( | |
| choices=dropdown_options["jewelry"], | |
| value=dropdown_options["jewelry"][0] if dropdown_options["jewelry"] else None, | |
| label="์ฅฌ์ผ๋ฆฌ ๋ฐฐ๊ฒฝ ์ ํ", | |
| visible=False, | |
| interactive=True | |
| ) | |
| additional_info = gr.Textbox( | |
| label="์ถ๊ฐ ์์ฒญ์ฌํญ (์ ํ์ฌํญ)", | |
| placeholder="์: ๊ณ ๊ธ์ค๋ฌ์ด ๋๋, ๋ฐ์ ์กฐ๋ช , ์์ฐ์ค๋ฌ์ด ๋ณด์กฐ ๊ฐ์ฒด ๋ฑ", | |
| lines=3, | |
| interactive=True | |
| ) | |
| def update_dropdowns(bg_type): | |
| return { | |
| simple_dropdown: gr.update(visible=(bg_type == "์ฌํ ๋ฐฐ๊ฒฝ")), | |
| studio_dropdown: gr.update(visible=(bg_type == "์คํ๋์ค ๋ฐฐ๊ฒฝ")), | |
| nature_dropdown: gr.update(visible=(bg_type == "์์ฐ ํ๊ฒฝ")), | |
| indoor_dropdown: gr.update(visible=(bg_type == "์ค๋ด ํ๊ฒฝ")), | |
| tech_dropdown: gr.update(visible=(bg_type == "ํ ํฌ๋๋ก์ง ๋ฐฐ๊ฒฝ")), | |
| colorful_dropdown: gr.update(visible=(bg_type == "์ปฌ๋ฌํ ํจํด ๋ฐฐ๊ฒฝ")), | |
| abstract_dropdown: gr.update(visible=(bg_type == "์ถ์/ํน์ ๋ฐฐ๊ฒฝ")), | |
| jewelry_dropdown: gr.update(visible=(bg_type == "์ฅฌ์ผ๋ฆฌ ๋ฐฐ๊ฒฝ")) # ์ฅฌ์ผ๋ฆฌ ๋๋กญ๋ค์ด ์ถ๊ฐ | |
| } | |
| background_type.change( | |
| fn=update_dropdowns, | |
| inputs=[background_type], | |
| outputs=[simple_dropdown, studio_dropdown, nature_dropdown, indoor_dropdown, tech_dropdown, colorful_dropdown, abstract_dropdown, jewelry_dropdown] | |
| ) | |
| # ํ๋กฌํํธ ์์ฑ ๋ฒํผ์ผ๋ก ๋ณ๊ฒฝ | |
| prompt_btn = gr.Button("ํ๋กฌํํธ ์์ฑ", variant="primary") | |
| with gr.Column(scale=1): | |
| # ํ๋กฌํํธ ์ถ๋ ฅ ์์ญ๋ง ์ ์ง | |
| prompt_output = gr.Textbox(label="์์ฑ๋ ํ๋กฌํํธ (์์ด)", lines=10) | |
| # ํ๋กฌํํธ ์์ฑ ํจ์ ์ฐ๊ฒฐ | |
| prompt_btn.click( | |
| fn=generate_prompt_only, | |
| inputs=[image_input, background_type, simple_dropdown, studio_dropdown, nature_dropdown, indoor_dropdown, tech_dropdown, colorful_dropdown, abstract_dropdown, jewelry_dropdown, product_name, additional_info], | |
| outputs=prompt_output | |
| ) | |
| return demo | |
| # ------------------- ๋ฉ์ธ ์คํ ํจ์ ------------------- | |
| if __name__ == "__main__": | |
| # ๋ฐฐ๊ฒฝ ์ต์ ์ด๊ธฐํ - JSON ํ์ผ์์ ๋ก๋ | |
| initialize_backgrounds() | |
| # ์ฑ ์์ฑ ๋ฐ ์คํ | |
| app = create_app() | |
| app.queue() | |
| app.launch() |