File size: 1,366 Bytes
e72060d 88c30c6 e72060d fe58d29 3f39c12 e72060d 88c30c6 e72060d 88c30c6 e72060d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import os
import gradio as gr
import ctranslate2
from transformers import AutoTokenizer
LANG_TOKEN = {"assamese": "<2asm>", "hindi": "<2hin>", "marathi": "<2mar>"}
MODEL_DIR = "ct2_mt5_xlit" # directory in Space repo
TOKENIZER_DIR = "tokenizer" # directory in Space repo
tokenizer = AutoTokenizer.from_pretrained(TOKENIZER_DIR)
device = "cuda" if ctranslate2.get_cuda_device_count() > 0 else "cpu"
compute_type = "float16" if device == "cuda" else "int8"
translator = ctranslate2.Translator(MODEL_DIR, device=device, compute_type=compute_type)
def transliterate(text, lang):
lang = lang.lower()
tagged = f"{LANG_TOKEN[lang]} {text.strip().lower()}"
enc = tokenizer([tagged], return_tensors=None, padding=True, truncation=True, max_length=24)
tokens = [tokenizer.convert_ids_to_tokens(enc["input_ids"][0])]
out = translator.translate_batch(tokens, beam_size=3, max_decoding_length=24)[0]
return tokenizer.convert_tokens_to_string(out.hypotheses[0]).strip()
demo = gr.Interface(
fn=transliterate,
inputs=[
gr.Textbox(label="Input (romanized)", placeholder="e.g., Saurav"),
gr.Dropdown(["hindi", "marathi", "assamese"], value="hin", label="Target language"),
],
outputs=gr.Textbox(label="Transliteration"),
title="Multilingual Transliteration",
)
if __name__ == "__main__":
demo.launch() |