Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
4 |
import os
|
5 |
from huggingface_hub import login
|
@@ -7,63 +7,79 @@ from huggingface_hub import login
|
|
7 |
# تسجيل الدخول
|
8 |
login(token=os.environ.get('HUGGING_FACE_HUB_TOKEN'))
|
9 |
|
10 |
-
# تهيئة النموذج
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
14 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
15 |
-
|
|
|
16 |
|
17 |
-
def
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
{
|
22 |
|
23 |
-
قدم
|
24 |
-
1
|
25 |
-
2
|
26 |
-
3
|
27 |
"""
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
45 |
|
46 |
-
# CSS لتحسين المظهر
|
47 |
css = """
|
48 |
.gradio-container {background: #f9fafb !important}
|
49 |
-
.rtl-text {
|
50 |
-
direction: rtl;
|
51 |
-
text-align: right;
|
52 |
-
font-family: Arial, sans-serif;
|
53 |
-
line-height: 1.6;
|
54 |
-
}
|
55 |
"""
|
56 |
|
57 |
interface = gr.Interface(
|
58 |
-
fn=
|
59 |
inputs=[
|
60 |
gr.Textbox(lines=8, label="النص", elem_classes="rtl-text"),
|
|
|
61 |
gr.Slider(50, 250, value=150, label="طول الملخص"),
|
62 |
-
gr.Slider(1, 10, value=7, step=1, label="دقة التلخيص
|
63 |
gr.Slider(0.1, 2.0, value=0.8, step=0.1, label="معامل الطول")
|
64 |
],
|
65 |
outputs=gr.Textbox(label="الملخص", elem_classes="rtl-text"),
|
66 |
-
title="ملخص النصوص الفلسفية",
|
67 |
theme=gr.themes.Soft(),
|
68 |
css=css
|
69 |
)
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
4 |
import os
|
5 |
from huggingface_hub import login
|
|
|
7 |
# تسجيل الدخول
|
8 |
login(token=os.environ.get('HUGGING_FACE_HUB_TOKEN'))
|
9 |
|
10 |
+
# تهيئة النموذج الأول (المتخصص)
|
11 |
+
specialist_model = AutoModelForSeq2SeqLM.from_pretrained("methodya/arabic-summarizer-philosophy")
|
12 |
+
specialist_tokenizer = AutoTokenizer.from_pretrained("methodya/arabic-summarizer-philosophy")
|
13 |
+
|
14 |
+
# تهيئة النموذج الثاني (Gemma)
|
15 |
+
gemma_model = AutoModelForCausalLM.from_pretrained("google/gemma-2b-it")
|
16 |
+
gemma_tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b-it")
|
17 |
+
|
18 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
19 |
+
specialist_model = specialist_model.to(device)
|
20 |
+
gemma_model = gemma_model.to(device)
|
21 |
|
22 |
+
def generate_summary(text, use_pipeline=True, max_length=150, num_beams=7, length_penalty=0.8):
|
23 |
+
if use_pipeline:
|
24 |
+
# المرحلة الأولى: التلخيص بالنموذج المتخصص
|
25 |
+
inputs = specialist_tokenizer(text, return_tensors="pt", max_length=2048, truncation=True).to(device)
|
26 |
+
specialist_outputs = specialist_model.generate(
|
27 |
+
**inputs,
|
28 |
+
max_length=max_length,
|
29 |
+
num_beams=num_beams,
|
30 |
+
length_penalty=length_penalty,
|
31 |
+
early_stopping=True
|
32 |
+
)
|
33 |
+
first_summary = specialist_tokenizer.decode(specialist_outputs[0], skip_special_tokens=True)
|
34 |
+
|
35 |
+
# المرحلة الثانية: التحسين باستخدام Gemma
|
36 |
+
prompt = f"""راجع وحسن هذا الملخص مع الحفاظ على النقاط الرئيسية:
|
37 |
|
38 |
+
الملخص الأولي:
|
39 |
+
{first_summary}
|
40 |
|
41 |
+
قدم التحسين بالشكل التالي:
|
42 |
+
1. الفكرة المحورية
|
43 |
+
2. النقاط الرئيسية
|
44 |
+
3. العلاقات المهمة
|
45 |
"""
|
46 |
+
|
47 |
+
inputs = gemma_tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True).to(device)
|
48 |
+
final_outputs = gemma_model.generate(
|
49 |
+
**inputs,
|
50 |
+
max_length=max_length,
|
51 |
+
temperature=0.3,
|
52 |
+
do_sample=False
|
53 |
+
)
|
54 |
+
return gemma_tokenizer.decode(final_outputs[0], skip_special_tokens=True)
|
55 |
+
else:
|
56 |
+
# استخدام النموذج المتخصص فقط
|
57 |
+
inputs = specialist_tokenizer(text, return_tensors="pt", max_length=2048, truncation=True).to(device)
|
58 |
+
outputs = specialist_model.generate(
|
59 |
+
**inputs,
|
60 |
+
max_length=max_length,
|
61 |
+
num_beams=num_beams,
|
62 |
+
length_penalty=length_penalty,
|
63 |
+
early_stopping=True
|
64 |
+
)
|
65 |
+
return specialist_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
66 |
|
|
|
67 |
css = """
|
68 |
.gradio-container {background: #f9fafb !important}
|
69 |
+
.rtl-text { direction: rtl; text-align: right; }
|
|
|
|
|
|
|
|
|
|
|
70 |
"""
|
71 |
|
72 |
interface = gr.Interface(
|
73 |
+
fn=generate_summary,
|
74 |
inputs=[
|
75 |
gr.Textbox(lines=8, label="النص", elem_classes="rtl-text"),
|
76 |
+
gr.Checkbox(label="استخدام المعالجة المزدوجة", value=True),
|
77 |
gr.Slider(50, 250, value=150, label="طول الملخص"),
|
78 |
+
gr.Slider(1, 10, value=7, step=1, label="دقة التلخيص"),
|
79 |
gr.Slider(0.1, 2.0, value=0.8, step=0.1, label="معامل الطول")
|
80 |
],
|
81 |
outputs=gr.Textbox(label="الملخص", elem_classes="rtl-text"),
|
82 |
+
title="ملخص النصوص الفلسفية (نظام مدمج)",
|
83 |
theme=gr.themes.Soft(),
|
84 |
css=css
|
85 |
)
|