HeTalksInMaths commited on
Commit
90e3e68
·
1 Parent(s): 62f1601

Add retry logic for database initialization on HF Spaces

Browse files

- Handle embedding model download failures gracefully
- Retry DB init on first query if initial load failed
- Show helpful error message to users if model can't download
- Fixes runtime error when sentence-transformers model download fails

This handles the OSError: Can't load configuration error on HF Spaces

Files changed (1) hide show
  1. app.py +48 -7
app.py CHANGED
@@ -18,18 +18,33 @@ import os
18
  logging.basicConfig(level=logging.INFO)
19
  logger = logging.getLogger(__name__)
20
 
21
- # Initialize the vector database
22
  db_path = Path("./data/benchmark_vector_db")
23
- db = BenchmarkVectorDB(
24
- db_path=db_path,
25
- embedding_model="all-MiniLM-L6-v2"
26
- )
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  # Build database if not exists (first launch on Hugging Face)
29
  # Start with a manageable size to avoid build timeout
30
- current_count = db.collection.count()
 
 
 
 
31
 
32
- if current_count == 0:
33
  logger.info("Database is empty - building database...")
34
  logger.info("Building 5K questions to stay within build time limits.")
35
 
@@ -96,9 +111,35 @@ else:
96
 
97
  def analyze_prompt(prompt: str, k: int = 5) -> str:
98
  """Analyze a prompt and return difficulty assessment."""
 
 
99
  if not prompt.strip():
100
  return "Please enter a prompt to analyze."
101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  try:
103
  result = db.query_similar_questions(prompt, k=k)
104
 
 
18
  logging.basicConfig(level=logging.INFO)
19
  logger = logging.getLogger(__name__)
20
 
21
+ # Initialize the vector database with error handling
22
  db_path = Path("./data/benchmark_vector_db")
23
+ db = None
24
+
25
+ try:
26
+ logger.info("Initializing BenchmarkVectorDB...")
27
+ db = BenchmarkVectorDB(
28
+ db_path=db_path,
29
+ embedding_model="all-MiniLM-L6-v2"
30
+ )
31
+ logger.info("✓ BenchmarkVectorDB initialized successfully")
32
+ except Exception as e:
33
+ logger.error(f"Failed to initialize BenchmarkVectorDB: {e}")
34
+ logger.error("This might be due to network issues downloading the embedding model.")
35
+ logger.error("The app will attempt to initialize on first use.")
36
+ # Will try again on first query
37
+ db = None
38
 
39
  # Build database if not exists (first launch on Hugging Face)
40
  # Start with a manageable size to avoid build timeout
41
+ if db is not None:
42
+ current_count = db.collection.count()
43
+ else:
44
+ logger.warning("Database not initialized - will retry on first query")
45
+ current_count = 0
46
 
47
+ if db is not None and current_count == 0:
48
  logger.info("Database is empty - building database...")
49
  logger.info("Building 5K questions to stay within build time limits.")
50
 
 
111
 
112
  def analyze_prompt(prompt: str, k: int = 5) -> str:
113
  """Analyze a prompt and return difficulty assessment."""
114
+ global db
115
+
116
  if not prompt.strip():
117
  return "Please enter a prompt to analyze."
118
 
119
+ # Retry DB initialization if it failed before
120
+ if db is None:
121
+ try:
122
+ logger.info("Retrying database initialization...")
123
+ db = BenchmarkVectorDB(
124
+ db_path=db_path,
125
+ embedding_model="all-MiniLM-L6-v2"
126
+ )
127
+ logger.info("✓ Database initialized successfully on retry")
128
+ except Exception as e:
129
+ return f"""### ❌ Database Initialization Error
130
+
131
+ The vector database could not be initialized due to a network error downloading the embedding model.
132
+
133
+ **Error:** {str(e)}
134
+
135
+ **This is a temporary HuggingFace Spaces issue.** Please:
136
+ 1. Wait a few minutes for the model to download
137
+ 2. Try refreshing the page
138
+ 3. Contact support if the issue persists
139
+
140
+ The embedding model `sentence-transformers/all-MiniLM-L6-v2` is being downloaded from HuggingFace Hub.
141
+ """
142
+
143
  try:
144
  result = db.query_similar_questions(prompt, k=k)
145