Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -40,10 +40,14 @@ theses_df["المصدر"] = "رسالة"
|
|
| 40 |
|
| 41 |
library_df = pd.concat([books_df, theses_df], ignore_index=True)
|
| 42 |
|
| 43 |
-
# ================== ت
|
|
|
|
|
|
|
| 44 |
library_df["سنة النشر"] = pd.to_numeric(
|
| 45 |
library_df.get("سنة النشر"), errors="coerce"
|
| 46 |
-
)
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# ================== مسار embeddings ==================
|
| 49 |
def emb_path(name):
|
|
@@ -140,25 +144,25 @@ def results_to_html(df):
|
|
| 140 |
if df.empty:
|
| 141 |
return "<p>❌ لا توجد نتائج</p>"
|
| 142 |
|
| 143 |
-
# استبعاد أعمدة غير مطلوبة
|
| 144 |
df = df.drop(columns=["embedding", "score"], errors="ignore")
|
| 145 |
|
| 146 |
-
# اختيار الأعمدة الأساسية فقط
|
| 147 |
cols = ["العنوان", "المؤلف", "سنة النشر", "الموقع على الرف"]
|
| 148 |
-
cols = [c for c in cols if c in df.columns]
|
| 149 |
-
|
| 150 |
-
html = CUSTOM_CSS
|
| 151 |
-
html += "<div style='direction:rtl;text-align:right;'>"
|
| 152 |
-
html += df[cols].to_html(
|
| 153 |
-
index=False,
|
| 154 |
-
escape=False,
|
| 155 |
-
classes="styled-table",
|
| 156 |
-
border=0
|
| 157 |
-
)
|
| 158 |
-
html += "</div>"
|
| 159 |
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
|
|
|
|
|
|
| 162 |
|
| 163 |
# ================== البحث ==================
|
| 164 |
def local_search_df(query, mode, source_filter, year_range):
|
|
@@ -171,9 +175,15 @@ def local_search_df(query, mode, source_filter, year_range):
|
|
| 171 |
df_search = df_search[df_search["المصدر"] == source_filter]
|
| 172 |
|
| 173 |
start_year, end_year = year_range
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
df_search = df_search[
|
| 175 |
-
|
| 176 |
-
(df_search["سنة النشر"] <= end_year)
|
| 177 |
]
|
| 178 |
|
| 179 |
if df_search.empty:
|
|
@@ -200,12 +210,10 @@ def local_search_df(query, mode, source_filter, year_range):
|
|
| 200 |
scores = util.cos_sim(q_emb, emb_matrix)[0].cpu().numpy()
|
| 201 |
|
| 202 |
df_scored = df_search.assign(score=scores)
|
| 203 |
-
|
| 204 |
filtered = df_scored[df_scored["score"] >= SIM_THRESHOLD]
|
| 205 |
|
| 206 |
-
df = (
|
| 207 |
-
|
| 208 |
-
).sort_values("score", ascending=False).head(TOP_K)
|
| 209 |
|
| 210 |
return results_to_html(df), df
|
| 211 |
|
|
@@ -230,8 +238,8 @@ with gr.Blocks(title="نظام البحث الذكي بالمكتبة") as app:
|
|
| 230 |
mode = gr.Radio(["نصي", "دلالي (Semantic)"], value="نصي", label="نوع البحث")
|
| 231 |
source_filter = gr.Radio(["الكل", "كتاب", "رسالة"], value="الكل", label="المصدر")
|
| 232 |
|
| 233 |
-
year_min = int(library_df["سنة النشر"].min())
|
| 234 |
-
year_max = int(library_df["سنة النشر"].max())
|
| 235 |
|
| 236 |
year_range = gr.Slider(
|
| 237 |
minimum=year_min,
|
|
|
|
| 40 |
|
| 41 |
library_df = pd.concat([books_df, theses_df], ignore_index=True)
|
| 42 |
|
| 43 |
+
# ================== تنظيف الأعمدة ==================
|
| 44 |
+
library_df.columns = [c.strip() for c in library_df.columns]
|
| 45 |
+
|
| 46 |
library_df["سنة النشر"] = pd.to_numeric(
|
| 47 |
library_df.get("سنة النشر"), errors="coerce"
|
| 48 |
+
).fillna("-").astype(str)
|
| 49 |
+
|
| 50 |
+
library_df["الموقع على الرف"] = library_df.get("الموقع على الرف", "").fillna("-").astype(str).str.strip()
|
| 51 |
|
| 52 |
# ================== مسار embeddings ==================
|
| 53 |
def emb_path(name):
|
|
|
|
| 144 |
if df.empty:
|
| 145 |
return "<p>❌ لا توجد نتائج</p>"
|
| 146 |
|
|
|
|
| 147 |
df = df.drop(columns=["embedding", "score"], errors="ignore")
|
| 148 |
|
|
|
|
| 149 |
cols = ["العنوان", "المؤلف", "سنة النشر", "الموقع على الرف"]
|
| 150 |
+
cols = [c for c in cols if c in df.columns]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
+
html = CUSTOM_CSS + "<div style='direction:rtl;text-align:right;'>"
|
| 153 |
+
|
| 154 |
+
for i, row in df[cols].iterrows():
|
| 155 |
+
html += "<table class='styled-table' style='margin-bottom:15px;'>"
|
| 156 |
+
html += f"<caption style='caption-side:top;text-align:right;font-weight:bold;margin-bottom:5px;'>📘 النتيجة {i+1}</caption>"
|
| 157 |
+
for col in cols:
|
| 158 |
+
val = row[col]
|
| 159 |
+
if val is None or str(val).strip() == "" or str(val).lower() == "nan":
|
| 160 |
+
val = "-"
|
| 161 |
+
html += f"<tr><th>{col}</th><td>{val}</td></tr>"
|
| 162 |
+
html += "</table>"
|
| 163 |
|
| 164 |
+
html += "</div>"
|
| 165 |
+
return html
|
| 166 |
|
| 167 |
# ================== البحث ==================
|
| 168 |
def local_search_df(query, mode, source_filter, year_range):
|
|
|
|
| 175 |
df_search = df_search[df_search["المصدر"] == source_filter]
|
| 176 |
|
| 177 |
start_year, end_year = year_range
|
| 178 |
+
# تحويل "-" لسنة رقمية صغيرة لتجنب الفلاتر
|
| 179 |
+
def year_filter(y):
|
| 180 |
+
try:
|
| 181 |
+
return int(y)
|
| 182 |
+
except:
|
| 183 |
+
return 0
|
| 184 |
+
|
| 185 |
df_search = df_search[
|
| 186 |
+
df_search["سنة النشر"].apply(year_filter).between(start_year, end_year)
|
|
|
|
| 187 |
]
|
| 188 |
|
| 189 |
if df_search.empty:
|
|
|
|
| 210 |
scores = util.cos_sim(q_emb, emb_matrix)[0].cpu().numpy()
|
| 211 |
|
| 212 |
df_scored = df_search.assign(score=scores)
|
|
|
|
| 213 |
filtered = df_scored[df_scored["score"] >= SIM_THRESHOLD]
|
| 214 |
|
| 215 |
+
df = (filtered if not filtered.empty else df_scored)\
|
| 216 |
+
.sort_values("score", ascending=False).head(TOP_K)
|
|
|
|
| 217 |
|
| 218 |
return results_to_html(df), df
|
| 219 |
|
|
|
|
| 238 |
mode = gr.Radio(["نصي", "دلالي (Semantic)"], value="نصي", label="نوع البحث")
|
| 239 |
source_filter = gr.Radio(["الكل", "كتاب", "رسالة"], value="الكل", label="المصدر")
|
| 240 |
|
| 241 |
+
year_min = int(library_df["سنة النشر"].replace("-",0).astype(int).min())
|
| 242 |
+
year_max = int(library_df["سنة النشر"].replace("-",0).astype(int).max())
|
| 243 |
|
| 244 |
year_range = gr.Slider(
|
| 245 |
minimum=year_min,
|