Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- README.md +39 -6
- app.py +138 -0
- requirements.txt +6 -0
README.md
CHANGED
|
@@ -1,12 +1,45 @@
|
|
| 1 |
---
|
| 2 |
-
title: Loss
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Color Loss Demo
|
| 3 |
+
emoji: 🎨
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 3.32.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# Color Loss Demo
|
| 13 |
+
|
| 14 |
+
This app demonstrates different color loss functions that can modify the color distribution in images.
|
| 15 |
+
|
| 16 |
+
## Features
|
| 17 |
+
|
| 18 |
+
- Apply different color loss effects:
|
| 19 |
+
- Yellow Loss: Reduces yellow tones in the image
|
| 20 |
+
- Cyan Loss: Reduces cyan tones in the image
|
| 21 |
+
- Magenta Loss: Reduces magenta tones in the image
|
| 22 |
+
- Control the strength of the color loss effect
|
| 23 |
+
- Upload your own images to apply color loss effects
|
| 24 |
+
- Generate placeholder images in different styles
|
| 25 |
+
|
| 26 |
+
## How to Use
|
| 27 |
+
|
| 28 |
+
1. Enter a prompt (for placeholder image text)
|
| 29 |
+
2. Select a style for the placeholder image
|
| 30 |
+
3. Choose a color loss type (or "None" for no effect)
|
| 31 |
+
4. Set a seed value (for reproducibility)
|
| 32 |
+
5. Adjust the color loss strength
|
| 33 |
+
6. Upload your own image (optional)
|
| 34 |
+
7. Click "Apply Color Loss"
|
| 35 |
+
|
| 36 |
+
## Examples
|
| 37 |
+
|
| 38 |
+
The app includes several example configurations that you can try with a single click.
|
| 39 |
+
|
| 40 |
+
## Technical Details
|
| 41 |
+
|
| 42 |
+
This app uses:
|
| 43 |
+
- PIL for image processing
|
| 44 |
+
- NumPy for efficient array operations
|
| 45 |
+
- Custom color loss functions that target specific color ranges
|
app.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from PIL import Image, ImageDraw
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import base64
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
from diffusers import StableDiffusionPipeline
|
| 7 |
+
import torch
|
| 8 |
+
|
| 9 |
+
# Define style names and their corresponding prompts
|
| 10 |
+
style_configs = {
|
| 11 |
+
"Watercolor": {
|
| 12 |
+
"prompt": "a serene mountain landscape with lake and sunset, watercolor painting style, yellow sun, artistic",
|
| 13 |
+
"negative_prompt": "digital art, photorealistic, sketch"
|
| 14 |
+
},
|
| 15 |
+
"Cyberpunk": {
|
| 16 |
+
"prompt": "cyberpunk city at night with neon signs in yellow, futuristic buildings, blade runner style",
|
| 17 |
+
"negative_prompt": "daytime, natural, watercolor"
|
| 18 |
+
},
|
| 19 |
+
"Anime": {
|
| 20 |
+
"prompt": "anime character portrait, Studio Ghibli style, yellow hair, bright colors",
|
| 21 |
+
"negative_prompt": "photorealistic, western art"
|
| 22 |
+
},
|
| 23 |
+
"Oil Painting": {
|
| 24 |
+
"prompt": "still life with yellow sunflowers in vase, oil painting style, Van Gogh inspired",
|
| 25 |
+
"negative_prompt": "watercolor, digital art, photograph"
|
| 26 |
+
},
|
| 27 |
+
"Sketch": {
|
| 28 |
+
"prompt": "pencil sketch of a landscape with yellow highlights, detailed drawing",
|
| 29 |
+
"negative_prompt": "color, painting, digital"
|
| 30 |
+
}
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
# Initialize Stable Diffusion pipeline
|
| 34 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 35 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 36 |
+
"runwayml/stable-diffusion-v1-5",
|
| 37 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
| 38 |
+
)
|
| 39 |
+
pipe = pipe.to(device)
|
| 40 |
+
|
| 41 |
+
# Cache for generated images
|
| 42 |
+
image_cache = {}
|
| 43 |
+
|
| 44 |
+
def generate_styled_image(style):
|
| 45 |
+
"""Generate an image using Stable Diffusion based on style"""
|
| 46 |
+
if style in image_cache:
|
| 47 |
+
return image_cache[style]
|
| 48 |
+
|
| 49 |
+
config = style_configs[style]
|
| 50 |
+
image = pipe(
|
| 51 |
+
prompt=config["prompt"],
|
| 52 |
+
negative_prompt=config["negative_prompt"],
|
| 53 |
+
num_inference_steps=30,
|
| 54 |
+
guidance_scale=7.5
|
| 55 |
+
).images[0]
|
| 56 |
+
|
| 57 |
+
# Cache the generated image
|
| 58 |
+
image_cache[style] = image
|
| 59 |
+
return image
|
| 60 |
+
|
| 61 |
+
def yellow_loss(image, strength=0.8):
|
| 62 |
+
"""Reduces yellow colors in the image."""
|
| 63 |
+
try:
|
| 64 |
+
img_array = np.array(image).astype(np.float32) / 255.0
|
| 65 |
+
r, g, b = img_array[:, :, 0], img_array[:, :, 1], img_array[:, :, 2]
|
| 66 |
+
|
| 67 |
+
yellow_mask = np.logical_and(np.logical_and(r > 0.5, g > 0.5), b < 0.4)
|
| 68 |
+
|
| 69 |
+
if np.any(yellow_mask):
|
| 70 |
+
r[yellow_mask] *= (1 - strength * 0.7)
|
| 71 |
+
g[yellow_mask] *= (1 - strength)
|
| 72 |
+
b[yellow_mask] += (1 - b[yellow_mask]) * strength
|
| 73 |
+
|
| 74 |
+
img_array[:, :, 0] = r
|
| 75 |
+
img_array[:, :, 1] = g
|
| 76 |
+
img_array[:, :, 2] = b
|
| 77 |
+
|
| 78 |
+
return Image.fromarray((img_array * 255).astype(np.uint8))
|
| 79 |
+
except Exception as e:
|
| 80 |
+
print(f"Error in yellow_loss: {e}")
|
| 81 |
+
return image
|
| 82 |
+
|
| 83 |
+
def apply_color_loss(style, strength, image_input=None):
|
| 84 |
+
"""Apply yellow loss to an image."""
|
| 85 |
+
try:
|
| 86 |
+
if image_input is not None:
|
| 87 |
+
try:
|
| 88 |
+
image = Image.fromarray(image_input) if isinstance(image_input, np.ndarray) else image_input
|
| 89 |
+
image.thumbnail((512, 512), Image.LANCZOS)
|
| 90 |
+
except Exception as e:
|
| 91 |
+
print(f"Error processing input image: {e}")
|
| 92 |
+
image = generate_styled_image(style)
|
| 93 |
+
else:
|
| 94 |
+
image = generate_styled_image(style)
|
| 95 |
+
|
| 96 |
+
# Apply yellow loss
|
| 97 |
+
result = yellow_loss(image, strength)
|
| 98 |
+
|
| 99 |
+
# Create side-by-side comparison
|
| 100 |
+
comparison = Image.new('RGB', (image.width * 2 + 10, image.height), (240, 240, 240))
|
| 101 |
+
comparison.paste(image, (0, 0))
|
| 102 |
+
comparison.paste(result, (image.width + 10, 0))
|
| 103 |
+
|
| 104 |
+
# Add labels
|
| 105 |
+
draw = ImageDraw.Draw(comparison)
|
| 106 |
+
draw.text((10, 10), f"Original ({style})", fill=(255, 255, 255), stroke_fill=(0, 0, 0), stroke_width=2)
|
| 107 |
+
draw.text((image.width + 20, 10), f"Yellow Loss: {strength:.1f}", fill=(255, 255, 255), stroke_fill=(0, 0, 0), stroke_width=2)
|
| 108 |
+
|
| 109 |
+
return comparison
|
| 110 |
+
|
| 111 |
+
except Exception as e:
|
| 112 |
+
print(f"Error in apply_color_loss: {e}")
|
| 113 |
+
return Image.new('RGB', (512, 256), (200, 200, 200))
|
| 114 |
+
|
| 115 |
+
# Create Gradio interface
|
| 116 |
+
demo = gr.Interface(
|
| 117 |
+
fn=apply_color_loss,
|
| 118 |
+
inputs=[
|
| 119 |
+
gr.Dropdown(choices=list(style_configs.keys()), value="Watercolor", label="Style"),
|
| 120 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.8, step=0.1, label="Yellow Loss Strength"),
|
| 121 |
+
gr.Image(label="Upload an image (optional)", type="pil")
|
| 122 |
+
],
|
| 123 |
+
outputs=gr.Image(label="Result (Before and After)"),
|
| 124 |
+
title="Yellow Loss Demo",
|
| 125 |
+
description="This demo shows how yellow loss affects different artistic styles. Each style is generated using Stable Diffusion.",
|
| 126 |
+
examples=[
|
| 127 |
+
["Watercolor", 0.8, None],
|
| 128 |
+
["Cyberpunk", 0.5, None],
|
| 129 |
+
["Anime", 0.9, None],
|
| 130 |
+
["Oil Painting", 0.7, None],
|
| 131 |
+
["Sketch", 0.6, None]
|
| 132 |
+
],
|
| 133 |
+
cache_examples=True
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
# Launch the app
|
| 137 |
+
if __name__ == "__main__":
|
| 138 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy>=1.24.0
|
| 2 |
+
pillow>=9.0.0
|
| 3 |
+
gradio>=4.0.0
|
| 4 |
+
torch>=2.0.0
|
| 5 |
+
diffusers>=0.21.4
|
| 6 |
+
transformers>=4.31.0
|