Spaces:
Sleeping
Sleeping
Commit
·
de36c63
1
Parent(s):
d16043f
Updated files
Browse files- main.py +7 -5
- src/components/config.py +7 -2
- src/settings.py +33 -6
main.py
CHANGED
@@ -88,12 +88,14 @@ async def initialize_research_mate():
|
|
88 |
print("🚀 Starting ResearchMate background initialization...")
|
89 |
|
90 |
try:
|
91 |
-
#
|
92 |
-
|
93 |
-
|
|
|
|
|
|
|
|
|
94 |
chroma_dir.mkdir(parents=True, exist_ok=True)
|
95 |
-
|
96 |
-
# Set environment variable for ChromaDB persist directory (if needed by your code)
|
97 |
os.environ["CHROMA_PERSIST_DIR"] = str(chroma_dir)
|
98 |
|
99 |
# Run initialization in thread pool to avoid blocking
|
|
|
88 |
print("🚀 Starting ResearchMate background initialization...")
|
89 |
|
90 |
try:
|
91 |
+
# Use /data on Hugging Face Spaces, else use local project-relative path
|
92 |
+
running_on_hf = os.environ.get("HF_SPACE") == "1" or os.environ.get("SPACE_ID")
|
93 |
+
if running_on_hf:
|
94 |
+
chroma_dir = Path("/data/researchmate/chroma_persist")
|
95 |
+
else:
|
96 |
+
base_dir = Path(__file__).parent.resolve()
|
97 |
+
chroma_dir = base_dir / "tmp" / "researchmate" / "chroma_persist"
|
98 |
chroma_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
99 |
os.environ["CHROMA_PERSIST_DIR"] = str(chroma_dir)
|
100 |
|
101 |
# Run initialization in thread pool to avoid blocking
|
src/components/config.py
CHANGED
@@ -88,8 +88,13 @@ class Config:
|
|
88 |
print(f"Created/verified directory: {directory}")
|
89 |
except PermissionError as e:
|
90 |
print(f"Permission error creating {directory}: {e}")
|
91 |
-
# Try to create in /
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
93 |
try:
|
94 |
Path(fallback_dir).mkdir(parents=True, exist_ok=True)
|
95 |
print(f"Created fallback directory: {fallback_dir}")
|
|
|
88 |
print(f"Created/verified directory: {directory}")
|
89 |
except PermissionError as e:
|
90 |
print(f"Permission error creating {directory}: {e}")
|
91 |
+
# Try to create in /data as fallback on Hugging Face, else ./tmp
|
92 |
+
import os
|
93 |
+
running_on_hf = os.environ.get("HF_SPACE") == "1" or os.environ.get("SPACE_ID")
|
94 |
+
if running_on_hf:
|
95 |
+
fallback_dir = f"/data/researchmate/{Path(directory).name}"
|
96 |
+
else:
|
97 |
+
fallback_dir = f"./tmp/researchmate/{Path(directory).name}"
|
98 |
try:
|
99 |
Path(fallback_dir).mkdir(parents=True, exist_ok=True)
|
100 |
print(f"Created fallback directory: {fallback_dir}")
|
src/settings.py
CHANGED
@@ -51,7 +51,14 @@ class UploadConfig:
|
|
51 |
max_file_size: int = 50 * 1024 * 1024 # 50MB
|
52 |
allowed_extensions: List[str] = None
|
53 |
upload_directory: str = "./uploads"
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
def __post_init__(self):
|
57 |
if self.allowed_extensions is None:
|
@@ -118,7 +125,11 @@ class Settings:
|
|
118 |
def _get_default_config_file(self) -> str:
|
119 |
"""Get default configuration file path"""
|
120 |
# Use writable config directory
|
121 |
-
|
|
|
|
|
|
|
|
|
122 |
return str(Path(config_dir) / "settings.json")
|
123 |
|
124 |
def _load_config(self):
|
@@ -156,7 +167,11 @@ class Settings:
|
|
156 |
self.server.log_level = os.getenv("LOG_LEVEL", self.server.log_level)
|
157 |
|
158 |
# Database configuration - USE ENVIRONMENT VARIABLE
|
159 |
-
|
|
|
|
|
|
|
|
|
160 |
self.database.collection_name = os.getenv("COLLECTION_NAME", self.database.collection_name)
|
161 |
self.database.similarity_threshold = float(os.getenv("SIMILARITY_THRESHOLD", self.database.similarity_threshold))
|
162 |
self.database.max_results = int(os.getenv("MAX_RESULTS", self.database.max_results))
|
@@ -169,12 +184,24 @@ class Settings:
|
|
169 |
|
170 |
# Upload configuration - USE ENVIRONMENT VARIABLES
|
171 |
self.upload.max_file_size = int(os.getenv("MAX_FILE_SIZE", self.upload.max_file_size))
|
172 |
-
|
173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
|
175 |
# Logging configuration - USE ENVIRONMENT VARIABLE
|
176 |
self.logging.level = os.getenv("LOG_LEVEL", self.logging.level)
|
177 |
-
|
|
|
|
|
|
|
|
|
178 |
|
179 |
def _validate_config(self):
|
180 |
"""Validate configuration settings"""
|
|
|
51 |
max_file_size: int = 50 * 1024 * 1024 # 50MB
|
52 |
allowed_extensions: List[str] = None
|
53 |
upload_directory: str = "./uploads"
|
54 |
+
import os
|
55 |
+
@property
|
56 |
+
def temp_directory(self):
|
57 |
+
running_on_hf = os.environ.get("HF_SPACE") == "1" or os.environ.get("SPACE_ID")
|
58 |
+
if running_on_hf:
|
59 |
+
return "/data/tmp"
|
60 |
+
else:
|
61 |
+
return "./tmp"
|
62 |
|
63 |
def __post_init__(self):
|
64 |
if self.allowed_extensions is None:
|
|
|
125 |
def _get_default_config_file(self) -> str:
|
126 |
"""Get default configuration file path"""
|
127 |
# Use writable config directory
|
128 |
+
running_on_hf = os.environ.get("HF_SPACE") == "1" or os.environ.get("SPACE_ID")
|
129 |
+
if running_on_hf:
|
130 |
+
config_dir = os.environ.get('CONFIG_DIR', '/data/researchmate/config')
|
131 |
+
else:
|
132 |
+
config_dir = os.environ.get('CONFIG_DIR', './tmp/researchmate/config')
|
133 |
return str(Path(config_dir) / "settings.json")
|
134 |
|
135 |
def _load_config(self):
|
|
|
167 |
self.server.log_level = os.getenv("LOG_LEVEL", self.server.log_level)
|
168 |
|
169 |
# Database configuration - USE ENVIRONMENT VARIABLE
|
170 |
+
running_on_hf = os.environ.get("HF_SPACE") == "1" or os.environ.get("SPACE_ID")
|
171 |
+
if running_on_hf:
|
172 |
+
self.database.chroma_persist_dir = os.getenv("CHROMA_DIR", "/data/researchmate/chroma_persist")
|
173 |
+
else:
|
174 |
+
self.database.chroma_persist_dir = os.getenv("CHROMA_DIR", "./tmp/researchmate/chroma_persist")
|
175 |
self.database.collection_name = os.getenv("COLLECTION_NAME", self.database.collection_name)
|
176 |
self.database.similarity_threshold = float(os.getenv("SIMILARITY_THRESHOLD", self.database.similarity_threshold))
|
177 |
self.database.max_results = int(os.getenv("MAX_RESULTS", self.database.max_results))
|
|
|
184 |
|
185 |
# Upload configuration - USE ENVIRONMENT VARIABLES
|
186 |
self.upload.max_file_size = int(os.getenv("MAX_FILE_SIZE", self.upload.max_file_size))
|
187 |
+
running_on_hf = os.environ.get("HF_SPACE") == "1" or os.environ.get("SPACE_ID")
|
188 |
+
if running_on_hf:
|
189 |
+
self.upload.upload_directory = os.getenv("UPLOADS_DIR", "/data/researchmate/uploads")
|
190 |
+
else:
|
191 |
+
self.upload.upload_directory = os.getenv("UPLOADS_DIR", "./tmp/researchmate/uploads")
|
192 |
+
running_on_hf = os.environ.get("HF_SPACE") == "1" or os.environ.get("SPACE_ID")
|
193 |
+
if running_on_hf:
|
194 |
+
self.upload.temp_directory = os.getenv("TEMP_DIR", "/data/researchmate/tmp")
|
195 |
+
else:
|
196 |
+
self.upload.temp_directory = os.getenv("TEMP_DIR", "./tmp/researchmate/tmp")
|
197 |
|
198 |
# Logging configuration - USE ENVIRONMENT VARIABLE
|
199 |
self.logging.level = os.getenv("LOG_LEVEL", self.logging.level)
|
200 |
+
running_on_hf = os.environ.get("HF_SPACE") == "1" or os.environ.get("SPACE_ID")
|
201 |
+
if running_on_hf:
|
202 |
+
self.logging.file_path = os.getenv("LOG_FILE", os.path.join(os.getenv("LOGS_DIR", "/data/researchmate/logs"), "app.log"))
|
203 |
+
else:
|
204 |
+
self.logging.file_path = os.getenv("LOG_FILE", os.path.join(os.getenv("LOGS_DIR", "./tmp/researchmate/logs"), "app.log"))
|
205 |
|
206 |
def _validate_config(self):
|
207 |
"""Validate configuration settings"""
|