Ananthakr1shnan commited on
Commit
b77c84c
·
1 Parent(s): 73cf205

Updated settings

Browse files
Files changed (1) hide show
  1. src/settings.py +37 -38
src/settings.py CHANGED
@@ -105,12 +105,8 @@ class Settings:
105
 
106
  def _get_default_config_file(self) -> str:
107
  """Get default configuration file path"""
108
- # Use writable config directory
109
- running_on_hf = os.environ.get("HF_SPACE") == "1" or os.environ.get("SPACE_ID")
110
- if running_on_hf:
111
- config_dir = os.environ.get('CONFIG_DIR', '/data/researchmate/config')
112
- else:
113
- config_dir = os.environ.get('CONFIG_DIR', './tmp/researchmate/config')
114
  return str(Path(config_dir) / "settings.json")
115
 
116
  def _load_config(self):
@@ -147,12 +143,8 @@ class Settings:
147
  self.server.workers = int(os.getenv("WORKERS", self.server.workers))
148
  self.server.log_level = os.getenv("LOG_LEVEL", self.server.log_level)
149
 
150
- # Database configuration - USE ENVIRONMENT VARIABLE
151
- running_on_hf = os.environ.get("HF_SPACE") == "1" or os.environ.get("SPACE_ID")
152
- if running_on_hf:
153
- self.database.chroma_persist_dir = os.getenv("CHROMA_DIR", "/data/researchmate/chroma_persist")
154
- else:
155
- self.database.chroma_persist_dir = os.getenv("CHROMA_DIR", "./tmp/researchmate/chroma_persist")
156
  self.database.collection_name = os.getenv("COLLECTION_NAME", self.database.collection_name)
157
  self.database.similarity_threshold = float(os.getenv("SIMILARITY_THRESHOLD", self.database.similarity_threshold))
158
  self.database.max_results = int(os.getenv("MAX_RESULTS", self.database.max_results))
@@ -163,23 +155,14 @@ class Settings:
163
  self.ai_model.max_tokens = int(os.getenv("MAX_TOKENS", self.ai_model.max_tokens))
164
  self.ai_model.timeout = int(os.getenv("MODEL_TIMEOUT", self.ai_model.timeout))
165
 
166
- # Upload configuration - USE ENVIRONMENT VARIABLES
167
  self.upload.max_file_size = int(os.getenv("MAX_FILE_SIZE", self.upload.max_file_size))
168
- running_on_hf = os.environ.get("HF_SPACE") == "1" or os.environ.get("SPACE_ID")
169
- if running_on_hf:
170
- self.upload.upload_directory = os.getenv("UPLOADS_DIR", "/data/researchmate/uploads")
171
- self.upload.temp_directory = os.getenv("TEMP_DIR", "/data/researchmate/tmp")
172
- else:
173
- self.upload.upload_directory = os.getenv("UPLOADS_DIR", "./tmp/researchmate/uploads")
174
- self.upload.temp_directory = os.getenv("TEMP_DIR", "./tmp/researchmate/tmp")
175
 
176
- # Logging configuration - USE ENVIRONMENT VARIABLE
177
  self.logging.level = os.getenv("LOG_LEVEL", self.logging.level)
178
- running_on_hf = os.environ.get("HF_SPACE") == "1" or os.environ.get("SPACE_ID")
179
- if running_on_hf:
180
- self.logging.file_path = os.getenv("LOG_FILE", os.path.join(os.getenv("LOGS_DIR", "/data/researchmate/logs"), "app.log"))
181
- else:
182
- self.logging.file_path = os.getenv("LOG_FILE", os.path.join(os.getenv("LOGS_DIR", "./tmp/researchmate/logs"), "app.log"))
183
 
184
  def _validate_config(self):
185
  """Validate configuration settings"""
@@ -210,14 +193,6 @@ class Settings:
210
 
211
  def _create_directories(self):
212
  """Create necessary directories"""
213
- running_on_hf = os.environ.get("HF_SPACE") == "1" or os.environ.get("SPACE_ID")
214
- # If on Hugging Face, ensure /data exists first (should always exist, but be safe)
215
- if running_on_hf:
216
- try:
217
- Path("/data").mkdir(parents=True, exist_ok=True)
218
- except Exception:
219
- pass # Ignore if already exists or not allowed
220
-
221
  # Use writable paths from environment variables
222
  directories = [
223
  self.database.chroma_persist_dir,
@@ -226,20 +201,44 @@ class Settings:
226
  Path(self.logging.file_path).parent,
227
  Path(self.config_file).parent
228
  ]
229
-
230
  for directory in directories:
231
  try:
 
232
  Path(directory).mkdir(parents=True, exist_ok=True)
233
  print(f"Created/verified directory: {directory}")
234
  except PermissionError as e:
235
  print(f"Permission error creating directory {directory}: {e}")
236
- print(f"Current working directory: {os.getcwd()}")
237
- print(f"Directory path: {Path(directory).absolute()}")
238
- raise
 
 
 
 
 
 
 
 
 
 
239
  except Exception as e:
240
  print(f"Error creating directory {directory}: {e}")
241
  raise
242
 
 
 
 
 
 
 
 
 
 
 
 
 
 
243
  def save_config(self):
244
  """Save current configuration to file"""
245
  config_data = {
 
105
 
106
  def _get_default_config_file(self) -> str:
107
  """Get default configuration file path"""
108
+ # Use writable config directory with fallback
109
+ config_dir = os.environ.get('CONFIG_DIR', './tmp/researchmate/config')
 
 
 
 
110
  return str(Path(config_dir) / "settings.json")
111
 
112
  def _load_config(self):
 
143
  self.server.workers = int(os.getenv("WORKERS", self.server.workers))
144
  self.server.log_level = os.getenv("LOG_LEVEL", self.server.log_level)
145
 
146
+ # Database configuration - USE ENVIRONMENT VARIABLE with fallback
147
+ self.database.chroma_persist_dir = os.getenv("CHROMA_DIR", "./tmp/researchmate/chroma_persist")
 
 
 
 
148
  self.database.collection_name = os.getenv("COLLECTION_NAME", self.database.collection_name)
149
  self.database.similarity_threshold = float(os.getenv("SIMILARITY_THRESHOLD", self.database.similarity_threshold))
150
  self.database.max_results = int(os.getenv("MAX_RESULTS", self.database.max_results))
 
155
  self.ai_model.max_tokens = int(os.getenv("MAX_TOKENS", self.ai_model.max_tokens))
156
  self.ai_model.timeout = int(os.getenv("MODEL_TIMEOUT", self.ai_model.timeout))
157
 
158
+ # Upload configuration - USE ENVIRONMENT VARIABLES with fallback
159
  self.upload.max_file_size = int(os.getenv("MAX_FILE_SIZE", self.upload.max_file_size))
160
+ self.upload.upload_directory = os.getenv("UPLOADS_DIR", "./tmp/researchmate/uploads")
161
+ self.upload.temp_directory = os.getenv("TEMP_DIR", "./tmp/researchmate/tmp")
 
 
 
 
 
162
 
163
+ # Logging configuration - USE ENVIRONMENT VARIABLE with fallback
164
  self.logging.level = os.getenv("LOG_LEVEL", self.logging.level)
165
+ self.logging.file_path = os.getenv("LOG_FILE", os.path.join(os.getenv("LOGS_DIR", "./tmp/researchmate/logs"), "app.log"))
 
 
 
 
166
 
167
  def _validate_config(self):
168
  """Validate configuration settings"""
 
193
 
194
  def _create_directories(self):
195
  """Create necessary directories"""
 
 
 
 
 
 
 
 
196
  # Use writable paths from environment variables
197
  directories = [
198
  self.database.chroma_persist_dir,
 
201
  Path(self.logging.file_path).parent,
202
  Path(self.config_file).parent
203
  ]
204
+
205
  for directory in directories:
206
  try:
207
+ # Try to create the directory
208
  Path(directory).mkdir(parents=True, exist_ok=True)
209
  print(f"Created/verified directory: {directory}")
210
  except PermissionError as e:
211
  print(f"Permission error creating directory {directory}: {e}")
212
+ # If we can't create in the intended location, try a fallback
213
+ if directory.startswith('/data/'):
214
+ fallback_dir = directory.replace('/data/', './tmp/')
215
+ try:
216
+ Path(fallback_dir).mkdir(parents=True, exist_ok=True)
217
+ print(f"Created fallback directory: {fallback_dir}")
218
+ # Update the configuration to use the fallback
219
+ self._update_config_path(directory, fallback_dir)
220
+ except Exception as fallback_error:
221
+ print(f"Failed to create fallback directory {fallback_dir}: {fallback_error}")
222
+ raise
223
+ else:
224
+ raise
225
  except Exception as e:
226
  print(f"Error creating directory {directory}: {e}")
227
  raise
228
 
229
+ def _update_config_path(self, original_path: str, new_path: str):
230
+ """Update configuration paths when fallback is used"""
231
+ if self.database.chroma_persist_dir == original_path:
232
+ self.database.chroma_persist_dir = new_path
233
+ if self.upload.upload_directory == original_path:
234
+ self.upload.upload_directory = new_path
235
+ if self.upload.temp_directory == original_path:
236
+ self.upload.temp_directory = new_path
237
+ if str(Path(self.logging.file_path).parent) == original_path:
238
+ self.logging.file_path = str(Path(new_path) / Path(self.logging.file_path).name)
239
+ if str(Path(self.config_file).parent) == original_path:
240
+ self.config_file = str(Path(new_path) / Path(self.config_file).name)
241
+
242
  def save_config(self):
243
  """Save current configuration to file"""
244
  config_data = {