import os import yaml import gdown import gradio as gr from predict import PredictTri from huggingface_hub import hf_hub_download output_path = "tashkeela-d2.pt" gdrive_templ = "https://drive.google.com/file/d/{}/view?usp=sharing" if not os.path.exists(output_path): model_gdrive_id = "1FGelqImFkESbTyRsx_elkKIOZ9VbhRuo" gdown.download(gdrive_templ.format(model_gdrive_id), output=output_path, quiet=False, fuzzy=True) output_path = "vocab.vec" if not os.path.exists(output_path): vocab_gdrive_id = "1-0muGvcSYEf8RAVRcwXay4MRex6kmCii" gdown.download(gdrive_templ.format(vocab_gdrive_id), output=output_path, quiet=False, fuzzy=True) if not os.path.exists("td2/tashkeela-ashaar-td2.pt"): hf_hub_download(repo_id="munael/Partial-Arabic-Diacritization-TD2", filename="tashkeela-ashaar-td2.pt", local_dir="td2") with open("config.yaml", 'r', encoding="utf-8") as file: config = yaml.load(file, Loader=yaml.FullLoader) config["train"]["max-sent-len"] = config["predictor"]["window"] config["train"]["max-token-count"] = config["predictor"]["window"] * 3 predictor = PredictTri(config) current_model_name = "TD2" config["model-name"] = current_model_name def diacritze_full(text, model_name): global current_model_name, predictor if model_name != current_model_name: config["model-name"] = model_name current_model_name = model_name predictor = PredictTri(config) do_hard_mask = None threshold = None predictor.create_dataloader(text, False, do_hard_mask, threshold, model_name) diacritized_lines = predictor.predict_partial(do_partial=False, lines=text.split('\n')) return diacritized_lines def diacritze_partial(text, mask_mode, threshold, model_name): global current_model_name, predictor if model_name != current_model_name: config["model-name"] = model_name current_model_name = model_name predictor = PredictTri(config) do_partial = True predictor.create_dataloader(text, do_partial, mask_mode=="Hard", threshold, model_name) diacritized_lines = predictor.predict_partial(do_partial=do_partial, lines=text.split('\n')) return diacritized_lines with gr.Blocks(theme=gr.themes.Default(text_size="lg")) as demo: gr.Markdown( """

Partial Diacritization: A Context-Contrastive Inference Approach

Authors: Muhammad ElNokrashy, Badr AlKhamissi

Paper: https://arxiv.org/abs/2401.08919

Abstract

Diacritization plays a pivotal role in improving readability and disambiguating the meaning of Arabic texts. Efforts have so far focused on marking every eligible character (Full Diacritization). Comparatively overlooked, Partial Diacritzation (PD) is the selection of a subset of characters to be marked to aid comprehension where needed.Research has indicated that excessive diacritic marks can hinder skilled readers---reducing reading speed and accuracy. We conduct a behavioral experiment and show that partially marked text is often easier to read than fully marked text, and sometimes easier than plain text. In this light, we introduce Context-Contrastive Partial Diacritization (CCPD)---a novel approach to PD which integrates seamlessly with existing Arabic diacritization systems. CCPD processes each word twice, once with context and once without, and diacritizes only the characters with disparities between the two inferences. Further, we introduce novel indicators for measuring partial diacritization quality {SR, PDER, HDER, ERE}, essential for establishing this as a machine learning task. Lastly, we introduce TD2, a Transformer-variant of an established model which offers a markedly different performance profile on our proposed indicators compared to all other known systems.

""") model_choice = gr.Dropdown( choices=["D2", "TD2"], label="Diacritization Model", value=current_model_name ) with gr.Tab(label="Partial Diacritization") as partial_settings: with gr.Row(): masking_mode = gr.Radio(choices=["Hard", "Soft"], value="Hard", label="Masking Mode") threshold_slider = gr.Slider(label="Soft Masking Threshold", minimum=0, maximum=1, value=0.1) partial_input_txt = gr.Textbox( placeholder="اكتب هنا", lines=5, label="Input", type='text', rtl=True, text_align='right', ) partial_output_txt = gr.Textbox( lines=5, label="Output", type='text', rtl=True, text_align='right', show_copy_button=True, ) partial_btn = gr.Button(value="Shakkel") partial_btn.click(diacritze_partial, inputs=[partial_input_txt, masking_mode, threshold_slider, model_choice], outputs=[partial_output_txt], queue=True) gr.Examples( examples=[ ["ولو حمل من مجلس الخيار ، ولم يمنع من الكلام", "Hard", 0, "TD2"], ["ولو حمل من مجلس الخيار ، ولم يمنع من الكلام", "Soft", 0.1, "TD2"], ["ولو حمل من مجلس الخيار ، ولم يمنع من الكلام", "Soft", 0.01, "TD2"], ], inputs=[partial_input_txt, masking_mode, threshold_slider, model_choice], outputs=partial_output_txt, fn=diacritze_partial, cache_examples=True, ) with gr.Tab(label="Full Diacritization"): full_input_txt = gr.Textbox( placeholder="اكتب هنا", lines=5, label="Input", type='text', rtl=True, text_align='right', ) full_output_txt = gr.Textbox( lines=5, label="Output", type='text', rtl=True, text_align='right', show_copy_button=True, ) full_btn = gr.Button(value="Shakkel") full_btn.click(diacritze_full, inputs=[full_input_txt, model_choice], outputs=[full_output_txt], queue=True) gr.Examples( examples=[ ["ولو حمل من مجلس الخيار ، ولم يمنع من الكلام", "TD2"], ], inputs=[full_input_txt, model_choice], outputs=full_output_txt, fn=diacritze_full, cache_examples=True, ) if __name__ == "__main__": demo.queue().launch( # share=False, # debug=False, # server_port=7860, # server_name="0.0.0.0", # ssl_verify=False, # ssl_certfile="cert.pem", # ssl_keyfile="key.pem" )