MCPcopy Create free account
hub / github.com/MeiGen-AI/PosterCraft / QwenRecapAgent

Class QwenRecapAgent

inference.py:12–93  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10
11
12class QwenRecapAgent:
13 def __init__(self, model_path, device="auto"):
14 self.device = device
15 self.tokenizer = AutoTokenizer.from_pretrained(model_path)
16
17 model_kwargs = {"torch_dtype": "auto"}
18 if device != "auto":
19 model_kwargs["device_map"] = None
20
21 self.model = AutoModelForCausalLM.from_pretrained(model_path, **model_kwargs)
22
23 if device != "auto":
24 self.model.to(device)
25
26 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:
27
28 **Step 1: Analyze the Core Requirements**
29 Identify the key elements in the user's prompt. Do not miss any details.
30 - **Subject:** What is the main subject? (e.g., a person, an object, a scene)
31 - **Style:** What is the visual style? (e.g., photorealistic, cartoon, vintage, minimalist)
32 - **Text:** Is there any text, like a title or slogan?
33 - **Color Palette:** Are there specific colors mentioned?
34 - **Composition:** Are there any layout instructions?
35
36 **Step 2: Expand and Add Detail**
37 Elaborate on each core requirement to create a rich description.
38 - **Do Not Omit:** You must include every piece of information from the original prompt.
39 - **Enrich with Specifics:** Add professional and descriptive details.
40 - **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."
41 - **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."
42
43 **Step 3: Handle Text Precisely**
44 - **Identify All Text Elements:** Carefully look for any text mentioned in the prompt. This includes:
45 - **Explicit Text:** Subtitles, slogans, or any text in quotes.
46 - **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".
47 - **Rules for Text:**
48 - **If Text Exists:**
49 - You must use the exact text identified from the prompt.
50 - Do NOT add new text or delete existing text.
51 - 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.`
52 - **If No Text Exists:**
53 - Do not add any text elements. The poster must be purely visual.
54 - 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.
55
56 **Step 4: Final Output Rules**
57 - **Output ONLY the rewritten prompt.** No introductions, no explanations, no "Here is the prompt:".
58 - **Use a descriptive and confident tone.** Write as if you are describing a finished, beautiful poster.
59 - **Keep it concise.** The final prompt should be under 300 words.
60
61 ---
62 **User Prompt:**
63 {brief_description}"""
64
65 def recap_prompt(self, original_prompt):
66 full_prompt = self.prompt_template.format(brief_description=original_prompt)
67 messages = [{"role": "user", "content": full_prompt}]
68 try:
69 text = self.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True, enable_thinking=False)

Callers 1

__init__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected