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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -40
app.py CHANGED
@@ -1,47 +1,41 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
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()
 
1
  import gradio as gr
2
+ from llama_cpp import Llama
3
+ import os
 
4
 
5
+ model_path = "sambalingo-arabic-chat.Q5_K_M.gguf"
6
+
7
+ llm = Llama(
8
+ model_path=model_path,
9
+ n_ctx=2048,
10
+ n_threads=4
11
+ )
12
 
13
  def summarize(text: str) -> str:
14
+ if not text:
15
+ return "يرجى إدخال نص للتلخيص"
16
+
17
+ prompt = f"""أنت مساعد عربي متخصص في تلخيص النصوص الفلسفية. قم بتلخيص النص التالي:
18
+ {text}
19
+ """
20
+
21
+ try:
22
+ output = llm(
23
+ prompt,
24
+ max_tokens=2000,
25
+ temperature=0.7,
26
+ stop=["User:", "\n\n"]
27
+ )
28
+ return output['choices'][0]['text']
29
+ except Exception as e:
30
+ return f"حدث خطأ: {str(e)}"
 
 
 
 
 
31
 
32
+ interface = gr.Interface(
33
+ fn=summarize,
34
+ inputs=gr.Textbox(label="النص الفلسفي", lines=8, text_align="right"),
35
+ outputs=gr.Textbox(label="الملخص", lines=6, text_align="right"),
36
+ title="ملخص النصوص الفلسفية",
37
+ description="أداة لتلخيص النصوص الفلسفية باللغة العربية"
38
+ )
 
 
39
 
40
  if __name__ == "__main__":
41
+ interface.launch()