dcd018 commited on
Commit
2ab7a69
·
1 Parent(s): 55667d5

Inference.

Browse files
Files changed (1) hide show
  1. app/api/api_v1/endpoints/openai.py +31 -2
app/api/api_v1/endpoints/openai.py CHANGED
@@ -26,6 +26,8 @@ import glob
26
  import papermill as pm
27
  import shutil
28
  import nbformat as nbf
 
 
29
 
30
  from langchain.vectorstores.pgvector import (
31
  CollectionStore,
@@ -41,6 +43,25 @@ PIL.Image.MAX_IMAGE_PIXELS = None
41
  router = APIRouter()
42
  embeddings = OpenAIEmbeddings()
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  def convert_from_ls(result):
45
  if 'original_width' not in result or 'original_height' not in result:
46
  return None
@@ -65,6 +86,14 @@ def embed_documents(collection: CollectionStore):
65
  pre_delete_embeddings=True
66
  )
67
 
 
 
 
 
 
 
 
 
68
  def embed(db: Session, payload: schemas.LabelStudio):
69
 
70
  task_id = payload.task["id"]
@@ -83,7 +112,7 @@ def embed(db: Session, payload: schemas.LabelStudio):
83
 
84
  notebook_dir = "{}/notebooks".format(tmp_dir)
85
 
86
- label_studio_project.export_tasks("COCO", False, True, [task_id], tmp_archive)
87
 
88
  with zipfile.ZipFile(tmp_archive, 'r') as zip_ref:
89
  zip_ref.extractall(tmp_dir)
@@ -178,7 +207,7 @@ async def create_collection(
178
  target_height = round(height - (height * 0.75))
179
  cover = cover.resize((target_width, target_height), PIL.Image.Resampling.LANCZOS)
180
  cover.save(tmp_file, optimize=True, quality=95)
181
- tasks = label_studio_project.import_tasks(tmp_file)
182
  os.remove(tmp_file)
183
 
184
 
 
26
  import papermill as pm
27
  import shutil
28
  import nbformat as nbf
29
+ import random
30
+ import time
31
 
32
  from langchain.vectorstores.pgvector import (
33
  CollectionStore,
 
43
  router = APIRouter()
44
  embeddings = OpenAIEmbeddings()
45
 
46
+ def retry_with_backoff(retries = 5, backoff_in_seconds = 1):
47
+ def rwb(f):
48
+ def wrapper(*args, **kwargs):
49
+ x = 0
50
+ while True:
51
+ try:
52
+ return f(*args, **kwargs)
53
+ except:
54
+ if x == retries:
55
+ raise
56
+
57
+ sleep = (backoff_in_seconds * 2 ** x +
58
+ random.uniform(0, 1))
59
+ time.sleep(sleep)
60
+ x += 1
61
+
62
+ return wrapper
63
+ return rwb
64
+
65
  def convert_from_ls(result):
66
  if 'original_width' not in result or 'original_height' not in result:
67
  return None
 
86
  pre_delete_embeddings=True
87
  )
88
 
89
+ @retry_with_backoff(retries=6)
90
+ def export_tasks(task_id, tmp_archive):
91
+ label_studio_project.export_tasks("COCO", False, True, [task_id], tmp_archive)
92
+
93
+ @retry_with_backoff(retries=6)
94
+ def import_tasks(tmp_file):
95
+ return label_studio_project.import_tasks(tmp_file)
96
+
97
  def embed(db: Session, payload: schemas.LabelStudio):
98
 
99
  task_id = payload.task["id"]
 
112
 
113
  notebook_dir = "{}/notebooks".format(tmp_dir)
114
 
115
+ export_tasks(task_id, tmp_archive)
116
 
117
  with zipfile.ZipFile(tmp_archive, 'r') as zip_ref:
118
  zip_ref.extractall(tmp_dir)
 
207
  target_height = round(height - (height * 0.75))
208
  cover = cover.resize((target_width, target_height), PIL.Image.Resampling.LANCZOS)
209
  cover.save(tmp_file, optimize=True, quality=95)
210
+ tasks = import_tasks(tmp_file)
211
  os.remove(tmp_file)
212
 
213