Update modules/document_analysis/analyzer.py
Browse files
modules/document_analysis/analyzer.py
CHANGED
@@ -171,15 +171,27 @@ class DocumentAnalyzer:
|
|
171 |
raise
|
172 |
|
173 |
def _extract_text_from_pdf(self, document_path):
|
174 |
-
"""استخراج النص من ملف PDF
|
175 |
-
|
176 |
-
|
177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
|
179 |
def _analyze_contract_terms(self, text):
|
180 |
"""تحليل بنود العقد"""
|
181 |
-
|
182 |
-
|
|
|
|
|
|
|
|
|
183 |
|
184 |
def _analyze_financial_terms(self, text):
|
185 |
"""تحليل الجزء المالي"""
|
@@ -447,20 +459,27 @@ class DocumentAnalyzer:
|
|
447 |
if img.mode == 'RGBA':
|
448 |
img = img.convert('RGB')
|
449 |
|
450 |
-
#
|
|
|
451 |
max_size = (1200, 1200)
|
452 |
-
img.thumbnail(max_size, Image.Resampling.LANCZOS)
|
453 |
-
|
454 |
-
# ضغط الصورة بجودة أقل للحصول على حجم أصغر
|
455 |
-
buffer = io.BytesIO()
|
456 |
-
img.save(buffer, format='JPEG', quality=60, optimize=True)
|
457 |
|
458 |
-
|
459 |
-
|
460 |
-
# محاولة ضغط إضافية
|
461 |
-
img.thumbnail((800, 800), Image.Resampling.LANCZOS)
|
462 |
buffer = io.BytesIO()
|
463 |
-
img.save(buffer, format='JPEG', quality=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
464 |
|
465 |
# تحويل الصورة المضغوطة إلى base64
|
466 |
return base64.b64encode(buffer.getvalue()).decode('utf-8')
|
|
|
171 |
raise
|
172 |
|
173 |
def _extract_text_from_pdf(self, document_path):
|
174 |
+
"""استخراج النص من ملف PDF"""
|
175 |
+
try:
|
176 |
+
import PyPDF2
|
177 |
+
text = ""
|
178 |
+
with open(document_path, 'rb') as file:
|
179 |
+
reader = PyPDF2.PdfReader(file)
|
180 |
+
for page in reader.pages:
|
181 |
+
text += page.extract_text() + "\n"
|
182 |
+
return text
|
183 |
+
except Exception as e:
|
184 |
+
logger.error(f"خطأ في استخراج النص من PDF: {str(e)}")
|
185 |
+
raise
|
186 |
|
187 |
def _analyze_contract_terms(self, text):
|
188 |
"""تحليل بنود العقد"""
|
189 |
+
terms = []
|
190 |
+
sections = text.split('\n\n')
|
191 |
+
for section in sections:
|
192 |
+
if any(keyword in section.lower() for keyword in ['شروط', 'بند', 'يلتزم', 'يجب']):
|
193 |
+
terms.append(section.strip())
|
194 |
+
return terms
|
195 |
|
196 |
def _analyze_financial_terms(self, text):
|
197 |
"""تحليل الجزء المالي"""
|
|
|
459 |
if img.mode == 'RGBA':
|
460 |
img = img.convert('RGB')
|
461 |
|
462 |
+
# البدء بجودة عالية وتقليلها تدريجياً حتى نصل للحجم المطلوب
|
463 |
+
quality = 95
|
464 |
max_size = (1200, 1200)
|
|
|
|
|
|
|
|
|
|
|
465 |
|
466 |
+
while True:
|
467 |
+
img.thumbnail(max_size, Image.Resampling.LANCZOS)
|
|
|
|
|
468 |
buffer = io.BytesIO()
|
469 |
+
img.save(buffer, format='JPEG', quality=quality, optimize=True)
|
470 |
+
size = len(buffer.getvalue())
|
471 |
+
|
472 |
+
# إذا كان الحجم أقل من 5 ميجابايت، نخرج من الحلقة
|
473 |
+
if size <= 5000000:
|
474 |
+
break
|
475 |
+
|
476 |
+
# تقليل الجودة والحجم
|
477 |
+
quality = max(quality - 10, 20) # لا نقلل الجودة عن 20
|
478 |
+
max_size = (int(max_size[0] * 0.8), int(max_size[1] * 0.8))
|
479 |
+
|
480 |
+
# إذا وصلنا للحد الأدنى من الجودة والحجم ولم نصل للحجم المطلوب
|
481 |
+
if quality == 20 and max_size[0] < 400:
|
482 |
+
raise ValueError("لا يمكن ضغط الصورة للحجم المطلوب")
|
483 |
|
484 |
# تحويل الصورة المضغوطة إلى base64
|
485 |
return base64.b64encode(buffer.getvalue()).decode('utf-8')
|