Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,7 +19,7 @@ TOP_K = 20
|
|
| 19 |
TFIDF_TOP_N = 150
|
| 20 |
TFIDF_FALLBACK_MIN = 40
|
| 21 |
|
| 22 |
-
# ================== نموذج طبي
|
| 23 |
model = SentenceTransformer(
|
| 24 |
"pritamdeka/BioBERT-mnli-snli-scinli-scitail-mednli-stsb"
|
| 25 |
)
|
|
@@ -27,8 +27,8 @@ model = SentenceTransformer(
|
|
| 27 |
# ================== تحميل الملفات ==================
|
| 28 |
def download_from_drive(file_id, output):
|
| 29 |
if not os.path.exists(output):
|
| 30 |
-
url = f"https://
|
| 31 |
-
gdown.download(url, output, quiet=
|
| 32 |
|
| 33 |
download_from_drive(DRIVE_BOOKS_ID, BOOKS_FILE)
|
| 34 |
download_from_drive(DRIVE_THESES_ID, THESES_FILE)
|
|
@@ -37,15 +37,12 @@ download_from_drive(DRIVE_THESES_ID, THESES_FILE)
|
|
| 37 |
books_df = pd.read_excel(BOOKS_FILE).fillna("")
|
| 38 |
theses_df = pd.read_excel(THESES_FILE).fillna("")
|
| 39 |
|
| 40 |
-
books_df["المصدر"] = "كتاب"
|
| 41 |
-
theses_df["المصدر"] = "رسالة"
|
| 42 |
-
|
| 43 |
library_df = pd.concat([books_df, theses_df], ignore_index=True)
|
| 44 |
|
| 45 |
# ================== الأعمدة الدلالية ==================
|
| 46 |
def detect_semantic_columns(df):
|
| 47 |
exclude = ["م", "سنة", "تاريخ", "رقم", "كود", "الموقع", "الرف"]
|
| 48 |
-
include = ["عنوان", "العنوان", "مؤلف", "المؤلف", "موضوع", "ملخص", "وصف", "كلمات"]
|
| 49 |
|
| 50 |
cols = []
|
| 51 |
for c in df.columns:
|
|
@@ -62,7 +59,7 @@ def detect_semantic_columns(df):
|
|
| 62 |
|
| 63 |
semantic_cols = detect_semantic_columns(library_df)
|
| 64 |
|
| 65 |
-
# ================== Normalize
|
| 66 |
def normalize_text(text):
|
| 67 |
text = str(text).lower()
|
| 68 |
text = re.sub(r"[^a-zA-Z0-9\u0600-\u06FF\s]", " ", text)
|
|
@@ -108,7 +105,7 @@ def tfidf_prefilter(df, query):
|
|
| 108 |
|
| 109 |
return df.sort_values("tfidf_score", ascending=False).head(TFIDF_TOP_N)
|
| 110 |
|
| 111 |
-
# ================== CSS (
|
| 112 |
CUSTOM_CSS = """
|
| 113 |
<style>
|
| 114 |
.card{border:1px solid #ddd;border-radius:8px;padding:10px;margin:12px 0;background:#fafafa;}
|
|
@@ -118,6 +115,7 @@ CUSTOM_CSS = """
|
|
| 118 |
.row-title td{background:#e3f2fd;color:#0d47a1;font-weight:bold;}
|
| 119 |
.row-author td{background:#fce4ec;color:#880e4f;}
|
| 120 |
.row-year td{background:#e8f5e9;color:#1b5e20;}
|
|
|
|
| 121 |
.row-shelf td{background:#fff8e1;color:#e65100;font-weight:bold;font-size:16px;}
|
| 122 |
</style>
|
| 123 |
"""
|
|
@@ -138,14 +136,22 @@ def results_to_html(df):
|
|
| 138 |
<td class="label">العنوان</td>
|
| 139 |
<td>{safe(r.get("العنوان"))}</td>
|
| 140 |
</tr>
|
|
|
|
| 141 |
<tr class="row-author">
|
| 142 |
<td class="label">المؤلف</td>
|
| 143 |
<td>{safe(r.get("المؤلف"))}</td>
|
| 144 |
</tr>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
<tr class="row-year">
|
| 146 |
<td class="label">سنة النشر</td>
|
| 147 |
<td>{safe(r.get("سنة النشر"))}</td>
|
| 148 |
</tr>
|
|
|
|
| 149 |
<tr class="row-shelf">
|
| 150 |
<td class="label">الموقع على الرف</td>
|
| 151 |
<td>{safe(r.get("الموقع على الرف"))}</td>
|
|
@@ -163,14 +169,10 @@ def local_search_df(query, mode, source_filter):
|
|
| 163 |
|
| 164 |
df = library_df.copy()
|
| 165 |
|
| 166 |
-
if source_filter != "الكل":
|
| 167 |
-
df = df[df["المصدر"] == source_filter]
|
| 168 |
-
|
| 169 |
if mode == "نصي":
|
| 170 |
q = normalize_text(query)
|
| 171 |
mask = df.apply(lambda r: q in row_to_text(r), axis=1)
|
| 172 |
df = df[mask].head(TOP_K)
|
| 173 |
-
|
| 174 |
else:
|
| 175 |
df_pref = tfidf_prefilter(df, query)
|
| 176 |
if len(df_pref) < TFIDF_FALLBACK_MIN:
|
|
@@ -197,15 +199,14 @@ with gr.Blocks(title="نظام البحث الذكي بالمكتبة") as app:
|
|
| 197 |
gr.Markdown("## 🔍 البحث الذكي في مقتنيات المكتبة")
|
| 198 |
|
| 199 |
query = gr.Textbox(label="كلمة أو موضوع البحث")
|
| 200 |
-
mode = gr.Radio(["نصي", "دلالي (Semantic)"], value="دلالي", label="نوع البحث")
|
| 201 |
-
source_filter = gr.Radio(["الكل", "كتاب", "رسالة"], value="الكل", label="المصدر")
|
| 202 |
|
| 203 |
btn = gr.Button("🔎 بحث")
|
| 204 |
out_html = gr.HTML()
|
| 205 |
df_state = gr.State()
|
| 206 |
file_out = gr.File(label="⬇️ تحميل النتائج")
|
| 207 |
|
| 208 |
-
btn.click(local_search_df, [query, mode,
|
| 209 |
gr.Button("📥 حفظ النتائج").click(save_to_excel, df_state, file_out)
|
| 210 |
|
| 211 |
app.launch()
|
|
|
|
| 19 |
TFIDF_TOP_N = 150
|
| 20 |
TFIDF_FALLBACK_MIN = 40
|
| 21 |
|
| 22 |
+
# ================== نموذج طبي ==================
|
| 23 |
model = SentenceTransformer(
|
| 24 |
"pritamdeka/BioBERT-mnli-snli-scinli-scitail-mednli-stsb"
|
| 25 |
)
|
|
|
|
| 27 |
# ================== تحميل الملفات ==================
|
| 28 |
def download_from_drive(file_id, output):
|
| 29 |
if not os.path.exists(output):
|
| 30 |
+
url = f"https://docs.google.com/spreadsheets/d/{file_id}/export?format=xlsx"
|
| 31 |
+
gdown.download(url, output, quiet=False)
|
| 32 |
|
| 33 |
download_from_drive(DRIVE_BOOKS_ID, BOOKS_FILE)
|
| 34 |
download_from_drive(DRIVE_THESES_ID, THESES_FILE)
|
|
|
|
| 37 |
books_df = pd.read_excel(BOOKS_FILE).fillna("")
|
| 38 |
theses_df = pd.read_excel(THESES_FILE).fillna("")
|
| 39 |
|
|
|
|
|
|
|
|
|
|
| 40 |
library_df = pd.concat([books_df, theses_df], ignore_index=True)
|
| 41 |
|
| 42 |
# ================== الأعمدة الدلالية ==================
|
| 43 |
def detect_semantic_columns(df):
|
| 44 |
exclude = ["م", "سنة", "تاريخ", "رقم", "كود", "الموقع", "الرف"]
|
| 45 |
+
include = ["عنوان", "العنوان", "مؤلف", "المؤلف", "موضوع", "ملخص", "وصف", "نوع درجة الرسالة العلمية ", "كلمات"]
|
| 46 |
|
| 47 |
cols = []
|
| 48 |
for c in df.columns:
|
|
|
|
| 59 |
|
| 60 |
semantic_cols = detect_semantic_columns(library_df)
|
| 61 |
|
| 62 |
+
# ================== Normalize ==================
|
| 63 |
def normalize_text(text):
|
| 64 |
text = str(text).lower()
|
| 65 |
text = re.sub(r"[^a-zA-Z0-9\u0600-\u06FF\s]", " ", text)
|
|
|
|
| 105 |
|
| 106 |
return df.sort_values("tfidf_score", ascending=False).head(TFIDF_TOP_N)
|
| 107 |
|
| 108 |
+
# ================== CSS (تصميم النتائج) ==================
|
| 109 |
CUSTOM_CSS = """
|
| 110 |
<style>
|
| 111 |
.card{border:1px solid #ddd;border-radius:8px;padding:10px;margin:12px 0;background:#fafafa;}
|
|
|
|
| 115 |
.row-title td{background:#e3f2fd;color:#0d47a1;font-weight:bold;}
|
| 116 |
.row-author td{background:#fce4ec;color:#880e4f;}
|
| 117 |
.row-year td{background:#e8f5e9;color:#1b5e20;}
|
| 118 |
+
.row-degree td{background:#fff3e0;color:#ff6f00;font-weight:bold;}
|
| 119 |
.row-shelf td{background:#fff8e1;color:#e65100;font-weight:bold;font-size:16px;}
|
| 120 |
</style>
|
| 121 |
"""
|
|
|
|
| 136 |
<td class="label">العنوان</td>
|
| 137 |
<td>{safe(r.get("العنوان"))}</td>
|
| 138 |
</tr>
|
| 139 |
+
|
| 140 |
<tr class="row-author">
|
| 141 |
<td class="label">المؤلف</td>
|
| 142 |
<td>{safe(r.get("المؤلف"))}</td>
|
| 143 |
</tr>
|
| 144 |
+
|
| 145 |
+
<tr class="row-degree">
|
| 146 |
+
<td class="label">نوع درجة الرسالة العلمية</td>
|
| 147 |
+
<td>{safe(r.get("نوع درجة الرسالة العلمية "))}</td>
|
| 148 |
+
</tr>
|
| 149 |
+
|
| 150 |
<tr class="row-year">
|
| 151 |
<td class="label">سنة النشر</td>
|
| 152 |
<td>{safe(r.get("سنة النشر"))}</td>
|
| 153 |
</tr>
|
| 154 |
+
|
| 155 |
<tr class="row-shelf">
|
| 156 |
<td class="label">الموقع على الرف</td>
|
| 157 |
<td>{safe(r.get("الموقع على الرف"))}</td>
|
|
|
|
| 169 |
|
| 170 |
df = library_df.copy()
|
| 171 |
|
|
|
|
|
|
|
|
|
|
| 172 |
if mode == "نصي":
|
| 173 |
q = normalize_text(query)
|
| 174 |
mask = df.apply(lambda r: q in row_to_text(r), axis=1)
|
| 175 |
df = df[mask].head(TOP_K)
|
|
|
|
| 176 |
else:
|
| 177 |
df_pref = tfidf_prefilter(df, query)
|
| 178 |
if len(df_pref) < TFIDF_FALLBACK_MIN:
|
|
|
|
| 199 |
gr.Markdown("## 🔍 البحث الذكي في مقتنيات المكتبة")
|
| 200 |
|
| 201 |
query = gr.Textbox(label="كلمة أو موضوع البحث")
|
| 202 |
+
mode = gr.Radio(["نصي", "دلالي (Semantic)"], value="دلالي (Semantic)", label="نوع البحث")
|
|
|
|
| 203 |
|
| 204 |
btn = gr.Button("🔎 بحث")
|
| 205 |
out_html = gr.HTML()
|
| 206 |
df_state = gr.State()
|
| 207 |
file_out = gr.File(label="⬇️ تحميل النتائج")
|
| 208 |
|
| 209 |
+
btn.click(local_search_df, [query, mode, "��لكل"], [out_html, df_state])
|
| 210 |
gr.Button("📥 حفظ النتائج").click(save_to_excel, df_state, file_out)
|
| 211 |
|
| 212 |
app.launch()
|