|
|
|
|
|
import gradio as gr |
|
|
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer |
|
|
import stanza |
|
|
|
|
|
|
|
|
model_name = "Arabic-AraT5-POS-Text-Correction" |
|
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
|
model = AutoModelForSeq2SeqLM.from_pretrained(model_name) |
|
|
|
|
|
|
|
|
stanza.download("ar") |
|
|
nlp = stanza.Pipeline(lang="ar", processors="tokenize,pos") |
|
|
|
|
|
|
|
|
def correct_and_tag_text(input_text): |
|
|
|
|
|
inputs = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True) |
|
|
outputs = model.generate(**inputs, max_length=512, num_beams=5, early_stopping=True) |
|
|
corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
|
|
|
|
|
|
doc = nlp(corrected_text) |
|
|
pos_tags = " ".join([f"{word.text}/{word.upos}" for sentence in doc.sentences for word in sentence.words]) |
|
|
|
|
|
|
|
|
output = ( |
|
|
f"<task>تصحيح النص</task> |
|
|
" |
|
|
f"<input>{input_text}</input> |
|
|
" |
|
|
f"<output>{corrected_text}</output> |
|
|
" |
|
|
f"<pos_tags>{pos_tags}</pos_tags>" |
|
|
) |
|
|
return output |
|
|
|
|
|
|
|
|
examples = [ |
|
|
"الى التعليق رقم ٢ أكيد أن لحكام العرب والمسلمين مسؤوليه يتمثل ادناها في استدعاء السفراء في الصين للتشاور", |
|
|
"هذه الصور رائع جدا و اتمنى لو اخذتها في وقتً سابق", |
|
|
"السلام عليمم ورحمة الله وبركااته"] |
|
|
|
|
|
interface = gr.Interface( |
|
|
fn=correct_and_tag_text, |
|
|
inputs=gr.Textbox(label="أدخل النص:", placeholder="أدخل النص لتصحيحه..."), |
|
|
outputs=gr.Textbox(label="النص المصحح مع الوسوم:"), |
|
|
examples=examples, |
|
|
title="تصحيح النصوص العربية باستخدام AraT5 مع تحليل الوسوم", |
|
|
description="هذا التطبيق يقوم بتصحيح النصوص العربية وتحليلها باستخدام AraT5 مع الوسوم النحوية (POS)." |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
interface.launch() |
|
|
|