Samfredoly commited on
Commit
7c7b8a6
·
verified ·
1 Parent(s): 35457fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -29
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 Hugging Face.
334
  """
335
- # 1. Load the file_states from the local file, which is updated by client uploads.
336
- # We assume the local file exists because it's either downloaded on startup
337
- # or created by a client upload.
338
- if not os.path.exists(STATE_FILE_PATH):
339
- # If the file is missing, attempt a download from HF as a fallback
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
- # Merge the internal app_state counters (which are not critical for locking)
356
- # into the returned state for completeness, but the core file_states come from the file.
357
- state_content.update(app_state)
358
-
359
- return {"state": state_content}
360
-
361
- except Exception as e:
362
- print(f"Error reading local state file for /state/ endpoint: {e}")
363
- raise HTTPException(status_code=500, detail="Error processing state file.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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__":