TenzinGayche commited on
Commit
eba4307
·
verified ·
1 Parent(s): b241860

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +19 -4
handler.py CHANGED
@@ -213,14 +213,29 @@ def TemporaryDirectory():
213
  shutil.rmtree(str(tmpdir))
214
 
215
 
216
- def download_file(s3_public_url: str, output_fn) -> Path:
217
- """Download file from a public S3 bucket URL."""
 
 
 
218
  with requests.get(s3_public_url, stream=True) as r:
219
  r.raise_for_status()
220
- with open(output_fn, "wb") as f:
221
  for chunk in r.iter_content(chunk_size=8192):
222
  f.write(chunk)
223
- return output_fn
 
 
 
 
 
 
 
 
 
 
 
 
224
 
225
  def _run_align_script(bo_fn, en_fn, output_dir):
226
  start = time.time()
 
213
  shutil.rmtree(str(tmpdir))
214
 
215
 
216
+ def download_file(s3_public_url: str, output_fn: str) -> Path:
217
+ """Download file from a public S3 bucket URL and ensure UTF-8 encoding."""
218
+ temp_fn = output_fn + ".temp"
219
+
220
+ # Download the file as binary data
221
  with requests.get(s3_public_url, stream=True) as r:
222
  r.raise_for_status()
223
+ with open(temp_fn, "wb") as f:
224
  for chunk in r.iter_content(chunk_size=8192):
225
  f.write(chunk)
226
+
227
+ # Read the downloaded file and ensure UTF-8 encoding
228
+ with open(temp_fn, 'r', encoding='utf-8', errors='replace') as f:
229
+ content = f.read()
230
+
231
+ # Write the content back with correct encoding
232
+ with open(output_fn, 'w', encoding='utf-8') as f:
233
+ f.write(content)
234
+
235
+ # Clean up: remove the temporary file
236
+ Path(temp_fn).unlink()
237
+
238
+ return Path(output_fn)
239
 
240
  def _run_align_script(bo_fn, en_fn, output_dir):
241
  start = time.time()