Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,9 @@ from PIL import Image
|
|
| 14 |
|
| 15 |
# -------------------------- استيرادات الكممَة والبيئة -------------------------
|
| 16 |
try:
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
import torch
|
| 19 |
LOCAL_OPTIMUM = True
|
| 20 |
except ImportError:
|
|
@@ -28,8 +30,8 @@ except ImportError:
|
|
| 28 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
| 29 |
log = logging.getLogger("image_agent")
|
| 30 |
|
| 31 |
-
if '
|
| 32 |
-
log.info("✅ Optimum/OpenVINO متاح للتحسين على CPU.")
|
| 33 |
elif 'StableDiffusionPipeline' in globals():
|
| 34 |
log.info("⚠️ OpenVINO غير متاح. سيتم استخدام Diffusers القياسي.")
|
| 35 |
else:
|
|
@@ -66,11 +68,10 @@ def get_local_pipeline():
|
|
| 66 |
if not LOCAL_DIFFUSERS:
|
| 67 |
raise RuntimeError("التوليد المحلي غير متاح. تأكد من إعداد الاعتماديات.")
|
| 68 |
|
| 69 |
-
#
|
| 70 |
if HF_MODEL is None:
|
| 71 |
log.error("HF_MODEL is None. Cannot load pipeline.")
|
| 72 |
raise ValueError("❌ خطأ: لم يتم تعيين مسار النموذج (HF_MODEL).")
|
| 73 |
-
# =======================================================
|
| 74 |
|
| 75 |
log.info(f"Loading local diffusers pipeline from: {HF_MODEL}")
|
| 76 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
@@ -80,14 +81,11 @@ def get_local_pipeline():
|
|
| 80 |
# نحافظ على float16 لأداء GPU إذا كان متاحاً
|
| 81 |
pipeline_args['torch_dtype'] = torch.float16
|
| 82 |
|
| 83 |
-
# === استخدام
|
| 84 |
if LOCAL_OPTIMUM:
|
| 85 |
-
log.info("Loading
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
_pipeline = OVStableDiffusionPipeline.from_pretrained(
|
| 89 |
-
HF_MODEL
|
| 90 |
-
)
|
| 91 |
|
| 92 |
else:
|
| 93 |
log.info("Loading standard StableDiffusionPipeline.")
|
|
@@ -96,7 +94,6 @@ def get_local_pipeline():
|
|
| 96 |
**pipeline_args
|
| 97 |
)
|
| 98 |
|
| 99 |
-
# ملاحظة: تم إزالة enable_sequential_cpu_offload الفاشلة
|
| 100 |
_pipeline = _pipeline.to(device)
|
| 101 |
|
| 102 |
return _pipeline
|
|
@@ -148,7 +145,7 @@ def local_generate(prompt: str) -> Tuple[Image.Image, str]:
|
|
| 148 |
try:
|
| 149 |
pipe = get_local_pipeline()
|
| 150 |
|
| 151 |
-
#
|
| 152 |
INFERENCE_STEPS = 75
|
| 153 |
log.info(f"Local generation started for prompt: {prompt[:50]}... with {INFERENCE_STEPS} steps.")
|
| 154 |
|
|
|
|
| 14 |
|
| 15 |
# -------------------------- استيرادات الكممَة والبيئة -------------------------
|
| 16 |
try:
|
| 17 |
+
# استخدام الفئة الصحيحة والمسار المحدث لـ OpenVINO
|
| 18 |
+
# OVDiffusionPipeline هي الأنسب لـ Stable Diffusion XL
|
| 19 |
+
from optimum.intel.openvino import OVDiffusionPipeline
|
| 20 |
import torch
|
| 21 |
LOCAL_OPTIMUM = True
|
| 22 |
except ImportError:
|
|
|
|
| 30 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
| 31 |
log = logging.getLogger("image_agent")
|
| 32 |
|
| 33 |
+
if 'OVDiffusionPipeline' in globals(): # تم تحديث التحقق
|
| 34 |
+
log.info("✅ Optimum/OpenVINO متاح للتحسين على CPU باستخدام OVDiffusionPipeline.")
|
| 35 |
elif 'StableDiffusionPipeline' in globals():
|
| 36 |
log.info("⚠️ OpenVINO غير متاح. سيتم استخدام Diffusers القياسي.")
|
| 37 |
else:
|
|
|
|
| 68 |
if not LOCAL_DIFFUSERS:
|
| 69 |
raise RuntimeError("التوليد المحلي غير متاح. تأكد من إعداد الاعتماديات.")
|
| 70 |
|
| 71 |
+
# التحصين ضد NoneType في المسار
|
| 72 |
if HF_MODEL is None:
|
| 73 |
log.error("HF_MODEL is None. Cannot load pipeline.")
|
| 74 |
raise ValueError("❌ خطأ: لم يتم تعيين مسار النموذج (HF_MODEL).")
|
|
|
|
| 75 |
|
| 76 |
log.info(f"Loading local diffusers pipeline from: {HF_MODEL}")
|
| 77 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 81 |
# نحافظ على float16 لأداء GPU إذا كان متاحاً
|
| 82 |
pipeline_args['torch_dtype'] = torch.float16
|
| 83 |
|
| 84 |
+
# === استخدام OVDiffusionPipeline الجديدة والصحيحة ===
|
| 85 |
if LOCAL_OPTIMUM:
|
| 86 |
+
log.info("Loading OVDiffusionPipeline (Quantized pipeline).")
|
| 87 |
+
# التحميل باستخدام الفئة الصحيحة
|
| 88 |
+
_pipeline = OVDiffusionPipeline.from_pretrained(HF_MODEL)
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
else:
|
| 91 |
log.info("Loading standard StableDiffusionPipeline.")
|
|
|
|
| 94 |
**pipeline_args
|
| 95 |
)
|
| 96 |
|
|
|
|
| 97 |
_pipeline = _pipeline.to(device)
|
| 98 |
|
| 99 |
return _pipeline
|
|
|
|
| 145 |
try:
|
| 146 |
pipe = get_local_pipeline()
|
| 147 |
|
| 148 |
+
# إضافة خطوات الاستدلال للتحكم في الجودة والوقت
|
| 149 |
INFERENCE_STEPS = 75
|
| 150 |
log.info(f"Local generation started for prompt: {prompt[:50]}... with {INFERENCE_STEPS} steps.")
|
| 151 |
|