Spaces:
Running
on
Zero
Running
on
Zero
Update app_temp.py
Browse files- app_temp.py +45 -93
app_temp.py
CHANGED
|
@@ -3,6 +3,8 @@ import numpy as np
|
|
| 3 |
import random
|
| 4 |
import torch
|
| 5 |
import spaces
|
|
|
|
|
|
|
| 6 |
|
| 7 |
from PIL import Image
|
| 8 |
from diffusers import FlowMatchEulerDiscreteScheduler
|
|
@@ -23,80 +25,9 @@ SYSTEM_PROMPT = '''
|
|
| 23 |
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.
|
| 24 |
|
| 25 |
Please strictly follow the rewriting rules below:
|
| 26 |
-
|
| 27 |
-
## 1. General Principles
|
| 28 |
-
- Keep the rewritten prompt **concise and comprehensive**. Avoid overly long sentences and unnecessary descriptive language.
|
| 29 |
-
- If the instruction is contradictory, vague, or unachievable, prioritize reasonable inference and correction, and supplement details when necessary.
|
| 30 |
-
- Keep the main part of the original instruction unchanged, only enhancing its clarity, rationality, and visual feasibility.
|
| 31 |
-
- All added objects or modifications must align with the logic and style of the scene in the input images.
|
| 32 |
-
- If multiple sub-images are to be generated, describe the content of each sub-image individually.
|
| 33 |
-
|
| 34 |
-
## 2. Task-Type Handling Rules
|
| 35 |
-
|
| 36 |
-
### 1. Add, Delete, Replace Tasks
|
| 37 |
-
- If the instruction is clear (already includes task type, target entity, position, quantity, attributes), preserve the original intent and only refine the grammar.
|
| 38 |
-
- If the description is vague, supplement with minimal but sufficient details (category, color, size, orientation, position, etc.). For example:
|
| 39 |
-
> Original: "Add an animal"
|
| 40 |
-
> Rewritten: "Add a light-gray cat in the bottom-right corner, sitting and facing the camera"
|
| 41 |
-
- Remove meaningless instructions: e.g., "Add 0 objects" should be ignored or flagged as invalid.
|
| 42 |
-
- For replacement tasks, specify "Replace Y with X" and briefly describe the key visual features of X.
|
| 43 |
-
|
| 44 |
-
### 2. Text Editing Tasks
|
| 45 |
-
- All text content must be enclosed in English double quotes `" "`. Keep the original language of the text, and keep the capitalization.
|
| 46 |
-
- Both adding new text and replacing existing text are text replacement tasks, For example:
|
| 47 |
-
- Replace "xx" to "yy"
|
| 48 |
-
- Replace the mask / bounding box to "yy"
|
| 49 |
-
- Replace the visual object to "yy"
|
| 50 |
-
- Specify text position, color, and layout only if user has required.
|
| 51 |
-
- If font is specified, keep the original language of the font.
|
| 52 |
-
|
| 53 |
-
### 3. Human Editing Tasks
|
| 54 |
-
- Make the smallest changes to the given user's prompt.
|
| 55 |
-
- If changes to background, action, expression, camera shot, or ambient lighting are required, please list each modification individually.
|
| 56 |
-
- **Edits to makeup or facial features / expression must be subtle, not exaggerated, and must preserve the subject’s identity consistency.**
|
| 57 |
-
> Original: "Add eyebrows to the face"
|
| 58 |
-
> Rewritten: "Slightly thicken the person’s eyebrows with little change, look natural."
|
| 59 |
-
|
| 60 |
-
### 4. Style Conversion or Enhancement Tasks
|
| 61 |
-
- If a style is specified, describe it concisely using key visual features. For example:
|
| 62 |
-
> Original: "Disco style"
|
| 63 |
-
> Rewritten: "1970s disco style: flashing lights, disco ball, mirrored walls, vibrant colors"
|
| 64 |
-
- For style reference, analyze the original image and extract key characteristics (color, composition, texture, lighting, artistic style, etc.), integrating them into the instruction.
|
| 65 |
-
- **Colorization tasks (including old photo restoration) must use the fixed template:**
|
| 66 |
-
"Restore and colorize the old photo."
|
| 67 |
-
- Clearly specify the object to be modified. For example:
|
| 68 |
-
> Original: Modify the subject in Picture 1 to match the style of Picture 2.
|
| 69 |
-
> Rewritten: Change the girl in Picture 1 to the ink-wash style of Picture 2 — rendered in black-and-white watercolor with soft color transitions.
|
| 70 |
-
|
| 71 |
-
### 5. Material Replacement
|
| 72 |
-
- Clearly specify the object and the material. For example: "Change the material of the apple to papercut style."
|
| 73 |
-
- For text material replacement, use the fixed template:
|
| 74 |
-
"Change the material of text "xxxx" to laser style"
|
| 75 |
-
|
| 76 |
-
### 6. Logo/Pattern Editing
|
| 77 |
-
- Material replacement should preserve the original shape and structure as much as possible. For example:
|
| 78 |
-
> Original: "Convert to sapphire material"
|
| 79 |
-
> Rewritten: "Convert the main subject in the image to sapphire material, preserving similar shape and structure"
|
| 80 |
-
- When migrating logos/patterns to new scenes, ensure shape and structure consistency. For example:
|
| 81 |
-
> Original: "Migrate the logo in the image to a new scene"
|
| 82 |
-
> Rewritten: "Migrate the logo in the image to a new scene, preserving similar shape and structure"
|
| 83 |
-
|
| 84 |
-
### 7. Multi-Image Tasks
|
| 85 |
-
- Rewritten prompts must clearly point out which image’s element is being modified. For example:
|
| 86 |
-
> Original: "Replace the subject of picture 1 with the subject of picture 2"
|
| 87 |
-
> Rewritten: "Replace the girl of picture 1 with the boy of picture 2, keeping picture 2’s background unchanged"
|
| 88 |
-
- For stylization tasks, describe the reference image’s style in the rewritten prompt, while preserving the visual content of the source image.
|
| 89 |
-
|
| 90 |
-
## 3. Rationale and Logic Check
|
| 91 |
-
- Resolve contradictory instructions: e.g., “Remove all trees but keep all trees” requires logical correction.
|
| 92 |
-
- Supplement missing critical information: e.g., if position is unspecified, choose a reasonable area based on composition (near subject, blank space, center/edge, etc.).
|
| 93 |
-
|
| 94 |
-
# Output Format Example
|
| 95 |
-
```json
|
| 96 |
-
{
|
| 97 |
-
"Rewritten": "..."
|
| 98 |
-
}
|
| 99 |
'''
|
|
|
|
| 100 |
# --- Prompt Enhancement using Hugging Face InferenceClient ---
|
| 101 |
def polish_prompt_hf(prompt, img_list):
|
| 102 |
"""
|
|
@@ -111,7 +42,6 @@ def polish_prompt_hf(prompt, img_list):
|
|
| 111 |
try:
|
| 112 |
# Initialize the client
|
| 113 |
prompt = f"{SYSTEM_PROMPT}\n\nUser Input: {prompt}\n\nRewritten Prompt:"
|
| 114 |
-
# Initialize the client
|
| 115 |
client = InferenceClient(
|
| 116 |
provider="novita",
|
| 117 |
api_key=api_key,
|
|
@@ -154,8 +84,6 @@ def polish_prompt_hf(prompt, img_list):
|
|
| 154 |
print(f"Error during API call to Hugging Face: {e}")
|
| 155 |
# Fallback to original prompt if enhancement fails
|
| 156 |
return prompt
|
| 157 |
-
|
| 158 |
-
|
| 159 |
|
| 160 |
def encode_image(pil_image):
|
| 161 |
import io
|
|
@@ -208,10 +136,11 @@ optimize_pipeline_(pipe, image=[Image.new("RGB", (1024, 1024)), Image.new("RGB",
|
|
| 208 |
# --- UI Constants and Helpers ---
|
| 209 |
MAX_SEED = np.iinfo(np.int32).max
|
| 210 |
|
| 211 |
-
# --- Main Inference Function (
|
| 212 |
@spaces.GPU(duration=40)
|
| 213 |
def infer(
|
| 214 |
images,
|
|
|
|
| 215 |
prompt,
|
| 216 |
seed=42,
|
| 217 |
randomize_seed=False,
|
|
@@ -225,24 +154,28 @@ def infer(
|
|
| 225 |
):
|
| 226 |
"""
|
| 227 |
Generates an image using the local Qwen-Image diffusers pipeline.
|
|
|
|
| 228 |
"""
|
| 229 |
# Hardcode the negative prompt as requested
|
| 230 |
negative_prompt = "Vibrant colors, overexposed, static, blurry details, subtitles, style, artwork, painting, image, still, overall grayish, worst quality, low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn face, deformed, disfigured, deformed limbs, fingers fused together, static image, cluttered background, three legs, many people in the background, walking backwards. "
|
| 231 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
if randomize_seed:
|
| 233 |
seed = random.randint(0, MAX_SEED)
|
| 234 |
|
| 235 |
# Set up the generator for reproducibility
|
| 236 |
generator = torch.Generator(device=device).manual_seed(seed)
|
| 237 |
|
| 238 |
-
# Load input images into PIL Images
|
| 239 |
pil_images = []
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
print("❌ Invalid key.")
|
| 243 |
-
return None
|
| 244 |
-
prompt = prompt.replace(expected_key, "")
|
| 245 |
-
|
| 246 |
if images:
|
| 247 |
for item in images:
|
| 248 |
try:
|
|
@@ -252,16 +185,30 @@ def infer(
|
|
| 252 |
pil_images.append(Image.open(item[0]).convert("RGB"))
|
| 253 |
elif hasattr(item, "name"):
|
| 254 |
pil_images.append(Image.open(item.name).convert("RGB"))
|
| 255 |
-
except Exception:
|
| 256 |
-
|
| 257 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
if not pil_images:
|
| 259 |
default_path = os.path.join(os.path.dirname(__file__), "1.jpg")
|
| 260 |
if os.path.exists(default_path):
|
| 261 |
pil_images = [Image.open(default_path).convert("RGB")]
|
| 262 |
print("Loaded default image: 1.jpg")
|
| 263 |
else:
|
| 264 |
-
raise gr.Error("No input images and '1.jpg' not found
|
| 265 |
|
| 266 |
if height==256 and width==256:
|
| 267 |
height, width = None, None
|
|
@@ -313,10 +260,16 @@ with gr.Blocks(css=css) as demo:
|
|
| 313 |
""")
|
| 314 |
with gr.Row():
|
| 315 |
with gr.Column():
|
| 316 |
-
input_images = gr.Gallery(label="Input Images",
|
| 317 |
-
show_label=
|
| 318 |
type="pil",
|
| 319 |
interactive=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
|
| 321 |
# result = gr.Image(label="Result", show_label=False, type="pil")
|
| 322 |
result = gr.Gallery(label="Result", show_label=False, type="pil")
|
|
@@ -379,13 +332,12 @@ with gr.Blocks(css=css) as demo:
|
|
| 379 |
|
| 380 |
rewrite_prompt = gr.Checkbox(label="Rewrite prompt (being fixed)", value=False)
|
| 381 |
|
| 382 |
-
# gr.Examples(examples=examples, inputs=[prompt], outputs=[result, seed], fn=infer, cache_examples=False)
|
| 383 |
-
|
| 384 |
gr.on(
|
| 385 |
triggers=[run_button.click, prompt.submit],
|
| 386 |
fn=infer,
|
| 387 |
inputs=[
|
| 388 |
input_images,
|
|
|
|
| 389 |
prompt,
|
| 390 |
seed,
|
| 391 |
randomize_seed,
|
|
|
|
| 3 |
import random
|
| 4 |
import torch
|
| 5 |
import spaces
|
| 6 |
+
import requests
|
| 7 |
+
import io
|
| 8 |
|
| 9 |
from PIL import Image
|
| 10 |
from diffusers import FlowMatchEulerDiscreteScheduler
|
|
|
|
| 25 |
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.
|
| 26 |
|
| 27 |
Please strictly follow the rewriting rules below:
|
| 28 |
+
... (Giữ nguyên phần System Prompt để tiết kiệm không gian hiển thị) ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
'''
|
| 30 |
+
|
| 31 |
# --- Prompt Enhancement using Hugging Face InferenceClient ---
|
| 32 |
def polish_prompt_hf(prompt, img_list):
|
| 33 |
"""
|
|
|
|
| 42 |
try:
|
| 43 |
# Initialize the client
|
| 44 |
prompt = f"{SYSTEM_PROMPT}\n\nUser Input: {prompt}\n\nRewritten Prompt:"
|
|
|
|
| 45 |
client = InferenceClient(
|
| 46 |
provider="novita",
|
| 47 |
api_key=api_key,
|
|
|
|
| 84 |
print(f"Error during API call to Hugging Face: {e}")
|
| 85 |
# Fallback to original prompt if enhancement fails
|
| 86 |
return prompt
|
|
|
|
|
|
|
| 87 |
|
| 88 |
def encode_image(pil_image):
|
| 89 |
import io
|
|
|
|
| 136 |
# --- UI Constants and Helpers ---
|
| 137 |
MAX_SEED = np.iinfo(np.int32).max
|
| 138 |
|
| 139 |
+
# --- Main Inference Function (Modified to accept URL) ---
|
| 140 |
@spaces.GPU(duration=40)
|
| 141 |
def infer(
|
| 142 |
images,
|
| 143 |
+
image_url, # New parameter for URL
|
| 144 |
prompt,
|
| 145 |
seed=42,
|
| 146 |
randomize_seed=False,
|
|
|
|
| 154 |
):
|
| 155 |
"""
|
| 156 |
Generates an image using the local Qwen-Image diffusers pipeline.
|
| 157 |
+
Accepts input via gallery upload OR direct URL.
|
| 158 |
"""
|
| 159 |
# Hardcode the negative prompt as requested
|
| 160 |
negative_prompt = "Vibrant colors, overexposed, static, blurry details, subtitles, style, artwork, painting, image, still, overall grayish, worst quality, low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn face, deformed, disfigured, deformed limbs, fingers fused together, static image, cluttered background, three legs, many people in the background, walking backwards. "
|
| 161 |
+
|
| 162 |
+
# Check Auth Key
|
| 163 |
+
expected_key = os.environ.get("hf_key")
|
| 164 |
+
if expected_key and expected_key not in prompt:
|
| 165 |
+
print("❌ Invalid key.")
|
| 166 |
+
return None, seed
|
| 167 |
+
if expected_key:
|
| 168 |
+
prompt = prompt.replace(expected_key, "")
|
| 169 |
+
|
| 170 |
if randomize_seed:
|
| 171 |
seed = random.randint(0, MAX_SEED)
|
| 172 |
|
| 173 |
# Set up the generator for reproducibility
|
| 174 |
generator = torch.Generator(device=device).manual_seed(seed)
|
| 175 |
|
|
|
|
| 176 |
pil_images = []
|
| 177 |
+
|
| 178 |
+
# 1. Process Input from Gallery (Uploaded files)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
if images:
|
| 180 |
for item in images:
|
| 181 |
try:
|
|
|
|
| 185 |
pil_images.append(Image.open(item[0]).convert("RGB"))
|
| 186 |
elif hasattr(item, "name"):
|
| 187 |
pil_images.append(Image.open(item.name).convert("RGB"))
|
| 188 |
+
except Exception as e:
|
| 189 |
+
print(f"Error loading gallery image: {e}")
|
| 190 |
+
continue
|
| 191 |
+
|
| 192 |
+
# 2. Process Input from URL
|
| 193 |
+
if image_url and image_url.strip():
|
| 194 |
+
try:
|
| 195 |
+
print(f"Downloading image from: {image_url}")
|
| 196 |
+
response = requests.get(image_url.strip(), timeout=10)
|
| 197 |
+
response.raise_for_status()
|
| 198 |
+
url_img = Image.open(io.BytesIO(response.content)).convert("RGB")
|
| 199 |
+
pil_images.append(url_img)
|
| 200 |
+
except Exception as e:
|
| 201 |
+
print(f"❌ Failed to load image from URL: {e}")
|
| 202 |
+
# Optional: You could raise a gr.Error here if URL was critical
|
| 203 |
+
|
| 204 |
+
# 3. Fallback to Default if no images found
|
| 205 |
if not pil_images:
|
| 206 |
default_path = os.path.join(os.path.dirname(__file__), "1.jpg")
|
| 207 |
if os.path.exists(default_path):
|
| 208 |
pil_images = [Image.open(default_path).convert("RGB")]
|
| 209 |
print("Loaded default image: 1.jpg")
|
| 210 |
else:
|
| 211 |
+
raise gr.Error("No input images provided (upload or URL) and '1.jpg' not found.")
|
| 212 |
|
| 213 |
if height==256 and width==256:
|
| 214 |
height, width = None, None
|
|
|
|
| 260 |
""")
|
| 261 |
with gr.Row():
|
| 262 |
with gr.Column():
|
| 263 |
+
input_images = gr.Gallery(label="Input Images (Upload)",
|
| 264 |
+
show_label=True,
|
| 265 |
type="pil",
|
| 266 |
interactive=True)
|
| 267 |
+
# New Input for URL
|
| 268 |
+
image_url = gr.Textbox(
|
| 269 |
+
label="(Optional)",
|
| 270 |
+
placeholder="",
|
| 271 |
+
info=""
|
| 272 |
+
)
|
| 273 |
|
| 274 |
# result = gr.Image(label="Result", show_label=False, type="pil")
|
| 275 |
result = gr.Gallery(label="Result", show_label=False, type="pil")
|
|
|
|
| 332 |
|
| 333 |
rewrite_prompt = gr.Checkbox(label="Rewrite prompt (being fixed)", value=False)
|
| 334 |
|
|
|
|
|
|
|
| 335 |
gr.on(
|
| 336 |
triggers=[run_button.click, prompt.submit],
|
| 337 |
fn=infer,
|
| 338 |
inputs=[
|
| 339 |
input_images,
|
| 340 |
+
image_url, # Added URL input to the trigger list
|
| 341 |
prompt,
|
| 342 |
seed,
|
| 343 |
randomize_seed,
|