Inference.
Browse files
app/api/api_v1/endpoints/openai.py
CHANGED
|
@@ -91,6 +91,47 @@ def embed_documents(collection: CollectionStore):
|
|
| 91 |
pre_delete_embeddings=True
|
| 92 |
)
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
@retry_with_backoff(retries=6)
|
| 95 |
def export_task(task_id, tmp_archive):
|
| 96 |
response = requests.get(
|
|
@@ -212,7 +253,7 @@ async def create_collection(
|
|
| 212 |
collection_name = str(uuid.uuid4())
|
| 213 |
tmp_file = "/tmp/{}.jpg".format(collection_name)
|
| 214 |
|
| 215 |
-
results = requests.get(collection_in.location)
|
| 216 |
pages = convert_from_bytes(results.content, 500, None, 1, 1)
|
| 217 |
cover = pages[0]
|
| 218 |
width, height = cover.size
|
|
@@ -220,7 +261,7 @@ async def create_collection(
|
|
| 220 |
target_height = round(height - (height * 0.8))
|
| 221 |
cover = cover.resize((target_width, target_height), PIL.Image.Resampling.LANCZOS)
|
| 222 |
cover.save(tmp_file, optimize=True, quality=75)
|
| 223 |
-
tasks =
|
| 224 |
os.remove(tmp_file)
|
| 225 |
|
| 226 |
|
|
|
|
| 91 |
pre_delete_embeddings=True
|
| 92 |
)
|
| 93 |
|
| 94 |
+
@retry_with_backoff(retries=6)
|
| 95 |
+
def import_task(tmp_file):
|
| 96 |
+
with open(tmp_file, mode='rb') as f:
|
| 97 |
+
response = requests.get(
|
| 98 |
+
"{}/api/projects/{}/import".format(settings.LABEL_STUDIO_URL, label_studio_project.id),
|
| 99 |
+
headers={"Authorization": "Token {}".format(settings.LABEL_STUDIO_API_KEY)},
|
| 100 |
+
timeout=(500, 500),
|
| 101 |
+
files={'file': f})
|
| 102 |
+
|
| 103 |
+
response = response.json()
|
| 104 |
+
|
| 105 |
+
if 'import' in response:
|
| 106 |
+
# check import status
|
| 107 |
+
timeout = 500
|
| 108 |
+
fibonacci_backoff = [1, 1]
|
| 109 |
+
|
| 110 |
+
start_time = time.time()
|
| 111 |
+
|
| 112 |
+
while True:
|
| 113 |
+
import_status = requests.get(
|
| 114 |
+
"{}/api/projects/{}/imports/{}".format(settings.LABEL_STUDIO_URL, label_studio_project.id, response["import"]),
|
| 115 |
+
headers={"Authorization": "Token {}".format(settings.LABEL_STUDIO_API_KEY)},
|
| 116 |
+
timeout=(500, 500)).json()
|
| 117 |
+
|
| 118 |
+
if import_status['status'] == 'completed':
|
| 119 |
+
return import_status['task_ids']
|
| 120 |
+
|
| 121 |
+
if import_status['status'] == 'failed':
|
| 122 |
+
raise Exception(import_status['error'])
|
| 123 |
+
|
| 124 |
+
if time.time() - start_time >= timeout:
|
| 125 |
+
raise Exception('Import timeout')
|
| 126 |
+
|
| 127 |
+
time.sleep(fibonacci_backoff[0])
|
| 128 |
+
fibonacci_backoff = [
|
| 129 |
+
fibonacci_backoff[1],
|
| 130 |
+
fibonacci_backoff[0] + fibonacci_backoff[1],
|
| 131 |
+
]
|
| 132 |
+
|
| 133 |
+
return response['task_ids']
|
| 134 |
+
|
| 135 |
@retry_with_backoff(retries=6)
|
| 136 |
def export_task(task_id, tmp_archive):
|
| 137 |
response = requests.get(
|
|
|
|
| 253 |
collection_name = str(uuid.uuid4())
|
| 254 |
tmp_file = "/tmp/{}.jpg".format(collection_name)
|
| 255 |
|
| 256 |
+
results = requests.get(collection_in.location, timeout=(500, 500))
|
| 257 |
pages = convert_from_bytes(results.content, 500, None, 1, 1)
|
| 258 |
cover = pages[0]
|
| 259 |
width, height = cover.size
|
|
|
|
| 261 |
target_height = round(height - (height * 0.8))
|
| 262 |
cover = cover.resize((target_width, target_height), PIL.Image.Resampling.LANCZOS)
|
| 263 |
cover.save(tmp_file, optimize=True, quality=75)
|
| 264 |
+
tasks = import_task(tmp_file)
|
| 265 |
os.remove(tmp_file)
|
| 266 |
|
| 267 |
|