Spaces:
Build error
Build error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,111 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import pickle
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import numpy as np
|
| 5 |
-
from sentence_transformers import SentenceTransformer, util
|
| 6 |
-
import gradio as gr
|
| 7 |
-
import gdown
|
| 8 |
-
|
| 9 |
-
# -------- إعدادات --------
|
| 10 |
-
EMB_DIR = "embeddings_cache"
|
| 11 |
-
os.makedirs(EMB_DIR, exist_ok=True)
|
| 12 |
-
|
| 13 |
-
BOOKS_FILE = "book.xlsx"
|
| 14 |
-
THESES_FILE = "theses.xlsx"
|
| 15 |
-
|
| 16 |
-
# روابط Google Drive (Public links)
|
| 17 |
-
DRIVE_LINKS = {
|
| 18 |
-
"books": "https://drive.google.com/uc?export=download&id=1FElHiASfiVLeuHWYaqd2Q5foxWRlJT-O",
|
| 19 |
-
"theses": "https://drive.google.com/uc?export=download&id=1K2Mtze6ZdvfKUsFMCOWlRBjDq-ZnJNrv",
|
| 20 |
-
"booksEmb": "https://drive.google.com/uc?export=download&id=1i2Rrr9gfcNj4QIqfQ8AX8ZlO00whbnKJ",
|
| 21 |
-
"thesesEmb": "https://drive.google.com/uc?export=download&id=1v5UJy9ePQccyquFygHisFVBHS56KhlhQ"
|
| 22 |
-
}
|
| 23 |
-
|
| 24 |
-
# -------- تحميل الملفات من Drive لو مش موجودة ----------
|
| 25 |
-
def download_from_drive(link, output):
|
| 26 |
-
if not os.path.exists(output):
|
| 27 |
-
gdown.download(link, output, quiet=False)
|
| 28 |
-
|
| 29 |
-
for key, file in zip(["books","theses"], [BOOKS_FILE, THESES_FILE]):
|
| 30 |
-
download_from_drive(DRIVE_LINKS[key], file)
|
| 31 |
-
|
| 32 |
-
# -------- تحميل البيانات ----------
|
| 33 |
-
def load_data(file):
|
| 34 |
-
df = pd.read_excel(file).fillna("غير متوافر")
|
| 35 |
-
if "Title" not in df.columns and "العنوان" in df.columns:
|
| 36 |
-
df["Title"] = df["العنوان"].astype(str)
|
| 37 |
-
elif "Title" not in df.columns:
|
| 38 |
-
df["Title"] = df.iloc[:,0].astype(str)
|
| 39 |
-
return df
|
| 40 |
-
|
| 41 |
-
books_df = load_data(BOOKS_FILE)
|
| 42 |
-
theses_df = load_data(THESES_FILE)
|
| 43 |
-
|
| 44 |
-
# -------- تحميل أو إنشاء embeddings ----------
|
| 45 |
-
MODEL_NAME = "all-MiniLM-L6-v2"
|
| 46 |
-
model = SentenceTransformer(MODEL_NAME)
|
| 47 |
-
|
| 48 |
-
def embeddings_path(name):
|
| 49 |
-
return os.path.join(EMB_DIR, f"{name}_embeddings.pkl")
|
| 50 |
-
|
| 51 |
-
def load_or_create_embeddings(df, name, drive_link):
|
| 52 |
-
path = embeddings_path(name)
|
| 53 |
-
# تحميل من drive لو موجود
|
| 54 |
-
download_from_drive(drive_link, path)
|
| 55 |
-
try:
|
| 56 |
-
with open(path, "rb") as f:
|
| 57 |
-
emb = pickle.load(f)
|
| 58 |
-
if len(emb) == len(df):
|
| 59 |
-
return emb
|
| 60 |
-
except Exception:
|
| 61 |
-
texts = df["Title"].tolist()
|
| 62 |
-
emb = model.encode(texts, convert_to_numpy=True)
|
| 63 |
-
with open(path, "wb") as f:
|
| 64 |
-
pickle.dump(emb, f)
|
| 65 |
-
return emb
|
| 66 |
-
|
| 67 |
-
books_embeddings = load_or_create_embeddings(books_df, "books", DRIVE_LINKS["booksEmb"])
|
| 68 |
-
theses_embeddings = load_or_create_embeddings(theses_df, "theses", DRIVE_LINKS["thesesEmb"])
|
| 69 |
-
|
| 70 |
-
# -------- البحث ----------
|
| 71 |
-
def search(query, category, mode):
|
| 72 |
-
if not query.strip():
|
| 73 |
-
return "⚠️ اكتب كلمة أو جملة للبحث"
|
| 74 |
-
|
| 75 |
-
if mode == "نصي":
|
| 76 |
-
if category=="Books":
|
| 77 |
-
df = books_df[books_df["Title"].str.contains(query, case=False, na=False)]
|
| 78 |
-
else:
|
| 79 |
-
df = theses_df[theses_df["Title"].str.contains(query, case=False, na=False)]
|
| 80 |
-
else:
|
| 81 |
-
q_emb = model.encode([query], convert_to_numpy=True)
|
| 82 |
-
if category=="Books":
|
| 83 |
-
scores = util.cos_sim(q_emb, books_embeddings)[0].cpu().numpy()
|
| 84 |
-
df = books_df.iloc[np.argsort(-scores)]
|
| 85 |
-
else:
|
| 86 |
-
scores = util.cos_sim(q_emb, theses_embeddings)[0].cpu().numpy()
|
| 87 |
-
df = theses_df.iloc[np.argsort(-scores)]
|
| 88 |
-
|
| 89 |
-
if df.empty:
|
| 90 |
-
return "❌ لم يتم العثور على نتائج"
|
| 91 |
-
|
| 92 |
-
html = "<table border=1 style='border-collapse:collapse;width:100%;'>"
|
| 93 |
-
html += "<tr>" + "".join([f"<th>{col}</th>" for col in df.columns]) + "</tr>"
|
| 94 |
-
for _, row in df.iterrows():
|
| 95 |
-
html += "<tr>" + "".join([f"<td>{val}</td>" for val in row.values]) + "</tr>"
|
| 96 |
-
html += "</table>"
|
| 97 |
-
return html
|
| 98 |
-
|
| 99 |
-
# -------- واجهة Gradio ----------
|
| 100 |
-
iface = gr.Interface(
|
| 101 |
-
fn=search,
|
| 102 |
-
inputs=[
|
| 103 |
-
gr.Textbox(label="اكتب كلمة البحث"),
|
| 104 |
-
gr.Dropdown(["Books","Theses"], label="الفئة"),
|
| 105 |
-
gr.Radio(["نصي","دلالي"], label="نوع البحث")
|
| 106 |
-
],
|
| 107 |
-
outputs="html",
|
| 108 |
-
title="البحث في المكتبة الرقمية"
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|