rohanbelsare commited on
Commit
4e8930d
·
1 Parent(s): 74f4b1c

Fix: Add Pro-Level stability upgrades (asyncio LLM thread, markdown parsing, and Docker Healthcheck)

Browse files
Files changed (2) hide show
  1. Dockerfile +12 -9
  2. inference.py +9 -2
Dockerfile CHANGED
@@ -12,36 +12,39 @@ RUN npm run build
12
  FROM python:3.10-slim
13
  WORKDIR /app
14
 
15
- # Install system dependencies
16
  RUN apt-get update && apt-get install -y \
17
  build-essential \
 
18
  && rm -rf /var/lib/apt/lists/*
19
 
20
- # Install Backend Dependencies
21
  COPY backend/requirements.txt .
22
  RUN pip install --no-cache-dir -r requirements.txt
23
 
24
- # Copy RL, Environment, and new Server modules
25
  COPY openenv_env/ ./openenv_env/
26
  COPY rl/ ./rl/
27
  COPY backend/ ./backend/
28
  COPY server/ ./server/
29
 
30
- # Copy scripts and OpenEnv configuration files
31
  COPY inference.py pyproject.toml uv.lock ./
32
 
33
- # Install the project as a package (This automatically installs openenv-core, openai, torch, etc.)
34
  RUN pip install -e .
35
 
36
- # Copy built frontend from Stage 1 to a 'static' folder in backend
37
  COPY --from=frontend-builder /app/frontend/dist ./static
38
 
39
- # Set Environment Variables for RL pathing
40
  ENV PYTHONPATH=/app:/app/openenv_env:/app/rl
41
  ENV PORT=7860
42
 
43
- # Hugging Face runs on port 7860
44
  EXPOSE 7860
45
 
46
- # Start FastAPI and serve everything
 
 
47
  CMD ["sh", "-c", "python -u inference.py & uvicorn backend.api.main:app --host 0.0.0.0 --port 7860"]
 
12
  FROM python:3.10-slim
13
  WORKDIR /app
14
 
15
+ # Install system dependencies (added curl for the healthcheck)
16
  RUN apt-get update && apt-get install -y \
17
  build-essential \
18
+ curl \
19
  && rm -rf /var/lib/apt/lists/*
20
 
21
+
22
  COPY backend/requirements.txt .
23
  RUN pip install --no-cache-dir -r requirements.txt
24
 
25
+
26
  COPY openenv_env/ ./openenv_env/
27
  COPY rl/ ./rl/
28
  COPY backend/ ./backend/
29
  COPY server/ ./server/
30
 
31
+
32
  COPY inference.py pyproject.toml uv.lock ./
33
 
34
+
35
  RUN pip install -e .
36
 
37
+
38
  COPY --from=frontend-builder /app/frontend/dist ./static
39
 
40
+
41
  ENV PYTHONPATH=/app:/app/openenv_env:/app/rl
42
  ENV PORT=7860
43
 
44
+
45
  EXPOSE 7860
46
 
47
+ HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \
48
+ CMD curl -f http://localhost:7860/ || exit 1
49
+
50
  CMD ["sh", "-c", "python -u inference.py & uvicorn backend.api.main:app --host 0.0.0.0 --port 7860"]
inference.py CHANGED
@@ -84,13 +84,20 @@ async def main() -> None:
84
  try:
85
  # We ask the LLM for a quick high-level strategy to prove we are using it
86
  prompt = f"System state array: {obs.tolist()}. Give a 1-word strategy (e.g., 'Reroute' or 'Wait')."
87
- response = client.chat.completions.create(
 
 
 
88
  model=MODEL_NAME,
89
  messages=[{"role": "user", "content": prompt}],
90
  max_tokens=5,
91
  temperature=0.0,
92
  )
93
- llm_strategy = response.choices[0].message.content.strip()
 
 
 
 
94
  except Exception as e:
95
  # If the HuggingFace proxy fails, we don't want it to crash the script
96
  llm_strategy = "Fallback"
 
84
  try:
85
  # We ask the LLM for a quick high-level strategy to prove we are using it
86
  prompt = f"System state array: {obs.tolist()}. Give a 1-word strategy (e.g., 'Reroute' or 'Wait')."
87
+
88
+ # --- PRO FIX 1: Use asyncio.to_thread to prevent WebSocket blocking ---
89
+ response = await asyncio.to_thread(
90
+ client.chat.completions.create,
91
  model=MODEL_NAME,
92
  messages=[{"role": "user", "content": prompt}],
93
  max_tokens=5,
94
  temperature=0.0,
95
  )
96
+
97
+ # --- PRO FIX 2: Strip markdown backticks to prevent crash ---
98
+ raw_response = response.choices[0].message.content.strip()
99
+ llm_strategy = raw_response.replace("`", "").strip()
100
+
101
  except Exception as e:
102
  # If the HuggingFace proxy fails, we don't want it to crash the script
103
  llm_strategy = "Fallback"