Spaces:
Running on Zero
Running on Zero
Daryl Lim commited on
Commit ·
1f39aa6
1
Parent(s): ae98531
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,420 +1,124 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
from pathlib import Path
|
| 7 |
-
|
| 8 |
-
# Docling imports
|
| 9 |
-
from docling.datamodel.base_models import InputFormat
|
| 10 |
-
from docling.datamodel.pipeline_options import PdfPipelineOptions, TesseractCliOcrOptions
|
| 11 |
-
from docling.document_converter import DocumentConverter, PdfFormatOption, WordFormatOption, SimplePipeline
|
| 12 |
-
|
| 13 |
-
# LangChain imports for document splitting
|
| 14 |
-
from langchain.text_splitter import MarkdownHeaderTextSplitter, RecursiveCharacterTextSplitter
|
| 15 |
|
| 16 |
-
|
| 17 |
import spaces
|
|
|
|
| 18 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 19 |
from LangMap.langid_mapping import langid_to_language
|
| 20 |
|
| 21 |
-
# Constants
|
| 22 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 23 |
-
MODEL_NAME = "google/madlad400-3b-mt"
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
TOKENIZER = AutoTokenizer.from_pretrained(MODEL_NAME, use_fast=True)
|
| 28 |
-
MODEL = AutoModelForSeq2SeqLM.from_pretrained(
|
| 29 |
-
MODEL_NAME,
|
| 30 |
-
torch_dtype=torch.float16 if DEVICE.type == 'cuda' else torch.float32,
|
| 31 |
-
low_cpu_mem_usage=True
|
| 32 |
-
)
|
| 33 |
-
MODEL.to(DEVICE)
|
| 34 |
-
print("Model loaded successfully")
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
LANGUAGE_CODES = [token for token in
|
|
|
|
|
|
|
| 38 |
LANGUAGE_MAP = {k: v for k, v in langid_to_language.items() if k in LANGUAGE_CODES}
|
| 39 |
-
NAME_TO_CODE_MAP = {name: code for code, name in LANGUAGE_MAP.items()}
|
| 40 |
-
LANGUAGE_NAMES = list(LANGUAGE_MAP.values())
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
"""Determine the document format based on file extension"""
|
| 45 |
-
try:
|
| 46 |
-
file_path = str(file_path)
|
| 47 |
-
extension = os.path.splitext(file_path)[1].lower()
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
'.docx': InputFormat.DOCX,
|
| 52 |
-
'.doc': InputFormat.DOCX,
|
| 53 |
-
'.pptx': InputFormat.PPTX,
|
| 54 |
-
'.html': InputFormat.HTML,
|
| 55 |
-
'.htm': InputFormat.HTML
|
| 56 |
-
}
|
| 57 |
-
return format_map.get(extension, None)
|
| 58 |
-
except Exception as e:
|
| 59 |
-
return f"Error in get_document_format: {str(e)}"
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
|
| 63 |
-
""
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
format_options={
|
| 89 |
-
InputFormat.PDF: PdfFormatOption(
|
| 90 |
-
pipeline_options=pipeline_options,
|
| 91 |
-
),
|
| 92 |
-
InputFormat.DOCX: WordFormatOption(
|
| 93 |
-
pipeline_cls=SimplePipeline
|
| 94 |
-
)
|
| 95 |
-
}
|
| 96 |
-
)
|
| 97 |
-
|
| 98 |
-
# Convert document
|
| 99 |
-
print("Starting conversion...")
|
| 100 |
-
conv_result = converter.convert(temp_input)
|
| 101 |
-
|
| 102 |
-
if not conv_result or not conv_result.document:
|
| 103 |
-
raise ValueError(f"Failed to convert document: {doc_path}")
|
| 104 |
-
|
| 105 |
-
# Export to markdown
|
| 106 |
-
print("Exporting to markdown...")
|
| 107 |
-
md = conv_result.document.export_to_markdown()
|
| 108 |
-
|
| 109 |
-
# Create output path
|
| 110 |
-
output_dir = os.path.dirname(input_path)
|
| 111 |
-
base_name = os.path.splitext(os.path.basename(input_path))[0]
|
| 112 |
-
md_path = os.path.join(output_dir, f"{base_name}_converted.md")
|
| 113 |
-
|
| 114 |
-
# Write markdown file
|
| 115 |
-
print(f"Writing markdown to: {base_name}_converted.md")
|
| 116 |
-
with open(md_path, "w", encoding="utf-8") as fp:
|
| 117 |
-
fp.write(md)
|
| 118 |
-
|
| 119 |
-
return md_path, md
|
| 120 |
-
except Exception as e:
|
| 121 |
-
return None, f"Error converting document: {str(e)}"
|
| 122 |
|
| 123 |
-
|
| 124 |
-
def
|
| 125 |
-
"""
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
(
|
| 130 |
-
(
|
| 131 |
-
(
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
-
#
|
| 139 |
-
|
| 140 |
-
chunk_size=chunk_size,
|
| 141 |
-
chunk_overlap=chunk_overlap,
|
| 142 |
-
separators=["\n\n", "\n", " ", ""]
|
| 143 |
-
)
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
final_chunks = []
|
| 148 |
-
for doc in header_splits:
|
| 149 |
-
# Check if the chunk is larger than our desired size
|
| 150 |
-
if len(doc.page_content) > chunk_size:
|
| 151 |
-
smaller_chunks = text_splitter.split_text(doc.page_content)
|
| 152 |
-
# Add header metadata to each smaller chunk
|
| 153 |
-
for chunk in smaller_chunks:
|
| 154 |
-
chunk_with_metadata = {
|
| 155 |
-
"content": chunk,
|
| 156 |
-
"metadata": doc.metadata
|
| 157 |
-
}
|
| 158 |
-
final_chunks.append(chunk_with_metadata)
|
| 159 |
-
else:
|
| 160 |
-
chunk_with_metadata = {
|
| 161 |
-
"content": doc.page_content,
|
| 162 |
-
"metadata": doc.metadata
|
| 163 |
-
}
|
| 164 |
-
final_chunks.append(chunk_with_metadata)
|
| 165 |
-
else:
|
| 166 |
-
# If no headers, just split by character
|
| 167 |
-
text_chunks = text_splitter.split_text(markdown_text)
|
| 168 |
-
final_chunks = [{"content": chunk, "metadata": {}} for chunk in text_chunks]
|
| 169 |
|
| 170 |
-
|
|
|
|
| 171 |
|
| 172 |
-
|
| 173 |
-
def translate_text(text: str, target_language_name: str) -> str:
|
| 174 |
-
"""Translate the input text from English to another language using the pre-loaded model."""
|
| 175 |
-
try:
|
| 176 |
-
# Convert the selected language name back to its corresponding language code
|
| 177 |
-
target_language_code = NAME_TO_CODE_MAP.get(target_language_name)
|
| 178 |
-
if target_language_code is None:
|
| 179 |
-
raise ValueError(f"Unsupported language: {target_language_name}")
|
| 180 |
-
|
| 181 |
-
# Prepare input for the model
|
| 182 |
-
text = target_language_code + text
|
| 183 |
-
|
| 184 |
-
# Handle potential CUDA out of memory issues
|
| 185 |
-
try:
|
| 186 |
-
input_ids = TOKENIZER(text, return_tensors="pt").input_ids.to(DEVICE)
|
| 187 |
-
|
| 188 |
-
# Generate translation with reduced memory footprint
|
| 189 |
-
with torch.no_grad(): # Disable gradient calculation to save memory
|
| 190 |
-
outputs = MODEL.generate(
|
| 191 |
-
input_ids=input_ids,
|
| 192 |
-
max_new_tokens=1024, # Limiting tokens to avoid memory issues
|
| 193 |
-
do_sample=False,
|
| 194 |
-
num_beams=2 # Use fewer beams to reduce memory usage
|
| 195 |
-
)
|
| 196 |
-
|
| 197 |
-
# Decode the output
|
| 198 |
-
text_translated = TOKENIZER.batch_decode(outputs, skip_special_tokens=True)
|
| 199 |
-
return text_translated[0]
|
| 200 |
-
|
| 201 |
-
except torch.cuda.OutOfMemoryError:
|
| 202 |
-
# Fall back to CPU if CUDA runs out of memory
|
| 203 |
-
print("CUDA out of memory, falling back to CPU for this chunk")
|
| 204 |
-
# Move tensors to CPU
|
| 205 |
-
if DEVICE.type == 'cuda':
|
| 206 |
-
input_ids = TOKENIZER(text, return_tensors="pt").input_ids
|
| 207 |
-
model_cpu = MODEL.to('cpu')
|
| 208 |
-
with torch.no_grad():
|
| 209 |
-
outputs = model_cpu.generate(
|
| 210 |
-
input_ids=input_ids,
|
| 211 |
-
max_new_tokens=1024,
|
| 212 |
-
do_sample=False,
|
| 213 |
-
num_beams=1
|
| 214 |
-
)
|
| 215 |
-
# Move model back to GPU
|
| 216 |
-
MODEL.to(DEVICE)
|
| 217 |
-
text_translated = TOKENIZER.batch_decode(outputs, skip_special_tokens=True)
|
| 218 |
-
return text_translated[0]
|
| 219 |
-
else:
|
| 220 |
-
raise # Re-raise if not on CUDA
|
| 221 |
-
|
| 222 |
-
except Exception as e:
|
| 223 |
-
print(f"Translation error: {str(e)}")
|
| 224 |
-
# Return error message as translation result
|
| 225 |
-
return f"[Translation Error: {str(e)}]"
|
| 226 |
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
for i, chunk in enumerate(chunks):
|
| 233 |
-
try:
|
| 234 |
-
content = chunk["content"]
|
| 235 |
-
metadata = chunk["metadata"]
|
| 236 |
-
|
| 237 |
-
# Update progress before translation (to show we're working on this chunk)
|
| 238 |
-
if progress is not None:
|
| 239 |
-
progress((i) / total_chunks, f"Translating chunk {i+1}/{total_chunks}")
|
| 240 |
-
|
| 241 |
-
# Translate the content - limit chunk size if it's very large
|
| 242 |
-
if len(content) > 4000:
|
| 243 |
-
content = content[:4000] # Limit very large chunks to avoid memory issues
|
| 244 |
-
|
| 245 |
-
translated_content = translate_text(content, target_language_name)
|
| 246 |
-
|
| 247 |
-
# Store with original metadata
|
| 248 |
-
translated_chunks.append({
|
| 249 |
-
"content": translated_content,
|
| 250 |
-
"metadata": metadata
|
| 251 |
-
})
|
| 252 |
-
|
| 253 |
-
# Update progress after translation is complete
|
| 254 |
-
if progress is not None:
|
| 255 |
-
progress((i + 1) / total_chunks, f"Translated chunk {i+1}/{total_chunks}")
|
| 256 |
-
|
| 257 |
-
except Exception as e:
|
| 258 |
-
import traceback
|
| 259 |
-
error_message = f"Error translating chunk {i+1}: {str(e)}\n{traceback.format_exc()}"
|
| 260 |
-
print(error_message)
|
| 261 |
-
|
| 262 |
-
# Add error message as content
|
| 263 |
-
translated_chunks.append({
|
| 264 |
-
"content": f"[Translation Error in Chunk {i+1}: {str(e)}]",
|
| 265 |
-
"metadata": metadata if 'metadata' in locals() else {}
|
| 266 |
-
})
|
| 267 |
-
|
| 268 |
-
# Update progress to show error but still continue
|
| 269 |
-
if progress is not None:
|
| 270 |
-
progress((i + 1) / total_chunks, f"Error in chunk {i+1}/{total_chunks} - continuing...")
|
| 271 |
-
|
| 272 |
-
return translated_chunks
|
| 273 |
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
content = chunk["content"]
|
| 280 |
-
metadata = chunk["metadata"]
|
| 281 |
-
|
| 282 |
-
# Add headers if they exist in metadata
|
| 283 |
-
if "Header 1" in metadata:
|
| 284 |
-
result.append(f"# {metadata['Header 1']}")
|
| 285 |
-
if "Header 2" in metadata:
|
| 286 |
-
result.append(f"## {metadata['Header 2']}")
|
| 287 |
-
if "Header 3" in metadata:
|
| 288 |
-
result.append(f"### {metadata['Header 3']}")
|
| 289 |
-
if "Header 4" in metadata:
|
| 290 |
-
result.append(f"#### {metadata['Header 4']}")
|
| 291 |
-
|
| 292 |
-
# Add the translated content
|
| 293 |
-
result.append(content)
|
| 294 |
-
|
| 295 |
-
return "\n\n".join(result)
|
| 296 |
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
"
|
| 301 |
-
|
| 302 |
-
print(f"Starting document translation to {target_language_name}")
|
| 303 |
-
print(f"Chunk size: {chunk_size}, Chunk overlap: {chunk_overlap}")
|
| 304 |
-
|
| 305 |
-
# Handle file object based on type
|
| 306 |
-
if isinstance(file_obj, str):
|
| 307 |
-
# If it's a string path
|
| 308 |
-
temp_path = file_obj
|
| 309 |
-
else:
|
| 310 |
-
# Create temp file and save uploaded content
|
| 311 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(os.path.basename(file_obj.name))[1]) as temp_file:
|
| 312 |
-
temp_path = temp_file.name
|
| 313 |
-
# Save the uploaded file content
|
| 314 |
-
with open(file_obj.name, 'rb') as f:
|
| 315 |
-
shutil.copyfileobj(f, temp_file)
|
| 316 |
-
|
| 317 |
-
progress(0.1, "Document uploaded")
|
| 318 |
-
|
| 319 |
-
# Convert document to markdown
|
| 320 |
-
md_path, md_content = convert_document_to_markdown(temp_path)
|
| 321 |
-
if md_path is None:
|
| 322 |
-
return None, md_content # Return error message
|
| 323 |
-
|
| 324 |
-
progress(0.3, "Document converted to markdown")
|
| 325 |
-
|
| 326 |
-
# Split markdown into chunks
|
| 327 |
-
chunks = split_markdown_document(md_content, chunk_size=chunk_size, chunk_overlap=chunk_overlap)
|
| 328 |
-
print(f"Document split into {len(chunks)} chunks")
|
| 329 |
-
|
| 330 |
-
progress(0.4, "Document split into chunks")
|
| 331 |
-
|
| 332 |
-
# Translate chunks
|
| 333 |
-
translated_chunks = translate_chunks(chunks, target_language_name, progress)
|
| 334 |
-
|
| 335 |
-
progress(0.9, "Translation completed")
|
| 336 |
-
|
| 337 |
-
# Reconstruct markdown
|
| 338 |
-
translated_markdown = reconstruct_markdown(translated_chunks)
|
| 339 |
-
|
| 340 |
-
# Save translated markdown to file
|
| 341 |
-
base_name = os.path.splitext(os.path.basename(temp_path))[0]
|
| 342 |
-
translated_file_path = os.path.join(tempfile.gettempdir(), f"{base_name}_translated_{target_language_name}.md")
|
| 343 |
-
|
| 344 |
-
with open(translated_file_path, "w", encoding="utf-8") as f:
|
| 345 |
-
f.write(translated_markdown)
|
| 346 |
-
|
| 347 |
-
progress(1.0, "Translation saved")
|
| 348 |
-
|
| 349 |
-
# Clean up if we created a temp file
|
| 350 |
-
if temp_path != file_obj and os.path.exists(temp_path):
|
| 351 |
-
os.unlink(temp_path)
|
| 352 |
-
|
| 353 |
-
return translated_file_path, "Translation completed successfully!"
|
| 354 |
-
|
| 355 |
-
except Exception as e:
|
| 356 |
-
import traceback
|
| 357 |
-
error_message = f"Error processing document: {str(e)}\n{traceback.format_exc()}"
|
| 358 |
-
print(error_message)
|
| 359 |
-
return None, error_message
|
| 360 |
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
file_input = gr.File(label="Upload Document (PDF, DOCX, PPTX, HTML)")
|
| 378 |
-
|
| 379 |
-
target_language = gr.Dropdown(
|
| 380 |
-
choices=LANGUAGE_NAMES,
|
| 381 |
-
value="French",
|
| 382 |
-
label="Target Language"
|
| 383 |
-
)
|
| 384 |
-
|
| 385 |
-
with gr.Row():
|
| 386 |
-
chunk_size = gr.Slider(
|
| 387 |
-
minimum=500,
|
| 388 |
-
maximum=4000,
|
| 389 |
-
value=2000,
|
| 390 |
-
step=100,
|
| 391 |
-
label="Chunk Size (characters)"
|
| 392 |
-
)
|
| 393 |
-
|
| 394 |
-
chunk_overlap = gr.Slider(
|
| 395 |
-
minimum=50,
|
| 396 |
-
maximum=500,
|
| 397 |
-
value=200,
|
| 398 |
-
step=50,
|
| 399 |
-
label="Chunk Overlap (characters)"
|
| 400 |
-
)
|
| 401 |
-
|
| 402 |
-
translate_btn = gr.Button("Translate Document", variant="primary")
|
| 403 |
-
|
| 404 |
-
with gr.Column():
|
| 405 |
-
output_message = gr.Textbox(label="Status")
|
| 406 |
-
output_file = gr.File(label="Translated Document")
|
| 407 |
-
|
| 408 |
-
# Connect the components
|
| 409 |
-
translate_btn.click(
|
| 410 |
-
fn=process_document_for_translation,
|
| 411 |
-
inputs=[file_input, target_language, chunk_size, chunk_overlap],
|
| 412 |
-
outputs=[output_file, output_message]
|
| 413 |
-
)
|
| 414 |
-
|
| 415 |
-
return app
|
| 416 |
|
| 417 |
-
#
|
| 418 |
-
|
| 419 |
-
app = create_app()
|
| 420 |
-
app.launch()
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
This module provides an interface for translation using the MADLAD-400 models.
|
| 3 |
+
The interface allows users to enter English text, select the target language, and choose a model.
|
| 4 |
+
The user will receive the translated text.
|
| 5 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
import gradio as gr
|
| 8 |
import spaces
|
| 9 |
+
import torch
|
| 10 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 11 |
from LangMap.langid_mapping import langid_to_language
|
| 12 |
|
|
|
|
| 13 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
| 14 |
|
| 15 |
+
# Initialize the tokenizer
|
| 16 |
+
TOKENIZER_3B_MT = AutoTokenizer.from_pretrained("google/madlad400-3b-mt", use_fast=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Retrieve the language codes
|
| 19 |
+
LANGUAGE_CODES = [token for token in TOKENIZER_3B_MT.get_vocab().keys() if token in langid_to_language.keys()]
|
| 20 |
+
|
| 21 |
+
# Mapping language codes to human readable language names
|
| 22 |
LANGUAGE_MAP = {k: v for k, v in langid_to_language.items() if k in LANGUAGE_CODES}
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Invert the language mapping for reverse lookup (from language name to language code)
|
| 25 |
+
NAME_TO_CODE_MAP = {name: code for code, name in LANGUAGE_MAP.items()}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# Extract the language names for the dropdown in the Gradio interface
|
| 28 |
+
LANGUAGE_NAMES = list(LANGUAGE_MAP.values())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Model choices
|
| 31 |
+
MODEL_CHOICES = [
|
| 32 |
+
"google/madlad400-3b-mt",
|
| 33 |
+
"google/madlad400-7b-mt",
|
| 34 |
+
"google/madlad400-10b-mt",
|
| 35 |
+
"google/madlad400-7b-mt-bt"
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
MODEL_RESOURCES = {}
|
| 39 |
+
|
| 40 |
+
def load_tokenizer_model(model_name: str):
|
| 41 |
+
"""
|
| 42 |
+
Load tokenizer and model for a chosen model name.
|
| 43 |
+
|
| 44 |
+
Args:
|
| 45 |
+
model_name (str): The name of the model to load.
|
| 46 |
+
|
| 47 |
+
Returns:
|
| 48 |
+
tuple: The tokenizer and model for the specified model.
|
| 49 |
+
"""
|
| 50 |
+
if model_name not in MODEL_RESOURCES:
|
| 51 |
+
# Load tokenizer and model for the first time
|
| 52 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
|
| 53 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name, torch_dtype=torch.float16)
|
| 54 |
+
model.to(DEVICE)
|
| 55 |
+
MODEL_RESOURCES[model_name] = (tokenizer, model)
|
| 56 |
+
return MODEL_RESOURCES[model_name]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
@spaces.GPU
|
| 59 |
+
def translate(text: str, target_language_name: str, model_name: str) -> str:
|
| 60 |
+
"""
|
| 61 |
+
Translate the input text from English to another language.
|
| 62 |
+
|
| 63 |
+
Args:
|
| 64 |
+
text (str): The input text to be translated.
|
| 65 |
+
target_language_name (str): The human readable target language name.
|
| 66 |
+
model_name (str): The model name for translation.
|
| 67 |
+
|
| 68 |
+
Returns:
|
| 69 |
+
str: The translated text.
|
| 70 |
+
"""
|
| 71 |
+
# Convert the selected language name back to its corresponding language code
|
| 72 |
+
target_language_code = NAME_TO_CODE_MAP.get(target_language_name)
|
| 73 |
+
|
| 74 |
+
if target_language_code is None:
|
| 75 |
+
raise ValueError(f"Unsupported language: {target_language_name}")
|
| 76 |
|
| 77 |
+
# Load tokenizer and model if not already loaded
|
| 78 |
+
tokenizer, model = load_tokenizer_model(model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
text = target_language_code + text
|
| 81 |
+
input_ids = tokenizer(text, return_tensors="pt").input_ids.to(DEVICE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
outputs = model.generate(input_ids=input_ids, max_new_tokens=128000)
|
| 84 |
+
text_translated = tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
| 85 |
|
| 86 |
+
return text_translated[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
TITLE = "MADLAD-400 Translation"
|
| 89 |
+
DESCRIPTION = """
|
| 90 |
+
Translation from English to (almost) 400 languages based on [research](https://arxiv.org/pdf/2309.04662)
|
| 91 |
+
by Google DeepMind and Google Research.
|
| 92 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
# Gradio components
|
| 95 |
+
input_text = gr.Textbox(
|
| 96 |
+
label="Text",
|
| 97 |
+
placeholder="Enter text here"
|
| 98 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
target_language = gr.Dropdown(
|
| 101 |
+
choices=LANGUAGE_NAMES, # Use language names instead of codes
|
| 102 |
+
value="Hawaiian", # Default human readable language name
|
| 103 |
+
label="Target language"
|
| 104 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
model_choice = gr.Dropdown(
|
| 107 |
+
choices=MODEL_CHOICES,
|
| 108 |
+
value="google/madlad400-3b-mt",
|
| 109 |
+
label="Model"
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
output_text = gr.Textbox(label="Translation")
|
| 113 |
+
|
| 114 |
+
# Define the Gradio interface
|
| 115 |
+
demo = gr.Interface(
|
| 116 |
+
fn=translate,
|
| 117 |
+
inputs=[input_text, target_language, model_choice],
|
| 118 |
+
outputs=output_text,
|
| 119 |
+
title=TITLE,
|
| 120 |
+
description=DESCRIPTION
|
| 121 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
+
# Launch the Gradio interface
|
| 124 |
+
demo.launch()
|
|
|
|
|
|