Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,41 +3,45 @@ from huggingface_hub import InferenceClient
|
|
3 |
import re
|
4 |
from typing import Dict, List, Any
|
5 |
|
6 |
-
# نفس الفئات السابقة PhilosophicalTerms و TextProcessor
|
7 |
-
# [يمكنني تقديمها إذا أردت]
|
8 |
-
|
9 |
def validate_language(text: str) -> bool:
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
|
14 |
-
def summarize(text: str) ->
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
# واجهة Gradio
|
31 |
def create_interface():
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
|
41 |
if __name__ == "__main__":
|
42 |
-
|
43 |
-
|
|
|
3 |
import re
|
4 |
from typing import Dict, List, Any
|
5 |
|
|
|
|
|
|
|
6 |
def validate_language(text: str) -> bool:
|
7 |
+
arabic_pattern = re.compile(r'[\u0600-\u06FF]')
|
8 |
+
english_pattern = re.compile('[a-zA-Z]')
|
9 |
+
return len(arabic_pattern.findall(text)) > len(english_pattern.findall(text))
|
10 |
|
11 |
+
def summarize(text: str) -> str:
|
12 |
+
if not text:
|
13 |
+
return "يرجى إدخال نص للتلخيص"
|
14 |
+
|
15 |
+
client = InferenceClient("methodya/arabic-summarizer-philosophy-v3")
|
16 |
+
|
17 |
+
messages = [
|
18 |
+
{"role": "system", "content": "أنت مساعد عربي متخصص في تلخيص النصوص الفلسفية. قم بتلخيص النص التالي باللغة العربية فقط:"},
|
19 |
+
{"role": "user", "content": text}
|
20 |
+
]
|
21 |
+
|
22 |
+
try:
|
23 |
+
response = client.text_generation(
|
24 |
+
text,
|
25 |
+
max_new_tokens=2000,
|
26 |
+
temperature=0.7
|
27 |
+
)
|
28 |
+
if not validate_language(response):
|
29 |
+
return "عذراً، حدث خطأ في اللغة. الرجاء المحاولة مرة أخرى."
|
30 |
+
return response
|
31 |
+
|
32 |
+
except Exception as e:
|
33 |
+
return f"حدث خطأ: {str(e)}"
|
34 |
|
|
|
35 |
def create_interface():
|
36 |
+
with gr.Interface(
|
37 |
+
fn=summarize,
|
38 |
+
inputs=gr.Textbox(label="النص الفلسفي", lines=8, text_align="right"),
|
39 |
+
outputs=gr.Textbox(label="الملخص", lines=6, text_align="right"),
|
40 |
+
title="ملخص النصوص الفلسفية",
|
41 |
+
description="أداة لتلخيص النصوص الفلسفية باللغة العربية"
|
42 |
+
) as interface:
|
43 |
+
return interface
|
44 |
|
45 |
if __name__ == "__main__":
|
46 |
+
iface = create_interface()
|
47 |
+
iface.launch()
|