Skip to content

Commit 8dd33a6

Browse files
committed
Add progressbar for file uploads
1 parent 5be06e2 commit 8dd33a6

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/together/commands/files.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,5 @@ def _run_retrieve(args: argparse.Namespace) -> None:
125125

126126
def _run_retrieve_content(args: argparse.Namespace) -> None:
127127
files = Files(args.endpoint, log_level=args.log)
128-
output = files.retrieve_file_content(args.file_id, args.log)
128+
output = files.retrieve_file_content(args.file_id, args.output)
129129
print(output)

src/together/files.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import requests
1111
from tqdm import tqdm
12+
from tqdm.utils import CallbackIOWrapper
1213

1314

1415
DEFAULT_ENDPOINT = "https://api.together.xyz/"
@@ -133,12 +134,17 @@ def upload_file(self, file: str) -> Dict[str, Union[str, int]]:
133134

134135
self.logger.info("Uploading file...")
135136

137+
file_size = os.stat(file).st_size
136138
with open(file, "rb") as f:
137-
response = requests.put(r2_signed_url, data=f)
139+
with tqdm(
140+
total=file_size, unit="B", unit_scale=True, unit_divisor=1024
141+
) as t:
142+
wrapped_file = CallbackIOWrapper(t.update, f, "read")
143+
response = requests.put(r2_signed_url, data=wrapped_file)
138144

139-
self.logger.info("> File uploaded.")
145+
self.logger.info("File uploaded.")
140146
self.logger.debug(f"status code: {response.status_code}")
141-
self.logger.info("> Processing file...")
147+
self.logger.info("Processing file...")
142148
preprocess_url = urllib.parse.urljoin(
143149
self.endpoint_url, f"{file_id}/preprocess"
144150
)
@@ -148,7 +154,7 @@ def upload_file(self, file: str) -> Dict[str, Union[str, int]]:
148154
headers=headers,
149155
)
150156

151-
self.logger.info("> File processed")
157+
self.logger.info("File processed")
152158
self.logger.debug(f"Status code: {response.status_code}")
153159

154160
except Exception as e:

0 commit comments

Comments
 (0)