tahaListens commited on
Commit
d03547a
·
1 Parent(s): fa0ab61

front-end routing and missing imports

Browse files
Files changed (2) hide show
  1. Dockerfile +9 -10
  2. app.py +16 -15
Dockerfile CHANGED
@@ -1,4 +1,4 @@
1
- # Step 1: Build the React Frontend
2
  FROM node:20 AS frontend-builder
3
  WORKDIR /app/frontend
4
  COPY frontend/package*.json ./
@@ -6,11 +6,11 @@ RUN npm install
6
  COPY frontend/ ./
7
  RUN npm run build
8
 
9
- # 1. Start with a Python base
10
  FROM python:3.11-slim
11
  WORKDIR /app
12
 
13
- # 2. Install system-level audio dependencies
14
  USER root
15
  RUN apt-get update && apt-get install -y \
16
  ffmpeg \
@@ -25,23 +25,22 @@ RUN apt-get update && apt-get install -y \
25
  libasound2-dev \
26
  libportaudio2 \
27
  && rm -rf /var/lib/apt/lists/*
28
- USER user
29
 
30
- # 3. Set up a non-root user (Hugging Face Requirement)
31
- RUN useradd -m -u 1000 user
32
- USER user
33
  ENV HOME=/home/user \
34
  PATH=/home/user/.local/bin:$PATH
35
  WORKDIR $HOME/app
 
36
 
37
- # 4. Install Python dependencies
38
  COPY --chown=user requirements.txt .
39
  RUN pip install --no-cache-dir --upgrade pip && \
40
  pip install --no-cache-dir -r requirements.txt
41
 
42
- # 5. Copy the rest of your code
43
  COPY --chown=user . .
44
  COPY --from=frontend-builder --chown=user /app/frontend/dist ./frontend/dist
45
 
46
- # 6. Run the app on HF's specific port
47
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
1
+ #1: Build the React Frontend
2
  FROM node:20 AS frontend-builder
3
  WORKDIR /app/frontend
4
  COPY frontend/package*.json ./
 
6
  COPY frontend/ ./
7
  RUN npm run build
8
 
9
+ # 2. Start with a Python base
10
  FROM python:3.11-slim
11
  WORKDIR /app
12
 
13
+ # 3. Install system-level audio dependencies
14
  USER root
15
  RUN apt-get update && apt-get install -y \
16
  ffmpeg \
 
25
  libasound2-dev \
26
  libportaudio2 \
27
  && rm -rf /var/lib/apt/lists/*
 
28
 
29
+ # 4. Set up a non-root user
30
+ RUN useradd -m -u 1000 user || echo "User already exists"
 
31
  ENV HOME=/home/user \
32
  PATH=/home/user/.local/bin:$PATH
33
  WORKDIR $HOME/app
34
+ USER user
35
 
36
+ # 5. Install Python dependencies
37
  COPY --chown=user requirements.txt .
38
  RUN pip install --no-cache-dir --upgrade pip && \
39
  pip install --no-cache-dir -r requirements.txt
40
 
41
+ # 6. Copy the rest of your code
42
  COPY --chown=user . .
43
  COPY --from=frontend-builder --chown=user /app/frontend/dist ./frontend/dist
44
 
45
+ # 7. Run the app on HF's specific port
46
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py CHANGED
@@ -4,6 +4,7 @@ from agent import TeacherAgent
4
  from fastapi.staticfiles import StaticFiles
5
  from contextlib import asynccontextmanager
6
  import asyncio
 
7
 
8
  # 1. Global Agent Variable
9
  teacher_agent = None
@@ -20,12 +21,7 @@ async def lifespan(app: FastAPI):
20
 
21
  app = FastAPI(lifespan=lifespan)
22
 
23
- # 2. Simple HTML Frontend (Temporary for testing)
24
- @app.get("/")
25
- async def get():
26
- return FileResponse("index.html")
27
-
28
- # 3. The WebSocket "Bridge"
29
  @app.websocket("/ws")
30
  async def websocket_endpoint(websocket: WebSocket):
31
  await websocket.accept()
@@ -37,14 +33,19 @@ async def websocket_endpoint(websocket: WebSocket):
37
  except Exception as e:
38
  print(f"⚠️ Error: {e}")
39
 
40
- # 1. Mount the "dist" folder (where Vite builds the files)
41
  # Check if the directory exists to avoid errors during local dev
42
- if os.path.exists("frontend/dist"):
43
- app.mount("/", StaticFiles(directory="frontend/dist", html=True), name="static")
44
 
45
- # 2. Catch-all route for React Router (if you add pages later)
46
- @app.get("/{catchall:path}")
47
- async def read_index(catchall: str):
48
- if os.path.exists("frontend/dist/index.html"):
49
- return FileResponse("frontend/dist/index.html")
50
- return {"message": "Frontend not built yet. Run npm run build."}
 
 
 
 
 
 
 
4
  from fastapi.staticfiles import StaticFiles
5
  from contextlib import asynccontextmanager
6
  import asyncio
7
+ import os
8
 
9
  # 1. Global Agent Variable
10
  teacher_agent = None
 
21
 
22
  app = FastAPI(lifespan=lifespan)
23
 
24
+ # 2. The WebSocket "Bridge"
 
 
 
 
 
25
  @app.websocket("/ws")
26
  async def websocket_endpoint(websocket: WebSocket):
27
  await websocket.accept()
 
33
  except Exception as e:
34
  print(f"⚠️ Error: {e}")
35
 
36
+ # 3. Serve the React Frontend
37
  # Check if the directory exists to avoid errors during local dev
38
+ frontend_path = os.path.join(os.getcwd(), "frontend/dist")
 
39
 
40
+ if os.path.exists(frontend_path):
41
+ # This serves all the CSS/JS files
42
+ app.mount("/assets", StaticFiles(directory=os.path.join(frontend_path, "assets")), name="assets")
43
+
44
+ # This serves the main React page for the root and any sub-pages
45
+ @app.get("/{catchall:path}")
46
+ async def serve_react_app(catchall: str):
47
+ return FileResponse(os.path.join(frontend_path, "index.html"))
48
+ else:
49
+ @app.get("/")
50
+ async def index():
51
+ return {"error": "Frontend build not found. Please run 'npm run build'."}