forked from erp-dev/erp
fear: added imageBase64 param for image search api
This commit is contained in:
@@ -45,9 +45,25 @@ class SimpleRateLimiter:
|
||||
time.sleep(self._min_interval - elapsed)
|
||||
self._last_ts = time.monotonic()
|
||||
|
||||
def _strip_data_url_base64_prefix(s: str) -> str:
|
||||
"""
|
||||
Accept both raw base64 and data URL format like:
|
||||
data:image/png;base64,AAAA...
|
||||
Return the base64 payload part.
|
||||
"""
|
||||
s = (s or '').strip()
|
||||
if not s:
|
||||
return s
|
||||
lower = s[:64].lower()
|
||||
if lower.startswith('data:') and 'base64,' in lower:
|
||||
return s.split('base64,', 1)[1].strip()
|
||||
return s
|
||||
|
||||
|
||||
def search_image_url_in_tencent_tiia(
|
||||
*,
|
||||
image_url: str,
|
||||
image_url: str | None = None,
|
||||
image_base64: str | None = None,
|
||||
limit: int | None = 10,
|
||||
offset: int | None = 0,
|
||||
match_threshold: int | None = None,
|
||||
@@ -69,7 +85,8 @@ def search_image_url_in_tencent_tiia(
|
||||
- TENCENTCLOUD_TIIA_ENDPOINT (default: tiia.tencentcloudapi.com)
|
||||
|
||||
Args:
|
||||
image_url: Absolute URL to search.
|
||||
image_url: Absolute URL to search. (preferred when both provided)
|
||||
image_base64: Base64-encoded image content.
|
||||
limit/offset/match_threshold: Optional SearchImage request parameters.
|
||||
rate_limiter: Optional in-process limiter to keep requests under QPS.
|
||||
|
||||
@@ -77,12 +94,15 @@ def search_image_url_in_tencent_tiia(
|
||||
Parsed JSON response dict from TencentCloud SDK.
|
||||
"""
|
||||
image_url = (image_url or '').strip()
|
||||
if not image_url:
|
||||
raise ValueError('image_url 不能为空')
|
||||
image_base64 = _strip_data_url_base64_prefix(image_base64 or '')
|
||||
|
||||
parsed = urlparse(image_url)
|
||||
if not (parsed.scheme and parsed.netloc):
|
||||
raise ValueError('image_url 必须为绝对 URL(包含 scheme 与 host)')
|
||||
if not image_url and not image_base64:
|
||||
raise ValueError('imageUrl 和 imageBase64 必须至少提供一个')
|
||||
|
||||
if image_url:
|
||||
parsed = urlparse(image_url)
|
||||
if not (parsed.scheme and parsed.netloc):
|
||||
raise ValueError('imageUrl 必须为绝对 URL(包含 scheme 与 host)')
|
||||
|
||||
group_id = (getattr(settings, 'TENCENTCLOUD_TIIA_GROUP_ID', '') or '').strip()
|
||||
if not group_id:
|
||||
@@ -117,7 +137,11 @@ def search_image_url_in_tencent_tiia(
|
||||
|
||||
req = models.SearchImageRequest()
|
||||
req.GroupId = str(group_id)
|
||||
req.ImageUrl = str(image_url)
|
||||
# TencentCloud: ImageUrl + ImageBase64 can be both provided, but ImageUrl wins.
|
||||
if image_url:
|
||||
req.ImageUrl = str(image_url)
|
||||
else:
|
||||
req.ImageBase64 = str(image_base64)
|
||||
|
||||
if limit is not None:
|
||||
req.Limit = int(limit)
|
||||
|
||||
Reference in New Issue
Block a user