File size: 3,252 Bytes
66bea79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# =========================
# تثبيت المكتبات المطلوبة
# =========================
!pip install -q gradio pandas numpy gdown sentence-transformers

import gdown
import pickle
import pandas as pd
import numpy as np
from sentence_transformers import SentenceTransformer, util
import gradio as gr
import os
import zipfile

# =========================
# روابط الملفات على Google Drive
# =========================
files = {
    "books": "https://drive.google.com/uc?id=1v5UJy9ePQccyquFygHisFVBHS56KhlhQ",
    "theses": "https://drive.google.com/uc?id=1i2Rrr9gfcNj4QIqfQ8AX8ZlO00whbnKJ",
    "books_embeddings": "https://drive.google.com/uc?id=1uH5PVUGXYENeqGUaikQ55wtDVcU_04qC",
    "theses_embeddings": "https://drive.google.com/uc?id=1cYZtgYPzoWxePOW9B3bIdqANOI890rs2",
    "model_zip": "https://drive.google.com/uc?id=18D4xtDTKCWeGaZ3lsYW7_D79xo95daDL"
}

# =========================
# تحميل الملفات
# =========================
for name, url in files.items():
    output = f"{name}.pkl" if name != "model_zip" else "model.zip"
    if not os.path.exists(output):
        gdown.download(url, output, quiet=False)

# =========================
# فك ضغط الموديل
# =========================
if not os.path.exists("AI_Library_Model"):
    with zipfile.ZipFile("model.zip", "r") as zip_ref:
        zip_ref.extractall("AI_Library_Model")

# =========================
# قراءة البيانات وEmbeddings
# =========================
with open("books.pkl", "rb") as f:
    books = pickle.load(f)

with open("theses.pkl", "rb") as f:
    theses = pickle.load(f)

books["type"] = "Book"
theses["type"] = "Thesis"
library = pd.concat([books, theses], ignore_index=True)

with open("books_embeddings.pkl", "rb") as f:
    books_emb = pickle.load(f)

with open("theses_embeddings.pkl", "rb") as f:
    theses_emb = pickle.load(f)

library_embeddings = np.vstack([books_emb, theses_emb])

# =========================
# تحميل موديل MiniLM من الفولدر المحلي
# =========================
model = SentenceTransformer("AI_Library_Model")

# =========================
# دالة البحث
# =========================
def search_library(query, source_type):
    filtered = library[library["type"] == source_type]
    filtered_emb = library_embeddings[filtered.index]

    query_emb = model.encode(query)
    scores = util.cos_sim(query_emb, filtered_emb)[0]
    top_idx = np.argsort(scores.cpu().numpy())[::-1][:5]

    results = filtered.iloc[top_idx][["title","author","year","field"]]
    return results

# =========================
# واجهة Gradio تاب واحدة
# =========================
with gr.Blocks() as demo:
    gr.Markdown("## مكتبة AI Explorer - البحث الحر المحلي")
    with gr.Row():
        query = gr.Textbox(label="اكتب سؤال البحث")
        source = gr.Dropdown(choices=["Book", "Thesis"], label="نوع المصدر")
        btn = gr.Button("بحث")
    output = gr.Dataframe(headers=["العنوان","المؤلف","السنة","المجال"])
    btn.click(search_library, inputs=[query, source], outputs=output)

demo.launch()