Mohammad Wasil commited on
Commit
b7cb2c4
·
1 Parent(s): 91c745f

Fix frontend connection: use relative API path

Browse files
Dockerfile CHANGED
@@ -1,37 +1,87 @@
1
- FROM python:3.10.9-slim
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- # Hugging Face requires UID 1000
4
- RUN useradd -m -u 1000 appuser
5
- WORKDIR /app
6
 
7
- # Install system dependencies (gcc for chromadb, libmagic for file processing)
 
 
 
 
 
 
8
  RUN apt-get update && apt-get install -y \
9
  gcc \
10
  libmagic-dev \
 
 
 
11
  && rm -rf /var/lib/apt/lists/*
12
 
13
- # Optimize builds by pre-installing heavy libraries
14
- RUN pip install --no-cache-dir "pydantic>=2.9.0" \
15
- torch torchvision torchaudio \
16
- --extra-index-url https://download.pytorch.org/whl/cpu
17
-
18
 
 
 
 
19
  COPY requirements.txt .
20
  RUN pip install --no-cache-dir -r requirements.txt
21
 
22
- # Copy all project files
23
  COPY --chown=appuser:appuser . .
24
 
25
- # Hugging Face default port
26
- EXPOSE 7860
 
 
 
27
 
28
- USER appuser
29
- # RUN chmod -R 777 /app/data && chmod -R 777 /app/chroma_db
30
- COPY ./data /app/data
 
 
 
31
 
32
- # Update Healthcheck in Dockerfile
33
- HEALTHCHECK --interval=30s --timeout=3s \
34
- CMD curl --fail http://localhost:7860/health || exit 1
 
 
 
 
 
 
35
 
36
- # Ensure uvicorn runs on 7860
37
- CMD ["python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
 
1
+ # FROM python:3.10.9-slim
2
+
3
+ # RUN useradd -m -u 1000 appuser
4
+ # WORKDIR /app
5
+
6
+ # RUN apt-get update && apt-get install -y \
7
+ # gcc \
8
+ # libmagic-dev \
9
+ # && rm -rf /var/lib/apt/lists/*
10
+
11
+ # RUN pip install --no-cache-dir "pydantic>=2.9.0" \
12
+ # torch torchvision torchaudio \
13
+ # --extra-index-url https://download.pytorch.org/whl/cpu
14
+
15
+
16
+ # COPY requirements.txt .
17
+ # RUN pip install --no-cache-dir -r requirements.txt
18
+
19
+ # COPY --chown=appuser:appuser . .
20
+
21
+ # EXPOSE 7860
22
+
23
+ # USER appuser
24
+ # COPY ./data /app/data
25
+
26
+ # HEALTHCHECK --interval=30s --timeout=3s \
27
+ # CMD curl --fail http://localhost:7860/health || exit 1
28
+
29
+ # # Ensure uvicorn runs on 7860
30
+ # CMD ["python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
31
+
32
+
33
+
34
 
 
 
 
35
 
36
+
37
+
38
+
39
+
40
+ FROM python:3.10.9-slim
41
+
42
+ # Install system dependencies + Monitoring tools
43
  RUN apt-get update && apt-get install -y \
44
  gcc \
45
  libmagic-dev \
46
+ curl \
47
+ prometheus \
48
+ grafana-server \
49
  && rm -rf /var/lib/apt/lists/*
50
 
51
+ RUN useradd -m -u 1000 appuser
52
+ WORKDIR /app
 
 
 
53
 
54
+ # RAG dependencies (Same as before)
55
+ RUN pip install --no-cache-dir "pydantic>=2.9.0" \
56
+ torch torchvision torchaudio --extra-index-url download.pytorch.org
57
  COPY requirements.txt .
58
  RUN pip install --no-cache-dir -r requirements.txt
59
 
60
+ # Copy all project files (including monitoring folder)
61
  COPY --chown=appuser:appuser . .
62
 
63
+ # Setup Grafana Provisioning
64
+ RUN mkdir -p /etc/grafana/provisioning/datasources /etc/grafana/provisioning/dashboards
65
+ COPY monitoring/grafana/datasources/prometheus.yml /etc/grafana/provisioning/datasources/
66
+ COPY monitoring/grafana/dashboards/dashboard_provider.yml /etc/grafana/provisioning/dashboards/
67
+ COPY monitoring/grafana/dashboards/agent_dashboard.json /etc/grafana/provisioning/dashboards/
68
 
69
+ # Ensure write permissions for DBs and Logs
70
+ RUN mkdir -p /tmp/prometheus /tmp/grafana && \
71
+ chmod -R 777 /tmp/prometheus /tmp/grafana ./data ./chroma_db
72
+
73
+ # Expose main HF port
74
+ EXPOSE 7860
75
 
76
+ # --- STARTUP SCRIPT ---
77
+ # We create a script to run Prometheus, Grafana, and your App together
78
+ RUN echo '#!/bin/bash\n\
79
+ prometheus --config.file=monitoring/prometheus.yml --storage.tsdb.path=/tmp/prometheus &\n\
80
+ /usr/sbin/grafana-server --homepath /usr/share/grafana --config /etc/grafana/grafana.ini \
81
+ cfg:default.paths.data=/tmp/grafana \
82
+ cfg:default.paths.logs=/tmp/grafana \
83
+ cfg:server.http_port=3000 &\n\
84
+ python -m uvicorn main:app --host 0.0.0.0 --port 7860' > /app/start.sh && chmod +x /app/start.sh
85
 
86
+ USER appuser
87
+ CMD ["/app/start.sh"]
main.py CHANGED
@@ -8,6 +8,9 @@ from prometheus_client import generate_latest, CONTENT_TYPE_LATEST
8
  import os
9
  from schemas import ChatRequest, ChatResponse
10
  from agent import SupportAgent
 
 
 
11
 
12
  os.environ["ANONYMIZED_TELEMETRY"] = "False"
13
 
@@ -30,6 +33,30 @@ async def lifespan(app: FastAPI):
30
 
31
  app = FastAPI(title="SmartCoffee AI 2026", lifespan=lifespan)
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  # Replaces Nginx Security Headers & CORS
34
  app.add_middleware(
35
  CORSMiddleware,
 
8
  import os
9
  from schemas import ChatRequest, ChatResponse
10
  from agent import SupportAgent
11
+ import httpx
12
+ from fastapi import Request
13
+ from starlette.responses import StreamingResponse
14
 
15
  os.environ["ANONYMIZED_TELEMETRY"] = "False"
16
 
 
33
 
34
  app = FastAPI(title="SmartCoffee AI 2026", lifespan=lifespan)
35
 
36
+
37
+
38
+
39
+ """
40
+ Adding the dashaboard for monitoring
41
+ """
42
+ @app.api_route("/grafana/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
43
+ async def grafana_proxy(request: Request, path: str):
44
+ # Proxy requests from HF_URL/grafana to internal port 3000
45
+ client = httpx.AsyncClient(base_url="http://localhost:3000")
46
+ url = httpx.URL(path=path, query=request.query_params.raw)
47
+
48
+ # Forward the request to internal Grafana
49
+ rp_req = client.build_request(
50
+ request.method, url, headers=request.headers.raw, content=await request.body()
51
+ )
52
+ rp_resp = await client.send(rp_req, stream=True)
53
+ return StreamingResponse(
54
+ rp_resp.aiter_raw(),
55
+ status_code=rp_resp.status_code,
56
+ headers=rp_resp.headers
57
+ )
58
+
59
+
60
  # Replaces Nginx Security Headers & CORS
61
  app.add_middleware(
62
  CORSMiddleware,
monitoring/grafana/datasources/prometheus.yml CHANGED
@@ -4,6 +4,6 @@ datasources:
4
  - name: Prometheus
5
  type: prometheus
6
  access: proxy
7
- url: http://prometheus:9090
8
  isDefault: true
9
  editable: true
 
4
  - name: Prometheus
5
  type: prometheus
6
  access: proxy
7
+ url: http://localhost:9090
8
  isDefault: true
9
  editable: true
monitoring/prometheus.yml CHANGED
@@ -3,23 +3,13 @@ global:
3
  evaluation_interval: 15s
4
 
5
  scrape_configs:
6
- # Job 1: Collects User Feedback metrics from FastAPI
7
- - job_name: 'fastapi-gateway'
8
  metrics_path: '/metrics'
9
  static_configs:
10
- - targets: ['fastapi-gateway:8000', 'host.docker.internal:8000']
11
-
12
- # Job 2: Collects LLM Latency & Token metrics from the Worker
13
- - job_name: 'agent-worker'
14
- static_configs:
15
- - targets: ['agent-worker:8001']
16
- metrics_path: '/'
17
-
18
- # Job 3: Infrastructure and Health
19
- - job_name: 'cadvisor'
20
- static_configs:
21
- - targets: ['cadvisor:8080']
22
 
 
23
  - job_name: 'prometheus'
24
  static_configs:
25
  - targets: ['localhost:9090']
 
3
  evaluation_interval: 15s
4
 
5
  scrape_configs:
6
+ # Job 1: Collects all metrics from your combined FastAPI app
7
+ - job_name: 'smartcoffee-agent'
8
  metrics_path: '/metrics'
9
  static_configs:
10
+ - targets: ['localhost:7860']
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ # Job 2: Self-monitoring for Prometheus
13
  - job_name: 'prometheus'
14
  static_configs:
15
  - targets: ['localhost:9090']