Instructions to use openpecha/aligner with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use openpecha/aligner with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForSeq2SeqLM tokenizer = AutoTokenizer.from_pretrained("openpecha/aligner") model = AutoModelForSeq2SeqLM.from_pretrained("openpecha/aligner", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Update handler.py
Browse files- 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
|
| 217 |
-
"""Download file from a public S3 bucket URL
|
| 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(
|
| 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 |
|