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

Update modules/ai_assistant/ai_app.py

Browse files
Files changed (1) hide show
  1. modules/ai_assistant/ai_app.py +52 -35
modules/ai_assistant/ai_app.py CHANGED
@@ -69,43 +69,60 @@ class ClaudeAIService:
69
  return valid_models.get(short_name, short_name)
70
 
71
  def analyze_image(self, image_path, prompt, model_name="claude-3-7-sonnet"):
72
- """
73
- تحليل صورة باستخدام نموذج Claude AI
74
-
75
- المعلمات:
76
- image_path: مسار الصورة المراد تحليلها
77
- prompt: التوجيه للنموذج
78
- model_name: اسم نموذج Claude المراد استخدامه
 
 
 
 
 
 
 
79
 
80
- العوائد:
81
- dict: نتائج التحليل
82
- """
83
- try:
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:
 
69
  return valid_models.get(short_name, short_name)
70
 
71
  def analyze_image(self, image_path, prompt, model_name="claude-3-7-sonnet"):
72
+ """
73
+ تحليل صورة باستخدام نموذج Claude AI
74
+
75
+ المعلمات:
76
+ image_path: مسار الصورة المراد تحليلها
77
+ prompt: التوجيه للنموذج
78
+ model_name: اسم نموذج Claude المراد استخدامه
79
+
80
+ العوائد:
81
+ dict: نتائج التحليل
82
+ """
83
+ try:
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
+ # تحويل المحتوى إلى Base64
123
+ file_base64 = base64.b64encode(file_content).decode('utf-8')
124
+
125
+
126
  except Exception as e:
127
  logging.error(f"خطأ أثناء ضغط الصورة: {str(e)}")
128
  with open(image_path, 'rb') as f: