Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,29 +4,65 @@ import skops.io as sio
|
|
| 4 |
import joblib
|
| 5 |
import numpy as np
|
| 6 |
import os
|
|
|
|
| 7 |
|
| 8 |
print("Current directory contents:", os.listdir())
|
| 9 |
|
| 10 |
-
# تحميل أسماء الأعمدة
|
| 11 |
try:
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
except Exception as e:
|
| 15 |
print(f"❌ Error loading feature_names.pkl: {e}")
|
| 16 |
feature_names = ['tdp', 'cores', 'logicals', 'cpuCount', 'rank', 'samples',
|
| 17 |
'extracted_ghz', 'speed_ghz', 'turbo_ghz', 'cost_per_rank_point', 'cost_per_core',
|
| 18 |
'brand_encoded', 'category_final_encoded', 'socket_final_encoded']
|
|
|
|
| 19 |
|
| 20 |
-
# تحميل النموذج
|
| 21 |
try:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
except Exception as e:
|
| 26 |
-
print(f"❌ Error loading model: {e}")
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
BRAND_MAPPING = {
|
| 31 |
"Intel": 0, # 2593 معالج
|
| 32 |
"AMD": 1, # 1252 معالج
|
|
@@ -56,7 +92,7 @@ CATEGORY_MAPPING = {
|
|
| 56 |
"Unknown": 5, # 41 معالج
|
| 57 |
}
|
| 58 |
|
| 59 |
-
#السوكت وعدد المعالجات في البيانات
|
| 60 |
SOCKET_MAPPING = {
|
| 61 |
"Unknown": 0, # 868 معالج
|
| 62 |
"FCLGA3647": 1, # 178 معالج
|
|
@@ -66,6 +102,8 @@ SOCKET_MAPPING = {
|
|
| 66 |
"Other": 5 # باقي المقابس
|
| 67 |
}
|
| 68 |
|
|
|
|
|
|
|
| 69 |
def predict_price(tdp, cores, logicals, cpuCount, rank, samples,
|
| 70 |
extracted_ghz, speed_ghz, turbo_ghz,
|
| 71 |
cost_per_rank_point, cost_per_core,
|
|
@@ -96,7 +134,7 @@ def predict_price(tdp, cores, logicals, cpuCount, rank, samples,
|
|
| 96 |
|
| 97 |
# ========== واجهة المستخدم ==========
|
| 98 |
|
| 99 |
-
# قائمةالشركات
|
| 100 |
brand_choices = ["Intel", "AMD", "Other", "Qualcomm", "MediaTek",
|
| 101 |
"Samsung", "VIA", "Rockchip", "Unisoc", "Snapdragon",
|
| 102 |
"Apple", "QCT", "Spreadtrum", "Nvidia", "AArch64", "Microsoft"]
|
|
@@ -144,9 +182,7 @@ outputs = gr.Textbox(label="نتيجة التنبؤ", lines=2)
|
|
| 144 |
title = "💰 تنبؤ أسعار المعالجات"
|
| 145 |
description = """
|
| 146 |
### 🖥️ أدخل مواصفات المعالج لتحصل على تقدير لسعره
|
| 147 |
-
|
| 148 |
-
**ملاحظة:** هذا التطبيق للأغراض التعليمية والبحثية فقط. الأسعار تقديرية وقد لا تعكس الواقع الحالي⚠️.
|
| 149 |
-
|
| 150 |
"""
|
| 151 |
|
| 152 |
examples = [
|
|
@@ -160,6 +196,8 @@ examples = [
|
|
| 160 |
[35, 2, 4, 1, 2500, 20, 1.8, 1.6, 2.8, 0.2, 5.0, "Intel", "Embedded/IoT", "Unknown"],
|
| 161 |
]
|
| 162 |
|
|
|
|
|
|
|
| 163 |
demo = gr.Interface(
|
| 164 |
fn=predict_price,
|
| 165 |
inputs=inputs,
|
|
|
|
| 4 |
import joblib
|
| 5 |
import numpy as np
|
| 6 |
import os
|
| 7 |
+
from huggingface_hub import hf_hub_download
|
| 8 |
|
| 9 |
print("Current directory contents:", os.listdir())
|
| 10 |
|
| 11 |
+
# ========== تحميل أسماء الأعمدة من Hugging Face ==========
|
| 12 |
try:
|
| 13 |
+
feature_names_path = hf_hub_download(
|
| 14 |
+
repo_id="EngReem85/cpu-advisor-xgboost",
|
| 15 |
+
filename="feature_names.pkl"
|
| 16 |
+
)
|
| 17 |
+
feature_names = joblib.load(feature_names_path)
|
| 18 |
+
print(f"✅ Loaded feature names from Hugging Face: {len(feature_names)} features")
|
| 19 |
except Exception as e:
|
| 20 |
print(f"❌ Error loading feature_names.pkl: {e}")
|
| 21 |
feature_names = ['tdp', 'cores', 'logicals', 'cpuCount', 'rank', 'samples',
|
| 22 |
'extracted_ghz', 'speed_ghz', 'turbo_ghz', 'cost_per_rank_point', 'cost_per_core',
|
| 23 |
'brand_encoded', 'category_final_encoded', 'socket_final_encoded']
|
| 24 |
+
print("Using default feature names")
|
| 25 |
|
| 26 |
+
# ========== تحميل النموذج من Hugging Face ==========
|
| 27 |
try:
|
| 28 |
+
# محاولة تحميل بصيغة .joblib أولاً
|
| 29 |
+
try:
|
| 30 |
+
model_path = hf_hub_download(
|
| 31 |
+
repo_id="EngReem85/cpu-advisor-xgboost",
|
| 32 |
+
filename="model.joblib"
|
| 33 |
+
)
|
| 34 |
+
model = joblib.load(model_path)
|
| 35 |
+
print("✅ Model loaded successfully from Hugging Face (joblib format)!")
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print(f"⚠️ Failed to load model.joblib: {e}")
|
| 38 |
+
|
| 39 |
+
# محاولة تحميل بصيغة .skops
|
| 40 |
+
model_path = hf_hub_download(
|
| 41 |
+
repo_id="EngReem85/cpu-advisor-xgboost",
|
| 42 |
+
filename="cpu_price_model.skops"
|
| 43 |
+
)
|
| 44 |
+
untrusted_types = sio.get_untrusted_types(file=model_path)
|
| 45 |
+
model = sio.load(model_path, trusted=untrusted_types)
|
| 46 |
+
print("✅ Model loaded successfully from Hugging Face (skops format)!")
|
| 47 |
+
|
| 48 |
except Exception as e:
|
| 49 |
+
print(f"❌ Error loading model from Hugging Face: {e}")
|
| 50 |
+
|
| 51 |
+
# محاولة تحميل من الملف المحلي كحل أخير
|
| 52 |
+
try:
|
| 53 |
+
if os.path.exists("cpu_price_model.skops"):
|
| 54 |
+
untrusted_types = sio.get_untrusted_types(file="cpu_price_model.skops")
|
| 55 |
+
model = sio.load("cpu_price_model.skops", trusted=untrusted_types)
|
| 56 |
+
print("✅ Model loaded from local file as fallback!")
|
| 57 |
+
else:
|
| 58 |
+
raise FileNotFoundError("No model found locally or on Hugging Face")
|
| 59 |
+
except Exception as e2:
|
| 60 |
+
print(f"❌ All loading attempts failed: {e2}")
|
| 61 |
+
raise e2
|
| 62 |
+
|
| 63 |
+
# ========== تعريف الترميزات ==========
|
| 64 |
+
|
| 65 |
+
# أسماء الشركات مع عدد المعالجات من البيانات
|
| 66 |
BRAND_MAPPING = {
|
| 67 |
"Intel": 0, # 2593 معالج
|
| 68 |
"AMD": 1, # 1252 معالج
|
|
|
|
| 92 |
"Unknown": 5, # 41 معالج
|
| 93 |
}
|
| 94 |
|
| 95 |
+
# السوكت وعدد المعالجات في البيانات (أعلى الفئات لأنها كانت فوق 190)
|
| 96 |
SOCKET_MAPPING = {
|
| 97 |
"Unknown": 0, # 868 معالج
|
| 98 |
"FCLGA3647": 1, # 178 معالج
|
|
|
|
| 102 |
"Other": 5 # باقي المقابس
|
| 103 |
}
|
| 104 |
|
| 105 |
+
# ========== دالة التنبؤ ==========
|
| 106 |
+
|
| 107 |
def predict_price(tdp, cores, logicals, cpuCount, rank, samples,
|
| 108 |
extracted_ghz, speed_ghz, turbo_ghz,
|
| 109 |
cost_per_rank_point, cost_per_core,
|
|
|
|
| 134 |
|
| 135 |
# ========== واجهة المستخدم ==========
|
| 136 |
|
| 137 |
+
# قائمة الشركات
|
| 138 |
brand_choices = ["Intel", "AMD", "Other", "Qualcomm", "MediaTek",
|
| 139 |
"Samsung", "VIA", "Rockchip", "Unisoc", "Snapdragon",
|
| 140 |
"Apple", "QCT", "Spreadtrum", "Nvidia", "AArch64", "Microsoft"]
|
|
|
|
| 182 |
title = "💰 تنبؤ أسعار المعالجات"
|
| 183 |
description = """
|
| 184 |
### 🖥️ أدخل مواصفات المعالج لتحصل على تقدير لسعره
|
| 185 |
+
**ملاحظة:** هذا التطبيق للأغراض التعليمية والبحثية فقط. الأسعار تقديرية وقد لا تعكس الواقع الحا��ي ⚠️.
|
|
|
|
|
|
|
| 186 |
"""
|
| 187 |
|
| 188 |
examples = [
|
|
|
|
| 196 |
[35, 2, 4, 1, 2500, 20, 1.8, 1.6, 2.8, 0.2, 5.0, "Intel", "Embedded/IoT", "Unknown"],
|
| 197 |
]
|
| 198 |
|
| 199 |
+
# ========== إنشاء وتشغيل التطبيق ==========
|
| 200 |
+
|
| 201 |
demo = gr.Interface(
|
| 202 |
fn=predict_price,
|
| 203 |
inputs=inputs,
|