File size: 18,970 Bytes
64016ec
52fe588
64016ec
 
 
 
52fe588
64016ec
 
9819163
52fe588
64016ec
bbb89b4
64016ec
bbb89b4
 
9819163
 
bbb89b4
 
 
9819163
 
 
bbb89b4
 
9819163
bbb89b4
 
9819163
bbb89b4
64016ec
 
 
52fe588
64016ec
 
52fe588
64016ec
 
 
 
 
 
359c460
64016ec
 
1aca16b
64016ec
 
 
 
 
 
 
 
 
 
 
bbb89b4
 
 
 
 
 
 
 
 
 
64016ec
bbb89b4
 
 
 
 
64016ec
 
 
 
 
 
52fe588
64016ec
 
 
 
 
52fe588
64016ec
359c460
64016ec
359c460
 
 
 
 
 
 
bbb89b4
 
 
 
 
359c460
 
 
 
bbb89b4
 
 
 
 
359c460
 
 
 
 
 
 
1aca16b
359c460
1aca16b
 
 
 
 
359c460
1aca16b
 
 
 
 
 
 
 
 
359c460
64016ec
 
 
 
 
 
 
359c460
64016ec
 
 
 
 
 
359c460
64016ec
 
 
 
 
 
 
 
 
 
 
 
359c460
64016ec
 
 
 
359c460
64016ec
 
 
1aca16b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52fe588
1aca16b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64016ec
1aca16b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
359c460
1aca16b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64016ec
1aca16b
 
 
 
 
 
 
 
 
 
 
64016ec
 
1aca16b
64016ec
bbb89b4
64016ec
 
 
 
 
9819163
1aca16b
 
 
9819163
 
 
64016ec
9819163
64016ec
1aca16b
 
 
 
 
 
 
9819163
359c460
1aca16b
359c460
1aca16b
64016ec
1aca16b
 
 
 
 
 
 
 
 
 
 
 
9819163
64016ec
 
9819163
64016ec
 
1aca16b
64016ec
9819163
 
bbb89b4
9819163
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1aca16b
9819163
 
1aca16b
9819163
 
1aca16b
 
 
 
9819163
 
 
1aca16b
 
 
9819163
 
 
 
1aca16b
 
9819163
1aca16b
 
 
 
 
9819163
1aca16b
 
 
 
 
 
 
 
 
 
9819163
1aca16b
9819163
 
 
 
 
 
 
 
 
1aca16b
9819163
64016ec
9819163
52fe588
9819163
1aca16b
9819163
52fe588
9819163
 
 
 
 
1aca16b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
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
426
427
428
429
import os
import random
import logging
import numpy as np
import gradio as gr
import spaces
import torch
from diffusers import FluxPipeline, FluxTransformer2DModel
from transformers import AutoModelForCausalLM, AutoTokenizer
from huggingface_hub import login, whoami

# ------------------------------------------------------------------
# 1. Authentication and Global Configuration
# ------------------------------------------------------------------
# Authenticate with HF token
hf_token = os.getenv("HF_TOKEN")
auth_status = "🔴 Not Authenticated"

if hf_token:
    try:
        login(token=hf_token, add_to_git_credential=True)
        user_info = whoami(hf_token)
        auth_status = f"✅ Authenticated as {user_info['name']}"
        logging.info(f"Successfully authenticated with Hugging Face as {user_info['name']}")
    except Exception as e:
        logging.error(f"HF authentication failed: {e}")
        auth_status = f"🔴 Authentication Error: {str(e)}"
else:
    logging.warning("No HF_TOKEN found in environment variables")
    auth_status = "🔴 No HF_TOKEN found"

DEFAULT_PIPELINE_PATH = "black-forest-labs/FLUX.1-dev"
DEFAULT_QWEN_MODEL_PATH = "Qwen/Qwen3-8B"
DEFAULT_CUSTOM_WEIGHTS_PATH = "PosterCraft/PosterCraft-v1_RL"

MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 2048

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(levelname)s - %(message)s",
)

# ------------------------------------------------------------------
# 2. Model Download Function (CPU only)
# ------------------------------------------------------------------
def download_model_weights(target_dir, repo_id, subdir=None):
    """Download model weights to specified directory (CPU operation)"""
    from huggingface_hub import snapshot_download
    import shutil

    if os.path.exists(target_dir):
        logging.info(f"Directory {target_dir} already exists, skipping download")
        return

    tmp_dir = "hf_temp_download"
    os.makedirs(tmp_dir, exist_ok=True)

    try:
        download_kwargs = {
            "repo_id": repo_id,
            "repo_type": "model",
            "local_dir": tmp_dir,
            "local_dir_use_symlinks": False,
        }
        
        if hf_token:
            download_kwargs["token"] = hf_token
            
        if subdir:
            download_kwargs["allow_patterns"] = os.path.join(subdir, "**")
            
        snapshot_download(**download_kwargs)
        
        src_dir = os.path.join(tmp_dir, subdir) if subdir else tmp_dir

        if os.path.exists(src_dir):
            shutil.copytree(src_dir, target_dir)
            logging.info(f"Successfully downloaded {repo_id} to {target_dir}")
        else:
            logging.warning(f"Source directory {src_dir} does not exist")

    except Exception as e:
        logging.error(f"Download failed: {e}")
    finally:
        if os.path.exists(tmp_dir):
            shutil.rmtree(tmp_dir)

# ------------------------------------------------------------------
# 3. Pre-download models (CPU operation)
# ------------------------------------------------------------------
def ensure_models_downloaded():
    """Pre-download all models to avoid GPU timeout"""
    logging.info("Checking and downloading models if needed...")
    
    # Download custom weights
    custom_weights_local = "local_weights/PosterCraft-v1_RL"
    if not os.path.exists(custom_weights_local):
        try:
            logging.info("Downloading custom Transformer weights...")
            download_model_weights(custom_weights_local, DEFAULT_CUSTOM_WEIGHTS_PATH)
        except Exception as e:
            logging.warning(f"Failed to download custom weights: {e}")
    
    # Download Qwen model
    qwen_local = "local_weights/Qwen3-8B"
    if not os.path.exists(qwen_local):
        try:
            logging.info("Downloading Qwen model...")
            download_model_weights(qwen_local, DEFAULT_QWEN_MODEL_PATH)
        except Exception as e:
            logging.warning(f"Failed to download Qwen model: {e}")
    
    logging.info("Model download check completed")

# Pre-download models at startup (CPU)
ensure_models_downloaded()

# ------------------------------------------------------------------
# 4. Qwen Recap Agent (基于你的原始逻辑)
# ------------------------------------------------------------------
class QwenRecapAgent:
    def __init__(self, model_path, max_retries=3, retry_delay=2, device_map="auto"):
        self.max_retries = max_retries
        self.retry_delay = retry_delay
        self.device = device_map

        self.tokenizer = AutoTokenizer.from_pretrained(model_path, token=hf_token)
        model_kwargs = {"torch_dtype": torch.bfloat16, "device_map": device_map if device_map == "auto" else None}
        if hf_token:
            model_kwargs["token"] = hf_token
        self.model = AutoModelForCausalLM.from_pretrained(model_path, **model_kwargs)
        if device_map != "auto":
             self.model.to(device_map)
        
        self.prompt_template = """You are an expert poster prompt designer. Your task is to rewrite a user's short poster prompt into a detailed and vivid long-format prompt. Follow these steps carefully:

**Step 1: Analyze the Core Requirements**
Identify the key elements in the user's prompt. Do not miss any details.
- **Subject:** What is the main subject? (e.g., a person, an object, a scene)
- **Style:** What is the visual style? (e.g., photorealistic, cartoon, vintage, minimalist)
- **Text:** Is there any text, like a title or slogan?
- **Color Palette:** Are there specific colors mentioned?
- **Composition:** Are there any layout instructions?

**Step 2: Expand and Add Detail**
Elaborate on each core requirement to create a rich description.
- **Do Not Omit:** You must include every piece of information from the original prompt.
- **Enrich with Specifics:** Add professional and descriptive details.
    - **Example:** If the user says "a woman with a bow", you could describe her as "a young woman with a determined expression, holding a finely crafted wooden longbow, with an arrow nocked and ready to fire."
- **Fill in the Gaps:** If the original prompt is simple (e.g., "a poster for a coffee shop"), use your creativity to add fitting details. You might add "The poster features a top-down view of a steaming latte with delicate art on its foam, placed on a rustic wooden table next to a few scattered coffee beans."

**Step 3: Handle Text Precisely**
- **Identify All Text Elements:** Carefully look for any text mentioned in the prompt. This includes:
    - **Explicit Text:** Subtitles, slogans, or any text in quotes.
    - **Implicit Titles:** The name of an event, movie, or product is often the main title. For example, if the prompt is "generate a 'Inception' poster ...", the title is "Inception".
- **Rules for Text:**
    - **If Text Exists:**
        - You must use the exact text identified from the prompt.
        - Do NOT add new text or delete existing text.
        - Describe each text's appearance (font, style, color, position). Example: `The title 'Inception' is written in a bold, sans-serif font, integrated into the cityscape.`
    - **If No Text Exists:**
        - Do not add any text elements. The poster must be purely visual.
- Most posters have titles. When a title exists, you must extend the title's description. Only when you are absolutely sure that there is no text to render, you can allow the extended prompt not to render text.

**Step 4: Final Output Rules**
- **Output ONLY the rewritten prompt.** No introductions, no explanations, no "Here is the prompt:".
- **Use a descriptive and confident tone.** Write as if you are describing a finished, beautiful poster.
- **Keep it concise.** The final prompt should be under 300 words.

---
**User Prompt:**
{brief_description}"""
    
    def recap_prompt(self, original_prompt):
        full_prompt = self.prompt_template.format(brief_description=original_prompt)
        messages = [{"role": "user", "content": full_prompt}]
        try:
            text = self.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True, enable_thinking=False)
            model_inputs = self.tokenizer([text], return_tensors="pt").to(self.model.device)
            
            with torch.no_grad():
                generated_ids = self.model.generate(**model_inputs, max_new_tokens=1024, temperature=0.6)
            
            output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
            full_response = self.tokenizer.decode(output_ids, skip_special_tokens=True)
            final_answer = self._extract_final_answer(full_response)
            
            if final_answer:
                return final_answer.strip()
            
            logging.info("Qwen returned an empty answer. Using original prompt.")
            return original_prompt
        except Exception as e:
            logging.error(f"Qwen recap failed: {e}. Using original prompt.")
            return original_prompt

    def _extract_final_answer(self, full_response):
        if "</think>" in full_response:
            return full_response.split("</think>")[-1].strip()
        if "<think>" not in full_response:
            return full_response.strip()
        return None

# ------------------------------------------------------------------
# 5. Poster Generator Class (基于你的原始逻辑,但加上缓存)
# ------------------------------------------------------------------
class PosterGenerator:
    def __init__(self, pipeline_path, qwen_model_path, custom_weights_path, device):
        self.device = device
        self.pipeline_path = pipeline_path
        self.qwen_model_path = qwen_model_path
        self.custom_weights_path = custom_weights_path
        
        # 缓存变量
        self.qwen_agent = None
        self.pipeline = None
        
    def _load_qwen_agent(self):
        if self.qwen_agent is None:
            if not self.qwen_model_path:
                return None
            
            # 检查本地路径
            qwen_local = "local_weights/Qwen3-8B"
            model_path = qwen_local if os.path.exists(qwen_local) else self.qwen_model_path
            
            logging.info(f"Loading Qwen agent from {model_path}")
            self.qwen_agent = QwenRecapAgent(model_path=model_path, device_map=str(self.device))
        return self.qwen_agent

    def _load_flux_pipeline(self):
        if self.pipeline is None:
            logging.info("Loading FLUX pipeline...")
            self.pipeline = FluxPipeline.from_pretrained(
                self.pipeline_path, 
                torch_dtype=torch.bfloat16,
                token=hf_token
            )
            
            # 加载自定义权重
            custom_weights_local = "local_weights/PosterCraft-v1_RL"
            if os.path.exists(custom_weights_local):
                logging.info(f"Loading custom Transformer from directory: {custom_weights_local}")
                transformer = FluxTransformer2DModel.from_pretrained(
                    custom_weights_local, 
                    torch_dtype=torch.bfloat16,
                    token=hf_token
                )
                self.pipeline.transformer = transformer
            elif self.custom_weights_path and os.path.exists(self.custom_weights_path):
                logging.info(f"Loading custom Transformer from directory: {self.custom_weights_path}")
                transformer = FluxTransformer2DModel.from_pretrained(
                    self.custom_weights_path, 
                    torch_dtype=torch.bfloat16,
                    token=hf_token
                )
                self.pipeline.transformer = transformer
            
            self.pipeline.to(self.device)
        return self.pipeline

    def generate(self, prompt, enable_recap, **kwargs):
        final_prompt = prompt
        if enable_recap:
            qwen_agent = self._load_qwen_agent()
            if not qwen_agent:
                raise gr.Error("Recap is enabled, but the recap model is not available. Check model path.")
            final_prompt = qwen_agent.recap_prompt(prompt)

        pipeline = self._load_flux_pipeline()
        generator = torch.Generator(device=self.device).manual_seed(kwargs['seed'])
        
        with torch.inference_mode():
            image = pipeline(
                prompt=final_prompt,
                generator=generator,
                num_inference_steps=kwargs['num_inference_steps'],
                guidance_scale=kwargs['guidance_scale'],
                width=kwargs['width'],
                height=kwargs['height']
            ).images[0]
        
        return image, final_prompt

# ------------------------------------------------------------------
# 6. Main Generation Function (GPU) - 保持你的原始逻辑
# ------------------------------------------------------------------
@spaces.GPU(duration=300)
def generate_image_interface(
    original_prompt, enable_recap, height, width, 
    num_inference_steps, guidance_scale, seed_input,
    progress=gr.Progress(track_tqdm=True),
):
    """Generate image using FLUX pipeline"""
    if not original_prompt or not original_prompt.strip():
        return None, "❌ Prompt cannot be empty!", ""

    try:
        if not hf_token:
            return None, "❌ Error: HF_TOKEN not found. Please configure authentication.", ""

        device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        
        # 全局生成器实例
        if not hasattr(generate_image_interface, 'generator'):
            generate_image_interface.generator = PosterGenerator(
                pipeline_path=DEFAULT_PIPELINE_PATH,
                qwen_model_path=DEFAULT_QWEN_MODEL_PATH,
                custom_weights_path=DEFAULT_CUSTOM_WEIGHTS_PATH,
                device=device
            )
        
        actual_seed = int(seed_input) if seed_input and seed_input != -1 else random.randint(1, 2**32 - 1)
        
        progress(0.1, desc="Starting generation...")
        
        image, final_prompt = generate_image_interface.generator.generate(
            prompt=original_prompt,
            enable_recap=enable_recap,
            height=int(height),
            width=int(width),
            num_inference_steps=int(num_inference_steps),
            guidance_scale=float(guidance_scale),
            seed=actual_seed
        )
        
        status_log = f"✅ Generation complete! Seed: {actual_seed}"
        return image, status_log, final_prompt

    except Exception as e:
        logging.error(f"Generation failed: {e}")
        return None, f"❌ Generation failed: {str(e)}", ""

# ------------------------------------------------------------------
# 7. Gradio Interface (保持你的原始风格)
# ------------------------------------------------------------------
def create_interface():
    """Create Gradio interface"""
    
    with gr.Blocks(
        title="PosterCraft-v1.0",
        theme=gr.themes.Soft(),
        css="""
        .main-container { max-width: 1200px; margin: 0 auto; }
        .status-box { padding: 10px; border-radius: 5px; margin: 10px 0; }
        """
    ) as demo:
        
        gr.HTML("""
        <div class="main-container">
            <h1 style="text-align: center; margin-bottom: 20px;">🎨 PosterCraft-v1.0</h1>
            <p style="text-align: center; color: #666; margin-bottom: 30px;">
                Professional poster generation with FLUX.1-dev and custom fine-tuned weights
            </p>
        </div>
        """)
        
        with gr.Row():
            gr.Markdown(f"**Base Pipeline:** `{DEFAULT_PIPELINE_PATH}`")
            gr.Markdown(f"**Authentication Status:** {auth_status}")
        
        gr.HTML("""
        <div class="status-box">
            <p><strong>⚠️ First generation requires model loading (5-10 minutes). Subsequent generations are much faster!</strong></p>
        </div>
        """)

        with gr.Row():
            with gr.Column(scale=1):
                gr.Markdown("### 1. Configuration")
                prompt_input = gr.Textbox(
                    label="Poster Prompt", 
                    lines=3, 
                    placeholder="Enter your poster description...",
                    value="A vintage travel poster for Paris, featuring the Eiffel Tower at sunset with warm golden lighting"
                )
                enable_recap_checkbox = gr.Checkbox(
                    label="Enable Prompt Enhancement (Qwen3-8B)", 
                    value=True, 
                    info="Use AI to enhance and expand your prompt"
                )
                
                with gr.Row():
                    width_input = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, value=768, step=32)
                    height_input = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, value=1024, step=32)
                
                num_inference_steps_input = gr.Slider(label="Inference Steps", minimum=1, maximum=100, value=20, step=1)
                guidance_scale_input = gr.Slider(label="Guidance Scale", minimum=0.0, maximum=20.0, value=3.5, step=0.1)
                seed_number_input = gr.Number(label="Seed (-1 for random)", value=-1, minimum=-1, step=1)
                generate_button = gr.Button("🎨 Generate Poster", variant="primary", size="lg")

            with gr.Column(scale=1):
                gr.Markdown("### 2. Results")
                image_output = gr.Image(label="Generated Poster", type="pil", height=600)
                status_output = gr.Textbox(label="Generation Status", lines=2, interactive=False)
                recapped_prompt_output = gr.Textbox(label="Enhanced Prompt", lines=5, interactive=False, info="The final prompt used for generation")

        inputs_list = [
            prompt_input, enable_recap_checkbox, height_input, width_input,
            num_inference_steps_input, guidance_scale_input, seed_number_input
        ]
        outputs_list = [image_output, status_output, recapped_prompt_output]
        
        generate_button.click(fn=generate_image_interface, inputs=inputs_list, outputs=outputs_list)
        
        # Examples
        gr.Examples(
            examples=[
                ["A retro sci-fi movie poster with neon colors and flying cars"],
                ["An elegant art deco poster for a luxury hotel"],
                ["A minimalist concert poster with bold typography"],
                ["A vintage advertisement for organic coffee"],
            ],
            inputs=[prompt_input]
        )
    
    return demo

# ------------------------------------------------------------------
# 8. Launch Application
# ------------------------------------------------------------------
if __name__ == "__main__":
    demo = create_interface()
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        show_api=False
    )