Update app.py
Browse files
app.py
CHANGED
|
@@ -1,440 +1,451 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Fashion Advisor RAG - Hugging Face Deployment
|
| 3 |
-
Complete RAG system with FAISS vector store and local LLM
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import gradio as gr
|
| 7 |
-
import logging
|
| 8 |
-
import os
|
| 9 |
-
from pathlib import Path
|
| 10 |
-
from typing import List, Tuple, Dict, Optional
|
| 11 |
-
import pickle
|
| 12 |
-
|
| 13 |
-
# Core ML libraries
|
| 14 |
-
import torch
|
| 15 |
-
from transformers import pipeline
|
| 16 |
-
from sentence_transformers import SentenceTransformer
|
| 17 |
-
from langchain_community.vectorstores import FAISS
|
| 18 |
-
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 19 |
-
from langchain.schema import Document
|
| 20 |
-
|
| 21 |
-
# Setup logging
|
| 22 |
-
logging.basicConfig(level=logging.INFO)
|
| 23 |
-
logger = logging.getLogger(__name__)
|
| 24 |
-
|
| 25 |
-
# ============================================================================
|
| 26 |
-
# CONFIGURATION
|
| 27 |
-
# ============================================================================
|
| 28 |
-
|
| 29 |
-
CONFIG = {
|
| 30 |
-
"embedding_model": "sentence-transformers/all-MiniLM-L6-v2",
|
| 31 |
-
"llm_model": None, # Will be set during initialization
|
| 32 |
-
"vector_store_path": "
|
| 33 |
-
"top_k": 15,
|
| 34 |
-
"temperature": 0.75,
|
| 35 |
-
"max_tokens": 350,
|
| 36 |
-
}
|
| 37 |
-
|
| 38 |
-
# ============================================================================
|
| 39 |
-
# INITIALIZE MODELS
|
| 40 |
-
# ============================================================================
|
| 41 |
-
|
| 42 |
-
def initialize_llm():
|
| 43 |
-
"""Initialize free local LLM with transformers pipeline"""
|
| 44 |
-
logger.info("π Initializing FREE local language model...")
|
| 45 |
-
|
| 46 |
-
BACKUP_MODELS = [
|
| 47 |
-
"microsoft/Phi-3-mini-4k-instruct", # Primary - 3.8B, very efficient
|
| 48 |
-
"google/flan-t5-large", # Backup - 780M, good quality
|
| 49 |
-
"google/flan-t5-base", # Fallback - 250M, fast
|
| 50 |
-
]
|
| 51 |
-
|
| 52 |
-
for model_name in BACKUP_MODELS:
|
| 53 |
-
try:
|
| 54 |
-
logger.info(f" Trying {model_name}...")
|
| 55 |
-
device = 0 if torch.cuda.is_available() else -1
|
| 56 |
-
|
| 57 |
-
llm_client = pipeline(
|
| 58 |
-
"text-generation",
|
| 59 |
-
model=model_name,
|
| 60 |
-
device=device,
|
| 61 |
-
max_length=512,
|
| 62 |
-
truncation=True,
|
| 63 |
-
)
|
| 64 |
-
|
| 65 |
-
CONFIG["llm_model"] = model_name
|
| 66 |
-
logger.info(f"β
FREE LLM initialized: {model_name}")
|
| 67 |
-
logger.info(f" Device: {'GPU' if device == 0 else 'CPU'}")
|
| 68 |
-
return llm_client
|
| 69 |
-
|
| 70 |
-
except Exception as e:
|
| 71 |
-
logger.warning(f"β οΈ Failed {model_name}: {str(e)[:100]}")
|
| 72 |
-
continue
|
| 73 |
-
|
| 74 |
-
logger.error("β οΈ All models failed - will use fallback generation")
|
| 75 |
-
return None
|
| 76 |
-
|
| 77 |
-
def initialize_embeddings():
|
| 78 |
-
"""Initialize sentence transformer embeddings"""
|
| 79 |
-
logger.info("π Initializing embeddings model...")
|
| 80 |
-
|
| 81 |
-
embeddings = HuggingFaceEmbeddings(
|
| 82 |
-
model_name=CONFIG["embedding_model"],
|
| 83 |
-
model_kwargs={'device': 'cpu'},
|
| 84 |
-
encode_kwargs={'normalize_embeddings': True}
|
| 85 |
-
)
|
| 86 |
-
|
| 87 |
-
logger.info(f"β
Embeddings initialized: {CONFIG['embedding_model']}")
|
| 88 |
-
return embeddings
|
| 89 |
-
|
| 90 |
-
def load_vector_store(embeddings):
|
| 91 |
-
"""Load FAISS vector store"""
|
| 92 |
-
logger.info("π Loading FAISS vector store...")
|
| 93 |
-
|
| 94 |
-
vector_store_path = CONFIG["vector_store_path"]
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
)
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 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 |
-
|
| 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 |
-
if
|
| 280 |
-
logger.warning(f" β
|
| 281 |
-
return None
|
| 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 |
-
|
| 317 |
-
|
| 318 |
-
)
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 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 |
-
logger.
|
| 351 |
-
llm_answer =
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 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 |
-
llm_client
|
| 403 |
-
|
| 404 |
-
logger.info("
|
| 405 |
-
|
| 406 |
-
# Initialize
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Fashion Advisor RAG - Hugging Face Deployment
|
| 3 |
+
Complete RAG system with FAISS vector store and local LLM
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import logging
|
| 8 |
+
import os
|
| 9 |
+
from pathlib import Path
|
| 10 |
+
from typing import List, Tuple, Dict, Optional
|
| 11 |
+
import pickle
|
| 12 |
+
|
| 13 |
+
# Core ML libraries
|
| 14 |
+
import torch
|
| 15 |
+
from transformers import pipeline
|
| 16 |
+
from sentence_transformers import SentenceTransformer
|
| 17 |
+
from langchain_community.vectorstores import FAISS
|
| 18 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 19 |
+
from langchain.schema import Document
|
| 20 |
+
|
| 21 |
+
# Setup logging
|
| 22 |
+
logging.basicConfig(level=logging.INFO)
|
| 23 |
+
logger = logging.getLogger(__name__)
|
| 24 |
+
|
| 25 |
+
# ============================================================================
|
| 26 |
+
# CONFIGURATION
|
| 27 |
+
# ============================================================================
|
| 28 |
+
|
| 29 |
+
CONFIG = {
|
| 30 |
+
"embedding_model": "sentence-transformers/all-MiniLM-L6-v2",
|
| 31 |
+
"llm_model": None, # Will be set during initialization
|
| 32 |
+
"vector_store_path": ".", # Root directory (files are in root on HF Spaces)
|
| 33 |
+
"top_k": 15,
|
| 34 |
+
"temperature": 0.75,
|
| 35 |
+
"max_tokens": 350,
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
# ============================================================================
|
| 39 |
+
# INITIALIZE MODELS
|
| 40 |
+
# ============================================================================
|
| 41 |
+
|
| 42 |
+
def initialize_llm():
|
| 43 |
+
"""Initialize free local LLM with transformers pipeline"""
|
| 44 |
+
logger.info("π Initializing FREE local language model...")
|
| 45 |
+
|
| 46 |
+
BACKUP_MODELS = [
|
| 47 |
+
"microsoft/Phi-3-mini-4k-instruct", # Primary - 3.8B, very efficient
|
| 48 |
+
"google/flan-t5-large", # Backup - 780M, good quality
|
| 49 |
+
"google/flan-t5-base", # Fallback - 250M, fast
|
| 50 |
+
]
|
| 51 |
+
|
| 52 |
+
for model_name in BACKUP_MODELS:
|
| 53 |
+
try:
|
| 54 |
+
logger.info(f" Trying {model_name}...")
|
| 55 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 56 |
+
|
| 57 |
+
llm_client = pipeline(
|
| 58 |
+
"text-generation",
|
| 59 |
+
model=model_name,
|
| 60 |
+
device=device,
|
| 61 |
+
max_length=512,
|
| 62 |
+
truncation=True,
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
CONFIG["llm_model"] = model_name
|
| 66 |
+
logger.info(f"β
FREE LLM initialized: {model_name}")
|
| 67 |
+
logger.info(f" Device: {'GPU' if device == 0 else 'CPU'}")
|
| 68 |
+
return llm_client
|
| 69 |
+
|
| 70 |
+
except Exception as e:
|
| 71 |
+
logger.warning(f"β οΈ Failed {model_name}: {str(e)[:100]}")
|
| 72 |
+
continue
|
| 73 |
+
|
| 74 |
+
logger.error("β οΈ All models failed - will use fallback generation")
|
| 75 |
+
return None
|
| 76 |
+
|
| 77 |
+
def initialize_embeddings():
|
| 78 |
+
"""Initialize sentence transformer embeddings"""
|
| 79 |
+
logger.info("π Initializing embeddings model...")
|
| 80 |
+
|
| 81 |
+
embeddings = HuggingFaceEmbeddings(
|
| 82 |
+
model_name=CONFIG["embedding_model"],
|
| 83 |
+
model_kwargs={'device': 'cpu'},
|
| 84 |
+
encode_kwargs={'normalize_embeddings': True}
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
logger.info(f"β
Embeddings initialized: {CONFIG['embedding_model']}")
|
| 88 |
+
return embeddings
|
| 89 |
+
|
| 90 |
+
def load_vector_store(embeddings):
|
| 91 |
+
"""Load FAISS vector store"""
|
| 92 |
+
logger.info("π Loading FAISS vector store...")
|
| 93 |
+
|
| 94 |
+
vector_store_path = CONFIG["vector_store_path"]
|
| 95 |
+
|
| 96 |
+
# Check for required FAISS files
|
| 97 |
+
index_file = os.path.join(vector_store_path, "index.faiss")
|
| 98 |
+
pkl_file = os.path.join(vector_store_path, "index.pkl")
|
| 99 |
+
|
| 100 |
+
if not os.path.exists(index_file):
|
| 101 |
+
logger.error(f"β index.faiss not found at {index_file}")
|
| 102 |
+
raise FileNotFoundError(f"FAISS index file not found: {index_file}")
|
| 103 |
+
|
| 104 |
+
if not os.path.exists(pkl_file):
|
| 105 |
+
logger.error(f"β index.pkl not found at {pkl_file}")
|
| 106 |
+
raise FileNotFoundError(f"FAISS metadata file not found: {pkl_file}")
|
| 107 |
+
|
| 108 |
+
logger.info(f"β
Found index.faiss ({os.path.getsize(index_file)/1024/1024:.2f} MB)")
|
| 109 |
+
logger.info(f"β
Found index.pkl ({os.path.getsize(pkl_file)/1024:.2f} KB)")
|
| 110 |
+
|
| 111 |
+
vectorstore = FAISS.load_local(
|
| 112 |
+
vector_store_path,
|
| 113 |
+
embeddings,
|
| 114 |
+
allow_dangerous_deserialization=True
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
logger.info(f"β
FAISS vector store loaded successfully")
|
| 118 |
+
return vectorstore
|
| 119 |
+
|
| 120 |
+
# ============================================================================
|
| 121 |
+
# RAG PIPELINE FUNCTIONS
|
| 122 |
+
# ============================================================================
|
| 123 |
+
|
| 124 |
+
def retrieve_knowledge_langchain(
|
| 125 |
+
query: str,
|
| 126 |
+
vectorstore,
|
| 127 |
+
top_k: int = 15
|
| 128 |
+
) -> Tuple[List[Document], float]:
|
| 129 |
+
"""
|
| 130 |
+
Retrieve relevant documents using LangChain FAISS with query expansion
|
| 131 |
+
"""
|
| 132 |
+
logger.info(f"π Retrieving knowledge for: '{query}'")
|
| 133 |
+
|
| 134 |
+
# Create query variants for better coverage
|
| 135 |
+
query_variants = [
|
| 136 |
+
query, # Original
|
| 137 |
+
f"fashion advice clothing outfit style for {query}", # Semantic expansion
|
| 138 |
+
]
|
| 139 |
+
|
| 140 |
+
all_docs = []
|
| 141 |
+
|
| 142 |
+
# Retrieve for each variant
|
| 143 |
+
for variant in query_variants:
|
| 144 |
+
try:
|
| 145 |
+
docs_and_scores = vectorstore.similarity_search_with_score(variant, k=top_k)
|
| 146 |
+
|
| 147 |
+
for doc, score in docs_and_scores:
|
| 148 |
+
similarity = 1.0 / (1.0 + score)
|
| 149 |
+
doc.metadata['similarity'] = similarity
|
| 150 |
+
doc.metadata['query_variant'] = variant
|
| 151 |
+
all_docs.append(doc)
|
| 152 |
+
|
| 153 |
+
except Exception as e:
|
| 154 |
+
logger.error(f"Retrieval error for variant '{variant}': {e}")
|
| 155 |
+
|
| 156 |
+
# Deduplicate by content
|
| 157 |
+
unique_docs = {}
|
| 158 |
+
for doc in all_docs:
|
| 159 |
+
content_key = doc.page_content[:100]
|
| 160 |
+
if content_key not in unique_docs:
|
| 161 |
+
unique_docs[content_key] = doc
|
| 162 |
+
else:
|
| 163 |
+
# Keep document with higher similarity
|
| 164 |
+
if doc.metadata.get('similarity', 0) > unique_docs[content_key].metadata.get('similarity', 0):
|
| 165 |
+
unique_docs[content_key] = doc
|
| 166 |
+
|
| 167 |
+
final_docs = list(unique_docs.values())
|
| 168 |
+
|
| 169 |
+
# Sort by similarity
|
| 170 |
+
final_docs.sort(key=lambda x: x.metadata.get('similarity', 0), reverse=True)
|
| 171 |
+
|
| 172 |
+
# Calculate confidence
|
| 173 |
+
if final_docs:
|
| 174 |
+
avg_similarity = sum(d.metadata.get('similarity', 0) for d in final_docs) / len(final_docs)
|
| 175 |
+
confidence = min(avg_similarity, 1.0)
|
| 176 |
+
else:
|
| 177 |
+
confidence = 0.0
|
| 178 |
+
|
| 179 |
+
logger.info(f"β
Retrieved {len(final_docs)} unique documents (confidence: {confidence:.2f})")
|
| 180 |
+
|
| 181 |
+
return final_docs, confidence
|
| 182 |
+
|
| 183 |
+
def generate_llm_answer(
|
| 184 |
+
query: str,
|
| 185 |
+
retrieved_docs: List[Document],
|
| 186 |
+
llm_client,
|
| 187 |
+
attempt: int = 1
|
| 188 |
+
) -> Optional[str]:
|
| 189 |
+
"""
|
| 190 |
+
Generate answer using local LLM with retrieved context
|
| 191 |
+
"""
|
| 192 |
+
if not llm_client:
|
| 193 |
+
logger.error(" β LLM client not initialized")
|
| 194 |
+
return None
|
| 195 |
+
|
| 196 |
+
# Build focused context
|
| 197 |
+
query_lower = query.lower()
|
| 198 |
+
query_words = set(query_lower.split())
|
| 199 |
+
|
| 200 |
+
# Score documents by relevance
|
| 201 |
+
scored_docs = []
|
| 202 |
+
for doc in retrieved_docs[:20]:
|
| 203 |
+
content = doc.page_content.lower()
|
| 204 |
+
doc_words = set(content.split())
|
| 205 |
+
overlap = len(query_words.intersection(doc_words))
|
| 206 |
+
|
| 207 |
+
# Boost for verified/curated
|
| 208 |
+
if doc.metadata.get('verified', False):
|
| 209 |
+
overlap += 10
|
| 210 |
+
|
| 211 |
+
# Boost for longer content
|
| 212 |
+
if len(doc.page_content) > 200:
|
| 213 |
+
overlap += 3
|
| 214 |
+
|
| 215 |
+
scored_docs.append((doc, overlap))
|
| 216 |
+
|
| 217 |
+
# Sort and take top 8
|
| 218 |
+
scored_docs.sort(key=lambda x: x[1], reverse=True)
|
| 219 |
+
top_docs = [doc[0] for doc in scored_docs[:8]]
|
| 220 |
+
|
| 221 |
+
# Build context
|
| 222 |
+
context_parts = []
|
| 223 |
+
for doc in top_docs:
|
| 224 |
+
content = doc.page_content.strip()
|
| 225 |
+
if len(content) > 400:
|
| 226 |
+
content = content[:400] + "..."
|
| 227 |
+
context_parts.append(content)
|
| 228 |
+
|
| 229 |
+
context_text = "\n\n".join(context_parts)
|
| 230 |
+
|
| 231 |
+
# Progressive parameters based on attempt
|
| 232 |
+
if attempt == 1:
|
| 233 |
+
temperature = 0.75
|
| 234 |
+
max_tokens = 350
|
| 235 |
+
top_p = 0.92
|
| 236 |
+
repetition_penalty = 1.1
|
| 237 |
+
elif attempt == 2:
|
| 238 |
+
temperature = 0.85
|
| 239 |
+
max_tokens = 450
|
| 240 |
+
top_p = 0.94
|
| 241 |
+
repetition_penalty = 1.15
|
| 242 |
+
elif attempt == 3:
|
| 243 |
+
temperature = 0.92
|
| 244 |
+
max_tokens = 550
|
| 245 |
+
top_p = 0.96
|
| 246 |
+
repetition_penalty = 1.2
|
| 247 |
+
else:
|
| 248 |
+
temperature = 1.0
|
| 249 |
+
max_tokens = 600
|
| 250 |
+
top_p = 0.97
|
| 251 |
+
repetition_penalty = 1.25
|
| 252 |
+
|
| 253 |
+
# Create prompt
|
| 254 |
+
user_prompt = f"""[INST] Question: {query}
|
| 255 |
+
|
| 256 |
+
Fashion Knowledge:
|
| 257 |
+
{context_text}
|
| 258 |
+
|
| 259 |
+
Answer the question using the knowledge above. Be specific and helpful (100-250 words). [/INST]"""
|
| 260 |
+
|
| 261 |
+
try:
|
| 262 |
+
logger.info(f" β Calling {CONFIG['llm_model']} (temp={temperature}, tokens={max_tokens})...")
|
| 263 |
+
|
| 264 |
+
# Call pipeline
|
| 265 |
+
output = llm_client(
|
| 266 |
+
user_prompt,
|
| 267 |
+
max_new_tokens=max_tokens,
|
| 268 |
+
temperature=temperature,
|
| 269 |
+
top_p=top_p,
|
| 270 |
+
repetition_penalty=repetition_penalty,
|
| 271 |
+
do_sample=True,
|
| 272 |
+
return_full_text=False,
|
| 273 |
+
pad_token_id=llm_client.tokenizer.eos_token_id
|
| 274 |
+
)
|
| 275 |
+
|
| 276 |
+
# Extract generated text
|
| 277 |
+
response = output[0]['generated_text'].strip()
|
| 278 |
+
|
| 279 |
+
if not response:
|
| 280 |
+
logger.warning(f" β Empty response (attempt {attempt})")
|
| 281 |
+
return None
|
| 282 |
+
|
| 283 |
+
# Minimal validation
|
| 284 |
+
if len(response) < 20:
|
| 285 |
+
logger.warning(f" β Response too short: {len(response)} chars")
|
| 286 |
+
return None
|
| 287 |
+
|
| 288 |
+
# Check for apologies/refusals
|
| 289 |
+
apology_phrases = ["i cannot", "i can't", "i'm sorry", "i apologize", "i don't have"]
|
| 290 |
+
if any(phrase in response.lower()[:100] for phrase in apology_phrases):
|
| 291 |
+
logger.warning(f" β Apology detected")
|
| 292 |
+
return None
|
| 293 |
+
|
| 294 |
+
logger.info(f" β
Generated answer ({len(response)} chars)")
|
| 295 |
+
return response
|
| 296 |
+
|
| 297 |
+
except Exception as e:
|
| 298 |
+
logger.error(f" β Generation error: {e}")
|
| 299 |
+
return None
|
| 300 |
+
|
| 301 |
+
def synthesize_direct_answer(
|
| 302 |
+
query: str,
|
| 303 |
+
retrieved_docs: List[Document]
|
| 304 |
+
) -> str:
|
| 305 |
+
"""
|
| 306 |
+
Fallback: Synthesize answer directly from most relevant documents
|
| 307 |
+
"""
|
| 308 |
+
logger.info(" β Using fallback: direct synthesis")
|
| 309 |
+
|
| 310 |
+
if not retrieved_docs:
|
| 311 |
+
return "I don't have enough information to answer that question accurately."
|
| 312 |
+
|
| 313 |
+
# Get most relevant document
|
| 314 |
+
best_doc = retrieved_docs[0]
|
| 315 |
+
content = best_doc.page_content.strip()
|
| 316 |
+
|
| 317 |
+
# Create answer from top document
|
| 318 |
+
if len(content) > 500:
|
| 319 |
+
answer = content[:500] + "..."
|
| 320 |
+
else:
|
| 321 |
+
answer = content
|
| 322 |
+
|
| 323 |
+
return answer
|
| 324 |
+
|
| 325 |
+
def generate_answer_langchain(
|
| 326 |
+
query: str,
|
| 327 |
+
vectorstore,
|
| 328 |
+
llm_client
|
| 329 |
+
) -> str:
|
| 330 |
+
"""
|
| 331 |
+
Main RAG pipeline: Retrieve β Generate β Fallback
|
| 332 |
+
"""
|
| 333 |
+
logger.info(f"\n{'='*80}")
|
| 334 |
+
logger.info(f"Processing query: '{query}'")
|
| 335 |
+
logger.info(f"{'='*80}")
|
| 336 |
+
|
| 337 |
+
# Step 1: Retrieve documents
|
| 338 |
+
retrieved_docs, confidence = retrieve_knowledge_langchain(
|
| 339 |
+
query,
|
| 340 |
+
vectorstore,
|
| 341 |
+
top_k=CONFIG["top_k"]
|
| 342 |
+
)
|
| 343 |
+
|
| 344 |
+
if not retrieved_docs:
|
| 345 |
+
return "I couldn't find relevant information to answer your question."
|
| 346 |
+
|
| 347 |
+
# Step 2: Try LLM generation (4 attempts)
|
| 348 |
+
llm_answer = None
|
| 349 |
+
for attempt in range(1, 5):
|
| 350 |
+
logger.info(f"\n π€ LLM Generation Attempt {attempt}/4")
|
| 351 |
+
llm_answer = generate_llm_answer(query, retrieved_docs, llm_client, attempt)
|
| 352 |
+
|
| 353 |
+
if llm_answer:
|
| 354 |
+
logger.info(f" β
LLM answer generated successfully")
|
| 355 |
+
break
|
| 356 |
+
else:
|
| 357 |
+
logger.warning(f" β Attempt {attempt}/4 failed, retrying...")
|
| 358 |
+
|
| 359 |
+
# Step 3: Fallback if all attempts fail
|
| 360 |
+
if not llm_answer:
|
| 361 |
+
logger.error(f" β All 4 LLM attempts failed - using fallback")
|
| 362 |
+
llm_answer = synthesize_direct_answer(query, retrieved_docs)
|
| 363 |
+
|
| 364 |
+
return llm_answer
|
| 365 |
+
|
| 366 |
+
# ============================================================================
|
| 367 |
+
# GRADIO INTERFACE
|
| 368 |
+
# ============================================================================
|
| 369 |
+
|
| 370 |
+
def fashion_chatbot(message: str, history: List[List[str]]) -> str:
|
| 371 |
+
"""
|
| 372 |
+
Chatbot function for Gradio interface
|
| 373 |
+
"""
|
| 374 |
+
try:
|
| 375 |
+
if not message or not message.strip():
|
| 376 |
+
return "Please ask a fashion-related question!"
|
| 377 |
+
|
| 378 |
+
# Generate answer using RAG pipeline
|
| 379 |
+
answer = generate_answer_langchain(
|
| 380 |
+
message.strip(),
|
| 381 |
+
vectorstore,
|
| 382 |
+
llm_client
|
| 383 |
+
)
|
| 384 |
+
|
| 385 |
+
return answer
|
| 386 |
+
|
| 387 |
+
except Exception as e:
|
| 388 |
+
logger.error(f"Error in chatbot: {e}")
|
| 389 |
+
return f"Sorry, I encountered an error: {str(e)}"
|
| 390 |
+
|
| 391 |
+
# ============================================================================
|
| 392 |
+
# INITIALIZE AND LAUNCH
|
| 393 |
+
# ============================================================================
|
| 394 |
+
|
| 395 |
+
# Global variables
|
| 396 |
+
llm_client = None
|
| 397 |
+
embeddings = None
|
| 398 |
+
vectorstore = None
|
| 399 |
+
|
| 400 |
+
def startup():
|
| 401 |
+
"""Initialize all models and load vector store"""
|
| 402 |
+
global llm_client, embeddings, vectorstore
|
| 403 |
+
|
| 404 |
+
logger.info("π Starting Fashion Advisor RAG...")
|
| 405 |
+
|
| 406 |
+
# Initialize embeddings
|
| 407 |
+
embeddings = initialize_embeddings()
|
| 408 |
+
|
| 409 |
+
# Load vector store
|
| 410 |
+
vectorstore = load_vector_store(embeddings)
|
| 411 |
+
|
| 412 |
+
# Initialize LLM
|
| 413 |
+
llm_client = initialize_llm()
|
| 414 |
+
|
| 415 |
+
logger.info("β
All components initialized successfully!")
|
| 416 |
+
|
| 417 |
+
# Initialize on startup
|
| 418 |
+
startup()
|
| 419 |
+
|
| 420 |
+
# Create Gradio interface
|
| 421 |
+
demo = gr.ChatInterface(
|
| 422 |
+
fn=fashion_chatbot,
|
| 423 |
+
title="π Fashion Advisor - RAG System",
|
| 424 |
+
description="""
|
| 425 |
+
**Ask me anything about fashion!** π
|
| 426 |
+
|
| 427 |
+
I can help with:
|
| 428 |
+
- Outfit recommendations for occasions
|
| 429 |
+
- Color combinations and styling
|
| 430 |
+
- Seasonal fashion advice
|
| 431 |
+
- Body type and fit guidance
|
| 432 |
+
- Wardrobe essentials
|
| 433 |
+
|
| 434 |
+
*Powered by RAG with FAISS vector search and local LLM*
|
| 435 |
+
""",
|
| 436 |
+
examples=[
|
| 437 |
+
"What should I wear to a business meeting?",
|
| 438 |
+
"What colors go well with navy blue?",
|
| 439 |
+
"What are essential wardrobe items for fall?",
|
| 440 |
+
"How to dress for a summer wedding?",
|
| 441 |
+
"What's the best outfit for a university presentation?",
|
| 442 |
+
],
|
| 443 |
+
theme=gr.themes.Soft(),
|
| 444 |
+
retry_btn=None,
|
| 445 |
+
undo_btn="Delete Previous",
|
| 446 |
+
clear_btn="Clear Chat",
|
| 447 |
+
)
|
| 448 |
+
|
| 449 |
+
# Launch
|
| 450 |
+
if __name__ == "__main__":
|
| 451 |
+
demo.launch()
|