File size: 2,221 Bytes
fe6c89a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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"<task>تصحيح النص</task>
"
        f"<input>{input_text}</input>
"
        f"<output>{corrected_text}</output>
"
        f"<pos_tags>{pos_tags}</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()