Spaces:
Runtime error
Runtime error
Upload src/dashboard/app_standalone.py with huggingface_hub
Browse files
src/dashboard/app_standalone.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import sqlite3
|
| 3 |
+
import faiss
|
| 4 |
+
import numpy as np
|
| 5 |
+
from sentence_transformers import SentenceTransformer
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
import os
|
| 8 |
+
import time
|
| 9 |
+
|
| 10 |
+
st.set_page_config(page_title="PaperLens", page_icon="🔬", layout="wide")
|
| 11 |
+
|
| 12 |
+
@st.cache_resource
|
| 13 |
+
def load_resources():
|
| 14 |
+
os.makedirs("data", exist_ok=True)
|
| 15 |
+
for f in ["papers.db", "faiss_index.bin", "embeddings.npy"]:
|
| 16 |
+
if not os.path.exists(f"data/{f}"):
|
| 17 |
+
hf_hub_download(
|
| 18 |
+
repo_id="vengen9840/paperlens-data",
|
| 19 |
+
filename=f,
|
| 20 |
+
repo_type="dataset",
|
| 21 |
+
local_dir="data"
|
| 22 |
+
)
|
| 23 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 24 |
+
index = faiss.read_index("data/faiss_index.bin")
|
| 25 |
+
id_map = np.load("data/embeddings.npy", allow_pickle=True).item() if os.path.exists("data/embeddings.npy") else None
|
| 26 |
+
conn = sqlite3.connect("data/papers.db", check_same_thread=False)
|
| 27 |
+
return model, index, conn
|
| 28 |
+
|
| 29 |
+
def search_papers(query, model, index, conn, top_k=10):
|
| 30 |
+
vec = model.encode([query]).astype("float32")
|
| 31 |
+
faiss.normalize_L2(vec)
|
| 32 |
+
scores, indices = index.search(vec, top_k)
|
| 33 |
+
cur = conn.cursor()
|
| 34 |
+
results = []
|
| 35 |
+
for score, idx in zip(scores[0], indices[0]):
|
| 36 |
+
cur.execute("SELECT id, title, abstract, authors, published FROM papers WHERE rowid=?", (int(idx)+1,))
|
| 37 |
+
row = cur.fetchone()
|
| 38 |
+
if row:
|
| 39 |
+
results.append({
|
| 40 |
+
"id": row[0], "title": row[1], "abstract": row[2],
|
| 41 |
+
"authors": row[3], "published": row[4], "score": float(score)
|
| 42 |
+
})
|
| 43 |
+
return results
|
| 44 |
+
|
| 45 |
+
# UI
|
| 46 |
+
st.title("🔬 PaperLens")
|
| 47 |
+
st.caption("Semantic search over 1,854 arXiv papers (cs.AI · cs.LG · cs.CL)")
|
| 48 |
+
|
| 49 |
+
model, index, conn = load_resources()
|
| 50 |
+
|
| 51 |
+
query = st.text_input("Search papers...", placeholder="e.g. attention mechanism transformers")
|
| 52 |
+
|
| 53 |
+
if query:
|
| 54 |
+
start = time.time()
|
| 55 |
+
results = search_papers(query, model, index, conn)
|
| 56 |
+
latency = (time.time() - start) * 1000
|
| 57 |
+
st.caption(f"Found {len(results)} results in {latency:.1f}ms")
|
| 58 |
+
for r in results:
|
| 59 |
+
with st.expander(f"**{r['title']}** — score: {r['score']:.3f}"):
|
| 60 |
+
st.write(f"**Authors:** {r['authors']}")
|
| 61 |
+
st.write(f"**Published:** {r['published']}")
|
| 62 |
+
st.write(r['abstract'][:500] + "...")
|
| 63 |
+
st.link_button("View on arXiv", f"https://arxiv.org/abs/{r['id']}")
|