Update modules/ai_assistant/ai_app.py
Browse files- 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 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
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 |
|