Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,58 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
|
| 8 |
def summarize_text(text, language):
|
| 9 |
if not text.strip():
|
| 10 |
-
return " الرجاء إدخال
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
else:
|
| 19 |
-
|
| 20 |
-
final_summary = summary[0]['summary_text']
|
| 21 |
|
| 22 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
# Gradio
|
| 25 |
with gr.Blocks() as demo:
|
| 26 |
-
gr.
|
| 27 |
-
gr.Markdown("
|
| 28 |
-
|
| 29 |
-
input_text = gr.Textbox(label="Input Text", placeholder="Enter your text here ^^ ...")
|
| 30 |
-
language = gr.Dropdown(choices=["العربية", "English"], value="English", label="Select Language")
|
| 31 |
-
summarize_btn = gr.Button("Summarize")
|
| 32 |
-
output_text = gr.Textbox(label="Summarized Text")
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
demo.launch()
|
|
|
|
| 1 |
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="eslamxm/mt5-base-finetuned-persian-finetuned-persian-arabic")
|
| 8 |
|
| 9 |
def summarize_text(text, language):
|
| 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(text, max_length=100, min_length=10, do_sample=False)[0]['summary_text']
|
| 19 |
+
# إذا كانت اللغة العربية
|
| 20 |
+
elif detected_language == "ar":
|
| 21 |
+
summary = summarizer_ar(text, max_length=100, min_length=10, do_sample=False)[0]['summary_text']
|
| 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!",
|
| 31 |
+
"summarize_btn": " لخص النص" if language == "العربية" else " Summarize Text",
|
| 32 |
+
"text_input_label": "أدخل النص" if language == "العربية" else "Enter text",
|
| 33 |
+
"text_output_label": "النتيجة" if language == "العربية" else "Result"
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
def update_ui(language):
|
| 37 |
+
texts = translate_ui(language)
|
| 38 |
+
return texts["title"], texts["summarize_btn"], texts["text_input_label"], texts["text_output_label"]
|
| 39 |
|
|
|
|
| 40 |
with gr.Blocks() as demo:
|
| 41 |
+
lang_toggle = gr.Radio(["العربية", "English"], label="🌍 اختر لغة الواجهة", value="العربية")
|
| 42 |
+
title = gr.Markdown("## 👋 مرحبًا بك في أداة تلخيص النصوص")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
with gr.Row():
|
| 45 |
+
with gr.Column():
|
| 46 |
+
text_input_label = gr.Markdown("أدخل النص")
|
| 47 |
+
text_input = gr.Textbox(label="")
|
| 48 |
+
summarize_btn = gr.Button("لخص النص")
|
| 49 |
+
|
| 50 |
+
with gr.Column():
|
| 51 |
+
text_output_label = gr.Markdown("النتيجة")
|
| 52 |
+
text_output = gr.Textbox(label="")
|
| 53 |
+
|
| 54 |
+
# تغيير النصوص عند تغيير اللغة
|
| 55 |
+
lang_toggle.change(fn=lambda lang: update_ui(lang), inputs=lang_toggle, outputs=[title, summarize_btn, text_input_label, text_output_label])
|
| 56 |
+
summarize_btn.click(fn=summarize_text, inputs=[text_input, lang_toggle], outputs=text_output)
|
| 57 |
|
| 58 |
+
demo.launch()
|
|
|