Update app.py
Browse files
app.py
CHANGED
|
@@ -1,518 +1,514 @@
|
|
| 1 |
-
from flask import Flask, request, render_template, session, url_for, redirect, jsonify
|
| 2 |
-
from flask_session import Session
|
| 3 |
-
from langchain_core.messages import HumanMessage, AIMessage
|
| 4 |
-
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 5 |
-
import os
|
| 6 |
-
import logging
|
| 7 |
-
import re
|
| 8 |
-
import traceback
|
| 9 |
-
import base64
|
| 10 |
-
import shutil
|
| 11 |
-
import zipfile
|
| 12 |
-
from dotenv import load_dotenv
|
| 13 |
-
from huggingface_hub import hf_hub_download
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
from
|
| 19 |
-
from src.
|
| 20 |
-
from
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
logging.
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
#
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
#
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
app =
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
app.config["
|
| 82 |
-
app
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
'
|
| 99 |
-
'
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
app.
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
#
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
#
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
swarm_answer
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
)
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
# POST
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
has_image
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
logger.info("
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
if not
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
return
|
| 472 |
-
|
| 473 |
-
@app.route(
|
| 474 |
-
def
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
stats = rag_systems['
|
| 483 |
-
elif
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
rag_systems['
|
| 502 |
-
elif
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
if __name__ == "__main__":
|
| 516 |
-
logger.info("Starting Flask app for deployment testing...")
|
| 517 |
-
# This port 7860 is what Hugging Face Spaces expects by default
|
| 518 |
app.run(host="0.0.0.0", port=7860, debug=False)
|
|
|
|
| 1 |
+
from flask import Flask, request, render_template, session, url_for, redirect, jsonify
|
| 2 |
+
from flask_session import Session
|
| 3 |
+
from langchain_core.messages import HumanMessage, AIMessage
|
| 4 |
+
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 5 |
+
import os
|
| 6 |
+
import logging
|
| 7 |
+
import re
|
| 8 |
+
import traceback
|
| 9 |
+
import base64
|
| 10 |
+
import shutil
|
| 11 |
+
import zipfile
|
| 12 |
+
from dotenv import load_dotenv
|
| 13 |
+
from huggingface_hub import hf_hub_download
|
| 14 |
+
|
| 15 |
+
# --- Core Application Imports ---
|
| 16 |
+
# Make sure you have an empty __init__.py file in your 'src' folder
|
| 17 |
+
|
| 18 |
+
from src.medical_swarm import run_medical_swarm
|
| 19 |
+
from src.utils import load_rag_system, standardize_query, get_standalone_question, parse_agent_response, markdown_bold_to_html
|
| 20 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 21 |
+
|
| 22 |
+
# Setup logging
|
| 23 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 24 |
+
logger = logging.getLogger(__name__)
|
| 25 |
+
|
| 26 |
+
# Load environment variables
|
| 27 |
+
load_dotenv()
|
| 28 |
+
|
| 29 |
+
# --- 1. DATABASE SETUP FUNCTION (For Deployment) ---
|
| 30 |
+
def setup_database():
|
| 31 |
+
"""Downloads and unzips the ChromaDB folder from Hugging Face Datasets."""
|
| 32 |
+
|
| 33 |
+
# --- !!! IMPORTANT !!! ---
|
| 34 |
+
# YOU MUST CHANGE THIS to your Hugging Face Dataset repo ID
|
| 35 |
+
# For example: "your_username/your_database_repo_name"
|
| 36 |
+
DATASET_REPO_ID = "WanIrfan/atlast-db"
|
| 37 |
+
# -------------------------
|
| 38 |
+
|
| 39 |
+
ZIP_FILENAME = "chroma_db.zip"
|
| 40 |
+
DB_DIR = "chroma_db"
|
| 41 |
+
|
| 42 |
+
if os.path.exists(DB_DIR) and os.listdir(DB_DIR):
|
| 43 |
+
logger.info("✅ Database directory already exists. Skipping download.")
|
| 44 |
+
return
|
| 45 |
+
|
| 46 |
+
logger.info(f"📥 Downloading database from HF Hub: {DATASET_REPO_ID}")
|
| 47 |
+
try:
|
| 48 |
+
zip_path = hf_hub_download(
|
| 49 |
+
repo_id=DATASET_REPO_ID,
|
| 50 |
+
filename=ZIP_FILENAME,
|
| 51 |
+
repo_type="dataset",
|
| 52 |
+
# You might need to add your HF token to secrets if the dataset is private
|
| 53 |
+
# token=os.getenv("HF_TOKEN")
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
logger.info(f"📦 Unzipping database from {zip_path}...")
|
| 57 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 58 |
+
zip_ref.extractall(".") # Extracts to the root, creating ./chroma_db
|
| 59 |
+
|
| 60 |
+
logger.info("✅ Database setup complete!")
|
| 61 |
+
|
| 62 |
+
# Clean up the downloaded zip file to save space
|
| 63 |
+
if os.path.exists(zip_path):
|
| 64 |
+
os.remove(zip_path)
|
| 65 |
+
|
| 66 |
+
except Exception as e:
|
| 67 |
+
logger.error(f"❌ CRITICAL ERROR setting up database: {e}", exc_info=True)
|
| 68 |
+
# This will likely cause the RAG system to fail loading, which is expected
|
| 69 |
+
# if the database isn't available.
|
| 70 |
+
|
| 71 |
+
# --- RUN DATABASE SETUP *BEFORE* INITIALIZING THE APP ---
|
| 72 |
+
setup_database()
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
# --- STANDARD FLASK APP INITIALIZATION ---
|
| 76 |
+
app = Flask(__name__)
|
| 77 |
+
app.secret_key = os.urandom(24) # Set a secret key for session signing
|
| 78 |
+
|
| 79 |
+
# --- CONFIGURE SERVER-SIDE SESSIONS ---
|
| 80 |
+
app.config["SESSION_PERMANENT"] = False
|
| 81 |
+
app.config["SESSION_TYPE"] = "filesystem"
|
| 82 |
+
Session(app)
|
| 83 |
+
|
| 84 |
+
google_api_key = os.getenv("GOOGLE_API_KEY")
|
| 85 |
+
if not google_api_key:
|
| 86 |
+
logger.warning("⚠️ GOOGLE_API_KEY not found in environment variables. LLM calls will fail.")
|
| 87 |
+
else:
|
| 88 |
+
logger.info("GOOGLE_API_KEY loaded successfully.")
|
| 89 |
+
|
| 90 |
+
# Initialize LLM
|
| 91 |
+
llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash", temperature=0.05, google_api_key=google_api_key)
|
| 92 |
+
|
| 93 |
+
# --- LOAD RAG SYSTEMS (AFTER DB SETUP) ---
|
| 94 |
+
logger.info("🌟 Starting Multi-Domain AI Assistant...")
|
| 95 |
+
try:
|
| 96 |
+
rag_systems = {
|
| 97 |
+
'medical': load_rag_system(collection_name="medical_csv_Agentic_retrieval", domain="medical"),
|
| 98 |
+
'islamic': load_rag_system(collection_name="islamic_texts_Agentic_retrieval", domain="islamic"),
|
| 99 |
+
'insurance': load_rag_system(collection_name="etiqa_Agentic_retrieval", domain="insurance")
|
| 100 |
+
}
|
| 101 |
+
except Exception as e:
|
| 102 |
+
logger.error(f"❌ FAILED to load RAG systems. Check database path and permissions. Error: {e}", exc_info=True)
|
| 103 |
+
rag_systems = {'medical': None, 'islamic': None, 'insurance': None}
|
| 104 |
+
|
| 105 |
+
# Store systems and LLM on the app for blueprints
|
| 106 |
+
app.rag_systems = rag_systems
|
| 107 |
+
app.llm = llm
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
# Check initialization status
|
| 111 |
+
logger.info("\n📊 SYSTEM STATUS:")
|
| 112 |
+
for domain, system in rag_systems.items():
|
| 113 |
+
status = "✅ Ready" if system else "❌ Failed (DB missing?)"
|
| 114 |
+
logger.info(f" {domain}: {status}")
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
# --- FLASK ROUTES ---
|
| 118 |
+
|
| 119 |
+
@app.route("/")
|
| 120 |
+
def homePage():
|
| 121 |
+
# Clear all session history when visiting the home page
|
| 122 |
+
session.pop('medical_history', None)
|
| 123 |
+
session.pop('islamic_history', None)
|
| 124 |
+
session.pop('insurance_history', None)
|
| 125 |
+
session.pop('current_medical_document', None)
|
| 126 |
+
return render_template("homePage.html")
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
@app.route("/medical", methods=["GET", "POST"])
|
| 130 |
+
def medical_page():
|
| 131 |
+
# Use session for history and document context
|
| 132 |
+
if request.method == "GET":
|
| 133 |
+
# Load all latest data from session (or default to empty if not found)
|
| 134 |
+
latest_response = session.pop('latest_medical_response', {}) # POP to clear it after one display
|
| 135 |
+
|
| 136 |
+
answer = latest_response.get('answer', "")
|
| 137 |
+
thoughts = latest_response.get('thoughts', "")
|
| 138 |
+
validation = latest_response.get('validation', "")
|
| 139 |
+
source = latest_response.get('source', "")
|
| 140 |
+
|
| 141 |
+
# Clear history only when a user first navigates (not on redirect)
|
| 142 |
+
if not latest_response and 'medical_history' not in session:
|
| 143 |
+
session.pop('current_medical_document', None)
|
| 144 |
+
|
| 145 |
+
return render_template("medical_page.html",
|
| 146 |
+
history=session.get('medical_history', []),
|
| 147 |
+
answer=answer,
|
| 148 |
+
thoughts=thoughts,
|
| 149 |
+
validation=validation,
|
| 150 |
+
source=source)
|
| 151 |
+
|
| 152 |
+
# POST Request Logic
|
| 153 |
+
answer, thoughts, validation, source = "", "", "", ""
|
| 154 |
+
history = session.get('medical_history', [])
|
| 155 |
+
current_medical_document = session.get('current_medical_document', "")
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
try:
|
| 159 |
+
query=standardize_query(request.form.get("query", ""))
|
| 160 |
+
has_image = 'image' in request.files and request.files['image'].filename
|
| 161 |
+
has_document = 'document' in request.files and request.files['document'].filename
|
| 162 |
+
has_query = request.form.get("query") or request.form.get("question", "")
|
| 163 |
+
|
| 164 |
+
logger.info(f"POST request received: has_image={has_image}, has_document={has_document}, has_query={has_query}")
|
| 165 |
+
|
| 166 |
+
if has_document:
|
| 167 |
+
# Scenario 3: Query + Document
|
| 168 |
+
logger.info("Processing Scenario 3: Query + Document with Medical Swarm")
|
| 169 |
+
file = request.files['document']
|
| 170 |
+
try:
|
| 171 |
+
# Store the new document text in the session
|
| 172 |
+
document_text = file.read().decode("utf-8")
|
| 173 |
+
session['current_medical_document'] = document_text
|
| 174 |
+
current_medical_document = document_text # Use the new document for this turn
|
| 175 |
+
except UnicodeDecodeError:
|
| 176 |
+
answer = "Error: Could not decode the uploaded document. Please ensure it is a valid text or PDF file."
|
| 177 |
+
logger.error("Scenario 3: Document decode error")
|
| 178 |
+
thoughts = traceback.format_exc()
|
| 179 |
+
|
| 180 |
+
swarm_answer = run_medical_swarm(current_medical_document, query)
|
| 181 |
+
answer = markdown_bold_to_html(swarm_answer)
|
| 182 |
+
|
| 183 |
+
history.append(HumanMessage(content=f"[Document Uploaded] Query: '{query}'"))
|
| 184 |
+
history.append(AIMessage(content=swarm_answer))
|
| 185 |
+
thoughts = "Swarm analysis complete. The process is orchestrated and does not use the ReAct thought process. You can now ask follow-up questions."
|
| 186 |
+
source= "Medical Swarm"
|
| 187 |
+
validation = (True, "Swarm output generated.") # Swarm has its own validation logic
|
| 188 |
+
|
| 189 |
+
elif has_image :
|
| 190 |
+
#Scenario 1
|
| 191 |
+
logger.info("Processing Multimodal RAG: Query + Image")
|
| 192 |
+
# --- Step 1 & 2: Image Setup & Vision Analysis ---
|
| 193 |
+
file = request.files['image']
|
| 194 |
+
upload_dir = "Uploads"
|
| 195 |
+
os.makedirs(upload_dir, exist_ok=True)
|
| 196 |
+
image_path = os.path.join(upload_dir, file.filename)
|
| 197 |
+
|
| 198 |
+
try:
|
| 199 |
+
file.save(image_path)
|
| 200 |
+
file.close()
|
| 201 |
+
|
| 202 |
+
with open(image_path, "rb") as img_file:
|
| 203 |
+
img_data = base64.b64encode(img_file.read()).decode("utf-8")
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
vision_prompt = f"Analyze this image and identify the main subject in a single, concise sentence. The user's query is: '{query}'"
|
| 207 |
+
message = HumanMessage(content=[
|
| 208 |
+
{"type": "text", "text": vision_prompt},
|
| 209 |
+
{"type": "image_url", "image_url": f"data:image/jpeg;base64,{img_data}"}
|
| 210 |
+
])
|
| 211 |
+
vision_response = llm.invoke([message])
|
| 212 |
+
visual_prediction = vision_response.content
|
| 213 |
+
logger.info(f"Vision Prediction: {visual_prediction}")
|
| 214 |
+
|
| 215 |
+
# --- Create an Enhanced Query ---
|
| 216 |
+
enhanced_query = (
|
| 217 |
+
f'User Query: "{query}" '
|
| 218 |
+
f'Context from an image provided by the LLM: "{visual_prediction}" '
|
| 219 |
+
'Based on the user\'s query and the context from LLM, provide a comprehensive answer.'
|
| 220 |
+
)
|
| 221 |
+
logger.info(f"Enhanced query : {enhanced_query}")
|
| 222 |
+
|
| 223 |
+
agent = rag_systems['medical']
|
| 224 |
+
if not agent: raise Exception("Medical RAG system is not loaded.")
|
| 225 |
+
response_dict = agent.answer(enhanced_query, chat_history=history)
|
| 226 |
+
answer, thoughts, validation, source = parse_agent_response(response_dict)
|
| 227 |
+
history.append(HumanMessage(content=query))
|
| 228 |
+
history.append(AIMessage(content=answer))
|
| 229 |
+
|
| 230 |
+
finally:
|
| 231 |
+
if os.path.exists(image_path):
|
| 232 |
+
try:
|
| 233 |
+
os.remove(image_path)
|
| 234 |
+
logger.info(f"Successfully deleted temporary image file: {image_path}")
|
| 235 |
+
except PermissionError as e:
|
| 236 |
+
logger.warning(f"Could not remove {image_path} after processing. "
|
| 237 |
+
f"File may be locked by another process. Error: {e}")
|
| 238 |
+
|
| 239 |
+
elif query:
|
| 240 |
+
# --- SCENARIO 2: TEXT-ONLY QUERY OR SWARM FOLLOW-UP ---
|
| 241 |
+
history_for_agent = history
|
| 242 |
+
if current_medical_document:
|
| 243 |
+
logger.info("Processing Follow-up Query for Document")
|
| 244 |
+
history_for_agent = [HumanMessage(content=f"We are discussing this document:\n{current_medical_document}")] + history
|
| 245 |
+
else:
|
| 246 |
+
logger.info("Processing Text RAG query for Medical domain")
|
| 247 |
+
|
| 248 |
+
logger.info(f"Original Query: '{query}'")
|
| 249 |
+
print(f"📚 Using chat history with {len(history)} previous messages to create standalone query")
|
| 250 |
+
standalone_query = get_standalone_question(query, history_for_agent,llm)
|
| 251 |
+
logger.info(f"Standalone Query: '{standalone_query}'")
|
| 252 |
+
|
| 253 |
+
agent = rag_systems['medical']
|
| 254 |
+
if not agent: raise Exception("Medical RAG system is not loaded.")
|
| 255 |
+
response_dict = agent.answer(standalone_query, chat_history=history_for_agent)
|
| 256 |
+
answer, thoughts, validation, source = parse_agent_response(response_dict)
|
| 257 |
+
|
| 258 |
+
history.append(HumanMessage(content=query))
|
| 259 |
+
history.append(AIMessage(content=answer))
|
| 260 |
+
|
| 261 |
+
else:
|
| 262 |
+
raise ValueError("No query or file provided.")
|
| 263 |
+
except Exception as e:
|
| 264 |
+
logger.error(f"Error on /medical page: {e}", exc_info=True)
|
| 265 |
+
answer = f"An error occurred: {e}"
|
| 266 |
+
thoughts = traceback.format_exc()
|
| 267 |
+
|
| 268 |
+
# Save updated history and LATEST RESPONSE DATA back to the session
|
| 269 |
+
session['medical_history'] = history
|
| 270 |
+
session['latest_medical_response'] = {
|
| 271 |
+
'answer': answer,
|
| 272 |
+
'thoughts': thoughts,
|
| 273 |
+
'validation': validation,
|
| 274 |
+
'source': source
|
| 275 |
+
}
|
| 276 |
+
session.modified = True
|
| 277 |
+
|
| 278 |
+
logger.debug(f"Redirecting after saving latest response.")
|
| 279 |
+
return redirect(url_for('medical_page'))
|
| 280 |
+
|
| 281 |
+
@app.route("/medical/clear")
|
| 282 |
+
def clear_medical_chat():
|
| 283 |
+
session.pop('medical_history', None)
|
| 284 |
+
session.pop('current_medical_document', None)
|
| 285 |
+
logger.info("Medical chat history cleared.")
|
| 286 |
+
return redirect(url_for('medical_page'))
|
| 287 |
+
|
| 288 |
+
@app.route("/islamic", methods=["GET", "POST"])
|
| 289 |
+
def islamic_page():
|
| 290 |
+
#Use session
|
| 291 |
+
|
| 292 |
+
if request.method == "GET":
|
| 293 |
+
# Load all latest data from session (or default to empty if not found)
|
| 294 |
+
latest_response = session.pop('latest_islamic_response', {}) # POP to clear it after one display
|
| 295 |
+
|
| 296 |
+
answer = latest_response.get('answer', "")
|
| 297 |
+
thoughts = latest_response.get('thoughts', "")
|
| 298 |
+
validation = latest_response.get('validation', "")
|
| 299 |
+
source = latest_response.get('source', "")
|
| 300 |
+
|
| 301 |
+
# Clear history only when a user first navigates (no latest_response and no current history)
|
| 302 |
+
if not latest_response and 'islamic_history' not in session:
|
| 303 |
+
session.pop('islamic_history', None)
|
| 304 |
+
|
| 305 |
+
return render_template("islamic_page.html",
|
| 306 |
+
history=session.get('islamic_history', []),
|
| 307 |
+
answer=answer,
|
| 308 |
+
thoughts=thoughts,
|
| 309 |
+
validation=validation,
|
| 310 |
+
source=source)
|
| 311 |
+
|
| 312 |
+
# POST Request Logic
|
| 313 |
+
answer, thoughts, validation, source = "", "", "", ""
|
| 314 |
+
history = session.get('islamic_history', [])
|
| 315 |
+
|
| 316 |
+
# This try/except block wraps the ENTIRE POST logic
|
| 317 |
+
try:
|
| 318 |
+
query = standardize_query(request.form.get("query", ""))
|
| 319 |
+
has_image = 'image' in request.files and request.files['image'].filename
|
| 320 |
+
|
| 321 |
+
final_query = query # Default to the original query
|
| 322 |
+
|
| 323 |
+
if has_image:
|
| 324 |
+
logger.info("Processing Multimodal RAG query for Islamic domain")
|
| 325 |
+
|
| 326 |
+
file = request.files['image']
|
| 327 |
+
|
| 328 |
+
upload_dir = "Uploads"
|
| 329 |
+
os.makedirs(upload_dir, exist_ok=True)
|
| 330 |
+
image_path = os.path.join(upload_dir, file.filename)
|
| 331 |
+
|
| 332 |
+
try:
|
| 333 |
+
file.save(image_path)
|
| 334 |
+
file.close()
|
| 335 |
+
|
| 336 |
+
with open(image_path, "rb") as img_file:
|
| 337 |
+
img_base64 = base64.b64encode(img_file.read()).decode("utf-8")
|
| 338 |
+
|
| 339 |
+
vision_prompt = f"Analyze this image's main subject. User's query is: '{query}'"
|
| 340 |
+
message = HumanMessage(content=[{"type": "text", "text": vision_prompt}, {"type": "image_url", "image_url": f"data:image/jpeg;base64,{img_base64}"}])
|
| 341 |
+
visual_prediction = llm.invoke([message]).content
|
| 342 |
+
|
| 343 |
+
enhanced_query = (
|
| 344 |
+
f'User Query: "{query}" '
|
| 345 |
+
f'Context from an image provided by the LLM: "{visual_prediction}" '
|
| 346 |
+
'Based on the user\'s query and the context from LLM, provide a comprehensive answer.'
|
| 347 |
+
)
|
| 348 |
+
logger.info(f"Create enchanced query : {enhanced_query}")
|
| 349 |
+
|
| 350 |
+
final_query = enhanced_query
|
| 351 |
+
|
| 352 |
+
finally:
|
| 353 |
+
if os.path.exists(image_path):
|
| 354 |
+
try:
|
| 355 |
+
os.remove(image_path)
|
| 356 |
+
logger.info(f"Successfully cleaned up {image_path}")
|
| 357 |
+
except PermissionError as e:
|
| 358 |
+
logger.warning(f"Could not remove {image_path} after processing. "
|
| 359 |
+
f"File may be locked. Error: {e}")
|
| 360 |
+
|
| 361 |
+
elif query: # Only run text logic if there's a query and no image
|
| 362 |
+
logger.info("Processing Text RAG query for Islamic domain")
|
| 363 |
+
standalone_query = get_standalone_question(query, history,llm)
|
| 364 |
+
logger.info(f"Original Query: '{query}'")
|
| 365 |
+
print(f"📚 Using chat history with {len(history)} previous messages to create standalone query")
|
| 366 |
+
logger.info(f"Standalone Query: '{standalone_query}'")
|
| 367 |
+
final_query = standalone_query
|
| 368 |
+
|
| 369 |
+
if not final_query:
|
| 370 |
+
raise ValueError("No query or file provided.")
|
| 371 |
+
|
| 372 |
+
agent = rag_systems['islamic']
|
| 373 |
+
if not agent: raise Exception("Islamic RAG system is not loaded.")
|
| 374 |
+
response_dict = agent.answer(final_query, chat_history=history)
|
| 375 |
+
answer, thoughts , validation, source = parse_agent_response(response_dict)
|
| 376 |
+
history.append(HumanMessage(content=query))
|
| 377 |
+
history.append(AIMessage(content=answer))
|
| 378 |
+
|
| 379 |
+
except Exception as e:
|
| 380 |
+
logger.error(f"Error on /islamic page: {e}", exc_info=True)
|
| 381 |
+
answer = f"An error occurred: {e}"
|
| 382 |
+
thoughts = traceback.format_exc()
|
| 383 |
+
|
| 384 |
+
# Save updated history and LATEST RESPONSE DATA back to the session
|
| 385 |
+
session['islamic_history'] = history
|
| 386 |
+
session['latest_islamic_response'] = {
|
| 387 |
+
'answer': answer,
|
| 388 |
+
'thoughts': thoughts,
|
| 389 |
+
'validation': validation,
|
| 390 |
+
'source': source
|
| 391 |
+
}
|
| 392 |
+
session.modified = True
|
| 393 |
+
|
| 394 |
+
logger.debug(f"Redirecting after saving latest response.")
|
| 395 |
+
return redirect(url_for('islamic_page'))
|
| 396 |
+
|
| 397 |
+
@app.route("/islamic/clear")
|
| 398 |
+
def clear_islamic_chat():
|
| 399 |
+
session.pop('islamic_history', None)
|
| 400 |
+
logger.info("Islamic chat history cleared.")
|
| 401 |
+
return redirect(url_for('islamic_page'))
|
| 402 |
+
|
| 403 |
+
@app.route("/insurance", methods=["GET", "POST"])
|
| 404 |
+
def insurance_page():
|
| 405 |
+
if request.method == "GET" :
|
| 406 |
+
latest_response = session.pop('latest_insurance_response',{})
|
| 407 |
+
|
| 408 |
+
answer = latest_response.get('answer', "")
|
| 409 |
+
thoughts = latest_response.get('thoughts', "")
|
| 410 |
+
validation = latest_response.get('validation', "")
|
| 411 |
+
source = latest_response.get('source', "")
|
| 412 |
+
|
| 413 |
+
if not latest_response and 'insurance_history' not in session:
|
| 414 |
+
session.pop('insurance_history', None)
|
| 415 |
+
|
| 416 |
+
return render_template("insurance_page.html", # You will need to create this HTML file
|
| 417 |
+
history=session.get('insurance_history', []),
|
| 418 |
+
answer=answer,
|
| 419 |
+
thoughts=thoughts,
|
| 420 |
+
validation=validation,
|
| 421 |
+
source=source)
|
| 422 |
+
|
| 423 |
+
# POST Request Logic
|
| 424 |
+
answer, thoughts, validation, source = "", "", "", ""
|
| 425 |
+
history = session.get('insurance_history', [])
|
| 426 |
+
|
| 427 |
+
try:
|
| 428 |
+
query = standardize_query(request.form.get("query", ""))
|
| 429 |
+
|
| 430 |
+
if query:
|
| 431 |
+
logger.info("Processing Text RAG query for Insurance domain")
|
| 432 |
+
standalone_query = get_standalone_question(query, history, llm)
|
| 433 |
+
logger.info(f"Original Query: '{query}'")
|
| 434 |
+
logger.info(f"Standalone Query: '{standalone_query}'")
|
| 435 |
+
|
| 436 |
+
agent = rag_systems['insurance']
|
| 437 |
+
if not agent: raise Exception("Insurance RAG system is not loaded.")
|
| 438 |
+
response_dict = agent.answer(standalone_query, chat_history=history)
|
| 439 |
+
answer, thoughts, validation, source = parse_agent_response(response_dict)
|
| 440 |
+
|
| 441 |
+
history.append(HumanMessage(content=query))
|
| 442 |
+
history.append(AIMessage(content=answer))
|
| 443 |
+
else:
|
| 444 |
+
raise ValueError("No query provided.")
|
| 445 |
+
|
| 446 |
+
except Exception as e:
|
| 447 |
+
logger.error(f"Error on /insurance page: {e}", exc_info=True)
|
| 448 |
+
answer = f"An error occurred: {e}"
|
| 449 |
+
thoughts = traceback.format_exc()
|
| 450 |
+
|
| 451 |
+
session['insurance_history'] = history
|
| 452 |
+
session['latest_insurance_response'] = {
|
| 453 |
+
'answer': answer,
|
| 454 |
+
'thoughts': thoughts,
|
| 455 |
+
'validation': validation,
|
| 456 |
+
'source': source
|
| 457 |
+
}
|
| 458 |
+
session.modified = True
|
| 459 |
+
|
| 460 |
+
logger.debug(f"Redirecting after saving latest response.")
|
| 461 |
+
return redirect(url_for('insurance_page'))
|
| 462 |
+
|
| 463 |
+
@app.route("/insurance/clear")
|
| 464 |
+
def clear_insurance_chat():
|
| 465 |
+
session.pop('insurance_history', None)
|
| 466 |
+
logger.info("Insurance chat history cleared.")
|
| 467 |
+
return redirect(url_for('insurance_page'))
|
| 468 |
+
|
| 469 |
+
@app.route("/about", methods=["GET"])
|
| 470 |
+
def about():
|
| 471 |
+
return render_template("about.html")
|
| 472 |
+
|
| 473 |
+
@app.route('/metrics/<domain>')
|
| 474 |
+
def get_metrics(domain):
|
| 475 |
+
"""API endpoint to get metrics for a specific domain."""
|
| 476 |
+
try:
|
| 477 |
+
if domain == "medical" and rag_systems['medical']:
|
| 478 |
+
stats = rag_systems['medical'].metrics_tracker.get_stats()
|
| 479 |
+
elif domain == "islamic" and rag_systems['islamic']:
|
| 480 |
+
stats = rag_systems['islamic'].metrics_tracker.get_stats()
|
| 481 |
+
elif domain == "insurance" and rag_systems['insurance']:
|
| 482 |
+
stats = rag_systems['insurance'].metrics_tracker.get_stats()
|
| 483 |
+
elif not rag_systems.get(domain):
|
| 484 |
+
return jsonify({"error": f"{domain} RAG system not loaded"}), 500
|
| 485 |
+
else:
|
| 486 |
+
return jsonify({"error": "Invalid domain"}), 400
|
| 487 |
+
|
| 488 |
+
return jsonify(stats)
|
| 489 |
+
except Exception as e:
|
| 490 |
+
return jsonify({"error": str(e)}), 500
|
| 491 |
+
|
| 492 |
+
@app.route('/metrics/reset/<domain>', methods=['POST'])
|
| 493 |
+
def reset_metrics(domain):
|
| 494 |
+
"""Reset metrics for a domain (useful for testing)."""
|
| 495 |
+
try:
|
| 496 |
+
if domain == "medical" and rag_systems['medical']:
|
| 497 |
+
rag_systems['medical'].metrics_tracker.reset_metrics()
|
| 498 |
+
elif domain == "islamic" and rag_systems['islamic']:
|
| 499 |
+
rag_systems['islamic'].metrics_tracker.reset_metrics()
|
| 500 |
+
elif domain == "insurance" and rag_systems['insurance']:
|
| 501 |
+
rag_systems['insurance'].metrics_tracker.reset_metrics()
|
| 502 |
+
elif not rag_systems.get(domain):
|
| 503 |
+
return jsonify({"error": f"{domain} RAG system not loaded"}), 500
|
| 504 |
+
else:
|
| 505 |
+
return jsonify({"error": "Invalid domain"}), 400
|
| 506 |
+
|
| 507 |
+
return jsonify({"success": True, "message": f"Metrics reset for {domain}"})
|
| 508 |
+
except Exception as e:
|
| 509 |
+
return jsonify({"error": str(e)}), 500
|
| 510 |
+
|
| 511 |
+
if __name__ == "__main__":
|
| 512 |
+
logger.info("Starting Flask app for deployment testing...")
|
| 513 |
+
# This port 7860 is what Hugging Face Spaces expects by default
|
|
|
|
|
|
|
|
|
|
|
|
|
| 514 |
app.run(host="0.0.0.0", port=7860, debug=False)
|