Coverage for mindsdb / integrations / handlers / clipdrop_handler / clipdrop_handler.py: 0%
124 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-21 00:36 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-21 00:36 +0000
1from typing import Optional, Dict
2import pandas as pd
4from mindsdb.integrations.handlers.clipdrop_handler.clipdrop import ClipdropClient
6from mindsdb.integrations.libs.base import BaseMLEngine
8from mindsdb.utilities import log
10from mindsdb.integrations.utilities.handler_utils import get_api_key
13logger = log.getLogger(__name__)
16class ClipdropHandler(BaseMLEngine):
17 name = "clipdrop"
19 @staticmethod
20 def create_validation(target, args=None, **kwargs):
21 args = args['using']
23 available_tasks = ["remove_text", "remove_background", "sketch_to_image", "text_to_image", "replace_background", "reimagine"]
25 if 'task' not in args:
26 raise Exception(f'task has to be specified. Available tasks are - {available_tasks}')
28 if args['task'] not in available_tasks:
29 raise Exception(f'Unknown task specified. Available tasks are - {available_tasks}')
31 if 'local_directory_path' not in args:
32 raise Exception('local_directory_path has to be specified')
34 def create(self, target: str, df: Optional[pd.DataFrame] = None, args: Optional[Dict] = None) -> None:
35 if 'using' not in args:
36 raise Exception("Clipdrop AI Inference engine requires a USING clause! Refer to its documentation for more details.")
37 self.generative = True
39 args = args['using']
40 args['target'] = target
41 self.model_storage.json_set('args', args)
43 def _get_clipdrop_client(self, args):
44 api_key = get_api_key('clipdrop', args, self.engine_storage, strict=False)
46 local_directory_path = args["local_directory_path"]
48 return ClipdropClient(api_key=api_key, local_dir=local_directory_path)
50 def _process_remove_text(self, df, args):
52 def generate_remove_text(conds, client):
53 conds = conds.to_dict()
54 return client.remove_text(conds.get("image_url"))
56 supported_params = set(["image_url"])
58 if "image_url" not in df.columns:
59 raise Exception("`image_url` column has to be given in the query.")
61 for col in df.columns:
62 if col not in supported_params:
63 raise Exception(f"Unknown column {col}. Currently supported parameters for remove text - {supported_params}")
65 client = self._get_clipdrop_client(args)
67 return df[df.columns.intersection(supported_params)].apply(generate_remove_text, client=client, axis=1)
69 def _process_remove_background(self, df, args):
71 def generate_remove_background(conds, client):
72 conds = conds.to_dict()
73 return client.remove_background(conds.get("image_url"))
75 supported_params = set(["image_url"])
77 if "image_url" not in df.columns:
78 raise Exception("`image_url` column has to be given in the query.")
80 for col in df.columns:
81 if col not in supported_params:
82 raise Exception(f"Unknown column {col}. Currently supported parameters for remove background - {supported_params}")
84 client = self._get_clipdrop_client(args)
86 return df[df.columns.intersection(supported_params)].apply(generate_remove_background, client=client, axis=1)
88 def _process_sketch_to_image(self, df, args):
90 def generate_sketch_to_image(conds, client):
91 conds = conds.to_dict()
92 return client.sketch_to_image(conds.get("image_url"), conds.get("text"))
94 supported_params = set(["image_url", "text"])
96 if "image_url" not in df.columns:
97 raise Exception("`image_url` column has to be given in the query.")
99 if "text" not in df.columns:
100 raise Exception("`text` column has to be given in the query.")
102 for col in df.columns:
103 if col not in supported_params:
104 raise Exception(f"Unknown column {col}. Currently supported parameters for remove background - {supported_params}")
106 client = self._get_clipdrop_client(args)
108 return df[df.columns.intersection(supported_params)].apply(generate_sketch_to_image, client=client, axis=1)
110 def _process_text_to_image(self, df, args):
112 def generate_text_to_image(conds, client):
113 conds = conds.to_dict()
114 return client.text_to_image(conds.get("text"))
116 supported_params = set(["text"])
118 if "text" not in df.columns:
119 raise Exception("`text` column has to be given in the query.")
121 for col in df.columns:
122 if col not in supported_params:
123 raise Exception(f"Unknown column {col}. Currently supported parameters for remove background - {supported_params}")
125 client = self._get_clipdrop_client(args)
127 return df[df.columns.intersection(supported_params)].apply(generate_text_to_image, client=client, axis=1)
129 def _process_replace_background(self, df, args):
131 def generate_replace_background(conds, client):
132 conds = conds.to_dict()
133 return client.replace_background(conds.get("image_url"), conds.get("text"))
135 supported_params = set(["image_url", "text"])
137 if "image_url" not in df.columns:
138 raise Exception("`image_url` column has to be given in the query.")
140 if "text" not in df.columns:
141 raise Exception("`text` column has to be given in the query.")
143 for col in df.columns:
144 if col not in supported_params:
145 raise Exception(f"Unknown column {col}. Currently supported parameters for replace background - {supported_params}")
147 client = self._get_clipdrop_client(args)
149 return df[df.columns.intersection(supported_params)].apply(generate_replace_background, client=client, axis=1)
151 def _process_reimagine(self, df, args):
153 def generate_reimagine(conds, client):
154 conds = conds.to_dict()
155 return client.reimagine(conds.get("image_url"))
157 supported_params = set(["image_url"])
159 if "image_url" not in df.columns:
160 raise Exception("`image_url` column has to be given in the query.")
162 for col in df.columns:
163 if col not in supported_params:
164 raise Exception(f"Unknown column {col}. Currently supported parameters for reimagine - {supported_params}")
166 client = self._get_clipdrop_client(args)
168 return df[df.columns.intersection(supported_params)].apply(generate_reimagine, client=client, axis=1)
170 def predict(self, df, args=None):
172 args = self.model_storage.json_get('args')
174 if args["task"] == "remove_text":
175 preds = self._process_remove_text(df, args)
176 elif args["task"] == "remove_background":
177 preds = self._process_remove_background(df, args)
178 elif args["task"] == "sketch_to_image":
179 preds = self._process_sketch_to_image(df, args)
180 elif args["task"] == "text_to_image":
181 preds = self._process_text_to_image(df, args)
182 elif args["task"] == "replace_background":
183 preds = self._process_replace_background(df, args)
184 elif args["task"] == "reimagine":
185 preds = self._process_reimagine(df, args)
187 result_df = pd.DataFrame()
189 result_df['predictions'] = preds
191 result_df = result_df.rename(columns={'predictions': args['target']})
193 return result_df