Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -43,11 +43,16 @@ library_df = pd.concat([books_df, theses_df], ignore_index=True)
|
|
| 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(
|
| 49 |
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# ================== مسار embeddings ==================
|
| 53 |
def emb_path(name):
|
|
@@ -145,12 +150,10 @@ def results_to_html(df):
|
|
| 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>"
|
|
@@ -160,7 +163,6 @@ def results_to_html(df):
|
|
| 160 |
val = "-"
|
| 161 |
html += f"<tr><th>{col}</th><td>{val}</td></tr>"
|
| 162 |
html += "</table>"
|
| 163 |
-
|
| 164 |
html += "</div>"
|
| 165 |
return html
|
| 166 |
|
|
@@ -175,15 +177,8 @@ 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["سنة النشر"].
|
| 187 |
]
|
| 188 |
|
| 189 |
if df_search.empty:
|
|
@@ -205,13 +200,11 @@ def local_search_df(query, mode, source_filter, year_range):
|
|
| 205 |
convert_to_numpy=True,
|
| 206 |
normalize_embeddings=True
|
| 207 |
)
|
| 208 |
-
|
| 209 |
emb_matrix = np.vstack(df_search["embedding"].values)
|
| 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 |
|
|
@@ -220,11 +213,9 @@ def local_search_df(query, mode, source_filter, year_range):
|
|
| 220 |
# ================== حفظ النتائج ==================
|
| 221 |
def save_to_excel(df):
|
| 222 |
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx")
|
| 223 |
-
|
| 224 |
if df is None or df.empty:
|
| 225 |
pd.DataFrame().to_excel(tmp.name, index=False)
|
| 226 |
return tmp.name
|
| 227 |
-
|
| 228 |
df = df.drop(columns=["embedding", "score"], errors="ignore")
|
| 229 |
df.to_excel(tmp.name, index=False)
|
| 230 |
return tmp.name
|
|
@@ -238,8 +229,8 @@ with gr.Blocks(title="نظام البحث الذكي بالمكتبة") as app:
|
|
| 238 |
mode = gr.Radio(["نصي", "دلالي (Semantic)"], value="نصي", label="نوع البحث")
|
| 239 |
source_filter = gr.Radio(["الكل", "كتاب", "رسالة"], value="الكل", label="المصدر")
|
| 240 |
|
| 241 |
-
year_min = int(library_df["سنة النشر"].
|
| 242 |
-
year_max = int(library_df["سنة النشر"].
|
| 243 |
|
| 244 |
year_range = gr.Slider(
|
| 245 |
minimum=year_min,
|
|
|
|
| 43 |
# ================== تنظيف الأعمدة ==================
|
| 44 |
library_df.columns = [c.strip() for c in library_df.columns]
|
| 45 |
|
| 46 |
+
# ================== تجهيز سنة النشر ==================
|
| 47 |
library_df["سنة النشر"] = pd.to_numeric(
|
| 48 |
+
library_df.get("سنة النشر", 0), errors="coerce"
|
| 49 |
+
).fillna(0).astype(int)
|
| 50 |
|
| 51 |
+
# ================== تجهيز الموقع على الرف ==================
|
| 52 |
+
if "الموقع على الرف" in library_df.columns:
|
| 53 |
+
library_df["الموقع على الرف"] = library_df["الموقع على الرف"].fillna("-").astype(str).str.strip()
|
| 54 |
+
else:
|
| 55 |
+
library_df["الموقع على الرف"] = "-"
|
| 56 |
|
| 57 |
# ================== مسار embeddings ==================
|
| 58 |
def emb_path(name):
|
|
|
|
| 150 |
return "<p>❌ لا توجد نتائج</p>"
|
| 151 |
|
| 152 |
df = df.drop(columns=["embedding", "score"], errors="ignore")
|
|
|
|
| 153 |
cols = ["العنوان", "المؤلف", "سنة النشر", "الموقع على الرف"]
|
| 154 |
cols = [c for c in cols if c in df.columns]
|
| 155 |
|
| 156 |
html = CUSTOM_CSS + "<div style='direction:rtl;text-align:right;'>"
|
|
|
|
| 157 |
for i, row in df[cols].iterrows():
|
| 158 |
html += "<table class='styled-table' style='margin-bottom:15px;'>"
|
| 159 |
html += f"<caption style='caption-side:top;text-align:right;font-weight:bold;margin-bottom:5px;'>📘 النتيجة {i+1}</caption>"
|
|
|
|
| 163 |
val = "-"
|
| 164 |
html += f"<tr><th>{col}</th><td>{val}</td></tr>"
|
| 165 |
html += "</table>"
|
|
|
|
| 166 |
html += "</div>"
|
| 167 |
return html
|
| 168 |
|
|
|
|
| 177 |
df_search = df_search[df_search["المصدر"] == source_filter]
|
| 178 |
|
| 179 |
start_year, end_year = year_range
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
df_search = df_search[
|
| 181 |
+
df_search["سنة النشر"].between(start_year, end_year)
|
| 182 |
]
|
| 183 |
|
| 184 |
if df_search.empty:
|
|
|
|
| 200 |
convert_to_numpy=True,
|
| 201 |
normalize_embeddings=True
|
| 202 |
)
|
|
|
|
| 203 |
emb_matrix = np.vstack(df_search["embedding"].values)
|
| 204 |
scores = util.cos_sim(q_emb, emb_matrix)[0].cpu().numpy()
|
| 205 |
|
| 206 |
df_scored = df_search.assign(score=scores)
|
| 207 |
filtered = df_scored[df_scored["score"] >= SIM_THRESHOLD]
|
|
|
|
| 208 |
df = (filtered if not filtered.empty else df_scored)\
|
| 209 |
.sort_values("score", ascending=False).head(TOP_K)
|
| 210 |
|
|
|
|
| 213 |
# ================== حفظ النتائج ==================
|
| 214 |
def save_to_excel(df):
|
| 215 |
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx")
|
|
|
|
| 216 |
if df is None or df.empty:
|
| 217 |
pd.DataFrame().to_excel(tmp.name, index=False)
|
| 218 |
return tmp.name
|
|
|
|
| 219 |
df = df.drop(columns=["embedding", "score"], errors="ignore")
|
| 220 |
df.to_excel(tmp.name, index=False)
|
| 221 |
return tmp.name
|
|
|
|
| 229 |
mode = gr.Radio(["نصي", "دلالي (Semantic)"], value="نصي", label="نوع البحث")
|
| 230 |
source_filter = gr.Radio(["الكل", "كتاب", "رسالة"], value="الكل", label="المصدر")
|
| 231 |
|
| 232 |
+
year_min = int(library_df["سنة النشر"].min())
|
| 233 |
+
year_max = int(library_df["سنة النشر"].max())
|
| 234 |
|
| 235 |
year_range = gr.Slider(
|
| 236 |
minimum=year_min,
|