Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
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")],
|
26 |
+
outputs="text",
|
27 |
+
examples=examples,
|
28 |
+
title=" AI Study Summary ",
|
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()
|