Spaces:
Running
Running
Daryl Lim Claude Sonnet 4.6 commited on
Commit ·
5f9e08e
1
Parent(s): ffd953a
refactor: remove batch translation code
Browse filesCo-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
app.py
CHANGED
|
@@ -3,11 +3,8 @@ Translation interface using the MADLAD-400 3B model.
|
|
| 3 |
Translates English text to 22 production-ready languages from the MADLAD-400 paper.
|
| 4 |
"""
|
| 5 |
|
| 6 |
-
import csv
|
| 7 |
-
import tempfile
|
| 8 |
import warnings
|
| 9 |
from functools import lru_cache
|
| 10 |
-
from pathlib import Path
|
| 11 |
|
| 12 |
import gradio as gr
|
| 13 |
import spaces
|
|
@@ -83,93 +80,6 @@ def translate(
|
|
| 83 |
return result
|
| 84 |
|
| 85 |
|
| 86 |
-
def _parse_input_file(file_path: str) -> tuple[list[str], list[dict] | None]:
|
| 87 |
-
"""Parse a .txt or .csv file into a list of texts to translate.
|
| 88 |
-
|
| 89 |
-
Returns (texts, original_data). original_data is None for .txt files,
|
| 90 |
-
or a list of row dicts for .csv files (used to preserve extra columns in output).
|
| 91 |
-
"""
|
| 92 |
-
path = Path(file_path)
|
| 93 |
-
ext = path.suffix.lower()
|
| 94 |
-
|
| 95 |
-
if ext == ".txt":
|
| 96 |
-
texts = [line for line in path.read_text().splitlines() if line.strip()]
|
| 97 |
-
return texts, None
|
| 98 |
-
|
| 99 |
-
if ext == ".csv":
|
| 100 |
-
with open(path, newline="") as f:
|
| 101 |
-
reader = csv.DictReader(f)
|
| 102 |
-
if reader.fieldnames is None or "text" not in reader.fieldnames:
|
| 103 |
-
raise ValueError("CSV file must have a 'text' column.")
|
| 104 |
-
rows = list(reader)
|
| 105 |
-
texts = [row["text"] for row in rows]
|
| 106 |
-
return texts, rows
|
| 107 |
-
|
| 108 |
-
raise ValueError(f"Unsupported file type: {ext}. Use .txt or .csv.")
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
def _write_output_file(translations: list[str], original_data: list[dict] | None, ext: str) -> str:
|
| 112 |
-
"""Write translation results to a temp file and return its path."""
|
| 113 |
-
if ext == ".txt":
|
| 114 |
-
f = tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False)
|
| 115 |
-
f.write("\n".join(translations))
|
| 116 |
-
f.close()
|
| 117 |
-
return f.name
|
| 118 |
-
|
| 119 |
-
# CSV: append translation column to original data
|
| 120 |
-
f = tempfile.NamedTemporaryFile(mode="w", suffix=".csv", delete=False, newline="")
|
| 121 |
-
assert original_data is not None
|
| 122 |
-
fieldnames = list(original_data[0].keys()) + ["translation"]
|
| 123 |
-
writer = csv.DictWriter(f, fieldnames=fieldnames)
|
| 124 |
-
writer.writeheader()
|
| 125 |
-
for row, translation in zip(original_data, translations):
|
| 126 |
-
row["translation"] = translation
|
| 127 |
-
writer.writerow(row)
|
| 128 |
-
f.close()
|
| 129 |
-
return f.name
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
def _build_table_data(texts: list[str], translations: list[str], original_data: list[dict] | None) -> dict:
|
| 133 |
-
"""Build a dict with headers and data for gr.Dataframe display."""
|
| 134 |
-
if original_data is None:
|
| 135 |
-
return {"headers": ["source", "translation"], "data": [[t, tr] for t, tr in zip(texts, translations)]}
|
| 136 |
-
headers = list(original_data[0].keys()) + ["translation"]
|
| 137 |
-
data = [[row[k] for k in original_data[0].keys()] + [tr] for row, tr in zip(original_data, translations)]
|
| 138 |
-
return {"headers": headers, "data": data}
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
@spaces.GPU
|
| 142 |
-
def translate_batch(
|
| 143 |
-
file,
|
| 144 |
-
target_language_name: str,
|
| 145 |
-
max_new_tokens: int = 512,
|
| 146 |
-
num_beams: int = 1,
|
| 147 |
-
temperature: float = 1.0,
|
| 148 |
-
progress=gr.Progress(),
|
| 149 |
-
) -> tuple[dict, str]:
|
| 150 |
-
if file is None:
|
| 151 |
-
raise gr.Error("Please upload a file first.")
|
| 152 |
-
|
| 153 |
-
file_path = file.name if hasattr(file, "name") else str(file)
|
| 154 |
-
ext = Path(file_path).suffix.lower()
|
| 155 |
-
texts, original_data = _parse_input_file(file_path)
|
| 156 |
-
|
| 157 |
-
if not texts:
|
| 158 |
-
raise gr.Error("File is empty or contains no text to translate.")
|
| 159 |
-
|
| 160 |
-
if num_beams > 1 and temperature != 1.0:
|
| 161 |
-
gr.Info("Temperature has no effect when beam search is enabled (num_beams > 1).")
|
| 162 |
-
|
| 163 |
-
translations = []
|
| 164 |
-
for i, text in enumerate(progress.tqdm(texts, desc="Translating")):
|
| 165 |
-
result = translate(text, target_language_name, max_new_tokens, num_beams, temperature)
|
| 166 |
-
translations.append(result)
|
| 167 |
-
|
| 168 |
-
output_path = _write_output_file(translations, original_data, ext)
|
| 169 |
-
table_data = _build_table_data(texts, translations, original_data)
|
| 170 |
-
return table_data, output_path
|
| 171 |
-
|
| 172 |
-
|
| 173 |
def _build_demo() -> gr.Blocks:
|
| 174 |
_, language_names = _build_language_mappings()
|
| 175 |
|
|
@@ -228,24 +138,6 @@ def _build_demo() -> gr.Blocks:
|
|
| 228 |
inputs=[input_text, target_language],
|
| 229 |
)
|
| 230 |
|
| 231 |
-
with gr.Tab("Batch"):
|
| 232 |
-
with gr.Row():
|
| 233 |
-
batch_input = gr.File(
|
| 234 |
-
label="Upload file (.txt, one sentence per line, or .csv with 'text' column)",
|
| 235 |
-
file_types=[".txt", ".csv"],
|
| 236 |
-
)
|
| 237 |
-
batch_output = gr.File(label="Download translations", interactive=False)
|
| 238 |
-
|
| 239 |
-
batch_btn = gr.Button("Translate", variant="primary")
|
| 240 |
-
|
| 241 |
-
batch_preview = gr.Dataframe(label="Preview", interactive=False)
|
| 242 |
-
|
| 243 |
-
batch_btn.click(
|
| 244 |
-
fn=translate_batch,
|
| 245 |
-
inputs=[batch_input, target_language, max_new_tokens, num_beams, temperature],
|
| 246 |
-
outputs=[batch_preview, batch_output],
|
| 247 |
-
)
|
| 248 |
-
|
| 249 |
return demo
|
| 250 |
|
| 251 |
|
|
|
|
| 3 |
Translates English text to 22 production-ready languages from the MADLAD-400 paper.
|
| 4 |
"""
|
| 5 |
|
|
|
|
|
|
|
| 6 |
import warnings
|
| 7 |
from functools import lru_cache
|
|
|
|
| 8 |
|
| 9 |
import gradio as gr
|
| 10 |
import spaces
|
|
|
|
| 80 |
return result
|
| 81 |
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
def _build_demo() -> gr.Blocks:
|
| 84 |
_, language_names = _build_language_mappings()
|
| 85 |
|
|
|
|
| 138 |
inputs=[input_text, target_language],
|
| 139 |
)
|
| 140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
return demo
|
| 142 |
|
| 143 |
|