linoyts HF Staff commited on
Commit
7db469b
·
verified ·
1 Parent(s): cc7838f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -0
app.py CHANGED
@@ -12,6 +12,130 @@ from optimization import optimize_pipeline_
12
  from qwenimage.pipeline_qwen_image_edit import QwenImageEditPipeline
13
  from qwenimage.transformer_qwenimage import QwenImageTransformer2DModel
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  # --- Model Loading ---
17
  dtype = torch.bfloat16
@@ -34,6 +158,7 @@ def infer(
34
  randomize_seed=False,
35
  true_guidance_scale=4.0,
36
  num_inference_steps=50,
 
37
  progress=gr.Progress(track_tqdm=True),
38
  ):
39
  """
@@ -52,6 +177,10 @@ def infer(
52
  print(f"Negative Prompt: '{negative_prompt}'")
53
  print(f"Seed: {seed}, Steps: {num_inference_steps}")
54
 
 
 
 
 
55
  # Generate the image
56
  image = pipe(
57
  image,
@@ -124,6 +253,11 @@ with gr.Blocks(css=css) as demo:
124
  value=50,
125
  )
126
 
 
 
 
 
 
127
  # gr.Examples(examples=examples, inputs=[prompt], outputs=[result, seed], fn=infer, cache_examples=False)
128
 
129
  gr.on(
@@ -137,6 +271,7 @@ with gr.Blocks(css=css) as demo:
137
  randomize_seed,
138
  true_guidance_scale,
139
  num_inference_steps,
 
140
  ],
141
  outputs=[result, seed],
142
  )
 
12
  from qwenimage.pipeline_qwen_image_edit import QwenImageEditPipeline
13
  from qwenimage.transformer_qwenimage import QwenImageTransformer2DModel
14
 
15
+ from huggingface_hub import InferenceClient
16
+ import math
17
+
18
+ # --- Prompt Enhancement using Hugging Face InferenceClient ---
19
+ def polish_prompt_hf(original_prompt, system_prompt):
20
+ """
21
+ Rewrites the prompt using a Hugging Face InferenceClient.
22
+ """
23
+ # Ensure HF_TOKEN is set
24
+ api_key = os.environ.get("HF_TOKEN")
25
+ if not api_key:
26
+ print("Warning: HF_TOKEN not set. Falling back to original prompt.")
27
+ return original_prompt
28
+
29
+ try:
30
+ # Initialize the client
31
+ client = InferenceClient(
32
+ provider="cerebras",
33
+ api_key=api_key,
34
+ )
35
+
36
+ # Format the messages for the chat completions API
37
+ messages = [
38
+ {"role": "system", "content": system_prompt},
39
+ {"role": "user", "content": original_prompt}
40
+ ]
41
+
42
+ # Call the API
43
+ completion = client.chat.completions.create(
44
+ model="Qwen/Qwen3-235B-A22B-Instruct-2507",
45
+ messages=messages,
46
+ )
47
+
48
+ # Parse the response
49
+ result = completion.choices[0].message.content
50
+
51
+ # Try to extract JSON if present
52
+ if '{"Rewritten"' in result:
53
+ try:
54
+ # Clean up the response
55
+ result = result.replace('```json', '').replace('```', '')
56
+ result_json = json.loads(result)
57
+ polished_prompt = result_json.get('Rewritten', result)
58
+ except:
59
+ polished_prompt = result
60
+ else:
61
+ polished_prompt = result
62
+
63
+ polished_prompt = polished_prompt.strip().replace("\n", " ")
64
+ return polished_prompt
65
+
66
+ except Exception as e:
67
+ print(f"Error during API call to Hugging Face: {e}")
68
+ # Fallback to original prompt if enhancement fails
69
+ return original_prompt
70
+
71
+
72
+ def polish_prompt(prompt, img):
73
+ """
74
+ Main function to polish prompts for image editing using HF inference.
75
+ """
76
+ SYSTEM_PROMPT = '''
77
+ # Edit Instruction Rewriter
78
+ You are a professional edit instruction rewriter. Your task is to generate a precise, concise, and visually achievable professional-level edit instruction based on the user-provided instruction and the image to be edited.
79
+
80
+ Please strictly follow the rewriting rules below:
81
+
82
+ ## 1. General Principles
83
+ - Keep the rewritten prompt **concise**. Avoid overly long sentences and reduce unnecessary descriptive language.
84
+ - If the instruction is contradictory, vague, or unachievable, prioritize reasonable inference and correction, and supplement details when necessary.
85
+ - Keep the core intention of the original instruction unchanged, only enhancing its clarity, rationality, and visual feasibility.
86
+ - All added objects or modifications must align with the logic and style of the edited input image's overall scene.
87
+
88
+ ## 2. Task Type Handling Rules
89
+ ### 1. Add, Delete, Replace Tasks
90
+ - If the instruction is clear (already includes task type, target entity, position, quantity, attributes), preserve the original intent and only refine the grammar.
91
+ - If the description is vague, supplement with minimal but sufficient details (category, color, size, orientation, position, etc.). For example:
92
+ > Original: "Add an animal"
93
+ > Rewritten: "Add a light-gray cat in the bottom-right corner, sitting and facing the camera"
94
+ - Remove meaningless instructions: e.g., "Add 0 objects" should be ignored or flagged as invalid.
95
+ - For replacement tasks, specify "Replace Y with X" and briefly describe the key visual features of X.
96
+
97
+ ### 2. Text Editing Tasks
98
+ - All text content must be enclosed in English double quotes " ". Do not translate or alter the original language of the text, and do not change the capitalization.
99
+ - **For text replacement tasks, always use the fixed template:**
100
+ - Replace "xx" to "yy".
101
+ - Replace the xx bounding box to "yy".
102
+ - If the user does not specify text content, infer and add concise text based on the instruction and the input image's context. For example:
103
+ > Original: "Add a line of text" (poster)
104
+ > Rewritten: "Add text "LIMITED EDITION" at the top center with slight shadow"
105
+ - Specify text position, color, and layout in a concise way.
106
+
107
+ ### 3. Human Editing Tasks
108
+ - Maintain the person's core visual consistency (ethnicity, gender, age, hairstyle, expression, outfit, etc.).
109
+ - If modifying appearance (e.g., clothes, hairstyle), ensure the new element is consistent with the original style.
110
+ - **For expression changes, they must be natural and subtle, never exaggerated.**
111
+ - If deletion is not specifically emphasized, the most important subject in the original image (e.g., a person, an animal) should be preserved.
112
+ - For background change tasks, emphasize maintaining subject consistency at first.
113
+ - Example:
114
+ > Original: "Change the person's hat"
115
+ > Rewritten: "Replace the man's hat with a dark brown beret; keep smile, short hair, and gray jacket unchanged"
116
+
117
+ ### 4. Style Transformation or Enhancement Tasks
118
+ - If a style is specified, describe it concisely with key visual traits. For example:
119
+ > Original: "Disco style"
120
+ > Rewritten: "1970s disco: flashing lights, disco ball, mirrored walls, colorful tones"
121
+ - If the instruction says "use reference style" or "keep current style," analyze the input image, extract main features (color, composition, texture, lighting, art style), and integrate them concisely.
122
+ - **For coloring tasks, including restoring old photos, always use the fixed template:** "Restore old photograph, remove scratches, reduce noise, enhance details, high resolution, realistic, natural skin tones, clear facial features, no distortion, vintage photo restoration"
123
+ - If there are other changes, place the style description at the end.
124
+
125
+ ## 3. Rationality and Logic Checks
126
+ - Resolve contradictory instructions: e.g., "Remove all trees but keep all trees" should be logically corrected.
127
+ - Add missing key information: if position is unspecified, choose a reasonable area based on composition (near subject, empty space, center/edges).
128
+
129
+ # Output Format
130
+ Return only the rewritten instruction text directly, without JSON formatting or any other wrapper.
131
+ '''
132
+
133
+ # Note: We're not actually using the image in the HF version,
134
+ # but keeping the interface consistent
135
+ full_prompt = f"{SYSTEM_PROMPT}\n\nUser Input: {prompt}\n\nRewritten Prompt:"
136
+
137
+ return polish_prompt_hf(full_prompt, SYSTEM_PROMPT)
138
+
139
 
140
  # --- Model Loading ---
141
  dtype = torch.bfloat16
 
158
  randomize_seed=False,
159
  true_guidance_scale=4.0,
160
  num_inference_steps=50,
161
+ rewrite_prompt=True,
162
  progress=gr.Progress(track_tqdm=True),
163
  ):
164
  """
 
177
  print(f"Negative Prompt: '{negative_prompt}'")
178
  print(f"Seed: {seed}, Steps: {num_inference_steps}")
179
 
180
+ if rewrite_prompt:
181
+ prompt = polish_prompt(prompt, image)
182
+ print(f"Rewritten Prompt: {prompt}")
183
+
184
  # Generate the image
185
  image = pipe(
186
  image,
 
253
  value=50,
254
  )
255
 
256
+ rewrite_prompt = gr.Checkbox(
257
+ label="Enhance prompt (using HF Inference)",
258
+ value=True
259
+ )
260
+
261
  # gr.Examples(examples=examples, inputs=[prompt], outputs=[result, seed], fn=infer, cache_examples=False)
262
 
263
  gr.on(
 
271
  randomize_seed,
272
  true_guidance_scale,
273
  num_inference_steps,
274
+ rewrite_prompt,
275
  ],
276
  outputs=[result, seed],
277
  )