anaspro commited on
Commit
df14f5f
·
1 Parent(s): dbcf08f

🔧 إصلاحات مهمة للـ deployment

Browse files

✅ إصلاحات:
- إصلاح خطأ Gradio stop_btn (من True إلى string)
- تحسين قراءة ملف Markdown مع معالجة أفضل للأخطاء
- إضافة بيانات احتياطية في حالة عدم وجود الملف
- تحديث إصدار Gradio إلى 4.44.1
- تحسين رسائل التشخيص والـ debugging

📁 الملفات المحدثة:
- app.py: إصلاح stop_btn وتحسين RAG
- simple_rag.py: تحسين قراءة Markdown
- requirements.txt: تحديث Gradio

Files changed (3) hide show
  1. app.py +26 -5
  2. requirements.txt +1 -1
  3. simple_rag.py +39 -30
app.py CHANGED
@@ -45,13 +45,34 @@ try:
45
  # تحميل ملف شركة NBTEL
46
  nbtel_file = "./data/nbtel_company_profile.md"
47
  if os.path.exists(nbtel_file):
 
48
  documents = rag_system.load_markdown_file(nbtel_file)
49
- rag_system.add_documents(documents)
50
- rag_system.build_index()
51
- rag_system.save_index()
52
- print("✅ تم إنشاء فهرس RAG بنجاح")
 
 
 
53
  else:
54
  print(f"⚠️ لم يتم العثور على ملف البيانات: {nbtel_file}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
  print(f"✅ نظام RAG جاهز - {len(rag_system.documents)} مستند")
57
  RAG_ENABLED = True
@@ -399,7 +420,7 @@ demo = gr.ChatInterface(
399
  **للدعم المباشر**: 📞 6337 | 📱 واتساب: 0773 633 7777
400
  """,
401
  examples=examples,
402
- stop_btn=True,
403
  css="""
404
  .gradio-container, .chatbot, .chatbot * {
405
  direction: rtl !important;
 
45
  # تحميل ملف شركة NBTEL
46
  nbtel_file = "./data/nbtel_company_profile.md"
47
  if os.path.exists(nbtel_file):
48
+ print(f"📁 وجد ملف البيانات: {nbtel_file}")
49
  documents = rag_system.load_markdown_file(nbtel_file)
50
+ if documents:
51
+ rag_system.add_documents(documents)
52
+ rag_system.build_index()
53
+ rag_system.save_index()
54
+ print("✅ تم إنشاء فهرس RAG بنجاح")
55
+ else:
56
+ print("⚠️ لم يتم استخراج أي مستندات من الملف")
57
  else:
58
  print(f"⚠️ لم يتم العثور على ملف البيانات: {nbtel_file}")
59
+ # إنشاء بيانات تجريبية بسيطة
60
+ sample_docs = [
61
+ {
62
+ 'title': 'معلومات أساسية عن NBTEL',
63
+ 'content': 'شركة NBTEL عراقية متخصصة في خدمات الإنترنت والاتصالات. نقدم خدمات WiFi و FTTX في محافظات نينوى وكركوك وصلاح الدين.',
64
+ 'source': 'fallback'
65
+ },
66
+ {
67
+ 'title': 'معلومات التواصل',
68
+ 'content': 'للدعم الفني: 6337، واتساب: 0773 633 7777، إيميل: Info@nbtel.iq',
69
+ 'source': 'fallback'
70
+ }
71
+ ]
72
+ rag_system.add_documents(sample_docs)
73
+ rag_system.build_index()
74
+ rag_system.save_index()
75
+ print("✅ تم إنشاء فهرس تجريبي")
76
 
77
  print(f"✅ نظام RAG جاهز - {len(rag_system.documents)} مستند")
78
  RAG_ENABLED = True
 
420
  **للدعم المباشر**: 📞 6337 | 📱 واتساب: 0773 633 7777
421
  """,
422
  examples=examples,
423
+ stop_btn="إيقاف",
424
  css="""
425
  .gradio-container, .chatbot, .chatbot * {
426
  direction: rtl !important;
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- gradio>=4.0.0
2
  spaces[huggingface]>=0.28.0
3
  transformers>=4.35.0
4
  torch>=2.1.0
 
1
+ gradio>=4.44.1
2
  spaces[huggingface]>=0.28.0
3
  transformers>=4.35.0
4
  torch>=2.1.0
simple_rag.py CHANGED
@@ -49,56 +49,65 @@ class SimpleRAG:
49
  with open(file_path, 'r', encoding='utf-8') as f:
50
  content = f.read()
51
 
52
- # تقسيم حسب العناوين
53
- sections = re.split(r'\n(#{1,6})\s+', content)
54
 
55
- current_section = ""
56
- current_title = ""
57
 
58
- for i, part in enumerate(sections):
59
- if part.startswith('#'):
60
- # هذا عنوان
61
- if current_section and current_title:
 
 
 
 
 
 
 
 
62
  documents.append({
63
  'title': current_title,
64
- 'content': current_section.strip(),
65
  'source': 'nbtel_profile',
66
  'section_type': 'main'
67
  })
68
- current_title = ""
69
- current_section = ""
70
- elif part.strip().startswith('#'):
71
- # عنوان فرعي
72
- current_title = part.strip()
73
  else:
74
- # محتوى
75
- if i + 1 < len(sections) and not sections[i + 1].startswith('#'):
76
- current_title = part.strip().split('\n')[0] if part.strip() else current_title
77
- current_section = part
78
 
79
  # إضافة القسم الأخير
80
- if current_section and current_title:
81
  documents.append({
82
  'title': current_title,
83
- 'content': current_section.strip(),
84
  'source': 'nbtel_profile',
85
  'section_type': 'main'
86
  })
87
 
88
- # تقسيم إضافي للأقسام الطويلة
89
  final_docs = []
90
  for doc in documents:
91
- if len(doc['content']) > 1000:
92
- chunks = self._split_long_text(doc['content'])
93
  for i, chunk in enumerate(chunks):
94
- final_docs.append({
95
- 'title': f"{doc['title']} - جزء {i+1}",
96
- 'content': chunk,
97
- 'source': doc['source'],
98
- 'section_type': 'chunk'
99
- })
 
100
  else:
101
- final_docs.append(doc)
 
 
 
 
102
 
103
  print(f"✅ تم تحميل {len(final_docs)} قسم من الملف")
104
  return final_docs
 
49
  with open(file_path, 'r', encoding='utf-8') as f:
50
  content = f.read()
51
 
52
+ print(f"📄 قراءة الملف، الحجم: {len(content)} حرف")
 
53
 
54
+ # تقسيم بسيط حسب العناوين
55
+ sections = re.split(r'\n(#{1,3}\s+.*?)\n', content)
56
 
57
+ current_title = "مقدمة"
58
+ current_content = ""
59
+
60
+ for i, section in enumerate(sections):
61
+ section = section.strip()
62
+ if not section:
63
+ continue
64
+
65
+ # إذا كان عنوان (يبدأ بـ #)
66
+ if section.startswith('#'):
67
+ # حفظ القسم السابق
68
+ if current_content.strip():
69
  documents.append({
70
  'title': current_title,
71
+ 'content': current_content.strip(),
72
  'source': 'nbtel_profile',
73
  'section_type': 'main'
74
  })
75
+
76
+ # بدء قسم جديد
77
+ current_title = section.replace('#', '').strip()
78
+ current_content = ""
 
79
  else:
80
+ # إضافة المحتوى للقسم الحالي
81
+ current_content += section + "\n"
 
 
82
 
83
  # إضافة القسم الأخير
84
+ if current_content.strip():
85
  documents.append({
86
  'title': current_title,
87
+ 'content': current_content.strip(),
88
  'source': 'nbtel_profile',
89
  'section_type': 'main'
90
  })
91
 
92
+ # تقسيم إضافي للأقسام الطويلة جداً
93
  final_docs = []
94
  for doc in documents:
95
+ if len(doc['content']) > 1500:
96
+ chunks = self._split_long_text(doc['content'], max_length=1200)
97
  for i, chunk in enumerate(chunks):
98
+ if chunk.strip(): # تأكد من وجود محتوى
99
+ final_docs.append({
100
+ 'title': f"{doc['title']} - جزء {i+1}",
101
+ 'content': chunk,
102
+ 'source': doc['source'],
103
+ 'section_type': 'chunk'
104
+ })
105
  else:
106
+ if doc['content'].strip(): # تأكد من وجود محتوى
107
+ final_docs.append(doc)
108
+
109
+ # إزالة المستندات الفارغة
110
+ final_docs = [doc for doc in final_docs if len(doc['content'].strip()) > 50]
111
 
112
  print(f"✅ تم تحميل {len(final_docs)} قسم من الملف")
113
  return final_docs