Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions dashscope/aigc/image_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,15 @@ def _get_input(cls,
has_upload = False
if negative_prompt is not None:
inputs[NEGATIVE_PROMPT] = negative_prompt
if images is not None:
inputs[IMAGES] = images
if images is not None and images and len(images) > 0:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The condition to check if images is a non-empty list is redundant. In Python, you can simply use if images:, which evaluates to True for non-empty lists and False for None or empty lists. This is more idiomatic and readable.

Suggested change
if images is not None and images and len(images) > 0:
if images:

new_images = []
for image in images:
is_upload, new_image = check_and_upload_local(
model, image, api_key)
if is_upload:
has_upload = True
new_images.append(new_image)
inputs[IMAGES] = new_images
if sketch_image_url is not None and sketch_image_url:
is_upload, sketch_image_url = check_and_upload_local(
model, sketch_image_url, api_key)
Expand Down Expand Up @@ -178,10 +185,20 @@ def _get_input(cls,
headers['X-DashScope-OssResourceResolve'] = 'enable'
kwargs['headers'] = headers

if task is None:
task = ImageSynthesis.task
if model is not None and model and 'imageedit' in model:
task = 'image2image'
def __get_i2i_task(task, model) -> str:
# 处理task参数:优先使用有效的task值
if task is not None and task != "":
return task

# 根据model确定任务类型
if model is not None and model != "":
if 'imageedit' in model or "wan2.5-i2i" in model:
return 'image2image'

# 默认返回文本到图像任务
return ImageSynthesis.task

task = __get_i2i_task(task, model)
Comment on lines +188 to +201
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This nested function __get_i2i_task is defined and then called only once, which adds unnecessary complexity. The logic can be inlined for better readability.

A few other points:

  • The double underscore prefix __ is typically used for name mangling in classes and is not needed for a local helper function. A single underscore _ would be more appropriate if the function were kept.
  • The comments are in Chinese, which is inconsistent with the rest of the file's English comments. It's best to maintain a single language for comments within a file.

I suggest replacing the function definition and call with the equivalent inlined logic.

Suggested change
def __get_i2i_task(task, model) -> str:
# 处理task参数:优先使用有效的task值
if task is not None and task != "":
return task
# 根据model确定任务类型
if model is not None and model != "":
if 'imageedit' in model or "wan2.5-i2i" in model:
return 'image2image'
# 默认返回文本到图像任务
return ImageSynthesis.task
task = __get_i2i_task(task, model)
if not task:
if model and ('imageedit' in model or 'wan2.5-i2i' in model):
task = 'image2image'
else:
task = ImageSynthesis.task


return inputs, kwargs, task

Expand Down