Fred808 commited on
Commit
fb4bc1c
·
verified ·
1 Parent(s): 7d85243

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -12
app.py CHANGED
@@ -16,7 +16,7 @@ from pydantic import BaseModel, Field
16
  from huggingface_hub import HfApi, hf_hub_download
17
 
18
  # --- Configuration ---
19
- AUTO_START_INDEX = 190 # Hardcoded default start index if no progress is found
20
  FLOW_ID = os.getenv("FLOW_ID", "flow_default")
21
  FLOW_PORT = int(os.getenv("FLOW_PORT", 8001))
22
  HF_TOKEN = os.getenv("HF_TOKEN", "")
@@ -34,7 +34,7 @@ ZIP_FILE_PREFIX = "frames_zips/"
34
 
35
  # Using the full list from the user's original code for actual deployment
36
  CAPTION_SERVERS = [
37
-
38
  "https://samfred2-isherelike-1.hf.space/analyze",
39
  "https://samfred2-isherelike-2.hf.space/analyze",
40
  "https://samfred2-isherelike-3.hf.space/analyze",
@@ -74,6 +74,7 @@ CAPTION_SERVERS = [
74
  "https://Fred800-jam-18.hf.space/analyze",
75
  "https://Fred800-jam-19.hf.space/analyze",
76
  "https://Fred800-jam-20.hf.space/analyze"
 
77
  ]
78
  MODEL_TYPE = "Florence-2-large"
79
 
@@ -532,18 +533,19 @@ async def process_dataset_task(start_index: int):
532
  # Final progress report for the current file
533
  if len(all_captions) == len(image_paths):
534
  print(f"[{FLOW_ID}] FINAL PROGRESS for {zip_full_name}: Successfully completed all {len(all_captions)} captions.")
535
- current_file_success = True
536
  else:
537
- print(f"[{FLOW_ID}] FINAL PROGRESS for {zip_full_name}: Completed with partial result: {len(all_captions)}/{len(image_paths)} captions. Marking as partial failure.")
538
- current_file_success = False
 
 
539
 
540
  # 4. Upload Results
541
  if all_captions:
542
  print(f"[{FLOW_ID}] Uploading {len(all_captions)} captions for {zip_full_name}...")
543
  if await upload_captions_to_hf(zip_full_name, all_captions):
544
  print(f"[{FLOW_ID}] Successfully uploaded captions for {zip_full_name}.")
545
- # Partial success in captioning is still a success for the upload step
546
- pass
547
  else:
548
  print(f"[{FLOW_ID}] Failed to upload captions for {zip_full_name}.")
549
  current_file_success = False
@@ -595,12 +597,22 @@ app = FastAPI(
595
  async def startup_event():
596
  print(f"Flow Server {FLOW_ID} started on port {FLOW_PORT}.")
597
 
598
- # Automatically start the processing task
599
  progress = load_progress()
600
- # Start from the last processed index + 1, or the hardcoded AUTO_START_INDEX if the progress file is new/empty
601
- start_index = progress.get('last_processed_index', 0) + 1
602
- if start_index < AUTO_START_INDEX:
603
- start_index = AUTO_START_INDEX
 
 
 
 
 
 
 
 
 
 
604
 
605
  # Use a dummy BackgroundTasks object for the startup task
606
  # Note: FastAPI's startup events can't directly use BackgroundTasks, but we can use asyncio.create_task
 
16
  from huggingface_hub import HfApi, hf_hub_download
17
 
18
  # --- Configuration ---
19
+ AUTO_START_INDEX = 0 # Hardcoded default start index if no progress is found
20
  FLOW_ID = os.getenv("FLOW_ID", "flow_default")
21
  FLOW_PORT = int(os.getenv("FLOW_PORT", 8001))
22
  HF_TOKEN = os.getenv("HF_TOKEN", "")
 
34
 
35
  # Using the full list from the user's original code for actual deployment
36
  CAPTION_SERVERS = [
37
+
38
  "https://samfred2-isherelike-1.hf.space/analyze",
39
  "https://samfred2-isherelike-2.hf.space/analyze",
40
  "https://samfred2-isherelike-3.hf.space/analyze",
 
74
  "https://Fred800-jam-18.hf.space/analyze",
75
  "https://Fred800-jam-19.hf.space/analyze",
76
  "https://Fred800-jam-20.hf.space/analyze"
77
+
78
  ]
79
  MODEL_TYPE = "Florence-2-large"
80
 
 
533
  # Final progress report for the current file
534
  if len(all_captions) == len(image_paths):
535
  print(f"[{FLOW_ID}] FINAL PROGRESS for {zip_full_name}: Successfully completed all {len(all_captions)} captions.")
 
536
  else:
537
+ print(f"[{FLOW_ID}] FINAL PROGRESS for {zip_full_name}: Completed with partial result: {len(all_captions)}/{len(image_paths)} captions.")
538
+
539
+ # Consider the file successful if we have any captions
540
+ current_file_success = len(all_captions) > 0
541
 
542
  # 4. Upload Results
543
  if all_captions:
544
  print(f"[{FLOW_ID}] Uploading {len(all_captions)} captions for {zip_full_name}...")
545
  if await upload_captions_to_hf(zip_full_name, all_captions):
546
  print(f"[{FLOW_ID}] Successfully uploaded captions for {zip_full_name}.")
547
+ # Keep current_file_success as True since we have captions and successfully uploaded them
548
+ current_file_success = True
549
  else:
550
  print(f"[{FLOW_ID}] Failed to upload captions for {zip_full_name}.")
551
  current_file_success = False
 
597
  async def startup_event():
598
  print(f"Flow Server {FLOW_ID} started on port {FLOW_PORT}.")
599
 
600
+ # Get both local progress and HF state
601
  progress = load_progress()
602
+ current_state = await download_hf_state()
603
+
604
+ # Get the next_download_index from HF state if available
605
+ hf_next_index = current_state.get("next_download_index", 0)
606
+
607
+ # If HF state has a higher index, use that instead of local progress
608
+ if hf_next_index > 0:
609
+ start_index = hf_next_index
610
+ print(f"[{FLOW_ID}] Using next_download_index from HF state: {start_index}")
611
+ else:
612
+ # Fall back to local progress if HF state doesn't have a meaningful index
613
+ start_index = progress.get('last_processed_index', 0) + 1
614
+ if start_index < AUTO_START_INDEX:
615
+ start_index = AUTO_START_INDEX
616
 
617
  # Use a dummy BackgroundTasks object for the startup task
618
  # Note: FastAPI's startup events can't directly use BackgroundTasks, but we can use asyncio.create_task