Minendra Gangwar commited on
Commit
588c371
·
1 Parent(s): 293d6b1

Prepare for Docker deployment on Hugging Face

Browse files
Files changed (3) hide show
  1. Dockerfile +35 -0
  2. backend/main.py +13 -2
  3. frontend/src/App.jsx +4 -2
Dockerfile ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Stage 1: Build the React Frontend
2
+ FROM node:18 AS frontend-builder
3
+ WORKDIR /app/frontend
4
+ COPY frontend/package*.json ./
5
+ RUN npm install
6
+ COPY frontend/ ./
7
+ RUN npm run build
8
+
9
+ # Stage 2: Build the Python Backend
10
+ FROM python:3.10-slim
11
+ WORKDIR /app
12
+
13
+ # Install system dependencies
14
+ RUN apt-get update && apt-get install -y --no-install-recommends \
15
+ build-essential \
16
+ && rm -rf /var/lib/apt/lists/*
17
+
18
+ # Install Python requirements
19
+ COPY backend/requirements.txt ./
20
+ RUN pip install --no-cache-dir -r requirements.txt
21
+
22
+ # Copy the backend code
23
+ COPY backend/ ./backend/
24
+
25
+ # Copy the compiled frontend from Stage 1
26
+ COPY --from=frontend-builder /app/frontend/dist ./frontend/dist
27
+
28
+ # Create the data directories so FAISS doesn't crash
29
+ RUN mkdir -p /app/backend/data/extracted_images /app/backend/data/vectorstore
30
+
31
+ # Hugging Face Spaces expose port 7860 by default
32
+ EXPOSE 7860
33
+
34
+ # Start the FastAPI server
35
+ CMD ["uvicorn", "backend.main:app", "--host", "0.0.0.0", "--port", "7860"]
backend/main.py CHANGED
@@ -1,5 +1,7 @@
1
  from fastapi import FastAPI, UploadFile, File, HTTPException
2
  from fastapi.middleware.cors import CORSMiddleware
 
 
3
  from pydantic import BaseModel
4
  import os
5
  import shutil
@@ -33,8 +35,10 @@ class ChatRequest(BaseModel):
33
  async def upload_pdf(file: UploadFile = File(...)):
34
  global rag_chain
35
  try:
 
 
36
  pdf_path = os.path.join("data", "source_pdfs", file.filename)
37
- os.makedirs(os.path.dirname(pdf_path), exist_ok=True)
38
  with open(pdf_path, "wb") as buffer:
39
  shutil.copyfileobj(file.file, buffer)
40
 
@@ -47,7 +51,6 @@ async def upload_pdf(file: UploadFile = File(...)):
47
  image_captions_map[img_path] = caption
48
 
49
  vector_db = build_vector_store(parsed_data, image_captions_map)
50
-
51
  rag_chain = get_rag_chain(vector_db)
52
 
53
  return {"status": "success", "message": f"Successfully parsed and indexed {file.filename}."}
@@ -78,6 +81,14 @@ async def chat_with_pdf(request: ChatRequest):
78
  except Exception as e:
79
  raise HTTPException(status_code=500, detail=str(e))
80
 
 
 
 
 
 
 
 
 
81
 
82
  if __name__ == "__main__":
83
  import uvicorn
 
1
  from fastapi import FastAPI, UploadFile, File, HTTPException
2
  from fastapi.middleware.cors import CORSMiddleware
3
+ from fastapi.staticfiles import StaticFiles
4
+ from fastapi.responses import FileResponse
5
  from pydantic import BaseModel
6
  import os
7
  import shutil
 
35
  async def upload_pdf(file: UploadFile = File(...)):
36
  global rag_chain
37
  try:
38
+ # Use absolute paths for production
39
+ os.makedirs("data/source_pdfs", exist_ok=True)
40
  pdf_path = os.path.join("data", "source_pdfs", file.filename)
41
+
42
  with open(pdf_path, "wb") as buffer:
43
  shutil.copyfileobj(file.file, buffer)
44
 
 
51
  image_captions_map[img_path] = caption
52
 
53
  vector_db = build_vector_store(parsed_data, image_captions_map)
 
54
  rag_chain = get_rag_chain(vector_db)
55
 
56
  return {"status": "success", "message": f"Successfully parsed and indexed {file.filename}."}
 
81
  except Exception as e:
82
  raise HTTPException(status_code=500, detail=str(e))
83
 
84
+ frontend_dist = os.path.join(os.path.dirname(__file__), "frontend", "dist")
85
+
86
+ if os.path.exists(frontend_dist):
87
+ app.mount("/", StaticFiles(directory=frontend_dist, html=True), name="static")
88
+
89
+ @app.exception_handler(404)
90
+ async def fallback_to_index(request, exc):
91
+ return FileResponse(os.path.join(frontend_dist, "index.html"))
92
 
93
  if __name__ == "__main__":
94
  import uvicorn
frontend/src/App.jsx CHANGED
@@ -92,7 +92,8 @@ export default function App() {
92
  formData.append('file', file);
93
 
94
  try {
95
- const response = await fetch('http://127.0.0.1:8000/api/upload', {
 
96
  method: 'POST',
97
  body: formData,
98
  });
@@ -122,7 +123,8 @@ export default function App() {
122
  setLoadingAnswer(true);
123
 
124
  try {
125
- const response = await fetch('http://127.0.0.1:8000/api/chat', {
 
126
  method: 'POST',
127
  headers: { 'Content-Type': 'application/json' },
128
  body: JSON.stringify({ message: userQuery }),
 
92
  formData.append('file', file);
93
 
94
  try {
95
+ // CHANGED: Removed http://127.0.0.1:8000
96
+ const response = await fetch('/api/upload', {
97
  method: 'POST',
98
  body: formData,
99
  });
 
123
  setLoadingAnswer(true);
124
 
125
  try {
126
+ // CHANGED: Removed http://127.0.0.1:8000
127
+ const response = await fetch('/api/chat', {
128
  method: 'POST',
129
  headers: { 'Content-Type': 'application/json' },
130
  body: JSON.stringify({ message: userQuery }),