aelsaeed commited on
Commit
7d89609
·
verified ·
1 Parent(s): 55f3dc6

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -90
app.py DELETED
@@ -1,90 +0,0 @@
1
-
2
- import os
3
- import pandas as pd
4
- import numpy as np
5
- import requests
6
- import gradio as gr
7
- import gdown
8
- import pickle
9
-
10
- BOOKS_FILE = "book.xlsx"
11
- THESES_FILE = "theses.xlsx"
12
-
13
- # روابط الملفات على Google Drive
14
- DRIVE_LINKS = {
15
- "books": "1FElHiASfiVLeuHWYaqd2Q5foxWRlJT-O",
16
- "theses": "1K2Mtze6ZdvfKUsFMCOWlRBjDq-ZnJNrv"
17
- }
18
-
19
- def download_from_drive(file_id, output):
20
- url = f"https://drive.google.com/uc?export=download&id={file_id}"
21
- gdown.download(url, output, quiet=True)
22
-
23
- # تنزيل الملفات إذا مش موجودة
24
- if not os.path.exists(BOOKS_FILE):
25
- download_from_drive(DRIVE_LINKS["books"], BOOKS_FILE)
26
-
27
- if not os.path.exists(THESES_FILE):
28
- download_from_drive(DRIVE_LINKS["theses"], THESES_FILE)
29
-
30
- # قراءة البيانات
31
- def load_data(file):
32
- df = pd.read_excel(file).fillna("غير متوافر")
33
- if "Title" not in df.columns and "العنوان" in df.columns:
34
- df["Title"] = df["العنوان"].astype(str)
35
- elif "Title" not in df.columns:
36
- df["Title"] = df.iloc[:,0].astype(str)
37
- return df
38
-
39
- books_df = load_data(BOOKS_FILE)
40
- theses_df = load_data(THESES_FILE)
41
-
42
- API_TOKEN = os.environ.get("HF_TOKEN")
43
- API_URL = "https://api-inference.huggingface.co/models/aelsaeed/all-MiniLM-L6-v2-api"
44
- HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
45
-
46
- def get_embedding(text):
47
- response = requests.post(API_URL, headers=HEADERS, json={"inputs": [text]})
48
- return np.array(response.json()[0])
49
-
50
- def search(query, category, mode):
51
- if not query.strip():
52
- return "⚠️ اكتب كلمة أو جملة للبحث"
53
- if mode == "نصي":
54
- df = books_df if category=="Books" else theses_df
55
- results = df[df["Title"].str.contains(query, case=False, na=False)]
56
- else:
57
- df = books_df if category=="Books" else theses_df
58
- emb_cache_file = f"{category}_embeddings.pkl"
59
- if os.path.exists(emb_cache_file):
60
- with open(emb_cache_file,"rb") as f:
61
- embeddings = pickle.load(f)
62
- else:
63
- embeddings = np.array([get_embedding(t) for t in df["Title"].tolist()])
64
- with open(emb_cache_file,"wb") as f:
65
- pickle.dump(embeddings,f)
66
- query_emb = get_embedding(query)
67
- scores = np.dot(embeddings, query_emb) / (np.linalg.norm(embeddings,axis=1)*np.linalg.norm(query_emb))
68
- idx = np.argsort(-scores)
69
- results = df.iloc[idx]
70
- if results.empty:
71
- return "❌ لم يتم العثور على نتائج"
72
- html = "<table border=1 style='border-collapse:collapse;width:100%;'>"
73
- html += "<tr>" + "".join([f"<th>{col}</th>" for col in results.columns]) + "</tr>"
74
- for _, row in results.iterrows():
75
- html += "<tr>" + "".join([f"<td>{val}</td>" for val in row.values]) + "</tr>"
76
- html += "</table>"
77
- return html
78
-
79
- iface = gr.Interface(
80
- fn=search,
81
- inputs=[
82
- gr.Textbox(label="اكتب كلمة البحث"),
83
- gr.Dropdown(["Books","Theses"], label="الفئة"),
84
- gr.Radio(["نصي","دلالي"], label="نوع البحث")
85
- ],
86
- outputs="html",
87
- title="البحث في المكتبة الرقمية"
88
- )
89
-
90
- iface.launch()