Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,22 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
|
| 4 |
-
# تحميل موديل التلخيص
|
| 5 |
model_name = "csebuetnlp/mT5_multilingual_XLSum"
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)
|
| 7 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 8 |
|
| 9 |
-
|
| 10 |
def summarize_text(text, src_lang):
|
| 11 |
inputs = tokenizer(text, return_tensors="pt", max_length=512, truncation=True)
|
| 12 |
summary_ids = model.generate(inputs["input_ids"], max_length=150, min_length=30, length_penalty=2.0, num_beams=4, early_stopping=True)
|
| 13 |
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 14 |
return summary
|
| 15 |
|
| 16 |
-
# أمثلة للنصوص
|
| 17 |
examples = [
|
| 18 |
["الذكاء الاصطناعي هو فرع من علوم الكمبيوتر يهدف إلى إنشاء آلات ذكية تعمل وتتفاعل مثل البشر. بعض الأنشطة التي صممت أجهزة الكمبيوتر الذكية للقيام بها تشمل: التعرف على الصوت، التعلم، التخطيط، وحل المشاكل.", "ar"],
|
| 19 |
["Artificial intelligence is a branch of computer science that aims to create intelligent machines that work and react like humans. Some of the activities computers with artificial intelligence are designed for include: Speech recognition, learning, planning, and problem-solving.", "en"]
|
| 20 |
]
|
| 21 |
|
| 22 |
-
# واجهة Gradio
|
| 23 |
iface = gr.Interface(
|
| 24 |
fn=summarize_text,
|
| 25 |
inputs=[gr.Textbox(lines=10, placeholder="Enter the text here....."), gr.Dropdown(["ar", "en"], label="Language")],
|
|
@@ -29,6 +26,5 @@ iface = gr.Interface(
|
|
| 29 |
description="Enter a text in Arabic or English and the model will summarize it, you can also choose one of the available examples."
|
| 30 |
)
|
| 31 |
|
| 32 |
-
# تشغيل التطبيق
|
| 33 |
if __name__ == "__main__":
|
| 34 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
|
|
|
|
| 4 |
model_name = "csebuetnlp/mT5_multilingual_XLSum"
|
| 5 |
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)
|
| 6 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 7 |
|
| 8 |
+
|
| 9 |
def summarize_text(text, src_lang):
|
| 10 |
inputs = tokenizer(text, return_tensors="pt", max_length=512, truncation=True)
|
| 11 |
summary_ids = model.generate(inputs["input_ids"], max_length=150, min_length=30, length_penalty=2.0, num_beams=4, early_stopping=True)
|
| 12 |
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 13 |
return summary
|
| 14 |
|
|
|
|
| 15 |
examples = [
|
| 16 |
["الذكاء الاصطناعي هو فرع من علوم الكمبيوتر يهدف إلى إنشاء آلات ذكية تعمل وتتفاعل مثل البشر. بعض الأنشطة التي صممت أجهزة الكمبيوتر الذكية للقيام بها تشمل: التعرف على الصوت، التعلم، التخطيط، وحل المشاكل.", "ar"],
|
| 17 |
["Artificial intelligence is a branch of computer science that aims to create intelligent machines that work and react like humans. Some of the activities computers with artificial intelligence are designed for include: Speech recognition, learning, planning, and problem-solving.", "en"]
|
| 18 |
]
|
| 19 |
|
|
|
|
| 20 |
iface = gr.Interface(
|
| 21 |
fn=summarize_text,
|
| 22 |
inputs=[gr.Textbox(lines=10, placeholder="Enter the text here....."), gr.Dropdown(["ar", "en"], label="Language")],
|
|
|
|
| 26 |
description="Enter a text in Arabic or English and the model will summarize it, you can also choose one of the available examples."
|
| 27 |
)
|
| 28 |
|
|
|
|
| 29 |
if __name__ == "__main__":
|
| 30 |
iface.launch()
|