File size: 1,748 Bytes
c43abb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import gradio as gr
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
import torch

# تحميل النموذج والتوكينايزر
model_name = "methodya/arabic-summarizer-philosophy-v2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)

# التأكد من استخدام GPU إذا كان متوفراً
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device)
model.eval()

def summarize(text):
    # التأكد من أن النص ليس فارغاً
    if not text.strip():
        return "الرجاء إدخال نص للتلخيص"
    
    # تحضير المدخلات
    inputs = tokenizer(text, return_tensors="pt", max_length=2048, padding=True, truncation=True)
    inputs = {k: v.to(device) for k, v in inputs.items()}
    
    # توليد الملخص
    with torch.no_grad():
        outputs = model.generate(
            **inputs,
            max_length=512,
            min_length=30,
            num_beams=4,
            length_penalty=2.0,
            no_repeat_ngram_size=3
        )
    
    # فك ترميز الملخص
    summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return summary

# تعريف واجهة gradio
iface = gr.Interface(
    fn=summarize,
    inputs=gr.Textbox(label="النص", placeholder="أدخل النص العربي هنا..."),
    outputs=gr.Textbox(label="الملخص"),
    title="نظام تلخيص النصوص الفلسفية العربية",
    description="نموذج لتلخيص النصوص الفلسفية باللغة العربية",
    submit_btn="تلخيص",
    clear_btn="مسح"
)

# تشغيل التطبيق
iface.launch()