LiamKhoaLe commited on
Commit
986cdbd
·
1 Parent(s): 1061730

Update logger

Browse files
Files changed (2) hide show
  1. app.py +8 -14
  2. memory.py +3 -3
app.py CHANGED
@@ -28,7 +28,6 @@ logger.setLevel(logging.DEBUG)
28
 
29
  # Debug Start
30
  logger.info("🚀 Starting Medical Chatbot API...")
31
- print("🚀 Starting Medical Chatbot API...")
32
 
33
  # ✅ Environment Variables
34
  mongo_uri = os.getenv("MONGO_URI")
@@ -47,7 +46,7 @@ def check_system_resources():
47
  cpu = psutil.cpu_percent(interval=1)
48
  disk = psutil.disk_usage("/")
49
  # Defines log info messages
50
- logger.info(f"🔍 System Resources - RAM: {memory.percent}%, CPU: {cpu}%, Disk: {disk.percent}%")
51
  if memory.percent > 85:
52
  logger.warning("⚠️ High RAM usage detected!")
53
  if cpu > 90:
@@ -85,14 +84,12 @@ app.add_middleware(
85
  index = None # Delay FAISS Index loading until first query
86
 
87
  # ✅ Load SentenceTransformer Model (Quantized/Halved)
88
- logger.info("📥 Loading SentenceTransformer Model...")
89
- print("📥 Loading SentenceTransformer Model...")
90
  MODEL_CACHE_DIR = "/app/model_cache"
91
  try:
92
  embedding_model = SentenceTransformer(MODEL_CACHE_DIR, device="cpu")
93
  embedding_model = embedding_model.half() # Reduce memory
94
  logger.info("✅ Model Loaded Successfully.")
95
- print("✅ Model Loaded Successfully.")
96
  except Exception as e:
97
  logger.error(f"❌ Model Loading Failed: {e}")
98
  exit(1)
@@ -115,17 +112,15 @@ fs = gridfs.GridFS(idb, collection="faiss_index_files")
115
  def load_faiss_index():
116
  global index
117
  if index is None:
118
- print("⏳ Loading FAISS index from GridFS...")
119
  existing_file = fs.find_one({"filename": "faiss_index.bin"})
120
  if existing_file:
121
  stored_index_bytes = existing_file.read()
122
  index_bytes_np = np.frombuffer(stored_index_bytes, dtype='uint8')
123
  index = faiss.deserialize_index(index_bytes_np)
124
- print("✅ FAISS Index Loaded")
125
- logger.info("✅ FAISS Index Loaded")
126
  else:
127
- print("❌ FAISS index not found in GridFS.")
128
- logger.error("❌ FAISS index not found in GridFS.")
129
  return index
130
 
131
  # ✅ Retrieve Medical Info
@@ -148,8 +143,7 @@ def gemini_flash_completion(prompt, model, temperature=0.7):
148
  response = client_genai.models.generate_content(model=model, contents=prompt)
149
  return response.text
150
  except Exception as e:
151
- logger.error(f"❌ Error calling Gemini API: {e}")
152
- print(f"❌ Error calling Gemini API: {e}")
153
  return "Error generating response from Gemini."
154
 
155
  # ✅ Chatbot Class
@@ -177,6 +171,7 @@ class RAGMedicalChatbot:
177
  parts.append(f"Question: {user_query}")
178
  parts.append(f"Language: {lang}")
179
  prompt = "\n\n".join(parts)
 
180
  response = gemini_flash_completion(prompt, model=self.model_name, temperature=0.7)
181
  # Store exchange + chunking
182
  if user_id:
@@ -205,8 +200,7 @@ async def chat_endpoint(req: Request):
205
 
206
  # ✅ Run Uvicorn
207
  if __name__ == "__main__":
208
- logger.info("✅ Starting FastAPI Server...")
209
- print("✅ Starting FastAPI Server...")
210
  try:
211
  uvicorn.run(app, host="0.0.0.0", port=7860, log_level="debug")
212
  except Exception as e:
 
28
 
29
  # Debug Start
30
  logger.info("🚀 Starting Medical Chatbot API...")
 
31
 
32
  # ✅ Environment Variables
33
  mongo_uri = os.getenv("MONGO_URI")
 
46
  cpu = psutil.cpu_percent(interval=1)
47
  disk = psutil.disk_usage("/")
48
  # Defines log info messages
49
+ logger.info(f"[System] 🔍 System Resources - RAM: {memory.percent}%, CPU: {cpu}%, Disk: {disk.percent}%")
50
  if memory.percent > 85:
51
  logger.warning("⚠️ High RAM usage detected!")
52
  if cpu > 90:
 
84
  index = None # Delay FAISS Index loading until first query
85
 
86
  # ✅ Load SentenceTransformer Model (Quantized/Halved)
87
+ logger.info("[Embedder] 📥 Loading SentenceTransformer Model...")
 
88
  MODEL_CACHE_DIR = "/app/model_cache"
89
  try:
90
  embedding_model = SentenceTransformer(MODEL_CACHE_DIR, device="cpu")
91
  embedding_model = embedding_model.half() # Reduce memory
92
  logger.info("✅ Model Loaded Successfully.")
 
93
  except Exception as e:
94
  logger.error(f"❌ Model Loading Failed: {e}")
95
  exit(1)
 
112
  def load_faiss_index():
113
  global index
114
  if index is None:
115
+ logger.info("[KB] ⏳ Loading FAISS index from GridFS...")
116
  existing_file = fs.find_one({"filename": "faiss_index.bin"})
117
  if existing_file:
118
  stored_index_bytes = existing_file.read()
119
  index_bytes_np = np.frombuffer(stored_index_bytes, dtype='uint8')
120
  index = faiss.deserialize_index(index_bytes_np)
121
+ logger.info("[KB] ✅ FAISS Index Loaded")
 
122
  else:
123
+ logger.error("[KB] ❌ FAISS index not found in GridFS.")
 
124
  return index
125
 
126
  # ✅ Retrieve Medical Info
 
143
  response = client_genai.models.generate_content(model=model, contents=prompt)
144
  return response.text
145
  except Exception as e:
146
+ logger.error(f"[LLM] ❌ Error calling Gemini API: {e}")
 
147
  return "Error generating response from Gemini."
148
 
149
  # ✅ Chatbot Class
 
171
  parts.append(f"Question: {user_query}")
172
  parts.append(f"Language: {lang}")
173
  prompt = "\n\n".join(parts)
174
+ logger.info(f"[LLM] Question query in `prompt`: {prompt}") # Debug out checking RAG on kb and history
175
  response = gemini_flash_completion(prompt, model=self.model_name, temperature=0.7)
176
  # Store exchange + chunking
177
  if user_id:
 
200
 
201
  # ✅ Run Uvicorn
202
  if __name__ == "__main__":
203
+ logger.info("[System] ✅ Starting FastAPI Server...")
 
204
  try:
205
  uvicorn.run(app, host="0.0.0.0", port=7860, log_level="debug")
206
  except Exception as e:
memory.py CHANGED
@@ -78,6 +78,7 @@ class MemoryManager:
78
  results.append((score, chunk))
79
  # Sort result on best scored
80
  results.sort(key=lambda x: x[0], reverse=True)
 
81
  return [f"### Topic: {c['tag']}\n{c['text']}" for _, c in results]
82
 
83
 
@@ -162,14 +163,13 @@ class MemoryManager:
162
  # ,generation_config={"temperature": 0.4} # Skip temp configs for gem-flash
163
  )
164
  output = result.text.strip()
165
- logger.info(f"📦 Gemini summarized chunk output: {output}")
166
- print(f"📦 Gemini summarized chunk output: {output}")
167
  return [
168
  {"tag": self._quick_extract_topic(chunk), "text": chunk.strip()}
169
  for chunk in output.split('---') if chunk.strip()
170
  ]
171
  except Exception as e:
172
- logger.warning(f"❌ Gemini chunking failed: {e}")
173
  retries += 1
174
  time.sleep(0.5)
175
  return [{"tag": "general", "text": response.strip()}] # fallback
 
78
  results.append((score, chunk))
79
  # Sort result on best scored
80
  results.sort(key=lambda x: x[0], reverse=True)
81
+ logger.info(f"[Memory] RAG Retrieved Topic: {results}")
82
  return [f"### Topic: {c['tag']}\n{c['text']}" for _, c in results]
83
 
84
 
 
163
  # ,generation_config={"temperature": 0.4} # Skip temp configs for gem-flash
164
  )
165
  output = result.text.strip()
166
+ logger.info(f"[Memory] 📦 Gemini summarized chunk output: {output}")
 
167
  return [
168
  {"tag": self._quick_extract_topic(chunk), "text": chunk.strip()}
169
  for chunk in output.split('---') if chunk.strip()
170
  ]
171
  except Exception as e:
172
+ logger.warning(f"[Memory] ❌ Gemini chunking failed: {e}")
173
  retries += 1
174
  time.sleep(0.5)
175
  return [{"tag": "general", "text": response.strip()}] # fallback