Alexandros Popov
		
	commited on
		
		
					Commit 
							
							·
						
						367869b
	
1
								Parent(s):
							
							648949f
								
set call_to_director.
Browse files
    	
        judges.py
    CHANGED
    
    | @@ -1,6 +1,7 @@ | |
| 1 | 
             
            import base64
         | 
| 2 | 
             
            import io
         | 
| 3 | 
             
            import os
         | 
|  | |
| 4 | 
             
            import tempfile
         | 
| 5 |  | 
| 6 | 
             
            from openai import OpenAI
         | 
| @@ -16,19 +17,16 @@ def pil_image_to_data_url(pil_image, format="JPEG"): | |
| 16 | 
             
                return f"data:{mime_type};base64,{base64_encoded_data}"
         | 
| 17 |  | 
| 18 |  | 
| 19 | 
            -
            def call_to_llm( | 
| 20 | 
            -
                img = Image.open(image_path)
         | 
| 21 | 
            -
                data_url = pil_image_to_data_url(img, format=img.format)
         | 
| 22 |  | 
| 23 | 
            -
                user_content = [
         | 
| 24 | 
            -
             | 
| 25 | 
            -
                     | 
| 26 | 
            -
             | 
|  | |
| 27 |  | 
| 28 | 
            -
                if  | 
| 29 | 
            -
                     | 
| 30 | 
            -
                    data_url2 = pil_image_to_data_url(img2, format=img2.format)
         | 
| 31 | 
            -
                    user_content.append({"type": "image_url", "image_url": {"url": data_url2}})
         | 
| 32 |  | 
| 33 | 
             
                client = OpenAI(
         | 
| 34 | 
             
                    base_url="https://api.studio.nebius.com/v1/",
         | 
| @@ -55,46 +53,6 @@ def call_to_llm(image_path, model, system_prompt=None, user_prompt=None, second_ | |
| 55 | 
             
                return completion.to_dict()
         | 
| 56 |  | 
| 57 |  | 
| 58 | 
            -
            @tool
         | 
| 59 | 
            -
            def propose_operations(image_path: str, user_prompt: str = "Improve this image.") -> str:
         | 
| 60 | 
            -
                """
         | 
| 61 | 
            -
                Analyzes the provided image and suggests a series of enhancement operations.
         | 
| 62 | 
            -
             | 
| 63 | 
            -
                Args:
         | 
| 64 | 
            -
                    image_path (str): The file path to the image to be analyzed.
         | 
| 65 | 
            -
                    user_prompt (str): Additional instructions or context provided by the user.
         | 
| 66 | 
            -
             | 
| 67 | 
            -
                Returns:
         | 
| 68 | 
            -
                    str: A response from the AI art director suggesting operations to apply to the image.
         | 
| 69 | 
            -
                """
         | 
| 70 | 
            -
             | 
| 71 | 
            -
                system_prompt = (
         | 
| 72 | 
            -
                    "You are an AI art director. Your task is to propose a sequence of image enhancement operations "
         | 
| 73 | 
            -
                    "to transform the provided image according to the user's request. "
         | 
| 74 | 
            -
                    "You take strong decisions with important consequences on the image.\n"
         | 
| 75 | 
            -
                    "Consider the following possible operations:\n"
         | 
| 76 | 
            -
                    "- adjust_contrast\n"
         | 
| 77 | 
            -
                    "- adjust_exposure\n"
         | 
| 78 | 
            -
                    "- adjust_saturation\n"
         | 
| 79 | 
            -
                    "- adjust_shadows_highlights\n"
         | 
| 80 | 
            -
                    "- adjust_temperature\n"
         | 
| 81 | 
            -
                    "- adjust_tint\n"
         | 
| 82 | 
            -
                    "- adjust_hue_color colors are [red, orange, yellow, green, aqua, blue, purple, magenta]\n"
         | 
| 83 | 
            -
                    "- adjust_saturation_color colors are [red, orange, yellow, green, aqua, blue, purple, magenta]\n"
         | 
| 84 | 
            -
                    "- adjust_luminance_color colors are [red, orange, yellow, green, aqua, blue, purple, magenta]\n"
         | 
| 85 | 
            -
                    "- add_vignette\n"
         | 
| 86 | 
            -
                    "- add_grain\n"
         | 
| 87 | 
            -
                    "In particular, you should use the methods that adjust colors luminance staturation and hue. "
         | 
| 88 | 
            -
                    "I want at least 3 colors to be adjusted.\n"
         | 
| 89 | 
            -
                    "When citing the methods, describe qualitatively how much the effect should be applied : "
         | 
| 90 | 
            -
                    "a lot, bearly, to the maximum, ..."
         | 
| 91 | 
            -
                )
         | 
| 92 | 
            -
                response = call_to_llm(
         | 
| 93 | 
            -
                    image_path, model="Qwen/Qwen2.5-VL-72B-Instruct", system_prompt=system_prompt, user_prompt=user_prompt
         | 
| 94 | 
            -
                )
         | 
| 95 | 
            -
                return response["choices"][0]["message"]["content"]
         | 
| 96 | 
            -
             | 
| 97 | 
            -
             | 
| 98 | 
             
            def concatenate_images_side_by_side(image_path1: str, image_path2: str) -> str:
         | 
| 99 | 
             
                """
         | 
| 100 | 
             
                Concatenates two images side by side, saves the result to a temporary file, and returns the file path.
         | 
| @@ -126,6 +84,51 @@ def concatenate_images_side_by_side(image_path1: str, image_path2: str) -> str: | |
| 126 | 
             
                return temp_file.name
         | 
| 127 |  | 
| 128 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 129 | 
             
            @tool
         | 
| 130 | 
             
            def critic(new_image_path: str, original_image_path: str, user_prompt: str, list_of_enhancements: str) -> str:
         | 
| 131 | 
             
                """
         | 
| @@ -162,8 +165,8 @@ def critic(new_image_path: str, original_image_path: str, user_prompt: str, list | |
| 162 | 
             
                )
         | 
| 163 |  | 
| 164 | 
             
                response = call_to_llm(
         | 
| 165 | 
            -
                     | 
| 166 | 
            -
                     | 
| 167 | 
             
                    system_prompt=system_prompt,
         | 
| 168 | 
             
                    user_prompt=f"the user wishes for : {user_prompt}.\n The enhancement applied are {list_of_enhancements}",
         | 
| 169 | 
             
                )
         | 
| @@ -172,5 +175,7 @@ def critic(new_image_path: str, original_image_path: str, user_prompt: str, list | |
| 172 |  | 
| 173 |  | 
| 174 | 
             
            if __name__ == "__main__":
         | 
| 175 | 
            -
                res = propose_operations(image_path="small_test_image.jpg")
         | 
| 176 | 
            -
                print(res["choices"][0]["message"]["content"])
         | 
|  | |
|  | 
|  | |
| 1 | 
             
            import base64
         | 
| 2 | 
             
            import io
         | 
| 3 | 
             
            import os
         | 
| 4 | 
            +
            import re
         | 
| 5 | 
             
            import tempfile
         | 
| 6 |  | 
| 7 | 
             
            from openai import OpenAI
         | 
|  | |
| 17 | 
             
                return f"data:{mime_type};base64,{base64_encoded_data}"
         | 
| 18 |  | 
| 19 |  | 
| 20 | 
            +
            def call_to_llm(model, image_path=None, system_prompt=None, user_prompt=None):
         | 
|  | |
|  | |
| 21 |  | 
| 22 | 
            +
                user_content = []
         | 
| 23 | 
            +
                if image_path:
         | 
| 24 | 
            +
                    img = Image.open(image_path)
         | 
| 25 | 
            +
                    data_url = pil_image_to_data_url(img, format=img.format)
         | 
| 26 | 
            +
                    user_content.append({"type": "image_url", "image_url": {"url": data_url}})
         | 
| 27 |  | 
| 28 | 
            +
                if user_prompt:
         | 
| 29 | 
            +
                    user_content.append({"type": "text", "text": user_prompt})
         | 
|  | |
|  | |
| 30 |  | 
| 31 | 
             
                client = OpenAI(
         | 
| 32 | 
             
                    base_url="https://api.studio.nebius.com/v1/",
         | 
|  | |
| 53 | 
             
                return completion.to_dict()
         | 
| 54 |  | 
| 55 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 56 | 
             
            def concatenate_images_side_by_side(image_path1: str, image_path2: str) -> str:
         | 
| 57 | 
             
                """
         | 
| 58 | 
             
                Concatenates two images side by side, saves the result to a temporary file, and returns the file path.
         | 
|  | |
| 84 | 
             
                return temp_file.name
         | 
| 85 |  | 
| 86 |  | 
| 87 | 
            +
            def call_to_director(image_path: str, user_prompt):
         | 
| 88 | 
            +
                describe_prompt = "describe this image. Include color distribution and exposition description"
         | 
| 89 | 
            +
                image_description = call_to_llm(
         | 
| 90 | 
            +
                    "mistralai/Mistral-Small-3.1-24B-Instruct-2503",
         | 
| 91 | 
            +
                    image_path=image_path,
         | 
| 92 | 
            +
                    user_prompt=describe_prompt,
         | 
| 93 | 
            +
                    system_prompt="you are a helpful AI",
         | 
| 94 | 
            +
                )
         | 
| 95 | 
            +
             | 
| 96 | 
            +
                image_description = image_description["choices"][0]["message"]["content"]
         | 
| 97 | 
            +
                system_prompt = (
         | 
| 98 | 
            +
                    "You are an AI art director. Your task is to propose a sequence of image enhancement operations "
         | 
| 99 | 
            +
                    "to transform the provided image according to the user's request. "
         | 
| 100 | 
            +
                    "You take strong decisions with important consequences on the image.\n"
         | 
| 101 | 
            +
                    "Consider the following possible operations:\n"
         | 
| 102 | 
            +
                    "- adjust_contrast\n"
         | 
| 103 | 
            +
                    "- adjust_exposure\n"
         | 
| 104 | 
            +
                    "- adjust_saturation\n"
         | 
| 105 | 
            +
                    "- adjust_shadows_highlights\n"
         | 
| 106 | 
            +
                    "- adjust_temperature\n"
         | 
| 107 | 
            +
                    "- adjust_tint\n"
         | 
| 108 | 
            +
                    "- adjust_hue_color colors are [red, orange, yellow, green, aqua, blue, purple, magenta]\n"
         | 
| 109 | 
            +
                    "- adjust_saturation_color colors are [red, orange, yellow, green, aqua, blue, purple, magenta]'n"
         | 
| 110 | 
            +
                    "- adjust_luminance_color colors are [red, orange, yellow, green, aqua, blue, purple, magenta]\n"
         | 
| 111 | 
            +
                    "- add_vignette\n"
         | 
| 112 | 
            +
                    "- add_grain\n"
         | 
| 113 | 
            +
                    "In particular, you should use the methods that adjust colors luminance staturation and hue. "
         | 
| 114 | 
            +
                    "I want at least 3 colors to be adjusted.\n"
         | 
| 115 | 
            +
                    "When citing the methods, you should say more or less and describe qualitatively"
         | 
| 116 | 
            +
                    " how much the effect should be applied : a lot, bearly, to the maximum, ..."
         | 
| 117 | 
            +
                    "DO NOT INCLUDE ANY NUMBER. SIMPLY DESCRIBE THE STRENGTH OF THE EFFECT."
         | 
| 118 | 
            +
                )
         | 
| 119 | 
            +
                print(image_description)
         | 
| 120 | 
            +
                directions = call_to_llm(
         | 
| 121 | 
            +
                    "Qwen/Qwen3-235B-A22B",
         | 
| 122 | 
            +
                    user_prompt=f"my image is : {image_description}. The user request is {user_prompt}",
         | 
| 123 | 
            +
                    system_prompt=system_prompt,
         | 
| 124 | 
            +
                )
         | 
| 125 | 
            +
                directions_str = directions["choices"][0]["message"]["content"]
         | 
| 126 | 
            +
                if "<think>" in directions_str and "</think>" in directions_str:
         | 
| 127 | 
            +
                    directions_str = re.sub(r"<think>.*?</think>", "", directions_str, flags=re.DOTALL)
         | 
| 128 | 
            +
                directions["choices"][0]["message"]["content"] = directions_str
         | 
| 129 | 
            +
                return directions["choices"][0]["message"]["content"]
         | 
| 130 | 
            +
             | 
| 131 | 
            +
             | 
| 132 | 
             
            @tool
         | 
| 133 | 
             
            def critic(new_image_path: str, original_image_path: str, user_prompt: str, list_of_enhancements: str) -> str:
         | 
| 134 | 
             
                """
         | 
|  | |
| 165 | 
             
                )
         | 
| 166 |  | 
| 167 | 
             
                response = call_to_llm(
         | 
| 168 | 
            +
                    "Qwen/Qwen2.5-VL-72B-Instruct",
         | 
| 169 | 
            +
                    image_path=path_to_concat,
         | 
| 170 | 
             
                    system_prompt=system_prompt,
         | 
| 171 | 
             
                    user_prompt=f"the user wishes for : {user_prompt}.\n The enhancement applied are {list_of_enhancements}",
         | 
| 172 | 
             
                )
         | 
|  | |
| 175 |  | 
| 176 |  | 
| 177 | 
             
            if __name__ == "__main__":
         | 
| 178 | 
            +
                # res = propose_operations(image_path="small_test_image.jpg")
         | 
| 179 | 
            +
                # print(res["choices"][0]["message"]["content"])
         | 
| 180 | 
            +
                directions = call_to_director("small_forest.jpg", "give it a winter vibe")
         | 
| 181 | 
            +
                print(directions)
         | 
