Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -330,37 +330,45 @@ async def list_files():
|
|
| 330 |
async def get_state():
|
| 331 |
"""
|
| 332 |
Get the current application state (including file processing status).
|
| 333 |
-
This endpoint now ensures it fetches the latest state from
|
| 334 |
"""
|
| 335 |
-
# 1.
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
download_state_file_from_hf()
|
| 341 |
-
if not os.path.exists(STATE_FILE_PATH):
|
| 342 |
-
# If still not found (e.g., first run and 404 on HF), return default
|
| 343 |
-
return {"state": {"next_download_index": 0, "file_states": {}}}
|
| 344 |
-
|
| 345 |
-
try:
|
| 346 |
-
with open(STATE_FILE_PATH, "r") as f:
|
| 347 |
-
state_content = json.load(f)
|
| 348 |
-
|
| 349 |
-
# Ensure the required keys for the client are present
|
| 350 |
-
if "file_states" not in state_content:
|
| 351 |
-
state_content["file_states"] = {}
|
| 352 |
-
if "next_download_index" not in state_content:
|
| 353 |
-
state_content["next_download_index"] = 0
|
| 354 |
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
|
| 365 |
# --- Main execution block for testing/running ---
|
| 366 |
if __name__ == "__main__":
|
|
|
|
| 330 |
async def get_state():
|
| 331 |
"""
|
| 332 |
Get the current application state (including file processing status).
|
| 333 |
+
This endpoint now ensures it fetches the latest state from the local file.
|
| 334 |
"""
|
| 335 |
+
# 1. Always read the latest state from the local file (which gets updated by client uploads)
|
| 336 |
+
if os.path.exists(STATE_FILE_PATH):
|
| 337 |
+
try:
|
| 338 |
+
with open(STATE_FILE_PATH, "r") as f:
|
| 339 |
+
state_content = json.load(f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
|
| 341 |
+
# Ensure the required keys for the client are present
|
| 342 |
+
if "file_states" not in state_content:
|
| 343 |
+
state_content["file_states"] = {}
|
| 344 |
+
if "next_download_index" not in state_content:
|
| 345 |
+
state_content["next_download_index"] = 0
|
| 346 |
+
|
| 347 |
+
print(f"✅ Read state from file: next_download_index={state_content.get('next_download_index')}, file_states={len(state_content.get('file_states', {}))}")
|
| 348 |
+
return {"state": state_content}
|
| 349 |
+
|
| 350 |
+
except Exception as e:
|
| 351 |
+
print(f"Error reading local state file for /state/ endpoint: {e}")
|
| 352 |
+
raise HTTPException(status_code=500, detail="Error processing state file.")
|
| 353 |
+
|
| 354 |
+
# 2. If file doesn't exist, try to download from HF as fallback
|
| 355 |
+
download_state_file_from_hf()
|
| 356 |
+
if os.path.exists(STATE_FILE_PATH):
|
| 357 |
+
try:
|
| 358 |
+
with open(STATE_FILE_PATH, "r") as f:
|
| 359 |
+
state_content = json.load(f)
|
| 360 |
+
|
| 361 |
+
if "file_states" not in state_content:
|
| 362 |
+
state_content["file_states"] = {}
|
| 363 |
+
if "next_download_index" not in state_content:
|
| 364 |
+
state_content["next_download_index"] = 0
|
| 365 |
+
|
| 366 |
+
return {"state": state_content}
|
| 367 |
+
except Exception as e:
|
| 368 |
+
print(f"Error reading downloaded state file: {e}")
|
| 369 |
+
|
| 370 |
+
# 3. Return default state if file still not found
|
| 371 |
+
return {"state": {"next_download_index": 0, "file_states": {}}}
|
| 372 |
|
| 373 |
# --- Main execution block for testing/running ---
|
| 374 |
if __name__ == "__main__":
|