aelsaeed commited on
Commit
66bea79
·
verified ·
1 Parent(s): 4847263

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # =========================
2
+ # تثبيت المكتبات المطلوبة
3
+ # =========================
4
+ !pip install -q gradio pandas numpy gdown sentence-transformers
5
+
6
+ import gdown
7
+ import pickle
8
+ import pandas as pd
9
+ import numpy as np
10
+ from sentence_transformers import SentenceTransformer, util
11
+ import gradio as gr
12
+ import os
13
+ import zipfile
14
+
15
+ # =========================
16
+ # روابط الملفات على Google Drive
17
+ # =========================
18
+ files = {
19
+ "books": "https://drive.google.com/uc?id=1v5UJy9ePQccyquFygHisFVBHS56KhlhQ",
20
+ "theses": "https://drive.google.com/uc?id=1i2Rrr9gfcNj4QIqfQ8AX8ZlO00whbnKJ",
21
+ "books_embeddings": "https://drive.google.com/uc?id=1uH5PVUGXYENeqGUaikQ55wtDVcU_04qC",
22
+ "theses_embeddings": "https://drive.google.com/uc?id=1cYZtgYPzoWxePOW9B3bIdqANOI890rs2",
23
+ "model_zip": "https://drive.google.com/uc?id=18D4xtDTKCWeGaZ3lsYW7_D79xo95daDL"
24
+ }
25
+
26
+ # =========================
27
+ # تحميل الملفات
28
+ # =========================
29
+ for name, url in files.items():
30
+ output = f"{name}.pkl" if name != "model_zip" else "model.zip"
31
+ if not os.path.exists(output):
32
+ gdown.download(url, output, quiet=False)
33
+
34
+ # =========================
35
+ # فك ضغط الموديل
36
+ # =========================
37
+ if not os.path.exists("AI_Library_Model"):
38
+ with zipfile.ZipFile("model.zip", "r") as zip_ref:
39
+ zip_ref.extractall("AI_Library_Model")
40
+
41
+ # =========================
42
+ # قراءة البيانات وEmbeddings
43
+ # =========================
44
+ with open("books.pkl", "rb") as f:
45
+ books = pickle.load(f)
46
+
47
+ with open("theses.pkl", "rb") as f:
48
+ theses = pickle.load(f)
49
+
50
+ books["type"] = "Book"
51
+ theses["type"] = "Thesis"
52
+ library = pd.concat([books, theses], ignore_index=True)
53
+
54
+ with open("books_embeddings.pkl", "rb") as f:
55
+ books_emb = pickle.load(f)
56
+
57
+ with open("theses_embeddings.pkl", "rb") as f:
58
+ theses_emb = pickle.load(f)
59
+
60
+ library_embeddings = np.vstack([books_emb, theses_emb])
61
+
62
+ # =========================
63
+ # تحميل موديل MiniLM من الفولدر المحلي
64
+ # =========================
65
+ model = SentenceTransformer("AI_Library_Model")
66
+
67
+ # =========================
68
+ # دالة البحث
69
+ # =========================
70
+ def search_library(query, source_type):
71
+ filtered = library[library["type"] == source_type]
72
+ filtered_emb = library_embeddings[filtered.index]
73
+
74
+ query_emb = model.encode(query)
75
+ scores = util.cos_sim(query_emb, filtered_emb)[0]
76
+ top_idx = np.argsort(scores.cpu().numpy())[::-1][:5]
77
+
78
+ results = filtered.iloc[top_idx][["title","author","year","field"]]
79
+ return results
80
+
81
+ # =========================
82
+ # واجهة Gradio تاب واحدة
83
+ # =========================
84
+ with gr.Blocks() as demo:
85
+ gr.Markdown("## مكتبة AI Explorer - البحث الحر المحلي")
86
+ with gr.Row():
87
+ query = gr.Textbox(label="اكتب سؤال البحث")
88
+ source = gr.Dropdown(choices=["Book", "Thesis"], label="نوع المصدر")
89
+ btn = gr.Button("بحث")
90
+ output = gr.Dataframe(headers=["العنوان","المؤلف","السنة","المجال"])
91
+ btn.click(search_library, inputs=[query, source], outputs=output)
92
+
93
+ demo.launch()