Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,59 @@
|
|
| 1 |
import joblib
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# =========================
|
| 6 |
# تحميل البيانات
|
| 7 |
# =========================
|
| 8 |
books = joblib.load("books.pkl")
|
| 9 |
-
theses = joblib.load("
|
|
|
|
| 10 |
books_emb = joblib.load("books_embeddings.pkl")
|
| 11 |
theses_emb = joblib.load("theses_embeddings.pkl")
|
| 12 |
|
| 13 |
# =========================
|
| 14 |
-
# م
|
| 15 |
# =========================
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
def search_library(query_embedding, top_k=5):
|
| 19 |
-
# دمج كل Embeddings
|
| 20 |
-
all_emb = np.vstack([books_emb, theses_emb])
|
| 21 |
-
similarity = cosine_similarity([query_embedding], all_emb)[0]
|
| 22 |
-
top_indices = similarity.argsort()[-top_k:][::-1]
|
| 23 |
-
|
| 24 |
results = []
|
| 25 |
-
for
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
results.append(theses[idx - len(books)])
|
| 30 |
-
return results
|
| 31 |
|
| 32 |
# =========================
|
| 33 |
# واجهة Gradio
|
| 34 |
# =========================
|
| 35 |
-
def search_interface(query_embedding):
|
| 36 |
-
return search_library(query_embedding)
|
| 37 |
-
|
| 38 |
demo = gr.Interface(
|
| 39 |
-
fn=
|
| 40 |
-
inputs=gr.
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
)
|
| 45 |
|
| 46 |
demo.launch()
|
|
|
|
| 1 |
import joblib
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 6 |
|
| 7 |
# =========================
|
| 8 |
# تحميل البيانات
|
| 9 |
# =========================
|
| 10 |
books = joblib.load("books.pkl")
|
| 11 |
+
theses = joblib.load("theses.pkl")
|
| 12 |
+
|
| 13 |
books_emb = joblib.load("books_embeddings.pkl")
|
| 14 |
theses_emb = joblib.load("theses_embeddings.pkl")
|
| 15 |
|
| 16 |
# =========================
|
| 17 |
+
# تحميل الموديل المحلي
|
| 18 |
# =========================
|
| 19 |
+
model = SentenceTransformer("AI_Library_Model")
|
| 20 |
+
|
| 21 |
+
# =========================
|
| 22 |
+
# دمج البيانات
|
| 23 |
+
# =========================
|
| 24 |
+
all_embeddings = np.vstack([books_emb, theses_emb])
|
| 25 |
+
all_texts = books + theses
|
| 26 |
+
|
| 27 |
+
# =========================
|
| 28 |
+
# دالة البحث
|
| 29 |
+
# =========================
|
| 30 |
+
def search_library(query, top_k=5):
|
| 31 |
+
if not query.strip():
|
| 32 |
+
return "من فضلك اكتب سؤالًا"
|
| 33 |
+
|
| 34 |
+
query_emb = model.encode(query)
|
| 35 |
+
|
| 36 |
+
scores = cosine_similarity([query_emb], all_embeddings)[0]
|
| 37 |
+
top_indices = scores.argsort()[-top_k:][::-1]
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
results = []
|
| 40 |
+
for i in top_indices:
|
| 41 |
+
results.append(f"- {all_texts[i]}")
|
| 42 |
+
|
| 43 |
+
return "\n".join(results)
|
|
|
|
|
|
|
| 44 |
|
| 45 |
# =========================
|
| 46 |
# واجهة Gradio
|
| 47 |
# =========================
|
|
|
|
|
|
|
|
|
|
| 48 |
demo = gr.Interface(
|
| 49 |
+
fn=search_library,
|
| 50 |
+
inputs=gr.Textbox(
|
| 51 |
+
label="اكتب سؤالك",
|
| 52 |
+
placeholder="مثال: organic chemistry synthesis"
|
| 53 |
+
),
|
| 54 |
+
outputs=gr.Textbox(label="النتائج"),
|
| 55 |
+
title="📚 AI Library Explorer",
|
| 56 |
+
description="بحث ذكي في الكتب والرسائل العلمية (عربي / إنجليزي)"
|
| 57 |
)
|
| 58 |
|
| 59 |
demo.launch()
|