TenzinGayche commited on
Commit
9e25996
·
verified ·
1 Parent(s): 6140cf3

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +8 -19
handler.py CHANGED
@@ -106,6 +106,7 @@ def convert_raw_align_to_tm(align_fn: Path, tm_path: Path):
106
 
107
  def load_alignment(fn: Path):
108
  content = fn.read_text()
 
109
  if not content:
110
  return []
111
 
@@ -213,30 +214,16 @@ def TemporaryDirectory():
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
- output_path = Path(output_fn)
219
- temp_path = output_path.with_suffix('.temp')
220
-
221
- # Download the file as binary data
222
  with requests.get(s3_public_url, stream=True) as r:
223
  r.raise_for_status()
224
- with open(temp_path, "wb") as f:
225
  for chunk in r.iter_content(chunk_size=8192):
226
  f.write(chunk)
 
227
 
228
- # Read the downloaded file and ensure UTF-8 encoding
229
- with open(temp_path, 'r', encoding='utf-8', errors='replace') as f:
230
- content = f.read()
231
-
232
- # Write the content back with correct encoding
233
- with open(output_path, 'w', encoding='utf-8') as f:
234
- f.write(content)
235
-
236
- # Clean up: remove the temporary file
237
- temp_path.unlink()
238
 
239
- return output_path
240
  def _run_align_script(bo_fn, en_fn, output_dir):
241
  start = time.time()
242
  cmd = [str(ALIGNER_SCRIPT_PATH), str(bo_fn), str(en_fn), str(output_dir)]
@@ -259,7 +246,10 @@ def align(text_pair):
259
  output_dir = Path(tmpdir)
260
  bo_fn = download_file(text_pair["bo_file_url"], output_fn=output_dir / "bo.tx")
261
  en_fn = download_file(text_pair["en_file_url"], output_fn=output_dir / "en.tx")
 
 
262
  aligned_fn = _run_align_script(bo_fn, en_fn, output_dir)
 
263
  repo_url = create_tm(aligned_fn, text_pair=text_pair)
264
  return {"tm_repo_url": repo_url}
265
 
@@ -275,6 +265,5 @@ class EndpointHandler():
275
  Return:
276
  A :obj:`list`:. The list contains the embeddings of the inference inputs
277
  """
278
- data = data.pop("inputs",data)
279
  return align(data)
280
 
 
106
 
107
  def load_alignment(fn: Path):
108
  content = fn.read_text()
109
+ print("Content !!! \n\n"+content)
110
  if not content:
111
  return []
112
 
 
214
  shutil.rmtree(str(tmpdir))
215
 
216
 
217
+ def download_file(s3_public_url: str, output_fn) -> Path:
218
+ """Download file from a public S3 bucket URL."""
 
 
 
 
219
  with requests.get(s3_public_url, stream=True) as r:
220
  r.raise_for_status()
221
+ with open(output_fn, "wb") as f:
222
  for chunk in r.iter_content(chunk_size=8192):
223
  f.write(chunk)
224
+ return output_fn
225
 
 
 
 
 
 
 
 
 
 
 
226
 
 
227
  def _run_align_script(bo_fn, en_fn, output_dir):
228
  start = time.time()
229
  cmd = [str(ALIGNER_SCRIPT_PATH), str(bo_fn), str(en_fn), str(output_dir)]
 
246
  output_dir = Path(tmpdir)
247
  bo_fn = download_file(text_pair["bo_file_url"], output_fn=output_dir / "bo.tx")
248
  en_fn = download_file(text_pair["en_file_url"], output_fn=output_dir / "en.tx")
249
+ print("bo_fn: ", bo_fn)
250
+ print("en_fn: ", en_fn)
251
  aligned_fn = _run_align_script(bo_fn, en_fn, output_dir)
252
+ print("aligned_fn: ", aligned_fn)
253
  repo_url = create_tm(aligned_fn, text_pair=text_pair)
254
  return {"tm_repo_url": repo_url}
255
 
 
265
  Return:
266
  A :obj:`list`:. The list contains the embeddings of the inference inputs
267
  """
 
268
  return align(data)
269