# app.py
import gradio as gr
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
import stanza
# Load the model and tokenizer
model_name = "Arabic-AraT5-POS-Text-Correction"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
# Initialize Stanza for POS tagging
stanza.download("ar")
nlp = stanza.Pipeline(lang="ar", processors="tokenize,pos")
# Define the correction and POS tagging function
def correct_and_tag_text(input_text):
# Generate corrected text using the model
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)
# Perform POS tagging on the corrected text
doc = nlp(corrected_text)
pos_tags = " ".join([f"{word.text}/{word.upos}" for sentence in doc.sentences for word in sentence.words])
# Format output
output = (
f"تصحيح النص
"
f"{input_text}
"
f"
"
f"{pos_tags}"
)
return output
# Define Gradio interface
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()