methodya commited on
Commit
09cbb4f
·
verified ·
1 Parent(s): daafd2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -32
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
- arabic_pattern = re.compile(r'[\u0600-\u06FF]')
11
- english_pattern = re.compile('[a-zA-Z]')
12
- return len(arabic_pattern.findall(text)) > len(english_pattern.findall(text))
13
 
14
- def summarize(text: str) -> Dict[str, str]:
15
- client = InferenceClient("methodya/arabic-summarizer-philosophy-v3")
16
-
17
- messages = [
18
- {"role": "system", "content": "أنت مساعد عربي متخصص في تلخيص النصوص الفلسفية..."},
19
- {"role": "user", "content": text}
20
- ]
21
-
22
- response = client.text_generation(
23
- text,
24
- max_new_tokens=2000,
25
- temperature=0.7
26
- )
27
-
28
- return {"summary": response, "status": "success"}
 
 
 
 
 
 
 
 
29
 
30
- # واجهة Gradio
31
  def create_interface():
32
- with gr.Interface(
33
- fn=summarize,
34
- inputs=gr.Textbox(label="النص الفلسفي", lines=8, dir="rtl"),
35
- outputs=gr.Textbox(label="الملخص", lines=6, dir="rtl"),
36
- title="ملخص النصوص الفلسفية",
37
- description="أداة لتلخيص النصوص الفلسفية باللغة العربية"
38
- ) as interface:
39
- return interface
40
 
41
  if __name__ == "__main__":
42
- iface = create_interface()
43
- iface.launch()
 
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()