aelsaeed commited on
Commit
f39c276
·
verified ·
1 Parent(s): 947db93

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -83
app.py DELETED
@@ -1,83 +0,0 @@
1
- import os
2
- import pandas as pd
3
- import numpy as np
4
- from sentence_transformers import SentenceTransformer, util
5
- import gradio as gr
6
- import gdown
7
-
8
- # --------- إعدادات الملفات ----------
9
- DRIVE_LINKS = {
10
- "books": "https://drive.google.com/uc?export=download&id=1FElHiASfiVLeuHWYaqd2Q5foxWRlJT-O",
11
- "theses": "https://drive.google.com/uc?export=download&id=1K2Mtze6ZdvfKUsFMCOWlRBjDq-ZnJNrv"
12
- }
13
-
14
- BOOKS_FILE = "book.xlsx"
15
- THESES_FILE = "theses.xlsx"
16
-
17
- # --------- تحميل الملفات من Drive لو مش موجودة ----------
18
- def download_from_drive(link, output):
19
- if not os.path.exists(output):
20
- gdown.download(link, output, quiet=False)
21
-
22
- download_from_drive(DRIVE_LINKS["books"], BOOKS_FILE)
23
- download_from_drive(DRIVE_LINKS["theses"], THESES_FILE)
24
-
25
- # --------- قراءة البيانات ----------
26
- def load_data(file):
27
- df = pd.read_excel(file).fillna("غير متوافر")
28
- if "Title" not in df.columns and "العنوان" in df.columns:
29
- df["Title"] = df["العنوان"].astype(str)
30
- elif "Title" not in df.columns:
31
- df["Title"] = df.iloc[:,0].astype(str)
32
- return df
33
-
34
- books_df = load_data(BOOKS_FILE)
35
- theses_df = load_data(THESES_FILE)
36
-
37
- # --------- إنشاء نموذج Semantic ----------
38
- MODEL_NAME = "all-MiniLM-L6-v2"
39
- model = SentenceTransformer(MODEL_NAME)
40
-
41
- # --------- إنشاء Embeddings مرة واحدة ----------
42
- books_embeddings = model.encode(books_df["Title"].tolist(), convert_to_numpy=True)
43
- theses_embeddings = model.encode(theses_df["Title"].tolist(), convert_to_numpy=True)
44
-
45
- # --------- البحث ----------
46
- def search(query, category, mode):
47
- if not query.strip():
48
- return "⚠️ اكتب كلمة أو جملة للبحث"
49
-
50
- if mode == "نصي":
51
- df = books_df if category=="Books" else theses_df
52
- results = df[df["Title"].str.contains(query, case=False, na=False)]
53
- else:
54
- q_emb = model.encode([query], convert_to_numpy=True)
55
- df = books_df if category=="Books" else theses_df
56
- emb = books_embeddings if category=="Books" else theses_embeddings
57
- scores = util.cos_sim(q_emb, emb)[0].cpu().numpy()
58
- idx = np.argsort(-scores)
59
- results = df.iloc[idx]
60
-
61
- if results.empty:
62
- return "❌ لم يتم العثور على نتائج"
63
-
64
- html = "<table border=1 style='border-collapse:collapse;width:100%;'>"
65
- html += "<tr>" + "".join([f"<th>{col}</th>" for col in results.columns]) + "</tr>"
66
- for _, row in results.iterrows():
67
- html += "<tr>" + "".join([f"<td>{val}</td>" for val in row.values]) + "</tr>"
68
- html += "</table>"
69
- return html
70
-
71
- # --------- واجهة Gradio ----------
72
- iface = gr.Interface(
73
- fn=search,
74
- inputs=[
75
- gr.Textbox(label="اكتب كلمة البحث"),
76
- gr.Dropdown(["Books","Theses"], label="الفئة"),
77
- gr.Radio(["نصي","دلالي"], label="نوع البحث")
78
- ],
79
- outputs="html",
80
- title="البحث في المكتبة الرقمية"
81
- )
82
-
83
- iface.launch()