File size: 2,566 Bytes
98f5c51
 
fdb4555
98f5c51
fdb4555
 
888f1f7
98f5c51
 
 
fdb4555
98f5c51
fdb4555
 
 
 
 
96db741
fdb4555
 
98f5c51
fdb4555
98f5c51
fdb4555
 
 
 
 
96d7c68
fdb4555
 
 
 
 
 
 
 
98f5c51
 
fdb4555
 
98f5c51
fdb4555
 
 
 
96db741
fdb4555
 
 
 
 
 
 
 
98f5c51
fdb4555
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
56
57
import gradio as gr
from transformers import pipeline
import langid  # مكتبة للكشف عن اللغة

# تحميل نماذج التلخيص
summarizer_en = pipeline("summarization", model="facebook/bart-large-cnn")  
summarizer_ar =  pipeline("summarization", model="malmarjeh/t5-arabic-text-summarization")

def summarize_text(text, language):
    if not text.strip():
        return "Please enter text." if language == "English" else "الرجاء إدخال نص."
    
    # الكشف عن لغة النص المدخل
    detected_language, _ = langid.classify(text)
    
    if detected_language == "en":
        summary = summarizer_en(text, max_length=100, min_length=10, do_sample=False)[0]['summary_text']

    elif detected_language == "ar":
        summary = summarizer_ar(text, max_length=100, min_length=10, do_sample=False)[0]['summary_text']
    else:
        return "Unsupported language."
    
    return summary

# gradio
def translate_ui(language):
    return {
        "title": " مرحبًا بك في أداة تلخيص النصوص👋" if language == "العربية" else "👋 Welcome to the Text Summarization Tool!",
        "summarize_btn": " لخص النص" if language == "العربية" else " Summarize Text",
        "text_input_label": "أدخل النص" if language == "العربية" else "Enter text",
        "text_output_label": "النتيجة" if language == "العربية" else "Result"
    }

def update_ui(language):
    texts = translate_ui(language)
    return texts["title"], texts["summarize_btn"], texts["text_input_label"], texts["text_output_label"]

with gr.Blocks() as demo:
    lang_toggle = gr.Radio(["العربية", "English"], label="🌍 اختر لغة الواجهة", value="العربية")
    title = gr.Markdown("## 👋 مرحبًا بك في أداة تلخيص النصوص")
    
    with gr.Row():
        with gr.Column():
            text_input_label = gr.Markdown("أدخل النص") 
            text_input = gr.Textbox(label="") 
            summarize_btn = gr.Button("لخص النص", variant="primary")
        
        with gr.Column():
            text_output_label = gr.Markdown("النتيجة")  
            text_output = gr.Textbox(label="")

    # تغيير النصوص عند تغيير اللغة
    lang_toggle.change(fn=lambda lang: update_ui(lang), inputs=lang_toggle, outputs=[title, summarize_btn, text_input_label, text_output_label])
    summarize_btn.click(fn=summarize_text, inputs=[text_input, lang_toggle], outputs=text_output)

demo.launch()