Delete app-backup2.py
Browse files- app-backup2.py +0 -404
 
    	
        app-backup2.py
    DELETED
    
    | 
         @@ -1,404 +0,0 @@ 
     | 
|
| 1 | 
         
            -
            # ===== CRITICAL: Import spaces FIRST before any CUDA operations =====
         
     | 
| 2 | 
         
            -
            try:
         
     | 
| 3 | 
         
            -
                import spaces
         
     | 
| 4 | 
         
            -
                HF_SPACES = True
         
     | 
| 5 | 
         
            -
            except ImportError:
         
     | 
| 6 | 
         
            -
                # If running locally, create a dummy decorator
         
     | 
| 7 | 
         
            -
                def spaces_gpu_decorator(duration=60):
         
     | 
| 8 | 
         
            -
                    def decorator(func):
         
     | 
| 9 | 
         
            -
                        return func
         
     | 
| 10 | 
         
            -
                    return decorator
         
     | 
| 11 | 
         
            -
                spaces = type('spaces', (), {'GPU': spaces_gpu_decorator})()
         
     | 
| 12 | 
         
            -
                HF_SPACES = False
         
     | 
| 13 | 
         
            -
                print("Warning: Running without Hugging Face Spaces GPU allocation")
         
     | 
| 14 | 
         
            -
             
     | 
| 15 | 
         
            -
            # ===== Now import other libraries =====
         
     | 
| 16 | 
         
            -
            import random
         
     | 
| 17 | 
         
            -
            import os
         
     | 
| 18 | 
         
            -
            import uuid
         
     | 
| 19 | 
         
            -
            import re
         
     | 
| 20 | 
         
            -
            import time
         
     | 
| 21 | 
         
            -
            from datetime import datetime
         
     | 
| 22 | 
         
            -
             
     | 
| 23 | 
         
            -
            import gradio as gr
         
     | 
| 24 | 
         
            -
            import numpy as np
         
     | 
| 25 | 
         
            -
            import requests
         
     | 
| 26 | 
         
            -
            import torch
         
     | 
| 27 | 
         
            -
            from diffusers import DiffusionPipeline
         
     | 
| 28 | 
         
            -
            from PIL import Image
         
     | 
| 29 | 
         
            -
             
     | 
| 30 | 
         
            -
            # ===== OpenAI ์ค์  =====
         
     | 
| 31 | 
         
            -
            from openai import OpenAI
         
     | 
| 32 | 
         
            -
             
     | 
| 33 | 
         
            -
            # Add error handling for API key
         
     | 
| 34 | 
         
            -
            try:
         
     | 
| 35 | 
         
            -
                client = OpenAI(api_key=os.getenv("LLM_API"))
         
     | 
| 36 | 
         
            -
            except Exception as e:
         
     | 
| 37 | 
         
            -
                print(f"Warning: OpenAI client initialization failed: {e}")
         
     | 
| 38 | 
         
            -
                client = None
         
     | 
| 39 | 
         
            -
             
     | 
| 40 | 
         
            -
            # ===== ํ๋กฌํํธ ์ฆ๊ฐ์ฉ ์คํ์ผ ํ๋ฆฌ์
 =====
         
     | 
| 41 | 
         
            -
            STYLE_PRESETS = {
         
     | 
| 42 | 
         
            -
                "None": "",
         
     | 
| 43 | 
         
            -
                "Realistic Photo": "photorealistic, 8k, ultra-detailed, cinematic lighting, realistic skin texture",
         
     | 
| 44 | 
         
            -
                "Oil Painting": "oil painting, rich brush strokes, canvas texture, baroque lighting",
         
     | 
| 45 | 
         
            -
                "Comic Book": "comic book style, bold ink outlines, cel shading, vibrant colors",
         
     | 
| 46 | 
         
            -
                "Watercolor": "watercolor illustration, soft gradients, splatter effect, pastel palette",
         
     | 
| 47 | 
         
            -
            }
         
     | 
| 48 | 
         
            -
             
     | 
| 49 | 
         
            -
            # ===== ์ ์ฅ ํด๋ =====
         
     | 
| 50 | 
         
            -
            SAVE_DIR = "saved_images"
         
     | 
| 51 | 
         
            -
            if not os.path.exists(SAVE_DIR):
         
     | 
| 52 | 
         
            -
                os.makedirs(SAVE_DIR, exist_ok=True)
         
     | 
| 53 | 
         
            -
             
     | 
| 54 | 
         
            -
            # ===== ๋๋ฐ์ด์ค & ๋ชจ๋ธ ๋ก๋ =====
         
     | 
| 55 | 
         
            -
            device = "cuda" if torch.cuda.is_available() else "cpu"
         
     | 
| 56 | 
         
            -
            print(f"Using device: {device}")
         
     | 
| 57 | 
         
            -
             
     | 
| 58 | 
         
            -
            repo_id = "black-forest-labs/FLUX.1-dev"
         
     | 
| 59 | 
         
            -
            adapter_id = "seawolf2357/kim-korea"
         
     | 
| 60 | 
         
            -
             
     | 
| 61 | 
         
            -
            # Add error handling for model loading
         
     | 
| 62 | 
         
            -
            try:
         
     | 
| 63 | 
         
            -
                pipeline = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=torch.bfloat16)
         
     | 
| 64 | 
         
            -
                pipeline.load_lora_weights(adapter_id)
         
     | 
| 65 | 
         
            -
                pipeline = pipeline.to(device)
         
     | 
| 66 | 
         
            -
                print("Model loaded successfully")
         
     | 
| 67 | 
         
            -
            except Exception as e:
         
     | 
| 68 | 
         
            -
                print(f"Error loading model: {e}")
         
     | 
| 69 | 
         
            -
                pipeline = None
         
     | 
| 70 | 
         
            -
             
     | 
| 71 | 
         
            -
            MAX_SEED = np.iinfo(np.int32).max
         
     | 
| 72 | 
         
            -
            MAX_IMAGE_SIZE = 1024
         
     | 
| 73 | 
         
            -
             
     | 
| 74 | 
         
            -
            # ===== ํ๊ธ ์ฌ๋ถ ํ๋ณ =====
         
     | 
| 75 | 
         
            -
            HANGUL_RE = re.compile(r"[\u3131-\u318E\uAC00-\uD7A3]+")
         
     | 
| 76 | 
         
            -
             
     | 
| 77 | 
         
            -
            def is_korean(text: str) -> bool:
         
     | 
| 78 | 
         
            -
                return bool(HANGUL_RE.search(text))
         
     | 
| 79 | 
         
            -
             
     | 
| 80 | 
         
            -
            # ===== ๋ฒ์ญ & ์ฆ๊ฐ ํจ์ =====
         
     | 
| 81 | 
         
            -
             
     | 
| 82 | 
         
            -
            def openai_translate(text: str, retries: int = 3) -> str:
         
     | 
| 83 | 
         
            -
                """ํ๊ธ์ ์์ด๋ก ๋ฒ์ญ (OpenAI GPT-4o-mini ์ฌ์ฉ). ์์ด ์
๋ ฅ์ด๋ฉด ๊ทธ๋๋ก ๋ฐํ."""
         
     | 
| 84 | 
         
            -
                if not is_korean(text):
         
     | 
| 85 | 
         
            -
                    return text
         
     | 
| 86 | 
         
            -
                
         
     | 
| 87 | 
         
            -
                if client is None:
         
     | 
| 88 | 
         
            -
                    print("Warning: OpenAI client not available, returning original text")
         
     | 
| 89 | 
         
            -
                    return text
         
     | 
| 90 | 
         
            -
             
     | 
| 91 | 
         
            -
                for attempt in range(retries):
         
     | 
| 92 | 
         
            -
                    try:
         
     | 
| 93 | 
         
            -
                        res = client.chat.completions.create(
         
     | 
| 94 | 
         
            -
                            model="gpt-4o-mini",
         
     | 
| 95 | 
         
            -
                            messages=[
         
     | 
| 96 | 
         
            -
                                {
         
     | 
| 97 | 
         
            -
                                    "role": "system",
         
     | 
| 98 | 
         
            -
                                    "content": "Translate the following Korean prompt into concise, descriptive English suitable for an image generation model. Keep the meaning, do not add new concepts."
         
     | 
| 99 | 
         
            -
                                },
         
     | 
| 100 | 
         
            -
                                {"role": "user", "content": text}
         
     | 
| 101 | 
         
            -
                            ],
         
     | 
| 102 | 
         
            -
                            temperature=0.3,
         
     | 
| 103 | 
         
            -
                            max_tokens=256,
         
     | 
| 104 | 
         
            -
                        )
         
     | 
| 105 | 
         
            -
                        return res.choices[0].message.content.strip()
         
     | 
| 106 | 
         
            -
                    except Exception as e:
         
     | 
| 107 | 
         
            -
                        print(f"[translate] attempt {attempt + 1} failed: {e}")
         
     | 
| 108 | 
         
            -
                        time.sleep(2)
         
     | 
| 109 | 
         
            -
                return text  # ๋ฒ์ญ ์คํจ ์ ์๋ฌธ ๊ทธ๋๋ก
         
     | 
| 110 | 
         
            -
             
     | 
| 111 | 
         
            -
            def enhance_prompt(text: str, retries: int = 3) -> str:
         
     | 
| 112 | 
         
            -
                """OpenAI๋ฅผ ํตํด ํ๋กฌํํธ๋ฅผ ์ฆ๊ฐํ์ฌ ๊ณ ํ์ง ์ด๋ฏธ์ง ์์ฑ์ ์ํ ์์ธํ ์ค๋ช
์ผ๋ก ๋ณํ."""
         
     | 
| 113 | 
         
            -
                if client is None:
         
     | 
| 114 | 
         
            -
                    print("Warning: OpenAI client not available, returning original text")
         
     | 
| 115 | 
         
            -
                    return text
         
     | 
| 116 | 
         
            -
             
     | 
| 117 | 
         
            -
                for attempt in range(retries):
         
     | 
| 118 | 
         
            -
                    try:
         
     | 
| 119 | 
         
            -
                        res = client.chat.completions.create(
         
     | 
| 120 | 
         
            -
                            model="gpt-4o-mini",
         
     | 
| 121 | 
         
            -
                            messages=[
         
     | 
| 122 | 
         
            -
                                {
         
     | 
| 123 | 
         
            -
                                    "role": "system",
         
     | 
| 124 | 
         
            -
                                    "content": """You are an expert prompt engineer for image generation models. Enhance the given prompt to create high-quality, detailed images.
         
     | 
| 125 | 
         
            -
             
     | 
| 126 | 
         
            -
            Guidelines:
         
     | 
| 127 | 
         
            -
            - Add specific visual details (lighting, composition, colors, textures)
         
     | 
| 128 | 
         
            -
            - Include technical photography terms (depth of field, focal length, etc.)
         
     | 
| 129 | 
         
            -
            - Add atmosphere and mood descriptors
         
     | 
| 130 | 
         
            -
            - Specify image quality terms (4K, ultra-detailed, professional, etc.)
         
     | 
| 131 | 
         
            -
            - Keep the core subject and meaning intact
         
     | 
| 132 | 
         
            -
            - Make it comprehensive but not overly long
         
     | 
| 133 | 
         
            -
            - Focus on visual elements that will improve image generation quality
         
     | 
| 134 | 
         
            -
             
     | 
| 135 | 
         
            -
            Example:
         
     | 
| 136 | 
         
            -
            Input: "A man giving a speech"
         
     | 
| 137 | 
         
            -
            Output: "A professional man giving an inspiring speech at a podium, dramatic lighting with warm spotlights, confident posture and gestures, high-resolution 4K photography, sharp focus, cinematic composition, bokeh background with audience silhouettes, professional event setting, detailed facial expressions, realistic skin texture"
         
     | 
| 138 | 
         
            -
            """
         
     | 
| 139 | 
         
            -
                                },
         
     | 
| 140 | 
         
            -
                                {"role": "user", "content": f"Enhance this prompt for high-quality image generation: {text}"}
         
     | 
| 141 | 
         
            -
                            ],
         
     | 
| 142 | 
         
            -
                            temperature=0.7,
         
     | 
| 143 | 
         
            -
                            max_tokens=512,
         
     | 
| 144 | 
         
            -
                        )
         
     | 
| 145 | 
         
            -
                        return res.choices[0].message.content.strip()
         
     | 
| 146 | 
         
            -
                    except Exception as e:
         
     | 
| 147 | 
         
            -
                        print(f"[enhance] attempt {attempt + 1} failed: {e}")
         
     | 
| 148 | 
         
            -
                        time.sleep(2)
         
     | 
| 149 | 
         
            -
                return text  # ์ฆ๊ฐ ์คํจ ์ ์๋ฌธ ๊ทธ๋๋ก
         
     | 
| 150 | 
         
            -
             
     | 
| 151 | 
         
            -
            def prepare_prompt(user_prompt: str, style_key: str, enhance_prompt_enabled: bool = False) -> str:
         
     | 
| 152 | 
         
            -
                """ํ๊ธ์ด๋ฉด ๋ฒ์ญํ๊ณ , ํ๋กฌํํธ ์ฆ๊ฐ ์ต์
์ด ํ์ฑํ๋๋ฉด ์ฆ๊ฐํ๊ณ , ์ ํํ ์คํ์ผ ํ๋ฆฌ์
์ ๋ถ์ฌ์ ์ต์ข
 ํ๋กฌํํธ๋ฅผ ๋ง๋ ๋ค."""
         
     | 
| 153 | 
         
            -
                # 1. ๋ฒ์ญ (ํ๊ธ์ธ ๊ฒฝ์ฐ)
         
     | 
| 154 | 
         
            -
                prompt_en = openai_translate(user_prompt)
         
     | 
| 155 | 
         
            -
                
         
     | 
| 156 | 
         
            -
                # 2. ํ๋กฌํํธ ์ฆ๊ฐ (ํ์ฑํ๋ ๊ฒฝ์ฐ)
         
     | 
| 157 | 
         
            -
                if enhance_prompt_enabled:
         
     | 
| 158 | 
         
            -
                    prompt_en = enhance_prompt(prompt_en)
         
     | 
| 159 | 
         
            -
                    print(f"Enhanced prompt: {prompt_en}")
         
     | 
| 160 | 
         
            -
                
         
     | 
| 161 | 
         
            -
                # 3. ์คํ์ผ ํ๋ฆฌ์
 ์ ์ฉ
         
     | 
| 162 | 
         
            -
                style_suffix = STYLE_PRESETS.get(style_key, "")
         
     | 
| 163 | 
         
            -
                if style_suffix:
         
     | 
| 164 | 
         
            -
                    final_prompt = f"{prompt_en}, {style_suffix}"
         
     | 
| 165 | 
         
            -
                else:
         
     | 
| 166 | 
         
            -
                    final_prompt = prompt_en
         
     | 
| 167 | 
         
            -
                
         
     | 
| 168 | 
         
            -
                return final_prompt
         
     | 
| 169 | 
         
            -
             
     | 
| 170 | 
         
            -
            # ===== ์ด๋ฏธ์ง ์ ์ฅ =====
         
     | 
| 171 | 
         
            -
             
     | 
| 172 | 
         
            -
            def save_generated_image(image: Image.Image, prompt: str) -> str:
         
     | 
| 173 | 
         
            -
                timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
         
     | 
| 174 | 
         
            -
                unique_id = str(uuid.uuid4())[:8]
         
     | 
| 175 | 
         
            -
                filename = f"{timestamp}_{unique_id}.png"
         
     | 
| 176 | 
         
            -
                filepath = os.path.join(SAVE_DIR, filename)
         
     | 
| 177 | 
         
            -
                image.save(filepath)
         
     | 
| 178 | 
         
            -
             
     | 
| 179 | 
         
            -
                # ๋ฉํ๋ฐ์ดํฐ ์ ์ฅ
         
     | 
| 180 | 
         
            -
                metadata_file = os.path.join(SAVE_DIR, "metadata.txt")
         
     | 
| 181 | 
         
            -
                with open(metadata_file, "a", encoding="utf-8") as f:
         
     | 
| 182 | 
         
            -
                    f.write(f"{filename}|{prompt}|{timestamp}\n")
         
     | 
| 183 | 
         
            -
                return filepath
         
     | 
| 184 | 
         
            -
             
     | 
| 185 | 
         
            -
            # ===== Diffusion ํธ์ถ =====
         
     | 
| 186 | 
         
            -
             
     | 
| 187 | 
         
            -
            def run_pipeline(prompt: str, seed: int, width: int, height: int, guidance_scale: float, num_steps: int, lora_scale: float):
         
     | 
| 188 | 
         
            -
                if pipeline is None:
         
     | 
| 189 | 
         
            -
                    raise ValueError("Model pipeline not loaded")
         
     | 
| 190 | 
         
            -
                
         
     | 
| 191 | 
         
            -
                generator = torch.Generator(device=device).manual_seed(int(seed))
         
     | 
| 192 | 
         
            -
                result = pipeline(
         
     | 
| 193 | 
         
            -
                    prompt=prompt,
         
     | 
| 194 | 
         
            -
                    guidance_scale=guidance_scale,
         
     | 
| 195 | 
         
            -
                    num_inference_steps=num_steps,
         
     | 
| 196 | 
         
            -
                    width=width,
         
     | 
| 197 | 
         
            -
                    height=height,
         
     | 
| 198 | 
         
            -
                    generator=generator,
         
     | 
| 199 | 
         
            -
                    joint_attention_kwargs={"scale": lora_scale},
         
     | 
| 200 | 
         
            -
                ).images[0]
         
     | 
| 201 | 
         
            -
                return result
         
     | 
| 202 | 
         
            -
             
     | 
| 203 | 
         
            -
            # ===== Gradio inference ๋ํผ =====
         
     | 
| 204 | 
         
            -
             
     | 
| 205 | 
         
            -
            @spaces.GPU(duration=60)
         
     | 
| 206 | 
         
            -
            def generate_image(
         
     | 
| 207 | 
         
            -
                user_prompt: str,
         
     | 
| 208 | 
         
            -
                style_key: str,
         
     | 
| 209 | 
         
            -
                enhance_prompt_enabled: bool = False,
         
     | 
| 210 | 
         
            -
                seed: int = 42,
         
     | 
| 211 | 
         
            -
                randomize_seed: bool = True,
         
     | 
| 212 | 
         
            -
                width: int = 1024,
         
     | 
| 213 | 
         
            -
                height: int = 768,
         
     | 
| 214 | 
         
            -
                guidance_scale: float = 3.5,
         
     | 
| 215 | 
         
            -
                num_inference_steps: int = 30,
         
     | 
| 216 | 
         
            -
                lora_scale: float = 1.0,
         
     | 
| 217 | 
         
            -
                progress=None,
         
     | 
| 218 | 
         
            -
            ):
         
     | 
| 219 | 
         
            -
                try:
         
     | 
| 220 | 
         
            -
                    if randomize_seed:
         
     | 
| 221 | 
         
            -
                        seed = random.randint(0, MAX_SEED)
         
     | 
| 222 | 
         
            -
             
     | 
| 223 | 
         
            -
                    # 1) ๋ฒ์ญ + ์ฆ๊ฐ
         
     | 
| 224 | 
         
            -
                    final_prompt = prepare_prompt(user_prompt, style_key, enhance_prompt_enabled)
         
     | 
| 225 | 
         
            -
                    print(f"Final prompt: {final_prompt}")
         
     | 
| 226 | 
         
            -
             
     | 
| 227 | 
         
            -
                    # 2) ํ์ดํ๋ผ์ธ ํธ์ถ
         
     | 
| 228 | 
         
            -
                    image = run_pipeline(final_prompt, seed, width, height, guidance_scale, num_inference_steps, lora_scale)
         
     | 
| 229 | 
         
            -
             
     | 
| 230 | 
         
            -
                    # 3) ์ ์ฅ
         
     | 
| 231 | 
         
            -
                    save_generated_image(image, final_prompt)
         
     | 
| 232 | 
         
            -
             
     | 
| 233 | 
         
            -
                    return image, seed
         
     | 
| 234 | 
         
            -
                
         
     | 
| 235 | 
         
            -
                except Exception as e:
         
     | 
| 236 | 
         
            -
                    print(f"Error generating image: {e}")
         
     | 
| 237 | 
         
            -
                    # Return a placeholder or error message
         
     | 
| 238 | 
         
            -
                    error_image = Image.new('RGB', (width, height), color='red')
         
     | 
| 239 | 
         
            -
                    return error_image, seed
         
     | 
| 240 | 
         
            -
             
     | 
| 241 | 
         
            -
            # ===== ์์ ํ๋กฌํํธ (ํ๊ตญ์ด/์์ด ํผ์ฉ ํ์ฉ) =====
         
     | 
| 242 | 
         
            -
             
     | 
| 243 | 
         
            -
            examples = [
         
     | 
| 244 | 
         
            -
                "Mr. KIM์ด ๋ ์์ผ๋ก 'Fighting!' ํ์๋ง์ ๋ค๊ณ  ์๋ ๋ชจ์ต, ์ ๊ตญ์ฌ๊ณผ ๊ตญ๊ฐ ๋ฐ์ ์ ๋ํ ์์ง๋ฅผ ๋ณด์ฌ์ฃผ๊ณ  ์๋ค.",   
         
     | 
| 245 | 
         
            -
                "Mr. KIM์ด ์ํ์ ๋ค์ด ์ฌ๋ฆฌ๋ฉฐ ์น๋ฆฌ์ ํ์ ์ผ๋ก ํํธํ๋ ๋ชจ์ต, ์น๋ฆฌ์ ๋ฏธ๋์ ๋ํ ํฌ๋ง์ ๋ณด์ฌ์ฃผ๊ณ  ์๋ค.",
         
     | 
| 246 | 
         
            -
                "Mr. KIM์ด ์ด๋๋ณต์ ์
๊ณ  ๊ณต์์์ ์กฐ๊น
ํ๋ ๋ชจ์ต, ๊ฑด๊ฐํ ์ํ์ต๊ด๊ณผ ํ๊ธฐ์ฐฌ ๋ฆฌ๋์ญ์ ๋ณด์ฌ์ฃผ๊ณ  ์๋ค.",  
         
     | 
| 247 | 
         
            -
                "Mr. KIM์ด ๋ถ๋น๋ ๊ฑฐ๋ฆฌ์์ ์ฌ์ฑ ์๋ฏผ๋ค๊ณผ ๋ฐ๋ปํ๊ฒ ์
์ํ๋ ๋ชจ์ต, ์ฌ์ฑ ์ ๊ถ์๋ค์ ๋ํ ์ง์ ํ ๊ด์ฌ๊ณผ ์ํต์ ๋ณด์ฌ์ฃผ๊ณ  ์๋ค.",
         
     | 
| 248 | 
         
            -
                "Mr. KIM์ด ์ ๊ฑฐ ์ ์ธ์ฅ์์ ์งํ์ ์ ํฅํด ์๊ฐ๋ฝ์ผ๋ก ๊ฐ๋ฆฌํค๋ฉฐ ์๊ฐ์ ์ฃผ๋ ์ ์ค์ฒ๋ฅผ ์ทจํ๊ณ  ์๊ณ , ์ฌ์ฑ๋ค๊ณผ ์์ด๋ค์ด ๋ฐ์๋ฅผ ์น๊ณ  ์๋ค.",
         
     | 
| 249 | 
         
            -
                "Mr. KIM์ด ์ง์ญ ํ์ฌ์ ์ฐธ์ฌํ์ฌ ์ด์ ์ ์ผ๋ก ์์ํ๋ ์ฌ์ฑ ์ง์ง์๋ค์๊ฒ ๋๋ฌ์ธ์ฌ ์๋ ๋ชจ์ต.",
         
     | 
| 250 | 
         
            -
                "Mr. KIM visiting a local market, engaging in friendly conversation with female vendors and shopkeepers.",
         
     | 
| 251 | 
         
            -
                "Mr. KIM walking through a university campus, discussing education policies with female students and professors.",    
         
     | 
| 252 | 
         
            -
                "Mr. KIM delivering a powerful speech in front of a large crowd with confident gestures and determined expression.",
         
     | 
| 253 | 
         
            -
                "Mr. KIM in a dynamic interview setting, passionately outlining his visions for the future.",
         
     | 
| 254 | 
         
            -
                "Mr. KIM preparing for an important debate, surrounded by paperwork, looking focused and resolute.",
         
     | 
| 255 | 
         
            -
            ]
         
     | 
| 256 | 
         
            -
             
     | 
| 257 | 
         
            -
            # ===== ์ปค์คํ
 CSS (๋ถ์ ํค ์ ์ง) =====
         
     | 
| 258 | 
         
            -
            custom_css = """
         
     | 
| 259 | 
         
            -
            :root {
         
     | 
| 260 | 
         
            -
                --color-primary: #8F1A3A;
         
     | 
| 261 | 
         
            -
                --color-secondary: #FF4B4B;
         
     | 
| 262 | 
         
            -
                --background-fill-primary: linear-gradient(to right, #FFF5F5, #FED7D7, #FEB2B2);
         
     | 
| 263 | 
         
            -
            }
         
     | 
| 264 | 
         
            -
            footer {visibility: hidden;}
         
     | 
| 265 | 
         
            -
            .gradio-container {background: var(--background-fill-primary);} 
         
     | 
| 266 | 
         
            -
            .title {color: var(--color-primary)!important; font-size:3rem!important; font-weight:700!important; text-align:center; margin:1rem 0; font-family:'Playfair Display',serif;}
         
     | 
| 267 | 
         
            -
            .subtitle {color:#4A5568!important; font-size:1.2rem!important; text-align:center; margin-bottom:1.5rem; font-style:italic;}
         
     | 
| 268 | 
         
            -
            .collection-link {text-align:center; margin-bottom:2rem; font-size:1.1rem;}
         
     | 
| 269 | 
         
            -
            .collection-link a {color:var(--color-primary); text-decoration:underline; transition:color .3s ease;}
         
     | 
| 270 | 
         
            -
            .collection-link a:hover {color:var(--color-secondary);} 
         
     | 
| 271 | 
         
            -
            .model-description{background:rgba(255,255,255,.8); border-radius:12px; padding:24px; margin:20px 0; box-shadow:0 4px 12px rgba(0,0,0,.05); border-left:5px solid var(--color-primary);} 
         
     | 
| 272 | 
         
            -
            button.primary{background:var(--color-primary)!important; color:#fff!important; transition:all .3s ease;} 
         
     | 
| 273 | 
         
            -
            button:hover{transform:translateY(-2px); box-shadow:0 5px 15px rgba(0,0,0,.1);} 
         
     | 
| 274 | 
         
            -
            .input-container{border-radius:10px; box-shadow:0 2px 8px rgba(0,0,0,.05); background:rgba(255,255,255,.6); padding:20px; margin-bottom:1rem;} 
         
     | 
| 275 | 
         
            -
            .advanced-settings{margin-top:1rem; padding:1rem; border-radius:10px; background:rgba(255,255,255,.6);} 
         
     | 
| 276 | 
         
            -
            .example-region{background:rgba(255,255,255,.5); border-radius:10px; padding:1rem; margin-top:1rem;} 
         
     | 
| 277 | 
         
            -
             
     | 
| 278 | 
         
            -
            /* ํ๋กฌํํธ ์
๋ ฅ์นธ ํฌ๊ธฐ 2๋ฐฐ ์ฆ๊ฐ */
         
     | 
| 279 | 
         
            -
            .large-prompt textarea {
         
     | 
| 280 | 
         
            -
                min-height: 120px !important;
         
     | 
| 281 | 
         
            -
                font-size: 16px !important;
         
     | 
| 282 | 
         
            -
                line-height: 1.5 !important;
         
     | 
| 283 | 
         
            -
            }
         
     | 
| 284 | 
         
            -
             
     | 
| 285 | 
         
            -
            /* ์์ฑ ๋ฒํผ ์๊ฒ ๋ง๋ค๊ธฐ */
         
     | 
| 286 | 
         
            -
            .small-generate-btn {
         
     | 
| 287 | 
         
            -
                max-width: 120px !important;
         
     | 
| 288 | 
         
            -
                height: 40px !important;
         
     | 
| 289 | 
         
            -
                font-size: 14px !important;
         
     | 
| 290 | 
         
            -
                padding: 8px 16px !important;
         
     | 
| 291 | 
         
            -
            }
         
     | 
| 292 | 
         
            -
             
     | 
| 293 | 
         
            -
            /* ํ๋กฌํํธ ์ฆ๊ฐ ์น์
 ์คํ์ผ */
         
     | 
| 294 | 
         
            -
            .prompt-enhance-section {
         
     | 
| 295 | 
         
            -
                background: rgba(255,255,255,.7);
         
     | 
| 296 | 
         
            -
                border-radius: 8px;
         
     | 
| 297 | 
         
            -
                padding: 15px;
         
     | 
| 298 | 
         
            -
                margin-top: 10px;
         
     | 
| 299 | 
         
            -
                border-left: 3px solid var(--color-primary);
         
     | 
| 300 | 
         
            -
            }
         
     | 
| 301 | 
         
            -
             
     | 
| 302 | 
         
            -
            /* ์คํ์ผ ํ๋ฆฌ์
 ์น์
 */
         
     | 
| 303 | 
         
            -
            .style-preset-section {
         
     | 
| 304 | 
         
            -
                background: rgba(255,255,255,.6);
         
     | 
| 305 | 
         
            -
                border-radius: 8px;
         
     | 
| 306 | 
         
            -
                padding: 15px;
         
     | 
| 307 | 
         
            -
                margin-top: 10px;
         
     | 
| 308 | 
         
            -
            }
         
     | 
| 309 | 
         
            -
            """
         
     | 
| 310 | 
         
            -
             
     | 
| 311 | 
         
            -
            # ===== Gradio UI =====
         
     | 
| 312 | 
         
            -
            def create_interface():
         
     | 
| 313 | 
         
            -
                with gr.Blocks(css=custom_css, analytics_enabled=False) as demo:
         
     | 
| 314 | 
         
            -
                    gr.HTML('<div class="title">Mr. KIM in KOREA</div>')
         
     | 
| 315 | 
         
            -
                    gr.HTML('<div class="collection-link"><a href="https://huggingface.co/collections/openfree/painting-art-ai-681453484ec15ef5978bbeb1" target="_blank">Visit the LoRA Model Collection</a></div>')
         
     | 
| 316 | 
         
            -
             
     | 
| 317 | 
         
            -
                    with gr.Group(elem_classes="model-description"):
         
     | 
| 318 | 
         
            -
                        gr.HTML("""
         
     | 
| 319 | 
         
            -
                        <p>
         
     | 
| 320 | 
         
            -
                        ๋ณธ ๋ชจ๋ธ์ ์ฐ๊ตฌ ๋ชฉ์ ์ผ๋ก ํน์ ์ธ์ ์ผ๊ตด๊ณผ ์ธ๋ชจ๋ฅผ ํ์ตํ LoRA ๋ชจ๋ธ์
๋๋ค.<br>
         
     | 
| 321 | 
         
            -
                        ๋ชฉ์ ์ธ์ ์ฉ๋๋ก ๋ฌด๋จ ์ฌ์ฉ ์๋๋ก ์ ์ํด ์ฃผ์ธ์.<br>
         
     | 
| 322 | 
         
            -
                        (์์ prompt ์ฌ์ฉ ์ ๋ฐ๋์ 'kim'์ ํฌํจํ์ฌ์ผ ์ต์ ์ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค.)
         
     | 
| 323 | 
         
            -
                        </p>
         
     | 
| 324 | 
         
            -
                        """)
         
     | 
| 325 | 
         
            -
             
     | 
| 326 | 
         
            -
                    # ===== ๋ฉ์ธ ์
๋ ฅ =====
         
     | 
| 327 | 
         
            -
                    with gr.Column():
         
     | 
| 328 | 
         
            -
                        with gr.Row(elem_classes="input-container"):
         
     | 
| 329 | 
         
            -
                            with gr.Column(scale=4):
         
     | 
| 330 | 
         
            -
                                user_prompt = gr.Text(
         
     | 
| 331 | 
         
            -
                                    label="Prompt", 
         
     | 
| 332 | 
         
            -
                                    max_lines=5, 
         
     | 
| 333 | 
         
            -
                                    value=examples[0],
         
     | 
| 334 | 
         
            -
                                    elem_classes="large-prompt"
         
     | 
| 335 | 
         
            -
                                )
         
     | 
| 336 | 
         
            -
                            with gr.Column(scale=1):
         
     | 
| 337 | 
         
            -
                                run_button = gr.Button(
         
     | 
| 338 | 
         
            -
                                    "์์ฑ", 
         
     | 
| 339 | 
         
            -
                                    variant="primary",
         
     | 
| 340 | 
         
            -
                                    elem_classes="small-generate-btn"
         
     | 
| 341 | 
         
            -
                                )
         
     | 
| 342 | 
         
            -
                        
         
     | 
| 343 | 
         
            -
                        # ํ๋กฌํํธ ์ฆ๊ฐ ์ต์
 (์์ฑ ๋ฒํผ ์๋)
         
     | 
| 344 | 
         
            -
                        with gr.Group(elem_classes="prompt-enhance-section"):
         
     | 
| 345 | 
         
            -
                            enhance_prompt_checkbox = gr.Checkbox(
         
     | 
| 346 | 
         
            -
                                label="๐ ํ๋กฌํํธ ์ฆ๊ฐ (AI๋ก ํ๋กฌํํธ๋ฅผ ์๋์ผ๋ก ๊ฐ์ ํ์ฌ ๊ณ ํ์ง ์ด๋ฏธ์ง ์์ฑ)", 
         
     | 
| 347 | 
         
            -
                                value=False,
         
     | 
| 348 | 
         
            -
                                info="OpenAI API๋ฅผ ์ฌ์ฉํ์ฌ ์
๋ ฅํ ํ๋กฌํํธ๋ฅผ ๋์ฑ ์์ธํ๊ณ  ๊ณ ํ์ง์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ  ์ ์๋๋ก ์๋์ผ๋ก ์ฆ๊ฐํฉ๋๋ค."
         
     | 
| 349 | 
         
            -
                            )
         
     | 
| 350 | 
         
            -
                        
         
     | 
| 351 | 
         
            -
                        # ์คํ์ผ ํ๋ฆฌ์
 ์น์
         
     | 
| 352 | 
         
            -
                        with gr.Group(elem_classes="style-preset-section"):
         
     | 
| 353 | 
         
            -
                            style_select = gr.Radio(
         
     | 
| 354 | 
         
            -
                                label="๐จ Style Preset", 
         
     | 
| 355 | 
         
            -
                                choices=list(STYLE_PRESETS.keys()), 
         
     | 
| 356 | 
         
            -
                                value="None", 
         
     | 
| 357 | 
         
            -
                                interactive=True
         
     | 
| 358 | 
         
            -
                            )
         
     | 
| 359 | 
         
            -
             
     | 
| 360 | 
         
            -
                        result_image = gr.Image(label="Generated Image")
         
     | 
| 361 | 
         
            -
                        seed_output = gr.Number(label="Seed")
         
     | 
| 362 | 
         
            -
             
     | 
| 363 | 
         
            -
                        # ===== ๊ณ ๊ธ ์ค์  =====
         
     | 
| 364 | 
         
            -
                        with gr.Accordion("Advanced Settings", open=False, elem_classes="advanced-settings"):
         
     | 
| 365 | 
         
            -
                            seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42)
         
     | 
| 366 | 
         
            -
                            randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
         
     | 
| 367 | 
         
            -
                            with gr.Row():
         
     | 
| 368 | 
         
            -
                                width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
         
     | 
| 369 | 
         
            -
                                height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=768)
         
     | 
| 370 | 
         
            -
                            with gr.Row():
         
     | 
| 371 | 
         
            -
                                guidance_scale = gr.Slider(label="Guidance scale", minimum=0.0, maximum=10.0, step=0.1, value=3.5)
         
     | 
| 372 | 
         
            -
                                num_inference_steps = gr.Slider(label="Inference steps", minimum=1, maximum=50, step=1, value=30)
         
     | 
| 373 | 
         
            -
                                lora_scale = gr.Slider(label="LoRA scale", minimum=0.0, maximum=1.0, step=0.1, value=1.0)
         
     | 
| 374 | 
         
            -
             
     | 
| 375 | 
         
            -
                        # ===== ์์ ์์ญ =====
         
     | 
| 376 | 
         
            -
                        with gr.Group(elem_classes="example-region"):
         
     | 
| 377 | 
         
            -
                            gr.Markdown("### Examples")
         
     | 
| 378 | 
         
            -
                            gr.Examples(examples=examples, inputs=user_prompt, cache_examples=False)
         
     | 
| 379 | 
         
            -
             
     | 
| 380 | 
         
            -
                    # ===== ์ด๋ฒคํธ =====
         
     | 
| 381 | 
         
            -
                    run_button.click(
         
     | 
| 382 | 
         
            -
                        fn=generate_image,
         
     | 
| 383 | 
         
            -
                        inputs=[
         
     | 
| 384 | 
         
            -
                            user_prompt,
         
     | 
| 385 | 
         
            -
                            style_select,
         
     | 
| 386 | 
         
            -
                            enhance_prompt_checkbox,
         
     | 
| 387 | 
         
            -
                            seed,
         
     | 
| 388 | 
         
            -
                            randomize_seed,
         
     | 
| 389 | 
         
            -
                            width,
         
     | 
| 390 | 
         
            -
                            height,
         
     | 
| 391 | 
         
            -
                            guidance_scale,
         
     | 
| 392 | 
         
            -
                            num_inference_steps,
         
     | 
| 393 | 
         
            -
                            lora_scale,
         
     | 
| 394 | 
         
            -
                        ],
         
     | 
| 395 | 
         
            -
                        outputs=[result_image, seed_output],
         
     | 
| 396 | 
         
            -
                    )
         
     | 
| 397 | 
         
            -
                
         
     | 
| 398 | 
         
            -
                return demo
         
     | 
| 399 | 
         
            -
             
     | 
| 400 | 
         
            -
            # ===== ์ ํ๋ฆฌ์ผ์ด์
 ์คํ =====
         
     | 
| 401 | 
         
            -
            if __name__ == "__main__":
         
     | 
| 402 | 
         
            -
                demo = create_interface()
         
     | 
| 403 | 
         
            -
                demo.queue()
         
     | 
| 404 | 
         
            -
                demo.launch()
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         |