45 lines
1.2 KiB
Plaintext
45 lines
1.2 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': {
|
|
'texts': 'list[str] (required, up to 128 texts)',
|
|
},
|
|
'example': 'POST {texts: [a cat, a dog]}'
|
|
}, ensure_ascii=False)
|
|
|
|
texts = params_kw.get('texts', [])
|
|
if isinstance(texts, str):
|
|
texts = [texts]
|
|
|
|
if not texts:
|
|
return json.dumps({'error': 'texts is required (list of strings)'}, ensure_ascii=False)
|
|
|
|
if len(texts) > 128:
|
|
return json.dumps({'error': 'max 128 texts per request'}, ensure_ascii=False)
|
|
|
|
try:
|
|
import time
|
|
from workers.clip_model import embed_texts
|
|
t0 = time.time()
|
|
embeddings = embed_texts(texts)
|
|
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)
|
|
|