Spaces:
Sleeping
Sleeping
Update src/rag_engine.py
Browse files- src/rag_engine.py +29 -1
src/rag_engine.py
CHANGED
|
@@ -146,4 +146,32 @@ def list_documents(username: str = "default") -> List[str]:
|
|
| 146 |
if f.lower().endswith(('.pdf', '.txt', '.md')):
|
| 147 |
files.append(f)
|
| 148 |
|
| 149 |
-
return files
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
if f.lower().endswith(('.pdf', '.txt', '.md')):
|
| 147 |
files.append(f)
|
| 148 |
|
| 149 |
+
return files
|
| 150 |
+
|
| 151 |
+
def save_uploaded_file(uploaded_file, username: str = "default") -> str:
|
| 152 |
+
"""
|
| 153 |
+
Saves a StreamlitUploadedFile to a temporary location on disk.
|
| 154 |
+
Returns the absolute path to the saved file.
|
| 155 |
+
"""
|
| 156 |
+
try:
|
| 157 |
+
# Define the directory where files will be stored
|
| 158 |
+
# You can customize "source_documents" to match your preferred structure
|
| 159 |
+
base_dir = "source_documents"
|
| 160 |
+
user_dir = os.path.join(base_dir, username)
|
| 161 |
+
|
| 162 |
+
# Create the directory if it doesn't exist
|
| 163 |
+
os.makedirs(user_dir, exist_ok=True)
|
| 164 |
+
|
| 165 |
+
# Create the full file path
|
| 166 |
+
file_path = os.path.join(user_dir, uploaded_file.name)
|
| 167 |
+
|
| 168 |
+
# Write the file content
|
| 169 |
+
with open(file_path, "wb") as f:
|
| 170 |
+
f.write(uploaded_file.getbuffer())
|
| 171 |
+
|
| 172 |
+
logger.info(f"File saved successfully at: {file_path}")
|
| 173 |
+
return file_path
|
| 174 |
+
|
| 175 |
+
except Exception as e:
|
| 176 |
+
logger.error(f"Error saving uploaded file: {e}")
|
| 177 |
+
return None
|