mo-456 commited on
Commit
b754287
·
verified ·
1 Parent(s): 8445ea3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -31
app.py CHANGED
@@ -6,23 +6,23 @@ import re
6
  import os
7
  from typing import List, Tuple
8
 
9
- # إعداد تسجيل الأحداث
10
  logging.basicConfig(level=logging.INFO)
11
  logger = logging.getLogger(__name__)
12
 
13
- # تحميل نموذج اللغة العربية (MARBERT)
14
  try:
15
  model = SentenceTransformer("UBC-NLP/MARBERT", device="cuda" if torch.cuda.is_available() else "cpu")
16
  logger.info("تم تحميل النموذج بنجاح")
17
  except Exception as e:
18
- logger.error(f"خطأ في تحميل النموذج: {e}")
19
  raise
20
 
21
- # تحميل المعرفة من knowledge.txt
22
  def load_knowledge() -> List[str]:
23
  if not os.path.exists("knowledge.txt"):
24
  logger.warning("ملف المعرفة غير موجود.")
25
- return ["عام: لم يتم العثور على محتوى المعرفة."]
26
 
27
  chunks = []
28
  current_section = "عام"
@@ -34,16 +34,16 @@ def load_knowledge() -> List[str]:
34
  elif line:
35
  chunks.append(f"{current_section}: {line}")
36
 
37
- logger.info(f"تم تحميل عدد {len(chunks)} قطعة معرفة.")
38
  return chunks
39
 
40
  knowledge_chunks = load_knowledge()
41
  knowledge_embeddings = model.encode(knowledge_chunks, convert_to_tensor=True)
42
 
43
- # تنظيف السؤال وتعديله
44
  def preprocess_question(question: str) -> str:
45
  question = re.sub(r'[؟\?،,\.]', '', question).strip()
46
- substitutions = {
47
  r'\bماهي\b': 'ما هي',
48
  r'\bماهو\b': 'ما هو',
49
  r'\bكيفية\b': 'كيف',
@@ -52,15 +52,15 @@ def preprocess_question(question: str) -> str:
52
  r'\bعاوز\b': 'كيف يمكن',
53
  r'\bعايز\b': 'كيف يمكن'
54
  }
55
- for pattern, replacement in substitutions.items():
56
- question = re.sub(pattern, replacement, question)
57
  return question
58
 
59
- # توليد الرد على السؤال بناءً على أقرب المعرفة
60
  def generate_response(question: str, top_chunks: List[Tuple[str, float]]) -> str:
61
  if not top_chunks:
62
  suggestions = [
63
- "أعد صياغة سؤالك بشكل أوضح.",
64
  "جرّب استخدام كلمات أخرى.",
65
  "ابحث في قسم مختلف من المعرفة."
66
  ]
@@ -76,15 +76,13 @@ def generate_response(question: str, top_chunks: List[Tuple[str, float]]) -> str
76
  sections[section] = []
77
  sections[section].append((content.strip(), score))
78
 
79
- # القسم الرئيسي
80
- main_section = max(sections.keys(), key=lambda k: sum(s for _, s in sections[k]) / len(sections[k]))
81
 
82
  response = f"سؤالك: {question}\n\n"
83
  response += f"{main_section}:\n"
84
  for content, _ in sorted(sections[main_section], key=lambda x: x[1], reverse=True):
85
  response += f"- {content}\n"
86
 
87
- # معلومات إضافية
88
  other_sections = [s for s in sections if s != main_section]
89
  if other_sections:
90
  response += "\nمعلومات إضافية:\n"
@@ -92,16 +90,16 @@ def generate_response(question: str, top_chunks: List[Tuple[str, float]]) -> str
92
  response += f"\nمن {section}:\n"
93
  for content, _ in sorted(sections[section], key=lambda x: x[1], reverse=True)[:2]:
94
  response += f"- {content}\n"
95
-
96
  return response
97
 
98
- # معالجة السؤال من المستخدم
99
  def answer_question(question: str) -> str:
100
  if not question or len(question.strip()) < 3:
101
  return "يرجى إدخال سؤال واضح لا يقل عن ثلاث كلمات."
102
 
103
  question = preprocess_question(question)
104
- logger.info(f"جاري معالجة السؤال: {question}")
105
 
106
  try:
107
  q_embedding = model.encode(question, convert_to_tensor=True)
@@ -122,25 +120,25 @@ def answer_question(question: str) -> str:
122
  return generate_response(question, top_chunks)
123
 
124
  except Exception as e:
125
- logger.error(f"فشل في توليد الإجابة: {e}")
126
- return "حدث خطأ أثناء توليد الإجابة. حاول مرة أخرى."
127
 
128
  # واجهة Gradio
129
- with gr.Blocks(css=\".arabic-ui {direction: rtl; text-align: right; font-family: Tahoma;}\") as demo:
130
- with gr.Column(elem_classes=\"arabic-ui\"):
131
- gr.Markdown(\"\"\"### المساعد الذكي لوحدة الشفافية بوزارة المالية\nاسأل أي شيء عن الموازنة، المشاركة المجتمعية، أو المشروعات المالية\"\"\")
132
- question = gr.Textbox(label=\"سؤالك\", placeholder=\"ما هي أهداف الموازنة التشاركية؟\", lines=3)
133
- submit_btn = gr.Button(\"إرسال السؤال\", variant=\"primary\")
134
- answer = gr.Textbox(label=\"الإجابة\", lines=12, interactive=False)
135
  gr.Examples(
136
  examples=[
137
- [\"ما هي أهداف الموازنة التشاركية؟\"],
138
- [\"من هي رئيسة وحدة الشفافية؟\"],
139
- [\"ما هو دور المواطن في الموازنة؟\"]
140
  ],
141
  inputs=question
142
  )
143
  submit_btn.click(answer_question, inputs=question, outputs=answer)
144
 
145
- if __name__ == \"__main__\":
146
- demo.launch(server_name=\"0.0.0.0\", server_port=7860, share=False)
 
6
  import os
7
  from typing import List, Tuple
8
 
9
+ # إعداد التسجيل
10
  logging.basicConfig(level=logging.INFO)
11
  logger = logging.getLogger(__name__)
12
 
13
+ # تحميل نموذج MARBERT
14
  try:
15
  model = SentenceTransformer("UBC-NLP/MARBERT", device="cuda" if torch.cuda.is_available() else "cpu")
16
  logger.info("تم تحميل النموذج بنجاح")
17
  except Exception as e:
18
+ logger.error(f"فشل تحميل النموذج: {e}")
19
  raise
20
 
21
+ # تحميل المعرفة
22
  def load_knowledge() -> List[str]:
23
  if not os.path.exists("knowledge.txt"):
24
  logger.warning("ملف المعرفة غير موجود.")
25
+ return ["عام: لا يوجد محتوى معرفي متاح."]
26
 
27
  chunks = []
28
  current_section = "عام"
 
34
  elif line:
35
  chunks.append(f"{current_section}: {line}")
36
 
37
+ logger.info(f"تم تحميل {len(chunks)} قطعة معرفة.")
38
  return chunks
39
 
40
  knowledge_chunks = load_knowledge()
41
  knowledge_embeddings = model.encode(knowledge_chunks, convert_to_tensor=True)
42
 
43
+ # معالجة السؤال
44
  def preprocess_question(question: str) -> str:
45
  question = re.sub(r'[؟\?،,\.]', '', question).strip()
46
+ replacements = {
47
  r'\bماهي\b': 'ما هي',
48
  r'\bماهو\b': 'ما هو',
49
  r'\bكيفية\b': 'كيف',
 
52
  r'\bعاوز\b': 'كيف يمكن',
53
  r'\bعايز\b': 'كيف يمكن'
54
  }
55
+ for pattern, repl in replacements.items():
56
+ question = re.sub(pattern, repl, question)
57
  return question
58
 
59
+ # توليد الرد
60
  def generate_response(question: str, top_chunks: List[Tuple[str, float]]) -> str:
61
  if not top_chunks:
62
  suggestions = [
63
+ "أعد صياغة سؤالك.",
64
  "جرّب استخدام كلمات أخرى.",
65
  "ابحث في قسم مختلف من المعرفة."
66
  ]
 
76
  sections[section] = []
77
  sections[section].append((content.strip(), score))
78
 
79
+ main_section = max(sections, key=lambda s: sum(x[1] for x in sections[s]) / len(sections[s]))
 
80
 
81
  response = f"سؤالك: {question}\n\n"
82
  response += f"{main_section}:\n"
83
  for content, _ in sorted(sections[main_section], key=lambda x: x[1], reverse=True):
84
  response += f"- {content}\n"
85
 
 
86
  other_sections = [s for s in sections if s != main_section]
87
  if other_sections:
88
  response += "\nمعلومات إضافية:\n"
 
90
  response += f"\nمن {section}:\n"
91
  for content, _ in sorted(sections[section], key=lambda x: x[1], reverse=True)[:2]:
92
  response += f"- {content}\n"
93
+
94
  return response
95
 
96
+ # الرد النهائي
97
  def answer_question(question: str) -> str:
98
  if not question or len(question.strip()) < 3:
99
  return "يرجى إدخال سؤال واضح لا يقل عن ثلاث كلمات."
100
 
101
  question = preprocess_question(question)
102
+ logger.info(f"معالجة السؤال: {question}")
103
 
104
  try:
105
  q_embedding = model.encode(question, convert_to_tensor=True)
 
120
  return generate_response(question, top_chunks)
121
 
122
  except Exception as e:
123
+ logger.error(f"خطأ أثناء توليد الإجابة: {e}")
124
+ return "حدث خطأ غير متوقع أثناء توليد الإجابة."
125
 
126
  # واجهة Gradio
127
+ with gr.Blocks(css=".arabic-ui {direction: rtl; text-align: right; font-family: Tahoma;}") as demo:
128
+ with gr.Column(elem_classes="arabic-ui"):
129
+ gr.Markdown("### المساعد الذكي لوحدة الشفافية\nاسأل عن الموازنة، المشاركة المجتمعية، أو الشفافية المالية.")
130
+ question = gr.Textbox(label="سؤالك", placeholder="مثال: ما هي أهداف الموازنة التشاركية؟", lines=3)
131
+ submit_btn = gr.Button("إرسال السؤال", variant="primary")
132
+ answer = gr.Textbox(label="الإجابة", lines=12, interactive=False)
133
  gr.Examples(
134
  examples=[
135
+ ["ما هي أهداف الموازنة التشاركية؟"],
136
+ ["من هي رئيسة وحدة الشفافية؟"],
137
+ ["ما هو دور المواطن في إعداد الموازنة؟"]
138
  ],
139
  inputs=question
140
  )
141
  submit_btn.click(answer_question, inputs=question, outputs=answer)
142
 
143
+ if __name__ == "__main__":
144
+ demo.launch(server_name="0.0.0.0", server_port=7860, share=False)