vumichien commited on
Commit
69b0c8e
·
1 Parent(s): 7131999

fix dockerfile

Browse files
Files changed (3) hide show
  1. Dockerfile +5 -2
  2. app.py +7 -7
  3. download_images.py +22 -3
Dockerfile CHANGED
@@ -37,8 +37,11 @@ ENV ENV=production
37
 
38
  # Add script to download images from Hugging Face during build
39
  COPY download_images.py .
40
- RUN if [ -n "$HF_USERNAME" ] && [ -n "$HF_TOKEN" ] && [ -n "$HF_DATASET_REPO" ]; then \
41
- echo "Downloading images from Hugging Face dataset..." && \
 
 
 
42
  python download_images.py; \
43
  else \
44
  echo "Hugging Face credentials not set, skipping image download."; \
 
37
 
38
  # Add script to download images from Hugging Face during build
39
  COPY download_images.py .
40
+ # Use build secrets to access HF_TOKEN during build time
41
+ RUN --mount=type=secret,id=HF_TOKEN \
42
+ if [ -n "$HF_USERNAME" ] && [ -f /run/secrets/HF_TOKEN ] && [ -n "$HF_DATASET_REPO" ]; then \
43
+ export HF_TOKEN=$(cat /run/secrets/HF_TOKEN) && \
44
+ echo "Downloading images from Hugging Face dataset for $HF_USERNAME/$HF_DATASET_REPO..." && \
45
  python download_images.py; \
46
  else \
47
  echo "Hugging Face credentials not set, skipping image download."; \
app.py CHANGED
@@ -221,7 +221,7 @@ def get_hf_image_url(filename):
221
  """Get the URL for an image in the Hugging Face repo."""
222
  # Return the direct Space URL if available, otherwise use dataset URL
223
  if SPACE_NAME and HF_USERNAME:
224
- return f"https://{HF_USERNAME}-{SPACE_NAME}.hf.space/static/uploads/{filename}"
225
  elif HF_USERNAME:
226
  return f"https://huggingface.co/datasets/{HF_USERNAME}/{DATASET_REPO}/resolve/main/{IMAGES_PATH}/{filename}"
227
  return None
@@ -533,7 +533,7 @@ async def upload_image(
533
  and HF_USERNAME
534
  and os.environ.get("ENV", "development") == "production"
535
  ):
536
- full_url = f"https://{HF_USERNAME}-{SPACE_NAME}.hf.space/static/uploads/{unique_filename}"
537
  else:
538
  full_url = f"{request.base_url}static/uploads/{unique_filename}"
539
 
@@ -648,7 +648,7 @@ async def upload_with_replace(
648
  and HF_USERNAME
649
  and os.environ.get("ENV", "development") == "production"
650
  ):
651
- full_url = f"https://{HF_USERNAME}-{SPACE_NAME}.hf.space/static/uploads/{unique_filename}"
652
  else:
653
  full_url = f"{request.base_url}static/uploads/{unique_filename}"
654
 
@@ -693,9 +693,7 @@ async def view_image(request: Request, file_name: str):
693
  and HF_USERNAME
694
  and os.environ.get("ENV", "development") == "production"
695
  ):
696
- embed_url = (
697
- f"https://{HF_USERNAME}-{SPACE_NAME}.hf.space/static/uploads/{file_name}"
698
- )
699
  else:
700
  embed_url = f"{request.base_url}static/uploads/{file_name}"
701
 
@@ -803,7 +801,9 @@ async def startup_event():
803
  print(f" - Dataset: {DATASET_REPO}")
804
  if SPACE_NAME:
805
  print(f" - Space: {SPACE_NAME}")
806
- print(f" - Space URL: https://{HF_USERNAME}-{SPACE_NAME}.hf.space")
 
 
807
  else:
808
  print(" - Space name not set. Direct URLs will use dataset links.")
809
  else:
 
221
  """Get the URL for an image in the Hugging Face repo."""
222
  # Return the direct Space URL if available, otherwise use dataset URL
223
  if SPACE_NAME and HF_USERNAME:
224
+ return f"https://{HF_USERNAME.lower()}-{SPACE_NAME}.hf.space/static/uploads/{filename}"
225
  elif HF_USERNAME:
226
  return f"https://huggingface.co/datasets/{HF_USERNAME}/{DATASET_REPO}/resolve/main/{IMAGES_PATH}/{filename}"
227
  return None
 
533
  and HF_USERNAME
534
  and os.environ.get("ENV", "development") == "production"
535
  ):
536
+ full_url = f"https://{HF_USERNAME.lower()}-{SPACE_NAME}.hf.space/static/uploads/{unique_filename}"
537
  else:
538
  full_url = f"{request.base_url}static/uploads/{unique_filename}"
539
 
 
648
  and HF_USERNAME
649
  and os.environ.get("ENV", "development") == "production"
650
  ):
651
+ full_url = f"https://{HF_USERNAME.lower()}-{SPACE_NAME}.hf.space/static/uploads/{unique_filename}"
652
  else:
653
  full_url = f"{request.base_url}static/uploads/{unique_filename}"
654
 
 
693
  and HF_USERNAME
694
  and os.environ.get("ENV", "development") == "production"
695
  ):
696
+ embed_url = f"https://{HF_USERNAME.lower()}-{SPACE_NAME}.hf.space/static/uploads/{file_name}"
 
 
697
  else:
698
  embed_url = f"{request.base_url}static/uploads/{file_name}"
699
 
 
801
  print(f" - Dataset: {DATASET_REPO}")
802
  if SPACE_NAME:
803
  print(f" - Space: {SPACE_NAME}")
804
+ print(
805
+ f" - Space URL: https://{HF_USERNAME.lower()}-{SPACE_NAME}.hf.space"
806
+ )
807
  else:
808
  print(" - Space name not set. Direct URLs will use dataset links.")
809
  else:
download_images.py CHANGED
@@ -13,6 +13,18 @@ HF_USERNAME = os.environ.get("HF_USERNAME", "")
13
  HF_TOKEN = os.environ.get("HF_TOKEN", "")
14
  DATASET_REPO = os.environ.get("HF_DATASET_REPO", "image-uploader-data")
15
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  # Set HF cache directory to a writable location
17
  os.environ["HF_HOME"] = os.path.join(tempfile.gettempdir(), "huggingface")
18
  os.environ["HUGGINGFACE_HUB_CACHE"] = os.path.join(
@@ -39,6 +51,7 @@ try:
39
  # Check if repo exists
40
  print(f"Checking if repository {HF_USERNAME}/{DATASET_REPO} exists")
41
  hf_api.repo_info(repo_id=f"{HF_USERNAME}/{DATASET_REPO}", repo_type="dataset")
 
42
 
43
  # Download metadata first
44
  print(f"Downloading metadata from {HF_USERNAME}/{DATASET_REPO}")
@@ -58,8 +71,10 @@ try:
58
  # Save metadata locally
59
  with open(METADATA_FILE, "w") as f:
60
  json.dump(metadata, f)
 
61
  except Exception as e:
62
  print(f"Error downloading metadata: {e}")
 
63
  metadata = {}
64
  # Initialize metadata file if it doesn't exist
65
  with open(METADATA_FILE, "w") as f:
@@ -76,10 +91,11 @@ try:
76
  print(f"Found {len(image_files)} images")
77
 
78
  # Download each image
79
- for image_file in image_files:
 
80
  try:
81
  filename = os.path.basename(image_file)
82
- print(f"Downloading {filename}")
83
 
84
  # Download file
85
  download_path = hf_api.hf_hub_download(
@@ -96,10 +112,13 @@ try:
96
  dst.write(src.read())
97
 
98
  print(f"Saved {filename} to {destination}")
 
99
  except Exception as e:
100
  print(f"Error downloading {image_file}: {e}")
101
 
102
- print("Image download completed")
 
 
103
 
104
  except Exception as e:
105
  print(f"Error: {e}")
 
13
  HF_TOKEN = os.environ.get("HF_TOKEN", "")
14
  DATASET_REPO = os.environ.get("HF_DATASET_REPO", "image-uploader-data")
15
 
16
+ # Validate required environment variables
17
+ if not HF_USERNAME:
18
+ print("ERROR: HF_USERNAME environment variable is not set")
19
+ exit(1)
20
+
21
+ if not HF_TOKEN:
22
+ print("ERROR: HF_TOKEN environment variable is not set")
23
+ exit(1)
24
+
25
+ print(f"Using Hugging Face credentials for user: {HF_USERNAME}")
26
+ print(f"Dataset repository: {DATASET_REPO}")
27
+
28
  # Set HF cache directory to a writable location
29
  os.environ["HF_HOME"] = os.path.join(tempfile.gettempdir(), "huggingface")
30
  os.environ["HUGGINGFACE_HUB_CACHE"] = os.path.join(
 
51
  # Check if repo exists
52
  print(f"Checking if repository {HF_USERNAME}/{DATASET_REPO} exists")
53
  hf_api.repo_info(repo_id=f"{HF_USERNAME}/{DATASET_REPO}", repo_type="dataset")
54
+ print(f"Repository {HF_USERNAME}/{DATASET_REPO} exists")
55
 
56
  # Download metadata first
57
  print(f"Downloading metadata from {HF_USERNAME}/{DATASET_REPO}")
 
71
  # Save metadata locally
72
  with open(METADATA_FILE, "w") as f:
73
  json.dump(metadata, f)
74
+ print(f"Metadata saved to {METADATA_FILE}")
75
  except Exception as e:
76
  print(f"Error downloading metadata: {e}")
77
+ print("Creating empty metadata file")
78
  metadata = {}
79
  # Initialize metadata file if it doesn't exist
80
  with open(METADATA_FILE, "w") as f:
 
91
  print(f"Found {len(image_files)} images")
92
 
93
  # Download each image
94
+ success_count = 0
95
+ for i, image_file in enumerate(image_files):
96
  try:
97
  filename = os.path.basename(image_file)
98
+ print(f"[{i+1}/{len(image_files)}] Downloading {filename}")
99
 
100
  # Download file
101
  download_path = hf_api.hf_hub_download(
 
112
  dst.write(src.read())
113
 
114
  print(f"Saved {filename} to {destination}")
115
+ success_count += 1
116
  except Exception as e:
117
  print(f"Error downloading {image_file}: {e}")
118
 
119
+ print(
120
+ f"Image download completed. Successfully downloaded {success_count}/{len(image_files)} images."
121
+ )
122
 
123
  except Exception as e:
124
  print(f"Error: {e}")