J266501 commited on
Commit
ec37931
·
1 Parent(s): effe6e2
Files changed (2) hide show
  1. api_service.py +12 -5
  2. dockerfile +13 -17
api_service.py CHANGED
@@ -137,9 +137,16 @@ async def ask_rag(request: QueryRequest):
137
  except Exception as e:
138
  print(f"Error: {e}")
139
  raise HTTPException(status_code=500, detail=f"Failed to process query: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
140
 
141
- # --- 7. Run Command Hint ---
142
- # Run this API with:
143
- # uvicorn api_service:app --reload --port 8000
144
- # The --port 8000 parameter specifies the port where the service will run.
145
- # Open your browser at http://127.0.0.1:8000/docs#/default/ask_rag_ask_post
 
137
  except Exception as e:
138
  print(f"Error: {e}")
139
  raise HTTPException(status_code=500, detail=f"Failed to process query: {e}")
140
+
141
+ # ✅ --- 7. React 靜態檔案掛載 ---
142
+ from fastapi.staticfiles import StaticFiles
143
+ from fastapi.responses import FileResponse
144
+
145
+ # 掛載 build/static 資源
146
+ app.mount("/static", StaticFiles(directory="build/static"), name="static")
147
+
148
+ # 根目錄回傳 React 首頁
149
+ @app.get("/")
150
+ async def serve_react_app():
151
+ return FileResponse("build/index.html")
152
 
 
 
 
 
 
dockerfile CHANGED
@@ -1,34 +1,30 @@
1
- # Use a base image with Python
2
  FROM python:3.10-slim
3
 
4
- # Set environment variables
5
  ENV PYTHONDONTWRITEBYTECODE=1
6
  ENV PYTHONUNBUFFERED=1
7
 
8
- # Set working directory
9
  WORKDIR /app
10
 
11
- # Install system dependencies
12
- RUN apt-get update && apt-get install -y build-essential && rm -rf /var/lib/apt/lists/*
13
-
14
- # Install Node.js (for serving frontend if needed)
15
- RUN apt-get install -y curl && \
16
  curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && \
17
- apt-get install -y nodejs
 
18
 
19
- # Copy and install Python dependencies
20
- COPY requirements.txt ./
21
  RUN pip install --upgrade pip && pip install -r requirements.txt
22
 
23
- # Copy all files
24
  COPY . .
25
 
26
- # Set environment variable for Together API key (set via HF secrets)
27
- ENV TOGETHER_API_KEY=${TOGETHER_API_KEY}
28
 
29
- # Build React frontend
30
- RUN cd ./ && npm install && npm run build
31
 
32
- # Use uvicorn to launch FastAPI
33
  EXPOSE 7860
34
  CMD ["uvicorn", "api_service:app", "--host", "0.0.0.0", "--port", "7860"]
 
 
1
  FROM python:3.10-slim
2
 
3
+ # 環境變數
4
  ENV PYTHONDONTWRITEBYTECODE=1
5
  ENV PYTHONUNBUFFERED=1
6
 
 
7
  WORKDIR /app
8
 
9
+ # 安裝系統依賴與 Node.js(React 前端需要)
10
+ RUN apt-get update && \
11
+ apt-get install -y build-essential curl && \
 
 
12
  curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && \
13
+ apt-get install -y nodejs && \
14
+ rm -rf /var/lib/apt/lists/*
15
 
16
+ # 安裝 Python 套件
17
+ COPY requirements.txt .
18
  RUN pip install --upgrade pip && pip install -r requirements.txt
19
 
20
+ # 複製所有檔案(包含 build/ 資料夾)
21
  COPY . .
22
 
23
+ # 如果你已經事先有 build/,這行可以省略:
24
+ # RUN cd frontend && npm install && npm run build
25
 
26
+ # TOGETHER API KEY
27
+ ENV TOGETHER_API_KEY=${TOGETHER_API_KEY}
28
 
 
29
  EXPOSE 7860
30
  CMD ["uvicorn", "api_service:app", "--host", "0.0.0.0", "--port", "7860"]