Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
import gradio as gr
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
-
import os,
|
| 6 |
from sentence_transformers import SentenceTransformer, util
|
| 7 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 8 |
from sklearn.metrics.pairwise import cosine_similarity
|
|
@@ -38,7 +38,7 @@ theses_df = pd.read_excel(THESES_FILE).fillna("")
|
|
| 38 |
books_df["ุงูู
ุตุฏุฑ"] = "ูุชุงุจ"
|
| 39 |
theses_df["ุงูู
ุตุฏุฑ"] = "ุฑุณุงูุฉ"
|
| 40 |
|
| 41 |
-
#
|
| 42 |
for df in [books_df, theses_df]:
|
| 43 |
if "ุงูู
ููุน ุนูู ุงูุฑู" in df.columns:
|
| 44 |
df["ุงูู
ููุน ุนูู ุงูุฑู"] = (
|
|
@@ -84,7 +84,9 @@ def normalize_text(text):
|
|
| 84 |
semantic_cols = detect_semantic_columns(library_df)
|
| 85 |
|
| 86 |
def row_to_text(row):
|
| 87 |
-
return normalize_text(
|
|
|
|
|
|
|
| 88 |
|
| 89 |
embeddings = model.encode(
|
| 90 |
library_df.apply(row_to_text, axis=1).tolist(),
|
|
@@ -97,7 +99,6 @@ library_df["embedding"] = list(embeddings)
|
|
| 97 |
# ================== TF-IDF ==================
|
| 98 |
def tfidf_prefilter(df, query):
|
| 99 |
texts = df.apply(row_to_text, axis=1).tolist()
|
| 100 |
-
|
| 101 |
vec = TfidfVectorizer(ngram_range=(1, 2), max_features=20000)
|
| 102 |
mat = vec.fit_transform(texts)
|
| 103 |
q_vec = vec.transform([normalize_text(query)])
|
|
@@ -108,7 +109,7 @@ def tfidf_prefilter(df, query):
|
|
| 108 |
|
| 109 |
return df.sort_values("tfidf_score", ascending=False).head(TFIDF_TOP_N)
|
| 110 |
|
| 111 |
-
# ==================
|
| 112 |
CUSTOM_CSS = """
|
| 113 |
<style>
|
| 114 |
.card{
|
|
@@ -118,20 +119,52 @@ CUSTOM_CSS = """
|
|
| 118 |
margin:12px 0;
|
| 119 |
background:#fafafa;
|
| 120 |
}
|
|
|
|
| 121 |
.card table{
|
| 122 |
width:100%;
|
| 123 |
border-collapse:collapse;
|
| 124 |
direction:rtl;
|
| 125 |
}
|
|
|
|
| 126 |
.card td{
|
| 127 |
border:1px solid #ccc;
|
| 128 |
-
padding:
|
|
|
|
|
|
|
|
|
|
| 129 |
}
|
|
|
|
| 130 |
.card td.label{
|
| 131 |
-
background:#f0f0f0;
|
| 132 |
width:30%;
|
| 133 |
font-weight:bold;
|
| 134 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
</style>
|
| 136 |
"""
|
| 137 |
|
|
@@ -147,10 +180,22 @@ def results_to_html(df):
|
|
| 147 |
html += f"""
|
| 148 |
<div class="card">
|
| 149 |
<table>
|
| 150 |
-
<tr
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
</table>
|
| 155 |
</div>
|
| 156 |
"""
|
|
@@ -179,7 +224,6 @@ def local_search_df(query, mode, source_filter, year_range):
|
|
| 179 |
df["ุงูู
ุคูู"].str.contains(query, case=False, na=False)
|
| 180 |
)
|
| 181 |
df = df[mask].head(TOP_K)
|
| 182 |
-
|
| 183 |
else:
|
| 184 |
df_pref = tfidf_prefilter(df, query)
|
| 185 |
if len(df_pref) < TFIDF_FALLBACK_MIN:
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
+
import os, tempfile, re
|
| 6 |
from sentence_transformers import SentenceTransformer, util
|
| 7 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 8 |
from sklearn.metrics.pairwise import cosine_similarity
|
|
|
|
| 38 |
books_df["ุงูู
ุตุฏุฑ"] = "ูุชุงุจ"
|
| 39 |
theses_df["ุงูู
ุตุฏุฑ"] = "ุฑุณุงูุฉ"
|
| 40 |
|
| 41 |
+
# ุชุซุจูุช ุฑูู
ุงูุฑู ููุต
|
| 42 |
for df in [books_df, theses_df]:
|
| 43 |
if "ุงูู
ููุน ุนูู ุงูุฑู" in df.columns:
|
| 44 |
df["ุงูู
ููุน ุนูู ุงูุฑู"] = (
|
|
|
|
| 84 |
semantic_cols = detect_semantic_columns(library_df)
|
| 85 |
|
| 86 |
def row_to_text(row):
|
| 87 |
+
return normalize_text(
|
| 88 |
+
" ".join(str(row[c]) for c in semantic_cols if row[c])
|
| 89 |
+
)
|
| 90 |
|
| 91 |
embeddings = model.encode(
|
| 92 |
library_df.apply(row_to_text, axis=1).tolist(),
|
|
|
|
| 99 |
# ================== TF-IDF ==================
|
| 100 |
def tfidf_prefilter(df, query):
|
| 101 |
texts = df.apply(row_to_text, axis=1).tolist()
|
|
|
|
| 102 |
vec = TfidfVectorizer(ngram_range=(1, 2), max_features=20000)
|
| 103 |
mat = vec.fit_transform(texts)
|
| 104 |
q_vec = vec.transform([normalize_text(query)])
|
|
|
|
| 109 |
|
| 110 |
return df.sort_values("tfidf_score", ascending=False).head(TFIDF_TOP_N)
|
| 111 |
|
| 112 |
+
# ================== CSS ==================
|
| 113 |
CUSTOM_CSS = """
|
| 114 |
<style>
|
| 115 |
.card{
|
|
|
|
| 119 |
margin:12px 0;
|
| 120 |
background:#fafafa;
|
| 121 |
}
|
| 122 |
+
|
| 123 |
.card table{
|
| 124 |
width:100%;
|
| 125 |
border-collapse:collapse;
|
| 126 |
direction:rtl;
|
| 127 |
}
|
| 128 |
+
|
| 129 |
.card td{
|
| 130 |
border:1px solid #ccc;
|
| 131 |
+
padding:8px;
|
| 132 |
+
text-align:center;
|
| 133 |
+
vertical-align:middle;
|
| 134 |
+
font-size:15px;
|
| 135 |
}
|
| 136 |
+
|
| 137 |
.card td.label{
|
|
|
|
| 138 |
width:30%;
|
| 139 |
font-weight:bold;
|
| 140 |
}
|
| 141 |
+
|
| 142 |
+
/* ุงูุนููุงู */
|
| 143 |
+
.row-title td{
|
| 144 |
+
background:#e3f2fd;
|
| 145 |
+
color:#0d47a1;
|
| 146 |
+
font-weight:bold;
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
/* ุงูู
ุคูู */
|
| 150 |
+
.row-author td{
|
| 151 |
+
background:#fce4ec;
|
| 152 |
+
color:#880e4f;
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
/* ุณูุฉ ุงููุดุฑ */
|
| 156 |
+
.row-year td{
|
| 157 |
+
background:#e8f5e9;
|
| 158 |
+
color:#1b5e20;
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
/* ุงูู
ููุน ุนูู ุงูุฑู */
|
| 162 |
+
.row-shelf td{
|
| 163 |
+
background:#fff8e1;
|
| 164 |
+
color:#e65100;
|
| 165 |
+
font-weight:bold;
|
| 166 |
+
font-size:16px;
|
| 167 |
+
}
|
| 168 |
</style>
|
| 169 |
"""
|
| 170 |
|
|
|
|
| 180 |
html += f"""
|
| 181 |
<div class="card">
|
| 182 |
<table>
|
| 183 |
+
<tr class="row-title">
|
| 184 |
+
<td class="label">ุงูุนููุงู</td>
|
| 185 |
+
<td>{safe(r.get("ุงูุนููุงู"))}</td>
|
| 186 |
+
</tr>
|
| 187 |
+
<tr class="row-author">
|
| 188 |
+
<td class="label">ุงูู
ุคูู</td>
|
| 189 |
+
<td>{safe(r.get("ุงูู
ุคูู"))}</td>
|
| 190 |
+
</tr>
|
| 191 |
+
<tr class="row-year">
|
| 192 |
+
<td class="label">ุณูุฉ ุงููุดุฑ</td>
|
| 193 |
+
<td>{safe(r.get("ุณูุฉ ุงููุดุฑ"))}</td>
|
| 194 |
+
</tr>
|
| 195 |
+
<tr class="row-shelf">
|
| 196 |
+
<td class="label">ุงูู
ููุน ุนูู ุงูุฑู</td>
|
| 197 |
+
<td>{safe(r.get("ุงูู
ููุน ุนูู ุงูุฑู"))}</td>
|
| 198 |
+
</tr>
|
| 199 |
</table>
|
| 200 |
</div>
|
| 201 |
"""
|
|
|
|
| 224 |
df["ุงูู
ุคูู"].str.contains(query, case=False, na=False)
|
| 225 |
)
|
| 226 |
df = df[mask].head(TOP_K)
|
|
|
|
| 227 |
else:
|
| 228 |
df_pref = tfidf_prefilter(df, query)
|
| 229 |
if len(df_pref) < TFIDF_FALLBACK_MIN:
|