File size: 3,452 Bytes
845d6d1
 
 
 
 
b861f87
845d6d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b861f87
 
 
 
 
 
 
 
 
 
3b5056c
 
 
 
6ea3b3e
 
b861f87
 
 
 
 
 
 
 
 
 
845d6d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ── ResearchIT β€” HF Spaces Docker deployment ─────────────────────────────────
# Free tier: 16GB RAM, 2 vCPUs, ephemeral filesystem, port 7860 required
FROM python:3.12-slim

# System dependencies
RUN apt-get update && apt-get install -y --no-install-recommends gcc g++ curl && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Create non-root user BEFORE model downloads so getpwuid(1000) works
# and model caches are written to a known, accessible location.
RUN useradd -m -u 1000 -s /bin/bash user

# Set HuggingFace cache to a shared path accessible by uid 1000
ENV HF_HOME=/app/.cache/huggingface
ENV TRANSFORMERS_CACHE=/app/.cache/huggingface
ENV SENTENCE_TRANSFORMERS_HOME=/app/.cache/sentence-transformers
RUN mkdir -p /app/.cache/huggingface /app/.cache/sentence-transformers

# Install torch CPU-only first (smaller than full CUDA build)
RUN pip install --no-cache-dir torch --index-url https://download.pytorch.org/whl/cpu

# Install Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Pre-download models into the image (baked in, no cold-start download)
# BGE-M3 for dense+sparse embeddings (~2.2GB)
RUN python -c "from FlagEmbedding import BGEM3FlagModel; BGEM3FlagModel('BAAI/bge-m3', use_fp16=False)"
# MiniLM cross-encoder for search reranking (~80MB)
RUN python -c "from sentence_transformers import CrossEncoder; CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')"

# ── Metadata sidecar (optional but strongly recommended) ─────────────────────
# A local SQLite mirror of the Turso `papers` table, built by
# scripts/build_metadata_sidecar.py and hosted as an HF dataset.
#
# Turso has a single index (arxiv_id), so category and recency queries are full
# scans of 1.6M rows β€” trending alone is ~15s cold and lands on new users during
# onboarding. The sidecar carries the missing indexes locally.
#
# Download failure is non-fatal: app/local_meta.py falls back to Turso, so the
# build still produces a working image. Set to "" to skip entirely.
# Pinned to a dataset commit rather than `main` for two reasons: the build is
# reproducible, and publishing a new sidecar changes this line, which
# invalidates the Docker layer below. Pointing at `main` meant a rebuild
# silently reused the cached copy and shipped stale metadata.
# d241ff1 = 1,799,348 papers + FTS5 index (the sparse retrieval arm).
ENV METADATA_SIDECAR_URL=https://huggingface.co/datasets/siddhm11/researchit-metadata/resolve/d241ff1cb341523d9896412af0ac6f457713d9d1/metadata.sqlite
ENV METADATA_SIDECAR_PATH=/app/data/metadata.sqlite
RUN mkdir -p /app/data && \
    if [ -n "$METADATA_SIDECAR_URL" ]; then \
      echo "Fetching metadata sidecar..." && \
      (curl -fSL --retry 3 "$METADATA_SIDECAR_URL" -o /app/data/metadata.sqlite \
        && ls -lh /app/data/metadata.sqlite) \
      || (echo "WARNING: sidecar download failed β€” falling back to Turso at runtime" \
          && rm -f /app/data/metadata.sqlite); \
    fi

# Copy application code
COPY . .

# Make app dir writable for non-root user (HF Spaces requires USER 1000)
RUN chmod -R 777 /app

# HF Spaces requires port 7860 and non-root user
USER 1000
EXPOSE 7860

# SQLite must write to a writable path
ENV DB_PATH=/tmp/interactions.db
# Ensure HOME is set for the non-root user
ENV HOME=/home/user

CMD ["python", "run.py"]