EGYADMIN commited on
Commit
bfd41c6
·
verified ·
1 Parent(s): d1c9f8f

Update modules/ai_assistant/ai_app.py

Browse files
Files changed (1) hide show
  1. modules/ai_assistant/ai_app.py +48 -2
modules/ai_assistant/ai_app.py CHANGED
@@ -104,9 +104,55 @@ class ClaudeAIService:
104
  file_content = compressed_img_buffer.read()
105
  print(f"تم ضغط الصورة من {file_size/1024/1024:.2f} ميجابايت إلى {len(file_content)/1024/1024:.2f} ميجابايت")
106
  else:
107
- # استخدام الملف الأصلي إذا كان حجمه مقبول
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  with open(image_path, 'rb') as f:
109
  file_content = f.read()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
  # تحويل المحتوى إلى Base64
112
  file_base64 = base64.b64encode(file_content).decode('utf-8')
@@ -592,7 +638,7 @@ class AIAssistantApp:
592
  if images:
593
  # حفظ الصورة بشكل مؤقت
594
  temp_image_path = f"{temp_file_path}_image.jpg"
595
- images[0].save(temp_image_path, 'JPEG')
596
  # استخدام مسار الصورة بدلاً من PDF
597
  os.remove(temp_file_path)
598
  temp_file_path = temp_image_path
 
104
  file_content = compressed_img_buffer.read()
105
  print(f"تم ضغط الصورة من {file_size/1024/1024:.2f} ميجابايت إلى {len(file_content)/1024/1024:.2f} ميجابايت")
106
  else:
107
+ # قراءة محتوى الملف مع ضغط الصور فقط
108
+ file_size = os.path.getsize(image_path)
109
+ _, ext = os.path.splitext(image_path)
110
+ ext = ext.lower()
111
+
112
+ if ext in ['.jpg', '.jpeg', '.png', '.gif', '.webp'] and file_size > 5 * 1024 * 1024:
113
+ # فقط ضغط الصور العادية وليس ملفات PDF
114
+ try:
115
+ with Image.open(image_path) as img:
116
+ # حفظ الصورة المضغوطة في ذاكرة مؤقتة
117
+ compressed_img_buffer = io.BytesIO()
118
+
119
+ # حساب معامل الجودة المناسب
120
+ quality = min(95, int((5 * 1024 * 1024 / file_size) * 100))
121
+
122
+ # حفظ الصورة بالجودة المحسوبة
123
+ img.save(compressed_img_buffer, format='JPEG', quality=quality)
124
+ compressed_img_buffer.seek(0)
125
+
126
+ # استخدام البيانات المضغوطة
127
+ file_content = compressed_img_buffer.read()
128
+ logging.info(f"تم ضغط الصورة من {file_size/1024/1024:.2f} ميجابايت إلى {len(file_content)/1024/1024:.2f} ميجابايت")
129
+
130
+ except Exception as e:
131
+ logging.error(f"خطأ أثناء ضغط الصورة: {str(e)}")
132
+ with open(image_path, 'rb') as f:
133
+ file_content = f.read()
134
+ else:
135
+ # استخدام الملف الأصلي للملفات غير الصور أو الصور الصغيرة
136
  with open(image_path, 'rb') as f:
137
  file_content = f.read()
138
+
139
+ # التحقق من حجم الملف بعد القراءة
140
+ if len(file_content) > 5 * 1024 * 1024:
141
+ raise ValueError(f"حجم الملف ({len(file_content)/1024/1024:.2f} ميجابايت) يتجاوز الحد الأقصى (5 ميجابايت). يرجى تقليل حجم الملف قبل الرفع.")
142
+
143
+
144
+
145
+ # حساب معامل الجودة المناسب
146
+ quality = min(95, int((5 * 1024 * 1024 / file_size) * 100)
147
+
148
+ # حفظ الصورة بالجودة المحسوبة
149
+ img.save(compressed_img_buffer, format='JPEG', quality=quality)
150
+ compressed_img_buffer.seek(0)
151
+
152
+ # استخدام البيانات المضغوطة
153
+ file_content = compressed_img_buffer.read()
154
+
155
+
156
 
157
  # تحويل المحتوى إلى Base64
158
  file_base64 = base64.b64encode(file_content).decode('utf-8')
 
638
  if images:
639
  # حفظ الصورة بشكل مؤقت
640
  temp_image_path = f"{temp_file_path}_image.jpg"
641
+ images[0].save(temp_image_path, 'JPEG', quality=85)
642
  # استخدام مسار الصورة بدلاً من PDF
643
  os.remove(temp_file_path)
644
  temp_file_path = temp_image_path