Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import langid # مكتبة للكشف عن اللغة
|
| 4 |
|
| 5 |
-
|
| 6 |
summarizer_en = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 7 |
summarizer_ar = pipeline("summarization", model="malmarjeh/t5-arabic-text-summarization")
|
| 8 |
|
|
@@ -10,21 +10,29 @@ def summarize_text(text, language, min_length, max_length):
|
|
| 10 |
if not text.strip():
|
| 11 |
return "Please enter text." if language == "English" else "الرجاء إدخال نص."
|
| 12 |
|
| 13 |
-
# الكشف عن لغة النص المدخل
|
| 14 |
detected_language, _ = langid.classify(text)
|
| 15 |
|
| 16 |
-
# إذا كانت اللغة الإنجليزية
|
| 17 |
if detected_language == "en":
|
| 18 |
-
summary = summarizer_en(
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
elif detected_language == "ar":
|
| 21 |
-
summary = summarizer_ar(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
else:
|
| 23 |
return "Unsupported language."
|
| 24 |
|
| 25 |
return summary
|
| 26 |
|
| 27 |
-
# gradio
|
| 28 |
def translate_ui(language):
|
| 29 |
return {
|
| 30 |
"title": "👋 مرحبًا بك في أداة تلخيص النصوص!" if language == "العربية" else "👋 Welcome to the Text Summarization Tool!",
|
|
@@ -37,26 +45,97 @@ def translate_ui(language):
|
|
| 37 |
|
| 38 |
def update_ui(language):
|
| 39 |
texts = translate_ui(language)
|
| 40 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
with gr.Blocks() as demo:
|
| 43 |
-
lang_toggle = gr.Radio(
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
with gr.Row():
|
|
|
|
| 47 |
with gr.Column():
|
| 48 |
-
text_input_label = gr.Markdown(
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
with gr.Column():
|
| 55 |
-
text_output_label = gr.Markdown(
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
|
| 62 |
demo.launch()
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import langid # مكتبة للكشف عن اللغة
|
| 4 |
|
| 5 |
+
|
| 6 |
summarizer_en = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 7 |
summarizer_ar = pipeline("summarization", model="malmarjeh/t5-arabic-text-summarization")
|
| 8 |
|
|
|
|
| 10 |
if not text.strip():
|
| 11 |
return "Please enter text." if language == "English" else "الرجاء إدخال نص."
|
| 12 |
|
|
|
|
| 13 |
detected_language, _ = langid.classify(text)
|
| 14 |
|
|
|
|
| 15 |
if detected_language == "en":
|
| 16 |
+
summary = summarizer_en(
|
| 17 |
+
text,
|
| 18 |
+
max_length=int(max_length),
|
| 19 |
+
min_length=int(min_length),
|
| 20 |
+
do_sample=False,
|
| 21 |
+
no_repeat_ngram_size=2
|
| 22 |
+
)[0]['summary_text']
|
| 23 |
elif detected_language == "ar":
|
| 24 |
+
summary = summarizer_ar(
|
| 25 |
+
text,
|
| 26 |
+
max_length=int(max_length),
|
| 27 |
+
min_length=int(min_length),
|
| 28 |
+
do_sample=False,
|
| 29 |
+
no_repeat_ngram_size=2
|
| 30 |
+
)[0]['summary_text']
|
| 31 |
else:
|
| 32 |
return "Unsupported language."
|
| 33 |
|
| 34 |
return summary
|
| 35 |
|
|
|
|
| 36 |
def translate_ui(language):
|
| 37 |
return {
|
| 38 |
"title": "👋 مرحبًا بك في أداة تلخيص النصوص!" if language == "العربية" else "👋 Welcome to the Text Summarization Tool!",
|
|
|
|
| 45 |
|
| 46 |
def update_ui(language):
|
| 47 |
texts = translate_ui(language)
|
| 48 |
+
return (
|
| 49 |
+
texts["title"],
|
| 50 |
+
texts["summarize_btn"],
|
| 51 |
+
texts["text_input_label"],
|
| 52 |
+
texts["text_output_label"],
|
| 53 |
+
texts["min_length_label"],
|
| 54 |
+
texts["max_length_label"]
|
| 55 |
+
)
|
| 56 |
|
| 57 |
with gr.Blocks() as demo:
|
| 58 |
+
lang_toggle = gr.Radio(
|
| 59 |
+
["العربية", "English"],
|
| 60 |
+
label="🌍 اختر لغة الواجهة",
|
| 61 |
+
value="العربية",
|
| 62 |
+
elem_id="language-toggle"
|
| 63 |
+
)
|
| 64 |
+
title = gr.Markdown(
|
| 65 |
+
"## 👋 مرحبًا بك في أداة تلخيص النصوص",
|
| 66 |
+
elem_id="title"
|
| 67 |
+
)
|
| 68 |
|
| 69 |
with gr.Row():
|
| 70 |
+
|
| 71 |
with gr.Column():
|
| 72 |
+
text_input_label = gr.Markdown(
|
| 73 |
+
"أدخل النص",
|
| 74 |
+
elem_id="text-input-label"
|
| 75 |
+
)
|
| 76 |
+
text_input = gr.Textbox(
|
| 77 |
+
label="",
|
| 78 |
+
elem_id="text-input"
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
min_length_label = gr.Markdown(
|
| 82 |
+
"الحد الأدنى للطول",
|
| 83 |
+
elem_id="min-length-label"
|
| 84 |
+
)
|
| 85 |
+
min_length = gr.Slider(
|
| 86 |
+
minimum=10,
|
| 87 |
+
maximum=50,
|
| 88 |
+
value=10,
|
| 89 |
+
step=1,
|
| 90 |
+
label="",
|
| 91 |
+
elem_id="min-length-slider"
|
| 92 |
+
)
|
| 93 |
|
| 94 |
+
max_length_label = gr.Markdown(
|
| 95 |
+
"الحد الأقصى للطول",
|
| 96 |
+
elem_id="max-length-label"
|
| 97 |
+
)
|
| 98 |
+
max_length = gr.Slider(
|
| 99 |
+
minimum=50,
|
| 100 |
+
maximum=150,
|
| 101 |
+
value=100,
|
| 102 |
+
step=1,
|
| 103 |
+
label="",
|
| 104 |
+
elem_id="max-length-slider"
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
summarize_btn = gr.Button(
|
| 108 |
+
"لخص النص",
|
| 109 |
+
elem_id="summarize-btn", variant="primary"
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
with gr.Column():
|
| 113 |
+
text_output_label = gr.Markdown(
|
| 114 |
+
"النتيجة",
|
| 115 |
+
elem_id="text-output-label"
|
| 116 |
+
)
|
| 117 |
+
text_output = gr.Textbox(
|
| 118 |
+
label="",
|
| 119 |
+
elem_id="text-output"
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
lang_toggle.change(
|
| 123 |
+
fn=lambda lang: update_ui(lang),
|
| 124 |
+
inputs=lang_toggle,
|
| 125 |
+
outputs=[
|
| 126 |
+
title,
|
| 127 |
+
summarize_btn,
|
| 128 |
+
text_input_label,
|
| 129 |
+
text_output_label,
|
| 130 |
+
min_length_label,
|
| 131 |
+
max_length_label
|
| 132 |
+
]
|
| 133 |
+
)
|
| 134 |
|
| 135 |
+
summarize_btn.click(
|
| 136 |
+
fn=summarize_text,
|
| 137 |
+
inputs=[text_input, lang_toggle, min_length, max_length],
|
| 138 |
+
outputs=text_output
|
| 139 |
+
)
|
| 140 |
|
| 141 |
demo.launch()
|