Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
# ================== imports ==================
|
| 2 |
-
# ================== imports ==================
|
| 3 |
import gradio as gr
|
| 4 |
import pandas as pd
|
| 5 |
import numpy as np
|
|
@@ -63,22 +62,19 @@ def detect_semantic_columns(df):
|
|
| 63 |
|
| 64 |
semantic_cols = detect_semantic_columns(library_df)
|
| 65 |
|
| 66 |
-
# ================== Normalize (محسّن
|
| 67 |
def normalize_text(text):
|
| 68 |
text = str(text).lower()
|
| 69 |
text = re.sub(r"[^a-zA-Z0-9\u0600-\u06FF\s]", " ", text)
|
| 70 |
-
|
| 71 |
-
# Arabic normalization
|
| 72 |
text = text.replace("أ", "ا").replace("إ", "ا").replace("آ", "ا")
|
| 73 |
text = text.replace("ى", "ي").replace("ة", "ه")
|
| 74 |
-
|
| 75 |
return re.sub(r"\s+", " ", text).strip()
|
| 76 |
|
| 77 |
def row_to_text(row):
|
| 78 |
text = " ".join(str(row[c]) for c in semantic_cols if row[c])
|
| 79 |
text = normalize_text(text)
|
| 80 |
|
| 81 |
-
#
|
| 82 |
if any(x in text for x in ["cardio", "toxic", "toxicity"]):
|
| 83 |
text += " cardiotoxicity cardiac toxicity cardiotoxic effects"
|
| 84 |
|
|
@@ -112,7 +108,20 @@ def tfidf_prefilter(df, query):
|
|
| 112 |
|
| 113 |
return df.sort_values("tfidf_score", ascending=False).head(TFIDF_TOP_N)
|
| 114 |
|
| 115 |
-
# ==================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
def safe(v):
|
| 117 |
return "-" if pd.isna(v) or str(v).strip() == "" else str(v)
|
| 118 |
|
|
@@ -123,15 +132,29 @@ def results_to_html(df):
|
|
| 123 |
html = ""
|
| 124 |
for _, r in df.iterrows():
|
| 125 |
html += f"""
|
| 126 |
-
<div
|
| 127 |
-
<
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
</div>
|
| 133 |
"""
|
| 134 |
-
|
|
|
|
| 135 |
|
| 136 |
# ================== البحث ==================
|
| 137 |
def local_search_df(query, mode, source_filter):
|
|
@@ -153,10 +176,7 @@ def local_search_df(query, mode, source_filter):
|
|
| 153 |
if len(df_pref) < TFIDF_FALLBACK_MIN:
|
| 154 |
df_pref = df
|
| 155 |
|
| 156 |
-
q_emb = model.encode(
|
| 157 |
-
[normalize_text(query)],
|
| 158 |
-
normalize_embeddings=True
|
| 159 |
-
)
|
| 160 |
mat = np.vstack(df_pref["embedding"].values)
|
| 161 |
scores = util.cos_sim(q_emb, mat)[0].cpu().numpy()
|
| 162 |
|
|
|
|
| 1 |
# ================== imports ==================
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
|
|
|
| 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)
|
|
|
|
|
|
|
| 69 |
text = text.replace("أ", "ا").replace("إ", "ا").replace("آ", "ا")
|
| 70 |
text = text.replace("ى", "ي").replace("ة", "ه")
|
|
|
|
| 71 |
return re.sub(r"\s+", " ", text).strip()
|
| 72 |
|
| 73 |
def row_to_text(row):
|
| 74 |
text = " ".join(str(row[c]) for c in semantic_cols if row[c])
|
| 75 |
text = normalize_text(text)
|
| 76 |
|
| 77 |
+
# Boost للمصطلحات الطبية
|
| 78 |
if any(x in text for x in ["cardio", "toxic", "toxicity"]):
|
| 79 |
text += " cardiotoxicity cardiac toxicity cardiotoxic effects"
|
| 80 |
|
|
|
|
| 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;}
|
| 115 |
+
.card table{width:100%;border-collapse:collapse;direction:rtl;}
|
| 116 |
+
.card td{border:1px solid #ccc;padding:8px;text-align:center;vertical-align:middle;font-size:15px;}
|
| 117 |
+
.card td.label{width:30%;font-weight:bold;}
|
| 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 |
+
"""
|
| 124 |
+
|
| 125 |
def safe(v):
|
| 126 |
return "-" if pd.isna(v) or str(v).strip() == "" else str(v)
|
| 127 |
|
|
|
|
| 132 |
html = ""
|
| 133 |
for _, r in df.iterrows():
|
| 134 |
html += f"""
|
| 135 |
+
<div class="card">
|
| 136 |
+
<table>
|
| 137 |
+
<tr class="row-title">
|
| 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>
|
| 152 |
+
</tr>
|
| 153 |
+
</table>
|
| 154 |
</div>
|
| 155 |
"""
|
| 156 |
+
|
| 157 |
+
return CUSTOM_CSS + html
|
| 158 |
|
| 159 |
# ================== البحث ==================
|
| 160 |
def local_search_df(query, mode, source_filter):
|
|
|
|
| 176 |
if len(df_pref) < TFIDF_FALLBACK_MIN:
|
| 177 |
df_pref = df
|
| 178 |
|
| 179 |
+
q_emb = model.encode([normalize_text(query)], normalize_embeddings=True)
|
|
|
|
|
|
|
|
|
|
| 180 |
mat = np.vstack(df_pref["embedding"].values)
|
| 181 |
scores = util.cos_sim(q_emb, mat)[0].cpu().numpy()
|
| 182 |
|