NimrodDev commited on
Commit
e95e29f
·
1 Parent(s): ae6d8f9
Files changed (3) hide show
  1. Dockerfile +25 -9
  2. app.py +12 -25
  3. pre_download.py +6 -3
Dockerfile CHANGED
@@ -1,24 +1,40 @@
 
1
  FROM python:3.11-slim
2
 
3
- # install build deps
4
  RUN apt-get update && apt-get install -y --no-install-recommends \
5
  build-essential \
 
 
6
  && rm -rf /var/lib/apt/lists/*
7
 
 
8
  WORKDIR /code
9
 
10
- # create writable cache dir
11
- RUN mkdir -p /code/.cache && chmod 777 /code/.cache
 
 
 
 
12
 
13
- # install python deps
14
  COPY requirements.txt .
15
- RUN pip install --no-cache-dir -r requirements.txt
16
 
17
- # ---- NEW: pre-download embedding model (build-time, online) ----
18
  COPY pre_download.py .
19
- RUN HF_HOME=/code/.cache python pre_download.py
20
 
21
- # copy app code
22
  COPY . .
23
 
24
- CMD ["gunicorn", "app:app", "-b", "0.0.0.0:7860"]
 
 
 
 
 
 
 
 
 
1
+ # ---------- Base ----------
2
  FROM python:3.11-slim
3
 
4
+ # ---------- System Dependencies ----------
5
  RUN apt-get update && apt-get install -y --no-install-recommends \
6
  build-essential \
7
+ git \
8
+ wget \
9
  && rm -rf /var/lib/apt/lists/*
10
 
11
+ # ---------- Working Directory ----------
12
  WORKDIR /code
13
 
14
+ # ---------- Environment Variables ----------
15
+ ENV PYTHONUNBUFFERED=1
16
+ ENV PYTHONDONTWRITEBYTECODE=1
17
+ ENV HF_HOME=/code/.cache/huggingface
18
+ ENV TRANSFORMERS_CACHE=/code/.cache/huggingface
19
+ ENV TORCH_HOME=/code/.cache/torch
20
 
21
+ # ---------- Install Python Dependencies ----------
22
  COPY requirements.txt .
23
+ RUN pip install --upgrade pip && pip install --no-cache-dir -r requirements.txt
24
 
25
+ # ---------- Pre-Download Embedding Model ----------
26
  COPY pre_download.py .
27
+ RUN python pre_download.py
28
 
29
+ # ---------- Copy Application Code ----------
30
  COPY . .
31
 
32
+ # ---------- Ensure FAISS and Cache Dirs Exist ----------
33
+ RUN mkdir -p /code/faiss_db /code/.cache && chmod -R 777 /code/faiss_db /code/.cache
34
+
35
+ # ---------- Expose Web Port ----------
36
+ EXPOSE 7860
37
+
38
+ # ---------- Start the App ----------
39
+ CMD ["gunicorn", "app:app", "-b", "0.0.0.0:7860", "--timeout", "300"]
40
+
app.py CHANGED
@@ -1,36 +1,23 @@
1
- # app.py
2
- from __future__ import annotations
3
- import os
4
  from flask import Flask, request, jsonify
5
- from rag import ask_question
6
 
7
- # ------------------------------------------------------------------ APP INIT
8
  app = Flask(__name__)
9
 
10
  @app.route("/webhook", methods=["POST"])
11
  def webhook():
12
- """
13
- Handles POST requests from WhatsApp or frontend chat clients.
14
- Expects JSON payload:
15
- { "phone": "<number>", "question": "<message>" }
16
- """
17
- try:
18
- payload = request.get_json(force=True)
19
- phone = payload.get("phone", "").strip()
20
- question = payload.get("question", "").strip()
21
 
22
- if not phone or not question:
23
- return jsonify({"error": "Missing 'phone' or 'question' field"}), 400
24
 
25
- answer, docs = ask_question(phone, question)
26
- return jsonify({"answer": answer, "docs": len(docs)})
27
 
28
- except Exception as e:
29
- print(f" Webhook error: {e}")
30
- return jsonify({"error": str(e)}), 500
31
 
32
-
33
- # ------------------------------------------------------------------ ENTRY POINT
34
  if __name__ == "__main__":
35
- port = int(os.getenv("PORT", 7860))
36
- app.run(host="0.0.0.0", port=port)
 
 
 
 
1
  from flask import Flask, request, jsonify
 
2
 
 
3
  app = Flask(__name__)
4
 
5
  @app.route("/webhook", methods=["POST"])
6
  def webhook():
7
+ data = request.get_json()
8
+ question = data.get("question")
9
+ phone = data.get("phone")
 
 
 
 
 
 
10
 
11
+ # retrieve docs from FAISS or Supabase (pseudo)
12
+ retrieved_docs = retriever.get_relevant_documents(question)
13
 
14
+ if not retrieved_docs:
15
+ return jsonify({"answer": "I couldn’t find relevant info on that yet.", "docs": 0})
16
 
17
+ # Generate answer
18
+ answer = qa_chain.invoke({"question": question, "context": retrieved_docs})
19
+ return jsonify({"answer": answer, "docs": len(retrieved_docs)})
20
 
 
 
21
  if __name__ == "__main__":
22
+ app.run(host="0.0.0.0", port=7860)
23
+
pre_download.py CHANGED
@@ -1,4 +1,7 @@
1
- # pre_download.py
2
  from sentence_transformers import SentenceTransformer
3
- SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
4
- print("✓ Embedding model cached at build time")
 
 
 
 
 
 
1
  from sentence_transformers import SentenceTransformer
2
+
3
+ if __name__ == "__main__":
4
+ print("🔹 Pre-downloading embedding model: all-MiniLM-L6-v2 ...")
5
+ model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
6
+ model.encode(["test"]) # trigger download
7
+ print("✅ Embedding model cached successfully.")