Spaces:
Running
Running
Initial deploy: SciFact multilingual semantic search
Browse filesFastAPI app with ChromaDB + multilingual-e5-small over 5,183 SciFact
abstracts. Glassmorphism frontend with cross-lingual search (EN/FR/DE/ES).
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- .gitattributes +1 -0
- .gitignore +4 -0
- Dockerfile +7 -0
- README.md +4 -6
- app.py +76 -0
- data/chroma_db/chroma.sqlite3 +3 -0
- data/chroma_db/d0040b94-292a-45b3-9e5b-252ec249af2f/data_level0.bin +3 -0
- data/chroma_db/d0040b94-292a-45b3-9e5b-252ec249af2f/header.bin +3 -0
- data/chroma_db/d0040b94-292a-45b3-9e5b-252ec249af2f/index_metadata.pickle +3 -0
- data/chroma_db/d0040b94-292a-45b3-9e5b-252ec249af2f/length.bin +3 -0
- data/chroma_db/d0040b94-292a-45b3-9e5b-252ec249af2f/link_lists.bin +3 -0
- precompute.py +91 -0
- requirements.txt +8 -0
- static/index.html +284 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
*.sqlite3 filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.pyc
|
| 3 |
+
.DS_Store
|
| 4 |
+
*.egg-info/
|
Dockerfile
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
WORKDIR /app
|
| 3 |
+
COPY requirements.txt .
|
| 4 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 5 |
+
COPY . .
|
| 6 |
+
EXPOSE 7860
|
| 7 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
|
@@ -1,10 +1,8 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
colorTo: purple
|
| 6 |
sdk: docker
|
| 7 |
-
|
| 8 |
---
|
| 9 |
-
|
| 10 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: SciFact Multilingual Semantic Search
|
| 3 |
+
emoji: "\U0001F52C"
|
| 4 |
+
colorFrom: indigo
|
| 5 |
colorTo: purple
|
| 6 |
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
---
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
FastAPI Semantic Search over SciFact abstracts.
|
| 3 |
+
|
| 4 |
+
Uses precomputed ChromaDB + multilingual-e5-small for query encoding.
|
| 5 |
+
uvicorn app:app --host 0.0.0.0 --port 7860
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import os
|
| 9 |
+
from fastapi import FastAPI, Query
|
| 10 |
+
from fastapi.responses import FileResponse
|
| 11 |
+
from fastapi.staticfiles import StaticFiles
|
| 12 |
+
from sentence_transformers import SentenceTransformer
|
| 13 |
+
import chromadb
|
| 14 |
+
|
| 15 |
+
MODEL_NAME = "intfloat/multilingual-e5-small"
|
| 16 |
+
CHROMA_PATH = os.path.join(os.path.dirname(__file__), "data", "chroma_db")
|
| 17 |
+
COLLECTION_NAME = "scifact"
|
| 18 |
+
|
| 19 |
+
app = FastAPI(title="SciFact Multilingual Semantic Search")
|
| 20 |
+
|
| 21 |
+
model: SentenceTransformer = None
|
| 22 |
+
collection: chromadb.Collection = None
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@app.on_event("startup")
|
| 26 |
+
def startup():
|
| 27 |
+
global model, collection
|
| 28 |
+
print(f"Loading model: {MODEL_NAME}")
|
| 29 |
+
model = SentenceTransformer(MODEL_NAME)
|
| 30 |
+
print(f"Loading ChromaDB from: {CHROMA_PATH}")
|
| 31 |
+
client = chromadb.PersistentClient(path=CHROMA_PATH)
|
| 32 |
+
collection = client.get_collection(COLLECTION_NAME)
|
| 33 |
+
print(f"Collection '{COLLECTION_NAME}': {collection.count()} documents ready.")
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
@app.get("/")
|
| 37 |
+
def index():
|
| 38 |
+
return FileResponse(
|
| 39 |
+
os.path.join(os.path.dirname(__file__), "static", "index.html")
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
@app.get("/search")
|
| 44 |
+
def search(q: str = Query(..., min_length=1), top_k: int = Query(5, ge=1, le=20)):
|
| 45 |
+
query_embedding = model.encode(
|
| 46 |
+
[f"query: {q.strip()}"],
|
| 47 |
+
normalize_embeddings=True,
|
| 48 |
+
).tolist()
|
| 49 |
+
|
| 50 |
+
results = collection.query(
|
| 51 |
+
query_embeddings=query_embedding,
|
| 52 |
+
n_results=top_k,
|
| 53 |
+
include=["metadatas", "distances", "documents"],
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
items = []
|
| 57 |
+
for i, (meta, dist, doc) in enumerate(
|
| 58 |
+
zip(
|
| 59 |
+
results["metadatas"][0],
|
| 60 |
+
results["distances"][0],
|
| 61 |
+
results["documents"][0],
|
| 62 |
+
)
|
| 63 |
+
):
|
| 64 |
+
items.append(
|
| 65 |
+
{
|
| 66 |
+
"rank": i + 1,
|
| 67 |
+
"score": round(1 - dist, 4),
|
| 68 |
+
"title": meta.get("title", ""),
|
| 69 |
+
"text": doc[:300] if doc else meta.get("text", "")[:300],
|
| 70 |
+
}
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
return {"query": q, "results": items}
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
app.mount("/static", StaticFiles(directory=os.path.join(os.path.dirname(__file__), "static")), name="static")
|
data/chroma_db/chroma.sqlite3
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8ca388ada230b73d978ae2c0eeb10fa663e9e6c7f18ffad7d68323ddc86113ef
|
| 3 |
+
size 74473472
|
data/chroma_db/d0040b94-292a-45b3-9e5b-252ec249af2f/data_level0.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8c3eeb1713a73fbfdc76e4f9665a35adace0cebb6d1fb1f2cc2221f662975933
|
| 3 |
+
size 8380000
|
data/chroma_db/d0040b94-292a-45b3-9e5b-252ec249af2f/header.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:846f2c657c3ba2b0a104af38c8ebb9dc61608a697d40e5b65fc24cc3aeef35e6
|
| 3 |
+
size 100
|
data/chroma_db/d0040b94-292a-45b3-9e5b-252ec249af2f/index_metadata.pickle
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a50b5e0d676b6ffb51b3e66c54102e652aeb3aff3dbcccfef5fdbac646ae41b6
|
| 3 |
+
size 137928
|
data/chroma_db/d0040b94-292a-45b3-9e5b-252ec249af2f/length.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3e523baeec0e2a9456b889c28be512934efe127dae351d40012d1ff6170d8eba
|
| 3 |
+
size 20000
|
data/chroma_db/d0040b94-292a-45b3-9e5b-252ec249af2f/link_lists.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:716bad55f0037627ccab52b8b7204feaf572f95c3b891dc5e9bb6023090385e9
|
| 3 |
+
size 44752
|
precompute.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Precompute corpus embeddings and store in ChromaDB.
|
| 3 |
+
|
| 4 |
+
Run once locally:
|
| 5 |
+
python precompute.py
|
| 6 |
+
|
| 7 |
+
Produces: data/chroma_db/ (persistent ChromaDB directory)
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import os
|
| 11 |
+
import chromadb
|
| 12 |
+
import pandas as pd
|
| 13 |
+
from datasets import load_dataset
|
| 14 |
+
from sentence_transformers import SentenceTransformer
|
| 15 |
+
|
| 16 |
+
MODEL_NAME = "intfloat/multilingual-e5-small"
|
| 17 |
+
CHROMA_PATH = os.path.join(os.path.dirname(__file__), "data", "chroma_db")
|
| 18 |
+
COLLECTION_NAME = "scifact"
|
| 19 |
+
BATCH_SIZE = 64
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def load_scifact() -> pd.DataFrame:
|
| 23 |
+
corpus = load_dataset("mteb/scifact", "corpus", split="corpus").to_pandas()
|
| 24 |
+
corpus["title"] = corpus["title"].fillna("").astype(str)
|
| 25 |
+
corpus["text"] = corpus["text"].fillna("").astype(str)
|
| 26 |
+
corpus["full_text"] = (
|
| 27 |
+
corpus["title"].str.strip() + ". " + corpus["text"].str.strip()
|
| 28 |
+
).str.strip(" .")
|
| 29 |
+
corpus = corpus.rename(columns={"_id": "doc_id"})[
|
| 30 |
+
["doc_id", "title", "text", "full_text"]
|
| 31 |
+
]
|
| 32 |
+
return corpus.reset_index(drop=True)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def main():
|
| 36 |
+
print("Loading SciFact corpus...")
|
| 37 |
+
corpus_df = load_scifact()
|
| 38 |
+
print(f" {len(corpus_df)} documents loaded.")
|
| 39 |
+
|
| 40 |
+
print(f"Loading model: {MODEL_NAME}")
|
| 41 |
+
model = SentenceTransformer(MODEL_NAME)
|
| 42 |
+
|
| 43 |
+
passages = [f"passage: {t.strip()}" for t in corpus_df["full_text"].tolist()]
|
| 44 |
+
print(f"Encoding {len(passages)} passages...")
|
| 45 |
+
embeddings = model.encode(
|
| 46 |
+
passages,
|
| 47 |
+
batch_size=BATCH_SIZE,
|
| 48 |
+
show_progress_bar=True,
|
| 49 |
+
normalize_embeddings=True,
|
| 50 |
+
)
|
| 51 |
+
print(f" Embedding shape: {embeddings.shape}")
|
| 52 |
+
|
| 53 |
+
os.makedirs(CHROMA_PATH, exist_ok=True)
|
| 54 |
+
client = chromadb.PersistentClient(path=CHROMA_PATH)
|
| 55 |
+
|
| 56 |
+
# Delete existing collection if present
|
| 57 |
+
try:
|
| 58 |
+
client.delete_collection(COLLECTION_NAME)
|
| 59 |
+
except Exception:
|
| 60 |
+
pass
|
| 61 |
+
|
| 62 |
+
collection = client.create_collection(
|
| 63 |
+
name=COLLECTION_NAME,
|
| 64 |
+
metadata={"hnsw:space": "cosine"},
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
ids = [str(i) for i in range(len(corpus_df))]
|
| 68 |
+
titles = corpus_df["title"].tolist()
|
| 69 |
+
texts = [t[:500] for t in corpus_df["text"].tolist()]
|
| 70 |
+
emb_lists = embeddings.tolist()
|
| 71 |
+
|
| 72 |
+
# Add in batches
|
| 73 |
+
ADD_BATCH = 500
|
| 74 |
+
for start in range(0, len(ids), ADD_BATCH):
|
| 75 |
+
end = min(start + ADD_BATCH, len(ids))
|
| 76 |
+
collection.add(
|
| 77 |
+
ids=ids[start:end],
|
| 78 |
+
embeddings=emb_lists[start:end],
|
| 79 |
+
metadatas=[
|
| 80 |
+
{"title": titles[i], "text": texts[i]} for i in range(start, end)
|
| 81 |
+
],
|
| 82 |
+
documents=corpus_df["full_text"].tolist()[start:end],
|
| 83 |
+
)
|
| 84 |
+
print(f" Added {end}/{len(ids)} documents to ChromaDB.")
|
| 85 |
+
|
| 86 |
+
print(f"\nChromaDB persisted to: {CHROMA_PATH}")
|
| 87 |
+
print(f"Collection '{COLLECTION_NAME}': {collection.count()} documents")
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
chromadb
|
| 4 |
+
sentence-transformers
|
| 5 |
+
pandas
|
| 6 |
+
pyarrow
|
| 7 |
+
torch
|
| 8 |
+
datasets
|
static/index.html
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>SciFact Multilingual Semantic Search</title>
|
| 7 |
+
<style>
|
| 8 |
+
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
|
| 9 |
+
body{
|
| 10 |
+
min-height:100vh;
|
| 11 |
+
font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Oxygen,sans-serif;
|
| 12 |
+
overflow-x:hidden;
|
| 13 |
+
background:#0f0a1a;
|
| 14 |
+
color:#e2e0ea;
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
/* ββ Animated gradient background ββ */
|
| 18 |
+
.bg{
|
| 19 |
+
position:fixed;inset:0;z-index:0;
|
| 20 |
+
background:linear-gradient(135deg,#1a1040 0%,#0f0a1a 30%,#0a1628 60%,#0f0a1a 100%);
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
/* ββ Floating blobs ββ */
|
| 24 |
+
.blob{
|
| 25 |
+
position:fixed;border-radius:50%;filter:blur(80px);opacity:.35;
|
| 26 |
+
animation:drift 20s ease-in-out infinite alternate;z-index:0;
|
| 27 |
+
}
|
| 28 |
+
.blob-1{width:500px;height:500px;background:#6366f1;top:-120px;left:-100px;animation-duration:22s}
|
| 29 |
+
.blob-2{width:400px;height:400px;background:#8b5cf6;bottom:-80px;right:-80px;animation-duration:18s;animation-delay:-5s}
|
| 30 |
+
.blob-3{width:300px;height:300px;background:#14b8a6;top:40%;left:60%;animation-duration:25s;animation-delay:-10s}
|
| 31 |
+
@keyframes drift{
|
| 32 |
+
0%{transform:translate(0,0) scale(1)}
|
| 33 |
+
33%{transform:translate(30px,-40px) scale(1.05)}
|
| 34 |
+
66%{transform:translate(-20px,30px) scale(.95)}
|
| 35 |
+
100%{transform:translate(10px,-10px) scale(1.02)}
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
/* ββ Layout ββ */
|
| 39 |
+
.container{
|
| 40 |
+
position:relative;z-index:1;
|
| 41 |
+
max-width:720px;margin:0 auto;padding:40px 20px 60px;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
/* ββ Header ββ */
|
| 45 |
+
.header{text-align:center;margin-bottom:32px}
|
| 46 |
+
.header h1{
|
| 47 |
+
font-size:1.75rem;font-weight:700;
|
| 48 |
+
background:linear-gradient(135deg,#a78bfa,#6366f1,#14b8a6);
|
| 49 |
+
-webkit-background-clip:text;-webkit-text-fill-color:transparent;
|
| 50 |
+
margin-bottom:6px;
|
| 51 |
+
}
|
| 52 |
+
.header p{font-size:.92rem;color:#9f9bb0}
|
| 53 |
+
|
| 54 |
+
/* ββ Glass card ββ */
|
| 55 |
+
.glass{
|
| 56 |
+
background:rgba(255,255,255,.06);
|
| 57 |
+
backdrop-filter:blur(20px);-webkit-backdrop-filter:blur(20px);
|
| 58 |
+
border:1px solid rgba(255,255,255,.1);
|
| 59 |
+
border-radius:16px;
|
| 60 |
+
padding:24px;
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
/* ββ Search box ββ */
|
| 64 |
+
.search-box{display:flex;gap:10px;margin-bottom:16px}
|
| 65 |
+
.search-box input{
|
| 66 |
+
flex:1;padding:14px 18px;
|
| 67 |
+
background:rgba(255,255,255,.07);
|
| 68 |
+
border:1.5px solid rgba(255,255,255,.12);
|
| 69 |
+
border-radius:12px;color:#e2e0ea;
|
| 70 |
+
font-size:1rem;outline:none;
|
| 71 |
+
transition:border-color .2s,box-shadow .2s;
|
| 72 |
+
}
|
| 73 |
+
.search-box input::placeholder{color:#7a7490}
|
| 74 |
+
.search-box input:focus{
|
| 75 |
+
border-color:#6366f1;
|
| 76 |
+
box-shadow:0 0 0 3px rgba(99,102,241,.25);
|
| 77 |
+
}
|
| 78 |
+
.search-box button{
|
| 79 |
+
padding:0 22px;
|
| 80 |
+
background:linear-gradient(135deg,#6366f1,#8b5cf6);
|
| 81 |
+
border:none;border-radius:12px;color:#fff;
|
| 82 |
+
font-size:.95rem;font-weight:600;cursor:pointer;
|
| 83 |
+
transition:opacity .2s;white-space:nowrap;
|
| 84 |
+
}
|
| 85 |
+
.search-box button:hover{opacity:.88}
|
| 86 |
+
.search-box button:disabled{opacity:.5;cursor:not-allowed}
|
| 87 |
+
|
| 88 |
+
/* ββ Language chips ββ */
|
| 89 |
+
.chips{display:flex;flex-wrap:wrap;gap:8px;margin-bottom:4px}
|
| 90 |
+
.chip{
|
| 91 |
+
padding:6px 14px;
|
| 92 |
+
background:rgba(255,255,255,.07);
|
| 93 |
+
border:1px solid rgba(255,255,255,.1);
|
| 94 |
+
border-radius:20px;font-size:.8rem;color:#c4bfd6;
|
| 95 |
+
cursor:pointer;transition:background .2s,color .2s;
|
| 96 |
+
}
|
| 97 |
+
.chip:hover{background:rgba(99,102,241,.25);color:#e2e0ea}
|
| 98 |
+
.chip span{font-weight:600;margin-right:4px;color:#a78bfa}
|
| 99 |
+
|
| 100 |
+
/* ββ Results ββ */
|
| 101 |
+
.results{margin-top:24px;display:flex;flex-direction:column;gap:14px}
|
| 102 |
+
|
| 103 |
+
.result-card{
|
| 104 |
+
background:rgba(255,255,255,.05);
|
| 105 |
+
backdrop-filter:blur(12px);-webkit-backdrop-filter:blur(12px);
|
| 106 |
+
border:1px solid rgba(255,255,255,.08);
|
| 107 |
+
border-radius:14px;padding:18px 20px;
|
| 108 |
+
transition:border-color .2s;
|
| 109 |
+
}
|
| 110 |
+
.result-card:hover{border-color:rgba(99,102,241,.35)}
|
| 111 |
+
|
| 112 |
+
.result-header{display:flex;align-items:center;gap:10px;margin-bottom:8px}
|
| 113 |
+
.rank-badge{
|
| 114 |
+
width:28px;height:28px;border-radius:8px;
|
| 115 |
+
display:flex;align-items:center;justify-content:center;
|
| 116 |
+
font-size:.78rem;font-weight:700;color:#fff;flex-shrink:0;
|
| 117 |
+
background:linear-gradient(135deg,#6366f1,#8b5cf6);
|
| 118 |
+
}
|
| 119 |
+
.result-title{font-size:.95rem;font-weight:600;color:#e2e0ea;line-height:1.3}
|
| 120 |
+
|
| 121 |
+
.score-row{display:flex;align-items:center;gap:10px;margin-bottom:10px}
|
| 122 |
+
.score-bar-bg{
|
| 123 |
+
flex:1;height:6px;border-radius:3px;
|
| 124 |
+
background:rgba(255,255,255,.08);overflow:hidden;
|
| 125 |
+
}
|
| 126 |
+
.score-bar{
|
| 127 |
+
height:100%;border-radius:3px;
|
| 128 |
+
background:linear-gradient(90deg,#6366f1,#14b8a6);
|
| 129 |
+
transition:width .6s ease;
|
| 130 |
+
}
|
| 131 |
+
.score-label{font-size:.78rem;color:#a78bfa;font-weight:600;white-space:nowrap}
|
| 132 |
+
|
| 133 |
+
.result-text{font-size:.85rem;color:#9f9bb0;line-height:1.55}
|
| 134 |
+
|
| 135 |
+
/* ββ Empty state ββ */
|
| 136 |
+
.empty-state{text-align:center;padding:40px 20px}
|
| 137 |
+
.empty-state .icon{font-size:2.5rem;margin-bottom:12px}
|
| 138 |
+
.empty-state h3{font-size:1.1rem;color:#c4bfd6;margin-bottom:8px}
|
| 139 |
+
.empty-state p{font-size:.88rem;color:#7a7490;line-height:1.5;max-width:400px;margin:0 auto}
|
| 140 |
+
|
| 141 |
+
/* ββ Skeleton loading ββ */
|
| 142 |
+
.skeleton-card{
|
| 143 |
+
background:rgba(255,255,255,.04);
|
| 144 |
+
border:1px solid rgba(255,255,255,.06);
|
| 145 |
+
border-radius:14px;padding:20px;
|
| 146 |
+
animation:pulse 1.5s ease-in-out infinite;
|
| 147 |
+
}
|
| 148 |
+
.skeleton-line{
|
| 149 |
+
height:12px;border-radius:6px;
|
| 150 |
+
background:rgba(255,255,255,.08);margin-bottom:10px;
|
| 151 |
+
}
|
| 152 |
+
.skeleton-line.short{width:40%}
|
| 153 |
+
.skeleton-line.medium{width:70%}
|
| 154 |
+
.skeleton-line.long{width:100%}
|
| 155 |
+
@keyframes pulse{0%,100%{opacity:1}50%{opacity:.5}}
|
| 156 |
+
|
| 157 |
+
/* ββ Info bar ββ */
|
| 158 |
+
.info-bar{
|
| 159 |
+
text-align:center;margin-top:28px;
|
| 160 |
+
font-size:.78rem;color:#5e5874;
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
@media(max-width:500px){
|
| 164 |
+
.container{padding:24px 14px 40px}
|
| 165 |
+
.header h1{font-size:1.35rem}
|
| 166 |
+
.search-box{flex-direction:column}
|
| 167 |
+
.search-box button{padding:12px}
|
| 168 |
+
}
|
| 169 |
+
</style>
|
| 170 |
+
</head>
|
| 171 |
+
<body>
|
| 172 |
+
<div class="bg"></div>
|
| 173 |
+
<div class="blob blob-1"></div>
|
| 174 |
+
<div class="blob blob-2"></div>
|
| 175 |
+
<div class="blob blob-3"></div>
|
| 176 |
+
|
| 177 |
+
<div class="container">
|
| 178 |
+
<div class="header">
|
| 179 |
+
<h1>SciFact Semantic Search</h1>
|
| 180 |
+
<p>Multilingual search over 5,183 scientific abstracts — powered by E5-small</p>
|
| 181 |
+
</div>
|
| 182 |
+
|
| 183 |
+
<div class="glass">
|
| 184 |
+
<div class="search-box">
|
| 185 |
+
<input id="q" type="text" placeholder="Search scientific literature..." autocomplete="off">
|
| 186 |
+
<button id="btn" onclick="doSearch()">Search</button>
|
| 187 |
+
</div>
|
| 188 |
+
<div class="chips">
|
| 189 |
+
<div class="chip" onclick="chipSearch(this)"><span>EN</span> effects of vaccination</div>
|
| 190 |
+
<div class="chip" onclick="chipSearch(this)"><span>FR</span> effets de la vaccination</div>
|
| 191 |
+
<div class="chip" onclick="chipSearch(this)"><span>DE</span> Auswirkungen der Impfung</div>
|
| 192 |
+
<div class="chip" onclick="chipSearch(this)"><span>ES</span> efectos de la vacunación</div>
|
| 193 |
+
</div>
|
| 194 |
+
</div>
|
| 195 |
+
|
| 196 |
+
<div id="results" class="results">
|
| 197 |
+
<div class="empty-state">
|
| 198 |
+
<div class="icon">🔬</div>
|
| 199 |
+
<h3>Multilingual Semantic Search</h3>
|
| 200 |
+
<p>Search 5,183 SciFact abstracts in any language. The model understands meaning, not just keywords. Try a query above or click a language chip.</p>
|
| 201 |
+
</div>
|
| 202 |
+
</div>
|
| 203 |
+
|
| 204 |
+
<div class="info-bar">
|
| 205 |
+
intfloat/multilingual-e5-small · ChromaDB · FastAPI · CPU inference
|
| 206 |
+
</div>
|
| 207 |
+
</div>
|
| 208 |
+
|
| 209 |
+
<script>
|
| 210 |
+
const input = document.getElementById('q');
|
| 211 |
+
const btn = document.getElementById('btn');
|
| 212 |
+
const results = document.getElementById('results');
|
| 213 |
+
|
| 214 |
+
input.addEventListener('keydown', e => { if (e.key === 'Enter') doSearch(); });
|
| 215 |
+
|
| 216 |
+
function chipSearch(el) {
|
| 217 |
+
// Get text after the language tag span
|
| 218 |
+
const text = el.textContent.replace(/^[A-Z]{2}\s*/, '').trim();
|
| 219 |
+
input.value = text;
|
| 220 |
+
doSearch();
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
async function doSearch() {
|
| 224 |
+
const q = input.value.trim();
|
| 225 |
+
if (!q) return;
|
| 226 |
+
|
| 227 |
+
btn.disabled = true;
|
| 228 |
+
btn.textContent = '...';
|
| 229 |
+
results.innerHTML = skeletonHTML();
|
| 230 |
+
|
| 231 |
+
try {
|
| 232 |
+
const res = await fetch(`/search?q=${encodeURIComponent(q)}&top_k=5`);
|
| 233 |
+
if (!res.ok) throw new Error(res.statusText);
|
| 234 |
+
const data = await res.json();
|
| 235 |
+
renderResults(data.results);
|
| 236 |
+
} catch (err) {
|
| 237 |
+
results.innerHTML = `<div class="empty-state"><div class="icon">⚠️</div><h3>Search failed</h3><p>${err.message}</p></div>`;
|
| 238 |
+
} finally {
|
| 239 |
+
btn.disabled = false;
|
| 240 |
+
btn.textContent = 'Search';
|
| 241 |
+
}
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
function skeletonHTML() {
|
| 245 |
+
let h = '';
|
| 246 |
+
for (let i = 0; i < 3; i++) {
|
| 247 |
+
h += `<div class="skeleton-card">
|
| 248 |
+
<div class="skeleton-line short"></div>
|
| 249 |
+
<div class="skeleton-line long"></div>
|
| 250 |
+
<div class="skeleton-line medium"></div>
|
| 251 |
+
</div>`;
|
| 252 |
+
}
|
| 253 |
+
return h;
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
function renderResults(items) {
|
| 257 |
+
if (!items || !items.length) {
|
| 258 |
+
results.innerHTML = `<div class="empty-state"><div class="icon">🔍</div><h3>No results</h3><p>Try a different query.</p></div>`;
|
| 259 |
+
return;
|
| 260 |
+
}
|
| 261 |
+
results.innerHTML = items.map(r => {
|
| 262 |
+
const pct = Math.max(0, Math.min(100, r.score * 100));
|
| 263 |
+
return `<div class="result-card">
|
| 264 |
+
<div class="result-header">
|
| 265 |
+
<div class="rank-badge">${r.rank}</div>
|
| 266 |
+
<div class="result-title">${esc(r.title)}</div>
|
| 267 |
+
</div>
|
| 268 |
+
<div class="score-row">
|
| 269 |
+
<div class="score-bar-bg"><div class="score-bar" style="width:${pct}%"></div></div>
|
| 270 |
+
<div class="score-label">${(pct).toFixed(1)}%</div>
|
| 271 |
+
</div>
|
| 272 |
+
<div class="result-text">${esc(r.text)}</div>
|
| 273 |
+
</div>`;
|
| 274 |
+
}).join('');
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
function esc(s) {
|
| 278 |
+
const d = document.createElement('div');
|
| 279 |
+
d.textContent = s || '';
|
| 280 |
+
return d.innerHTML;
|
| 281 |
+
}
|
| 282 |
+
</script>
|
| 283 |
+
</body>
|
| 284 |
+
</html>
|