selvaonline commited on
Commit
21ca564
·
verified ·
1 Parent(s): dac4d09

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +44 -13
app.py CHANGED
@@ -264,15 +264,15 @@ try:
264
  from transformers import pipeline
265
 
266
  # Initialize the zero-shot classification pipeline
267
- classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
268
- print("Using facebook/bart-large-mnli for classification")
269
 
270
- # 2. Load MPNet model for semantic search
271
  from sentence_transformers import SentenceTransformer, util
272
 
273
  # Load the sentence transformer model
274
- sentence_model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2')
275
- print("Using sentence-transformers/all-mpnet-base-v2 for semantic search")
276
 
277
  # Pre-compute embeddings for category descriptions
278
  category_texts = list(category_descriptions.values())
@@ -378,17 +378,48 @@ def load_deals_from_file():
378
  # Global variable to store deals data
379
  deals_cache = None
380
 
381
- # Load deals from file on startup
382
- try:
 
 
 
 
 
 
 
 
 
383
  # Try to load from file
384
- deals_cache = load_deals_from_file()
 
 
 
 
 
 
 
 
 
 
 
 
 
385
 
386
- # If file doesn't exist or is empty, use sample data
387
- if deals_cache is None or len(deals_cache) == 0:
388
- print("No deals found in local file. Using sample data...")
 
 
 
 
 
 
 
 
 
 
389
  deals_cache = process_deals_data(SAMPLE_DEALS)
390
-
391
- print(f"Initialized with {len(deals_cache) if deals_cache else 0} deals")
392
  except Exception as e:
393
  print(f"Error initializing deals cache: {str(e)}")
394
  # Fall back to sample data
 
264
  from transformers import pipeline
265
 
266
  # Initialize the zero-shot classification pipeline
267
+ classifier = pipeline("zero-shot-classification", model="facebook/bart-base-mnli")
268
+ print("Using facebook/bart-base-mnli for classification")
269
 
270
+ # 2. Load MiniLM model for semantic search
271
  from sentence_transformers import SentenceTransformer, util
272
 
273
  # Load the sentence transformer model
274
+ sentence_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
275
+ print("Using sentence-transformers/all-MiniLM-L6-v2 for semantic search")
276
 
277
  # Pre-compute embeddings for category descriptions
278
  category_texts = list(category_descriptions.values())
 
378
  # Global variable to store deals data
379
  deals_cache = None
380
 
381
+ # Function to load deals data lazily
382
+ def get_deals_cache():
383
+ """
384
+ Load deals data lazily to avoid startup timeout
385
+ """
386
+ global deals_cache
387
+
388
+ # If deals are already loaded, return them
389
+ if deals_cache is not None:
390
+ return deals_cache
391
+
392
  # Try to load from file
393
+ try:
394
+ deals_cache = load_deals_from_file()
395
+
396
+ # If file doesn't exist or is empty, use sample data
397
+ if deals_cache is None or len(deals_cache) == 0:
398
+ print("No deals found in local file. Using sample data...")
399
+ deals_cache = process_deals_data(SAMPLE_DEALS)
400
+
401
+ print(f"Loaded {len(deals_cache) if deals_cache else 0} deals")
402
+ except Exception as e:
403
+ print(f"Error loading deals cache: {str(e)}")
404
+ # Fall back to sample data
405
+ deals_cache = process_deals_data(SAMPLE_DEALS)
406
+ print(f"Loaded {len(deals_cache)} sample deals")
407
 
408
+ return deals_cache
409
+
410
+ # Load a small subset of deals on startup to avoid timeout
411
+ try:
412
+ # Load just the first 1000 deals for fast startup
413
+ if os.path.exists(DEALS_DATA_PATH):
414
+ with open(DEALS_DATA_PATH, "r") as f:
415
+ all_deals = json.load(f)
416
+ # Take just the first 1000 deals for initial loading
417
+ deals_cache = all_deals[:1000]
418
+ print(f"Initialized with {len(deals_cache)} deals (subset of {len(all_deals)} total deals)")
419
+ else:
420
+ # Fall back to sample data
421
  deals_cache = process_deals_data(SAMPLE_DEALS)
422
+ print(f"Initialized with {len(deals_cache)} sample deals")
 
423
  except Exception as e:
424
  print(f"Error initializing deals cache: {str(e)}")
425
  # Fall back to sample data