Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -99,6 +99,23 @@ Please strictly follow the rewriting rules below:
|
|
| 99 |
"Rewritten": "..."
|
| 100 |
}
|
| 101 |
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
# --- Prompt Enhancement using Hugging Face InferenceClient ---
|
| 103 |
def polish_prompt_hf(prompt, img_list):
|
| 104 |
"""
|
|
@@ -156,7 +173,65 @@ def polish_prompt_hf(prompt, img_list):
|
|
| 156 |
print(f"Error during API call to Hugging Face: {e}")
|
| 157 |
# Fallback to original prompt if enhancement fails
|
| 158 |
return prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
|
| 162 |
def encode_image(pil_image):
|
|
@@ -295,13 +370,15 @@ with gr.Blocks(css=css) as demo:
|
|
| 295 |
gr.HTML("""
|
| 296 |
<div id="logo-title">
|
| 297 |
<img src="https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-Image/qwen_image_edit_logo.png" alt="Qwen-Image Edit Logo" width="400" style="display: block; margin: 0 auto;">
|
| 298 |
-
<h2 style="font-style: italic;color: #5b47d1;margin-top: -27px !important;margin-left: 96px">[Plus] Fast, 4-steps with Qwen Rapid AIO</h2>
|
| 299 |
</div>
|
| 300 |
""")
|
| 301 |
gr.Markdown("""
|
| 302 |
[Learn more](https://github.com/QwenLM/Qwen-Image) about the Qwen-Image series.
|
| 303 |
This demo uses the new [Qwen-Image-Edit-2509](https://huggingface.co/Qwen/Qwen-Image-Edit-2509) with [Phr00t/Qwen-Image-Edit-Rapid-AIO](https://huggingface.co/Phr00t/Qwen-Image-Edit-Rapid-AIO/tree/main) + [AoT compilation & FA3](https://huggingface.co/blog/zerogpu-aoti) for accelerated inference.
|
| 304 |
Try on [Qwen Chat](https://chat.qwen.ai/), or [download model](https://huggingface.co/Qwen/Qwen-Image-Edit-2509) to run locally with ComfyUI or diffusers.
|
|
|
|
|
|
|
| 305 |
""")
|
| 306 |
with gr.Row():
|
| 307 |
with gr.Column():
|
|
@@ -319,7 +396,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 319 |
prompt = gr.Text(
|
| 320 |
label="Prompt",
|
| 321 |
show_label=False,
|
| 322 |
-
placeholder="describe the edit instruction",
|
| 323 |
container=False,
|
| 324 |
)
|
| 325 |
run_button = gr.Button("Edit!", variant="primary")
|
|
@@ -376,6 +453,13 @@ with gr.Blocks(css=css) as demo:
|
|
| 376 |
|
| 377 |
# gr.Examples(examples=examples, inputs=[prompt], outputs=[result, seed], fn=infer, cache_examples=False)
|
| 378 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
gr.on(
|
| 380 |
triggers=[run_button.click, prompt.submit],
|
| 381 |
fn=infer,
|
|
|
|
| 99 |
"Rewritten": "..."
|
| 100 |
}
|
| 101 |
'''
|
| 102 |
+
|
| 103 |
+
NEXT_SCENE_PROMPT = '''
|
| 104 |
+
You are a cinematic storytelling assistant. Analyze the provided image and suggest a "Next Scene" prompt that creates a natural narrative progression.
|
| 105 |
+
|
| 106 |
+
Follow these guidelines for Next Scene suggestions:
|
| 107 |
+
* Start with camera direction (dolly, pan, tilt, pull back, push in, track)
|
| 108 |
+
* Maintain compositional coherence while introducing organic transitions
|
| 109 |
+
* Consider: camera movement, framing evolution, environmental reveals, atmospheric shifts
|
| 110 |
+
|
| 111 |
+
Examples of good Next Scene prompts:
|
| 112 |
+
* "Next Scene: The camera pulls back from a tight close-up to a sweeping aerial view, revealing an entire fleet of vessels soaring through a fantasy landscape."
|
| 113 |
+
* "Next Scene: The camera tracks forward and tilts down, bringing the sun closer into frame as a strong lens flare intensifies."
|
| 114 |
+
* "Next Scene: The camera pans right, revealing more of the mountain range in the distance."
|
| 115 |
+
|
| 116 |
+
Based on the image provided, suggest ONE concise Next Scene prompt that would create a compelling cinematic transition. Output ONLY the prompt text, starting with "Next Scene:".
|
| 117 |
+
'''
|
| 118 |
+
|
| 119 |
# --- Prompt Enhancement using Hugging Face InferenceClient ---
|
| 120 |
def polish_prompt_hf(prompt, img_list):
|
| 121 |
"""
|
|
|
|
| 173 |
print(f"Error during API call to Hugging Face: {e}")
|
| 174 |
# Fallback to original prompt if enhancement fails
|
| 175 |
return prompt
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
def suggest_next_scene_prompt(images):
|
| 179 |
+
"""
|
| 180 |
+
Suggests a Next Scene prompt based on the uploaded image(s).
|
| 181 |
+
"""
|
| 182 |
+
if images is None or len(images) == 0:
|
| 183 |
+
return ""
|
| 184 |
|
| 185 |
+
api_key = os.environ.get("HF_TOKEN")
|
| 186 |
+
if not api_key:
|
| 187 |
+
print("Warning: HF_TOKEN not set. Cannot generate suggestions.")
|
| 188 |
+
return ""
|
| 189 |
+
|
| 190 |
+
try:
|
| 191 |
+
# Load input images into PIL Images
|
| 192 |
+
pil_images = []
|
| 193 |
+
for item in images:
|
| 194 |
+
try:
|
| 195 |
+
if isinstance(item[0], Image.Image):
|
| 196 |
+
pil_images.append(item[0].convert("RGB"))
|
| 197 |
+
elif isinstance(item[0], str):
|
| 198 |
+
pil_images.append(Image.open(item[0]).convert("RGB"))
|
| 199 |
+
elif hasattr(item, "name"):
|
| 200 |
+
pil_images.append(Image.open(item.name).convert("RGB"))
|
| 201 |
+
except Exception:
|
| 202 |
+
continue
|
| 203 |
+
|
| 204 |
+
if len(pil_images) == 0:
|
| 205 |
+
return ""
|
| 206 |
+
|
| 207 |
+
client = InferenceClient(
|
| 208 |
+
provider="cerebras",
|
| 209 |
+
api_key=api_key,
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
messages = [
|
| 213 |
+
{"role": "system", "content": "You are a helpful cinematic storytelling assistant."},
|
| 214 |
+
{"role": "user", "content": []}
|
| 215 |
+
]
|
| 216 |
+
|
| 217 |
+
# Add the first image only for suggestion
|
| 218 |
+
messages[1]["content"].append(
|
| 219 |
+
{"image": f"data:image/png;base64,{encode_image(pil_images[0])}"}
|
| 220 |
+
)
|
| 221 |
+
messages[1]["content"].append({"text": NEXT_SCENE_PROMPT})
|
| 222 |
+
|
| 223 |
+
completion = client.chat.completions.create(
|
| 224 |
+
model="Qwen/Qwen3-235B-A22B-Instruct-2507",
|
| 225 |
+
messages=messages,
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
suggested_prompt = completion.choices[0].message.content.strip()
|
| 229 |
+
print(f"Suggested Next Scene Prompt: {suggested_prompt}")
|
| 230 |
+
return suggested_prompt
|
| 231 |
+
|
| 232 |
+
except Exception as e:
|
| 233 |
+
print(f"Error generating Next Scene suggestion: {e}")
|
| 234 |
+
return ""
|
| 235 |
|
| 236 |
|
| 237 |
def encode_image(pil_image):
|
|
|
|
| 370 |
gr.HTML("""
|
| 371 |
<div id="logo-title">
|
| 372 |
<img src="https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-Image/qwen_image_edit_logo.png" alt="Qwen-Image Edit Logo" width="400" style="display: block; margin: 0 auto;">
|
| 373 |
+
<h2 style="font-style: italic;color: #5b47d1;margin-top: -27px !important;margin-left: 96px">[Plus] Fast, 4-steps with Qwen Rapid AIO + Next Scene Suggestions</h2>
|
| 374 |
</div>
|
| 375 |
""")
|
| 376 |
gr.Markdown("""
|
| 377 |
[Learn more](https://github.com/QwenLM/Qwen-Image) about the Qwen-Image series.
|
| 378 |
This demo uses the new [Qwen-Image-Edit-2509](https://huggingface.co/Qwen/Qwen-Image-Edit-2509) with [Phr00t/Qwen-Image-Edit-Rapid-AIO](https://huggingface.co/Phr00t/Qwen-Image-Edit-Rapid-AIO/tree/main) + [AoT compilation & FA3](https://huggingface.co/blog/zerogpu-aoti) for accelerated inference.
|
| 379 |
Try on [Qwen Chat](https://chat.qwen.ai/), or [download model](https://huggingface.co/Qwen/Qwen-Image-Edit-2509) to run locally with ComfyUI or diffusers.
|
| 380 |
+
|
| 381 |
+
**Upload an image to automatically receive a Next Scene prompt suggestion!**
|
| 382 |
""")
|
| 383 |
with gr.Row():
|
| 384 |
with gr.Column():
|
|
|
|
| 396 |
prompt = gr.Text(
|
| 397 |
label="Prompt",
|
| 398 |
show_label=False,
|
| 399 |
+
placeholder="describe the edit instruction (auto-suggested for Next Scene)",
|
| 400 |
container=False,
|
| 401 |
)
|
| 402 |
run_button = gr.Button("Edit!", variant="primary")
|
|
|
|
| 453 |
|
| 454 |
# gr.Examples(examples=examples, inputs=[prompt], outputs=[result, seed], fn=infer, cache_examples=False)
|
| 455 |
|
| 456 |
+
# Auto-suggest prompt when images are uploaded
|
| 457 |
+
input_images.change(
|
| 458 |
+
fn=suggest_next_scene_prompt,
|
| 459 |
+
inputs=[input_images],
|
| 460 |
+
outputs=[prompt]
|
| 461 |
+
)
|
| 462 |
+
|
| 463 |
gr.on(
|
| 464 |
triggers=[run_button.click, prompt.submit],
|
| 465 |
fn=infer,
|