NimrodDev commited on
Commit
efe7e34
·
1 Parent(s): b84c3b1
Files changed (1) hide show
  1. rag.py +24 -9
rag.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from __future__ import annotations
2
  import os, re, json, requests
3
  from functools import lru_cache
@@ -21,6 +22,13 @@ SUPABASE_URL = os.getenv("SUPABASE_URL")
21
  SUPABASE_KEY = os.getenv("SUPABASE_KEY")
22
  HF_TOKEN = os.getenv("HF_TOKEN")
23
 
 
 
 
 
 
 
 
24
  supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
25
 
26
  # ------------------------------------------------------------------ INTENT
@@ -93,6 +101,7 @@ def get_texts() -> List[str]:
93
  @lru_cache(maxsize=1)
94
  def get_vectorstore() -> FAISS:
95
  texts = get_texts()
 
96
 
97
  try:
98
  from langchain_huggingface import HuggingFaceInferenceAPIEmbeddings
@@ -101,14 +110,18 @@ def get_vectorstore() -> FAISS:
101
  model_name="sentence-transformers/all-MiniLM-L6-v2"
102
  )
103
  except ImportError:
104
- from langchain.embeddings import HuggingFaceEmbeddings
105
- embeddings = HuggingFaceEmbeddings(
106
- model_name="sentence-transformers/all-MiniLM-L6-v2"
107
- )
108
- print("⚠️ Falling back to HuggingFaceEmbeddings (local) – inference API not available")
 
 
 
 
109
 
110
  if not texts:
111
- return FAISS.from_texts([""], embeddings)
112
 
113
  splitter = RecursiveCharacterTextSplitter(chunk_size=600, chunk_overlap=50)
114
  docs = splitter.create_documents(texts, metadatas=[{"source": DATASET}] * len(texts))
@@ -124,11 +137,13 @@ def get_llm():
124
  huggingfacehub_api_token=HF_TOKEN
125
  )
126
 
127
- PROMPT = PromptTemplate.from_template("""You are Amina, assistant for {company}.
 
128
  Use only the context below. If unsure, say: "A human agent will follow up."
129
  Context: {context}
130
  Question: {question}
131
- Answer:""")
 
132
 
133
  # ------------------------------------------------------------------ MAIN
134
  def ask_question(phone: str, question: str) -> Tuple[str, List]:
@@ -160,4 +175,4 @@ def ask_question(phone: str, question: str) -> Tuple[str, List]:
160
 
161
  def _save_chat(phone: str, q: str, a: str) -> None:
162
  supabase.table("chat_memory").insert({"user_phone": phone, "role": "user", "message": q}).execute()
163
- supabase.table("chat_memory").insert({"user_phone": phone, "role": "assistant", "message": a}).execute()
 
1
+ # rag.py – bullet-proof and container-safe
2
  from __future__ import annotations
3
  import os, re, json, requests
4
  from functools import lru_cache
 
22
  SUPABASE_KEY = os.getenv("SUPABASE_KEY")
23
  HF_TOKEN = os.getenv("HF_TOKEN")
24
 
25
+ # Safe cache path for HuggingFace
26
+ CACHE_DIR = os.getenv("HF_HOME", "/tmp/hf_cache")
27
+ os.makedirs(CACHE_DIR, exist_ok=True)
28
+ os.environ["TRANSFORMERS_CACHE"] = CACHE_DIR
29
+ os.environ["HF_HOME"] = CACHE_DIR
30
+ os.environ["HF_HUB_CACHE"] = CACHE_DIR
31
+
32
  supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
33
 
34
  # ------------------------------------------------------------------ INTENT
 
101
  @lru_cache(maxsize=1)
102
  def get_vectorstore() -> FAISS:
103
  texts = get_texts()
104
+ embeddings = None
105
 
106
  try:
107
  from langchain_huggingface import HuggingFaceInferenceAPIEmbeddings
 
110
  model_name="sentence-transformers/all-MiniLM-L6-v2"
111
  )
112
  except ImportError:
113
+ try:
114
+ from langchain_huggingface import HuggingFaceEmbeddings
115
+ embeddings = HuggingFaceEmbeddings(
116
+ model_name="sentence-transformers/all-MiniLM-L6-v2",
117
+ cache_folder=CACHE_DIR
118
+ )
119
+ print("⚙️ Using HuggingFaceEmbeddings fallback")
120
+ except Exception as e:
121
+ raise RuntimeError(f"❌ Failed to load embeddings: {e}")
122
 
123
  if not texts:
124
+ return FAISS.from_texts([""], embeddings) # dummy FAISS
125
 
126
  splitter = RecursiveCharacterTextSplitter(chunk_size=600, chunk_overlap=50)
127
  docs = splitter.create_documents(texts, metadatas=[{"source": DATASET}] * len(texts))
 
137
  huggingfacehub_api_token=HF_TOKEN
138
  )
139
 
140
+ PROMPT = PromptTemplate.from_template("""
141
+ You are Amina, assistant for {company}.
142
  Use only the context below. If unsure, say: "A human agent will follow up."
143
  Context: {context}
144
  Question: {question}
145
+ Answer:
146
+ """)
147
 
148
  # ------------------------------------------------------------------ MAIN
149
  def ask_question(phone: str, question: str) -> Tuple[str, List]:
 
175
 
176
  def _save_chat(phone: str, q: str, a: str) -> None:
177
  supabase.table("chat_memory").insert({"user_phone": phone, "role": "user", "message": q}).execute()
178
+ supabase.table("chat_memory").insert({"user_phone": phone, "role": "assistant", "message": a}).execute()