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

Update modules/ai_assistant/ai_app.py

Browse files
Files changed (1) hide show
  1. modules/ai_assistant/ai_app.py +34 -55
modules/ai_assistant/ai_app.py CHANGED
@@ -84,61 +84,40 @@ class ClaudeAIService:
84
  # الحصول على مفتاح API
85
  api_key = self.get_api_key()
86
 
87
- # قراءة محتوى الصورة مع ضغطها إذا كان حجمها كبيرًا
88
- file_size = os.path.getsize(image_path)
89
- if file_size > 5 * 1024 * 1024: # إذا كان الحجم أكبر من 5 ميجابايت
90
- # فتح الصورة وضغطها
91
- with Image.open(image_path) as img:
92
- # حفظ الصورة المضغوطة في ذاكرة مؤقتة
93
- compressed_img_buffer = io.BytesIO()
94
-
95
- # حساب معامل الجودة المناسب (كلما قل الرقم، قلت الجودة وحجم الملف)
96
- quality = min(95, int((5 * 1024 * 1024 / file_size) * 100))
97
-
98
- # حفظ الصورة بالجودة المحسوبة
99
- img.save(compressed_img_buffer, format='JPEG', quality=quality)
100
- compressed_img_buffer.seek(0)
101
-
102
-
103
- # استخدام البيانات المضغوطة
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
 
 
84
  # الحصول على مفتاح API
85
  api_key = self.get_api_key()
86
 
87
+ # قراءة محتوى الملف مع ضغط الصور فقط
88
+ file_size = os.path.getsize(image_path)
89
+ _, ext = os.path.splitext(image_path)
90
+ ext = ext.lower()
91
+
92
+ if ext in ['.jpg', '.jpeg', '.png', '.gif', '.webp'] and file_size > 5 * 1024 * 1024:
93
+ # فقط ضغط الصور العادية وليس ملفات PDF
94
+ try:
95
+ with Image.open(image_path) as img:
96
+ # حفظ الصورة المضغوطة في ذاكرة مؤقتة
97
+ compressed_img_buffer = io.BytesIO()
98
+
99
+ # حساب معامل الجودة المناسب
100
+ quality = min(95, int((5 * 1024 * 1024 / file_size) * 100))
101
+
102
+ # حفظ الصورة بالجودة المحسوبة
103
+ img.save(compressed_img_buffer, format='JPEG', quality=quality)
104
+ compressed_img_buffer.seek(0)
105
+
106
+ # استخدام البيانات المضغوطة
107
+ file_content = compressed_img_buffer.read()
108
+ logging.info(f"تم ضغط الصورة من {file_size/1024/1024:.2f} ميجابايت إلى {len(file_content)/1024/1024:.2f} ميجابايت")
109
+ except Exception as e:
110
+ logging.error(f"خطأ أثناء ضغط الصورة: {str(e)}")
111
+ with open(image_path, 'rb') as f:
112
+ file_content = f.read()
113
+ else:
114
+ # استخدام الملف الأصلي للملفات غير الصور أو الصور الصغيرة
115
+ with open(image_path, 'rb') as f:
116
+ file_content = f.read()
117
+
118
+ # التحقق من حجم الملف بعد القراءة
119
+ if len(file_content) > 5 * 1024 * 1024:
120
+ raise ValueError(f"حجم الملف ({len(file_content)/1024/1024:.2f} ميجابايت) يتجاوز الحد الأقصى (5 ميجابايت). يرجى تقليل حجم الملف قبل الرفع.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
 
122
 
123