| import gradio as gr | |
| from transformers import AutoTokenizer, AutoModelForSeq2SeqLM | |
| model_name = "csebuetnlp/mT5_multilingual_XLSum" | |
| tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False) | |
| model = AutoModelForSeq2SeqLM.from_pretrained(model_name) | |
| def summarize_text(text, src_lang): | |
| inputs = tokenizer(text, return_tensors="pt", max_length=512, truncation=True) | |
| summary_ids = model.generate(inputs["input_ids"], max_length=150, min_length=30, length_penalty=2.0, num_beams=4, early_stopping=True) | |
| summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True) | |
| return summary | |
| examples = [ | |
| ["الذكاء الاصطناعي هو فرع من علوم الكمبيوتر يهدف إلى إنشاء آلات ذكية تعمل وتتفاعل مثل البشر. بعض الأنشطة التي صممت أجهزة الكمبيوتر الذكية للقيام بها تشمل: التعرف على الصوت، التعلم، التخطيط، وحل المشاكل.", "ar"], | |
| ["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"] | |
| ] | |
| iface = gr.Interface( | |
| fn=summarize_text, | |
| inputs=[gr.Textbox(lines=10, placeholder="Enter the text here....."), gr.Dropdown(["ar", "en"], label="Language")], | |
| outputs="text", | |
| examples=examples, | |
| title=" AI Study Summary ", | |
| description="Enter a text in Arabic or English and the model will summarize it, you can also choose one of the available examples." | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() |