Spaces:
Sleeping
Sleeping
Alexandros Popov
commited on
Commit
·
a60e987
1
Parent(s):
4eb456f
added few shots examples.
Browse files- agents.py +205 -2
- filters.py +6 -4
agents.py
CHANGED
|
@@ -29,7 +29,7 @@ SmolagentsInstrumentor().instrument(tracer_provider=trace_provider)
|
|
| 29 |
HUGGING_FACE_TOKEN = os.environ["HUGGING_FACE_TOKEN"]
|
| 30 |
|
| 31 |
image_operator_model = InferenceClientModel(
|
| 32 |
-
model_id="Qwen/Qwen3-
|
| 33 |
)
|
| 34 |
|
| 35 |
picture_operator_prompt = """
|
|
@@ -56,6 +56,210 @@ picture_operator_prompt = """
|
|
| 56 |
Once all enhancements are satisfactory, your task is complete.
|
| 57 |
You must call to conversion function rgb_to_hsl and hsl_to_rgb only once for each.
|
| 58 |
You will be fined every time you exceed 1 call for each function.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
"""
|
| 60 |
|
| 61 |
picture_operator = CodeAgent(
|
|
@@ -75,7 +279,6 @@ picture_operator = CodeAgent(
|
|
| 75 |
flt.load_image,
|
| 76 |
jdg.critic,
|
| 77 |
flt.rgb_to_hsl,
|
| 78 |
-
flt.hsl_to_rgb,
|
| 79 |
],
|
| 80 |
model=image_operator_model,
|
| 81 |
name="PictureOperator",
|
|
|
|
| 29 |
HUGGING_FACE_TOKEN = os.environ["HUGGING_FACE_TOKEN"]
|
| 30 |
|
| 31 |
image_operator_model = InferenceClientModel(
|
| 32 |
+
model_id="Qwen/Qwen3-32B", provider="nebius", token=HUGGING_FACE_TOKEN, max_tokens=5000
|
| 33 |
)
|
| 34 |
|
| 35 |
picture_operator_prompt = """
|
|
|
|
| 56 |
Once all enhancements are satisfactory, your task is complete.
|
| 57 |
You must call to conversion function rgb_to_hsl and hsl_to_rgb only once for each.
|
| 58 |
You will be fined every time you exceed 1 call for each function.
|
| 59 |
+
You must save the image only once.
|
| 60 |
+
|
| 61 |
+
The code must be structured as follows :
|
| 62 |
+
1. You load the image
|
| 63 |
+
2. You call the needed that take a rgb image as input
|
| 64 |
+
3. You convert the image into h, s, li canals
|
| 65 |
+
4. You adjust the saturation, luminance and hue of color channels
|
| 66 |
+
5. You convert back into r,g,b
|
| 67 |
+
6. You save the rgb image
|
| 68 |
+
|
| 69 |
+
Examples:
|
| 70 |
+
Prompt: Increase contrast by a lot and orange saturation by a bit.
|
| 71 |
+
Answer:
|
| 72 |
+
img = load_image(path=image_path)
|
| 73 |
+
|
| 74 |
+
# Apply strong contrast adjustment
|
| 75 |
+
img = adjust_contrast(
|
| 76 |
+
img=img,
|
| 77 |
+
factor=1.1 # 1.1 is considered a lot per tool documentation
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
# Convert to HSL for color-specific adjustments
|
| 81 |
+
h, s, li = rgb_to_hsl(img)
|
| 82 |
+
|
| 83 |
+
# Enhance orange saturation slightly
|
| 84 |
+
h, s, li = adjust_saturation_color(
|
| 85 |
+
h=h,
|
| 86 |
+
s=s,
|
| 87 |
+
li=li,
|
| 88 |
+
color='orange',
|
| 89 |
+
factor=0.2)
|
| 90 |
+
# Convert back to RGB
|
| 91 |
+
|
| 92 |
+
# Save the result
|
| 93 |
+
save_image(
|
| 94 |
+
h, s, li
|
| 95 |
+
output_directory=output_directory
|
| 96 |
+
)
|
| 97 |
+
critic(output_directory=output_path,
|
| 98 |
+
original_image_path=image_path,
|
| 99 |
+
user_prompt=user_query,
|
| 100 |
+
list_of_enhancements=enhancements)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
Prompt 2: increase contrast by a lot, raise saturation medium,
|
| 105 |
+
add some vignetage, a very little of grain,
|
| 106 |
+
raise the exposition by a tiny bit,
|
| 107 |
+
raise the orange saturation by a bit, the blue yellow and green luminance by a lot
|
| 108 |
+
|
| 109 |
+
Answer:
|
| 110 |
+
img = load_image(path=image_path)
|
| 111 |
+
|
| 112 |
+
# Convert back to RGB and apply global adjustments
|
| 113 |
+
img = adjust_contrast(img, factor=1.5) # Increase contrast a lot
|
| 114 |
+
img = adjust_saturation(img, factor=1.2) # Medium saturation increase
|
| 115 |
+
img = adjust_exposure(img, ev=0.05) # Tiny exposure increase
|
| 116 |
+
img = add_vignette(img, strength=0.5) # Add some vignette
|
| 117 |
+
img = add_grain(img, amount=0.01) # Add very little grain
|
| 118 |
+
|
| 119 |
+
h, s, li = rgb_to_hsl(img)
|
| 120 |
+
# Adjust orange saturation by a bit
|
| 121 |
+
h, s, li = adjust_saturation_color(h, s, li, color='orange', factor=1.1)
|
| 122 |
+
|
| 123 |
+
# Increase luminance for blue, yellow, and green by a lot (factor=2)
|
| 124 |
+
for color in ['blue', 'yellow', 'green']:
|
| 125 |
+
h, s, li = adjust_luminance_color(h, s, li, color=color, factor=2)
|
| 126 |
+
|
| 127 |
+
# Save the processed image
|
| 128 |
+
save_image(h, s, li, output_directory=output_directory)
|
| 129 |
+
|
| 130 |
+
# Final confirmation
|
| 131 |
+
critic(output_directory=output_path,
|
| 132 |
+
original_image_path=image_path,
|
| 133 |
+
user_prompt=user_query,
|
| 134 |
+
list_of_enhancements=enhancements)
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
Prompt 3:
|
| 138 |
+
Here’s my proposal to enhance the image with a vibrant,
|
| 139 |
+
Instagram-style aesthetic while preserving its serene energy:
|
| 140 |
+
**1. Global Adjustments**
|
| 141 |
+
**Contrast**: Slightly increased to add depth to the balloons'
|
| 142 |
+
patterns without flattening the sky's gradient.
|
| 143 |
+
**Exposure**: Brightened moderately to amplify the sunlit atmosphere,
|
| 144 |
+
especially on the foreground balloon's geometric design.
|
| 145 |
+
**Saturation**: Boosted a lot to intensify the mosaic of colors
|
| 146 |
+
(red, orange, yellow, green, blue) on the balloons, making them feel more dynamic.
|
| 147 |
+
**Temperature**: Warmed up to enhance the golden-hour glow, complementing the balloons' warm gradients.
|
| 148 |
+
**Shadows/Highlights**: Shadows lifted slightly to reveal texture
|
| 149 |
+
in the balloon fabrics, while highlights are tamed
|
| 150 |
+
to avoid blowing out the sky's delicate clouds.
|
| 151 |
+
**2. Color-Specific Tweaks**
|
| 152 |
+
**Red**: Boosted saturation significantly for the background Red Bull
|
| 153 |
+
balloon to make the brand text pop, while slightly increasing
|
| 154 |
+
luminance to prevent it from feeling too heavy.
|
| 155 |
+
**Orange**: Enhanced hue slightly, shifting toward amber to deepen the
|
| 156 |
+
middle balloon's gradient, adding warmth without muddiness.
|
| 157 |
+
**Blue**: Adjusted hue to a richer cobalt tone in the foreground balloon's
|
| 158 |
+
pattern, making the geometric shapes stand out against warmer hues.
|
| 159 |
+
**Green**: Increased luminance moderately in the foreground balloon's
|
| 160 |
+
green sections to balance the vibrant reds and oranges.
|
| 161 |
+
**3. Subtle Textures**
|
| 162 |
+
**Vignette**: Applied barely, with a subtle darkening at the corners
|
| 163 |
+
to frame the balloons without distracting from the sky's serenity.
|
| 164 |
+
**Grain**: Omitted entirely—this scene’s tranquility works best with a clean, smooth finish.
|
| 165 |
+
**Result**: A luminous, hyper-saturated scene where the balloons’ colors
|
| 166 |
+
feel bolder and more immersive, the sky appears crisper,
|
| 167 |
+
and the overall mood is elevated to evoke joyful adventure.
|
| 168 |
+
The adjustments amplify the image’s natural vibrancy
|
| 169 |
+
without sacrificing its peaceful essence.
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
Answer:
|
| 173 |
+
img = load_image(path=image_path)
|
| 174 |
+
|
| 175 |
+
# Apply global adjustments in RGB space
|
| 176 |
+
img = adjust_exposure(
|
| 177 |
+
img=img,
|
| 178 |
+
ev=0.1 # Moderate brightening for golden-hour amplification
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
img = adjust_contrast(
|
| 182 |
+
img=img,
|
| 183 |
+
factor=1.05 # Slightly increased depth for balloon patterns
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
img = adjust_saturation(
|
| 187 |
+
img=img,
|
| 188 |
+
factor=1.5 # "Boosted a lot" to enhance vibrancy
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
img = adjust_temperature(
|
| 192 |
+
img=img,
|
| 193 |
+
delta=500 # Warm up by 500 mireds for golden-hour glow
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
+
img = adjust_shadows_highlights(
|
| 197 |
+
img=img,
|
| 198 |
+
shadow=1.1, # Slight shadow lifting to reveal textures
|
| 199 |
+
highlight=0.9 # Tame highlights to preserve sky colors
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
img = add_vignette(
|
| 203 |
+
img=img,
|
| 204 |
+
strength=0.3 # Subtle corner darkening for framing effect
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
# Convert to HSL for color-specific adjustments
|
| 208 |
+
h, s, li = rgb_to_hsl(img)
|
| 209 |
+
|
| 210 |
+
# Red color tweaks: Boost saturation and luminance
|
| 211 |
+
h, s, li = adjust_saturation_color(
|
| 212 |
+
h=h,
|
| 213 |
+
s=s,
|
| 214 |
+
li=li,
|
| 215 |
+
color='red',
|
| 216 |
+
factor=1.3 # "Significantly" boosted saturation
|
| 217 |
+
)
|
| 218 |
+
h, s, li = adjust_luminance_color(
|
| 219 |
+
h=h,
|
| 220 |
+
s=s,
|
| 221 |
+
li=li,
|
| 222 |
+
color='red',
|
| 223 |
+
factor=1.1 # Slight luminance lift to avoid heaviness
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
# Orange hue shift toward amber
|
| 227 |
+
h, s, li = adjust_hue_color(
|
| 228 |
+
h=h,
|
| 229 |
+
s=s,
|
| 230 |
+
li=li,
|
| 231 |
+
color='orange',
|
| 232 |
+
delta=15 # 15° shift = slight hue adjustment
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
# Blue hue adjustment to cobalt
|
| 236 |
+
h, s, li = adjust_hue_color(
|
| 237 |
+
h=h,
|
| 238 |
+
s=s,
|
| 239 |
+
li=li,
|
| 240 |
+
color='blue',
|
| 241 |
+
delta=15 # Slight shift to richer tones
|
| 242 |
+
)
|
| 243 |
+
|
| 244 |
+
# Green luminance increase for balance
|
| 245 |
+
h, s, li = adjust_luminance_color(
|
| 246 |
+
h=h,
|
| 247 |
+
s=s,
|
| 248 |
+
li=li,
|
| 249 |
+
color='green',
|
| 250 |
+
factor=1.2 # Moderate luminance lift for balance
|
| 251 |
+
)
|
| 252 |
+
|
| 253 |
+
# Save final enhanced image
|
| 254 |
+
save_image(
|
| 255 |
+
h=h, s=s, li=li,
|
| 256 |
+
output_directory=output_path
|
| 257 |
+
)
|
| 258 |
+
|
| 259 |
+
critic(output_directory=output_path,
|
| 260 |
+
original_image_path=image_path,
|
| 261 |
+
user_prompt=user_query,
|
| 262 |
+
list_of_enhancements=enhancements)
|
| 263 |
"""
|
| 264 |
|
| 265 |
picture_operator = CodeAgent(
|
|
|
|
| 279 |
flt.load_image,
|
| 280 |
jdg.critic,
|
| 281 |
flt.rgb_to_hsl,
|
|
|
|
| 282 |
],
|
| 283 |
model=image_operator_model,
|
| 284 |
name="PictureOperator",
|
filters.py
CHANGED
|
@@ -211,7 +211,6 @@ def rgb_to_hsl(img: Image.Image) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
|
|
| 211 |
return h, s, li
|
| 212 |
|
| 213 |
|
| 214 |
-
@tool
|
| 215 |
def hsl_to_rgb(h: np.ndarray, s: np.ndarray, li: np.ndarray) -> np.ndarray:
|
| 216 |
"""Vectorised HSL→RGB conversion.
|
| 217 |
|
|
@@ -412,19 +411,22 @@ def add_grain(img: Image.Image, amount: float = 0.05) -> Image.Image:
|
|
| 412 |
|
| 413 |
|
| 414 |
@tool
|
| 415 |
-
def save_image(
|
| 416 |
-
"""Save
|
| 417 |
|
| 418 |
The image will be saved with a filename of the form "trial_N.jpeg", where N is the
|
| 419 |
current count of JPEG files in the directory.
|
| 420 |
|
| 421 |
Args:
|
| 422 |
-
|
|
|
|
|
|
|
| 423 |
output_directory (str): Path to the output directory.
|
| 424 |
|
| 425 |
Returns:
|
| 426 |
str: The full path to the saved image file.
|
| 427 |
"""
|
|
|
|
| 428 |
nb_iter = str(len([f for f in os.listdir(output_directory) if f.endswith(".jpeg")]))
|
| 429 |
output_path = os.path.join(output_directory, f"trial_{nb_iter}.jpeg")
|
| 430 |
img.save(output_path, format="JPEG", quality=95)
|
|
|
|
| 211 |
return h, s, li
|
| 212 |
|
| 213 |
|
|
|
|
| 214 |
def hsl_to_rgb(h: np.ndarray, s: np.ndarray, li: np.ndarray) -> np.ndarray:
|
| 215 |
"""Vectorised HSL→RGB conversion.
|
| 216 |
|
|
|
|
| 411 |
|
| 412 |
|
| 413 |
@tool
|
| 414 |
+
def save_image(h: np.ndarray, s: np.ndarray, li: np.ndarray, output_directory: str) -> None:
|
| 415 |
+
"""Save an HSL image as a JPEG file in the specified directory.
|
| 416 |
|
| 417 |
The image will be saved with a filename of the form "trial_N.jpeg", where N is the
|
| 418 |
current count of JPEG files in the directory.
|
| 419 |
|
| 420 |
Args:
|
| 421 |
+
h (np.ndarray): Hue channel `[0, 1]`.
|
| 422 |
+
s (np.ndarray): Saturation channel `[0, 1]`.
|
| 423 |
+
li (np.ndarray): Lightness channel `[0, 1]`.
|
| 424 |
output_directory (str): Path to the output directory.
|
| 425 |
|
| 426 |
Returns:
|
| 427 |
str: The full path to the saved image file.
|
| 428 |
"""
|
| 429 |
+
img = hsl_to_rgb(h, s, li)
|
| 430 |
nb_iter = str(len([f for f in os.listdir(output_directory) if f.endswith(".jpeg")]))
|
| 431 |
output_path = os.path.join(output_directory, f"trial_{nb_iter}.jpeg")
|
| 432 |
img.save(output_path, format="JPEG", quality=95)
|