Spaces:
Running
Running
Rajeev Ranjan Pandey commited on
Commit ·
abf7059
1
Parent(s): 9666e1d
build: prepare Dockerfile and FastAPI adjustments for Hugging Face Spaces deployment
Browse files- Dockerfile +40 -0
- backend/main.py +20 -0
- frontend/src/api/client.js +3 -1
Dockerfile
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Stage 1: Build the React Frontend
|
| 2 |
+
FROM node:20-alpine AS build-step
|
| 3 |
+
|
| 4 |
+
WORKDIR /app/frontend
|
| 5 |
+
COPY frontend/package*.json ./
|
| 6 |
+
RUN npm install
|
| 7 |
+
|
| 8 |
+
COPY frontend/ ./
|
| 9 |
+
RUN npm run build
|
| 10 |
+
|
| 11 |
+
# Stage 2: Serve with Python FastAPI
|
| 12 |
+
FROM python:3.10-slim
|
| 13 |
+
|
| 14 |
+
# Set up a new user named "user" with user ID 1000 (Required by Hugging Face Spaces)
|
| 15 |
+
RUN useradd -m -u 1000 user
|
| 16 |
+
USER user
|
| 17 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 18 |
+
|
| 19 |
+
WORKDIR /home/user/app
|
| 20 |
+
|
| 21 |
+
# Copy requirement files and install Python dependencies
|
| 22 |
+
COPY --chown=user:user requirements.txt ./
|
| 23 |
+
# We install torch CPU version to save space/memory if GPU is unavailable, though HF Spaces gives regular instances depending on config
|
| 24 |
+
RUN pip install --no-cache-dir --upgrade pip
|
| 25 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 26 |
+
|
| 27 |
+
# Copy the entire project code into the container
|
| 28 |
+
COPY --chown=user:user . .
|
| 29 |
+
|
| 30 |
+
# Copy the compiled React frontend from the build stage into the backend's reach
|
| 31 |
+
COPY --from=build-step --chown=user:user /app/frontend/dist ./frontend/dist
|
| 32 |
+
|
| 33 |
+
# Expose the standard port Hugging Face Spaces expects
|
| 34 |
+
EXPOSE 7860
|
| 35 |
+
|
| 36 |
+
# We mount the HF Spaces cache directory for transformers to avoid downloading models on every restart
|
| 37 |
+
ENV TRANSFORMERS_CACHE="/home/user/app/.cache/huggingface"
|
| 38 |
+
|
| 39 |
+
# Run the FastAPI app on port 7860
|
| 40 |
+
CMD ["uvicorn", "backend.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
backend/main.py
CHANGED
|
@@ -5,6 +5,8 @@ from pathlib import Path
|
|
| 5 |
import pandas as pd
|
| 6 |
from fastapi import FastAPI, HTTPException
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
|
|
| 8 |
|
| 9 |
from src.app.schemas import (
|
| 10 |
CompareRequest,
|
|
@@ -160,3 +162,21 @@ def compare(request: CompareRequest):
|
|
| 160 |
return CompareResponse(dataset_track=request.dataset_track, items=items)
|
| 161 |
except Exception as exc:
|
| 162 |
raise HTTPException(status_code=500, detail=str(exc)) from exc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import pandas as pd
|
| 6 |
from fastapi import FastAPI, HTTPException
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
+
from fastapi.responses import FileResponse
|
| 9 |
+
from fastapi.staticfiles import StaticFiles
|
| 10 |
|
| 11 |
from src.app.schemas import (
|
| 12 |
CompareRequest,
|
|
|
|
| 162 |
return CompareResponse(dataset_track=request.dataset_track, items=items)
|
| 163 |
except Exception as exc:
|
| 164 |
raise HTTPException(status_code=500, detail=str(exc)) from exc
|
| 165 |
+
|
| 166 |
+
# ── Serve React Frontend (Single-Container Deployment e.g., Hugging Face) ──
|
| 167 |
+
_DIST_PATH = Path(__file__).parent.parent / "frontend" / "dist"
|
| 168 |
+
|
| 169 |
+
if _DIST_PATH.exists() and _DIST_PATH.is_dir():
|
| 170 |
+
# Mount static assets first (CSS/JS files inside /assets)
|
| 171 |
+
app.mount("/assets", StaticFiles(directory=str(_DIST_PATH / "assets")), name="assets")
|
| 172 |
+
|
| 173 |
+
# Catch-all route for Single Page Application (React Router)
|
| 174 |
+
@app.get("/{full_path:path}", response_class=FileResponse)
|
| 175 |
+
async def serve_frontend(full_path: str):
|
| 176 |
+
# Prevent accessing files outside dist/
|
| 177 |
+
req_path = _DIST_PATH / full_path
|
| 178 |
+
if req_path.exists() and req_path.is_file():
|
| 179 |
+
return FileResponse(req_path)
|
| 180 |
+
# Otherwise, fall back to React's index.html
|
| 181 |
+
return FileResponse(_DIST_PATH / "index.html")
|
| 182 |
+
|
frontend/src/api/client.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
import axios from "axios";
|
| 2 |
|
| 3 |
const api = axios.create({
|
| 4 |
-
|
|
|
|
|
|
|
| 5 |
});
|
| 6 |
|
| 7 |
export async function summarizeText(payload) {
|
|
|
|
| 1 |
import axios from "axios";
|
| 2 |
|
| 3 |
const api = axios.create({
|
| 4 |
+
// Use relative paths in production (Hugging Face) so it hits the same domain.
|
| 5 |
+
// In local dev, continue reaching out to FastAPI on port 8000.
|
| 6 |
+
baseURL: import.meta.env.PROD ? "" : "http://127.0.0.1:8000"
|
| 7 |
});
|
| 8 |
|
| 9 |
export async function summarizeText(payload) {
|