| 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 |
|
|
| |
| 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() |