@@ -118,23 +118,31 @@ def edit_image(
118118 if not current_image_data :
119119 return None , _ ("Failed to export current image" )
120120
121- if (progress_callback and not progress_callback (
122- _ ("Building Replicate edit request..." ), PROGRESS_UPLOAD
123- )):
124- return None , _ ("Operation cancelled" )
125-
126- model_input = self ._build_edit_input (
127- prompt , current_image_data , reference_images
128- )
129-
130- if (progress_callback and not progress_callback (
131- _ ("Sending edit request to Replicate..." ), PROGRESS_PROCESS
132- )):
133- return None , _ ("Operation cancelled" )
134-
135- output = self ._execute_api_call (model_input )
136- if isinstance (output , str ):
137- return None , output
121+ with self ._prepare_reference_images (
122+ reference_images , self .model .max_reference_images_edit
123+ ) as ref_files :
124+ if (progress_callback and not progress_callback (
125+ _ ("Building Replicate edit request..." ), PROGRESS_UPLOAD
126+ )):
127+ return None , _ ("Operation cancelled" )
128+
129+ from settings import get_model_settings
130+ user_settings = get_model_settings (self .model .name )
131+ model_input = self .model .build_edit_input (
132+ prompt = prompt ,
133+ main_image = io .BytesIO (current_image_data ),
134+ reference_images = ref_files if ref_files else None ,
135+ user_settings = user_settings
136+ )
137+
138+ if (progress_callback and not progress_callback (
139+ _ ("Sending edit request to Replicate..." ), PROGRESS_PROCESS
140+ )):
141+ return None , _ ("Operation cancelled" )
142+
143+ output = self ._execute_api_call (model_input )
144+ if isinstance (output , str ):
145+ return None , output
138146
139147 return self ._process_response (output , progress_callback )
140148
@@ -171,67 +179,36 @@ def generate_image(
171179 )
172180
173181 try :
174- if (progress_callback and not progress_callback (
175- _ ("Generating image with Replicate..." ), PROGRESS_PREPARE
176- )):
177- return None , _ ("Operation cancelled" )
178-
179- model_input = self ._build_generation_input (
180- prompt , reference_images
181- )
182-
183- if (progress_callback and not progress_callback (
184- _ ("Sending request to Replicate..." ), PROGRESS_PROCESS
185- )):
186- return None , _ ("Operation cancelled" )
187-
188- output = self ._execute_api_call (model_input )
189- if isinstance (output , str ):
190- return None , output
182+ with self ._prepare_reference_images (
183+ reference_images , self .model .max_reference_images
184+ ) as ref_files :
185+ if (progress_callback and not progress_callback (
186+ _ ("Generating image with Replicate..." ), PROGRESS_PREPARE
187+ )):
188+ return None , _ ("Operation cancelled" )
189+
190+ from settings import get_model_settings
191+ user_settings = get_model_settings (self .model .name )
192+ model_input = self .model .build_generation_input (
193+ prompt = prompt ,
194+ reference_images = ref_files if reference_images else None ,
195+ user_settings = user_settings
196+ )
197+
198+ if (progress_callback and not progress_callback (
199+ _ ("Sending request to Replicate..." ), PROGRESS_PROCESS
200+ )):
201+ return None , _ ("Operation cancelled" )
202+
203+ output = self ._execute_api_call (model_input )
204+ if isinstance (output , str ):
205+ return None , output
191206
192207 return self ._process_response (output , progress_callback )
193208
194209 except Exception as e :
195210 return None , _ ("Unexpected error: {error}" ).format (error = str (e ))
196211
197- def _build_edit_input (
198- self ,
199- prompt : str ,
200- current_image_data : bytes ,
201- reference_images : Optional [List [str ]] = None
202- ) -> dict :
203- """Build input parameters for edit operation."""
204- from settings import get_model_settings
205-
206- with self ._prepare_reference_images (
207- reference_images , self .model .max_reference_images_edit
208- ) as ref_files :
209- user_settings = get_model_settings (self .model .name )
210- return self .model .build_edit_input (
211- prompt = prompt ,
212- main_image = io .BytesIO (current_image_data ),
213- reference_images = ref_files if ref_files else None ,
214- user_settings = user_settings
215- )
216-
217- def _build_generation_input (
218- self ,
219- prompt : str ,
220- reference_images : Optional [List [str ]] = None
221- ) -> dict :
222- """Build input parameters for generation operation."""
223- from settings import get_model_settings
224-
225- with self ._prepare_reference_images (
226- reference_images , self .model .max_reference_images
227- ) as ref_files :
228- user_settings = get_model_settings (self .model .name )
229- return self .model .build_generation_input (
230- prompt = prompt ,
231- reference_images = ref_files if reference_images else None ,
232- user_settings = user_settings
233- )
234-
235212 def _bytes_to_pixbuf (
236213 self , image_bytes : bytes
237214 ) -> Optional [GdkPixbuf .Pixbuf ]:
0 commit comments