48 lines
1.4 KiB
Plaintext
48 lines
1.4 KiB
Plaintext
# -*- coding:utf-8 -*-
|
|
import json
|
|
import sys
|
|
import os
|
|
sys.path.insert(0, os.getcwd())
|
|
|
|
method = request.method
|
|
|
|
if method == 'GET':
|
|
return json.dumps({
|
|
'usage': 'POST with JSON body',
|
|
'params': {
|
|
'images': 'list[str] (required, up to 32 images)',
|
|
' - file path': '/data/ymq/images/photo.jpg',
|
|
' - URL': 'https://example.com/image.jpg',
|
|
' - base64': 'data:image/jpeg;base64,/9j/4AAQ...'
|
|
},
|
|
'example': 'POST {images: [/path/to/img.jpg, https://example.com/img.png]}'
|
|
}, ensure_ascii=False)
|
|
|
|
images = params_kw.get('images', [])
|
|
if isinstance(images, str):
|
|
images = [images]
|
|
|
|
if not images:
|
|
return json.dumps({'error': 'images is required (list of file paths, URLs, or base64 data URIs)'}, ensure_ascii=False)
|
|
|
|
if len(images) > 32:
|
|
return json.dumps({'error': 'max 32 images per request'}, ensure_ascii=False)
|
|
|
|
try:
|
|
import time
|
|
from workers.clip_model import embed_images
|
|
t0 = time.time()
|
|
embeddings = embed_images(images)
|
|
elapsed = round(time.time() - t0, 3)
|
|
return json.dumps({
|
|
'status': 'ok',
|
|
'count': len(embeddings),
|
|
'dimension': len(embeddings[0]),
|
|
'embeddings': embeddings,
|
|
'elapsed': elapsed
|
|
}, ensure_ascii=False)
|
|
except Exception as e:
|
|
import traceback
|
|
return json.dumps({'error': str(e), 'traceback': traceback.format_exc()}, ensure_ascii=False)
|
|
|