Skip to content

Commit 1ca5929

Browse files
authored
Fix blocking code on upload and wait routine (#143)
1 parent c2b56e3 commit 1ca5929

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## 4.4.5 (unreleased)
44

55

6-
- Nothing changed yet.
6+
- Fix blocking code on upload and wait routine
77

88

99
## 4.4.4 (2025-01-10)

nuclia/lib/nua.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@
5252
Tokens,
5353
)
5454
from nuclia_models.predict.remi import RemiRequest, RemiResponse
55+
import os
56+
from tqdm import tqdm
57+
import asyncio
5558

59+
MB = 1024 * 1024
60+
CHUNK_SIZE = 10 * MB
5661
SENTENCE_PREDICT = "/api/v1/predict/sentence"
5762
CHAT_PREDICT = "/api/v1/predict/chat"
5863
SUMMARIZE_PREDICT = "/api/v1/predict/summarize"
@@ -672,10 +677,26 @@ async def process_file(
672677

673678
headers = self.headers.copy()
674679
headers["X-FILENAME"] = base64.b64encode(filename.encode()).decode()
675-
async with aiofiles.open(path, "rb") as file_to_upload:
676-
data = await file_to_upload.read()
677680

678-
resp = await self.client.post(upload_endpoint, content=data, headers=headers)
681+
async def iterator(path: str):
682+
total_size = os.path.getsize(path)
683+
with tqdm(
684+
desc="Uploading data",
685+
total=total_size,
686+
unit="iB",
687+
unit_scale=True,
688+
) as pbar:
689+
async with aiofiles.open(path, "rb") as f:
690+
while True:
691+
chunk = await f.read(CHUNK_SIZE)
692+
if not chunk:
693+
break
694+
pbar.update(len(chunk))
695+
yield chunk
696+
697+
resp = await self.client.post(
698+
upload_endpoint, content=iterator(path), headers=headers
699+
)
679700

680701
payload = PushPayload(
681702
uuid=None, source=Source.HTTP, kbid=RestrictedIDString(kbid)
@@ -697,7 +718,7 @@ async def wait_for_processing(
697718
count = timeout
698719
while status.completed is False and status.failed is False and count > 0:
699720
status = await self.processing_id_status(response.processing_id)
700-
sleep(3)
721+
await asyncio.sleep(1)
701722
count -= 1
702723

703724
bm = None

0 commit comments

Comments
 (0)