rahul7star commited on
Commit
9a419b5
·
verified ·
1 Parent(s): e8c29a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -23
app.py CHANGED
@@ -309,7 +309,7 @@ import uuid
309
  from pathlib import Path
310
  from huggingface_hub import hf_hub_download
311
  from fastapi.responses import JSONResponse
312
-
313
  # Constants
314
  REPO_ID = "rahul7star/ohamlab"
315
  FOLDER_IN_REPO = "filter-demo/upload_20250708_041329_9c5c81"
@@ -317,30 +317,26 @@ CONCEPT_SENTENCE = "ohamlab style"
317
  LORA_NAME = "ohami_filter_autorun"
318
  @app.get("/train-sample")
319
  def fetch_images_and_generate_captions():
320
- # Create temporary local dir
321
  local_dir = Path(f"/tmp/{LORA_NAME}-{uuid.uuid4()}")
322
  os.makedirs(local_dir, exist_ok=True)
323
 
324
- # Download all files from the given subfolder
325
- hf_hub_download(
326
  repo_id=REPO_ID,
327
  repo_type="dataset",
328
- subfolder=FOLDER_IN_REPO,
329
  local_dir=local_dir,
330
  local_dir_use_symlinks=False,
331
- force_download=False,
332
- etag_timeout=10,
333
  )
334
 
335
- # Full path to the downloaded image folder
336
- image_dir = local_dir / FOLDER_IN_REPO
337
  image_paths = list(image_dir.rglob("*.jpg")) + list(image_dir.rglob("*.jpeg")) + list(image_dir.rglob("*.png"))
338
 
339
- # If no image found, return error
340
  if not image_paths:
341
  return JSONResponse(status_code=400, content={"error": "No images found in the HF repo folder."})
342
 
343
- # Generate captions for each image
344
  captions = [
345
  f"Autogenerated caption for {img.stem} in the {CONCEPT_SENTENCE} [trigger]" for img in image_paths
346
  ]
@@ -369,7 +365,6 @@ def fetch_images_and_generate_captions():
369
 
370
 
371
 
372
-
373
 
374
 
375
  REPO_ID = "rahul7star/ohamlab"
@@ -512,18 +507,18 @@ def auto_run_lora_from_repo():
512
  local_dir = Path(f"/tmp/{LORA_NAME}-{uuid.uuid4()}")
513
  os.makedirs(local_dir, exist_ok=True)
514
 
515
- hf_hub_download(
516
- repo_id=REPO_ID,
517
- repo_type="dataset",
518
- subfolder=FOLDER_IN_REPO,
519
- local_dir=local_dir,
520
- local_dir_use_symlinks=False,
521
- force_download=False,
522
- etag_timeout=10,
523
-
524
- )
525
 
526
- image_dir = local_dir / FOLDER_IN_REPO
527
  image_paths = list(image_dir.rglob("*.jpg")) + list(image_dir.rglob("*.jpeg")) + list(image_dir.rglob("*.png"))
528
 
529
  if not image_paths:
 
309
  from pathlib import Path
310
  from huggingface_hub import hf_hub_download
311
  from fastapi.responses import JSONResponse
312
+ from huggingface_hub import snapshot_download
313
  # Constants
314
  REPO_ID = "rahul7star/ohamlab"
315
  FOLDER_IN_REPO = "filter-demo/upload_20250708_041329_9c5c81"
 
317
  LORA_NAME = "ohami_filter_autorun"
318
  @app.get("/train-sample")
319
  def fetch_images_and_generate_captions():
320
+ # Create a unique local directory
321
  local_dir = Path(f"/tmp/{LORA_NAME}-{uuid.uuid4()}")
322
  os.makedirs(local_dir, exist_ok=True)
323
 
324
+ # Download all files from the dataset repo
325
+ snapshot_path = snapshot_download(
326
  repo_id=REPO_ID,
327
  repo_type="dataset",
 
328
  local_dir=local_dir,
329
  local_dir_use_symlinks=False,
330
+ allow_patterns=[f"{FOLDER_IN_REPO}/*"], # only files inside the subfolder
 
331
  )
332
 
333
+ # Resolve image path relative to downloaded snapshot
334
+ image_dir = Path(snapshot_path) / FOLDER_IN_REPO
335
  image_paths = list(image_dir.rglob("*.jpg")) + list(image_dir.rglob("*.jpeg")) + list(image_dir.rglob("*.png"))
336
 
 
337
  if not image_paths:
338
  return JSONResponse(status_code=400, content={"error": "No images found in the HF repo folder."})
339
 
 
340
  captions = [
341
  f"Autogenerated caption for {img.stem} in the {CONCEPT_SENTENCE} [trigger]" for img in image_paths
342
  ]
 
365
 
366
 
367
 
 
368
 
369
 
370
  REPO_ID = "rahul7star/ohamlab"
 
507
  local_dir = Path(f"/tmp/{LORA_NAME}-{uuid.uuid4()}")
508
  os.makedirs(local_dir, exist_ok=True)
509
 
510
+ snapshot_path = snapshot_download(
511
+ repo_id=REPO_ID,
512
+ repo_type="dataset",
513
+ local_dir=local_dir,
514
+ local_dir_use_symlinks=False,
515
+ allow_patterns=[f"{FOLDER_IN_REPO}/*"], # only files inside the subfolder
516
+ )
517
+
518
+ # Resolve image path relative to downloaded snapshot
519
+ image_dir = Path(snapshot_path) / FOLDER_IN_REPO
520
 
521
+
522
  image_paths = list(image_dir.rglob("*.jpg")) + list(image_dir.rglob("*.jpeg")) + list(image_dir.rglob("*.png"))
523
 
524
  if not image_paths: