OppaAI commited on
Commit
aa65666
·
verified ·
1 Parent(s): f037a8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -4
app.py CHANGED
@@ -46,9 +46,12 @@ def analyze_human(clothing_color: str, estimated_action: str):
46
  # Save and upload image to HF
47
  # -----------------------------------------------------
48
  def save_and_upload_image(image_b64: str, hf_token: str):
49
- """Decode base64 image, save locally, and upload to HF dataset repo."""
50
  try:
51
  image_bytes = base64.b64decode(image_b64)
 
 
 
52
  os.makedirs("/tmp", exist_ok=True)
53
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
54
  local_path = f"/tmp/robot_img_{timestamp}.jpg"
@@ -57,12 +60,23 @@ def save_and_upload_image(image_b64: str, hf_token: str):
57
  f.write(image_bytes)
58
 
59
  filename = f"robot_{timestamp}.jpg"
60
- upload_file(local_path, path_in_repo=filename, repo_id=HF_DATASET_REPO, token=hf_token, repo_type="dataset")
 
 
 
 
 
 
 
 
 
 
 
61
  url = f"https://huggingface.co/datasets/{HF_DATASET_REPO}/resolve/main/{filename}"
62
- return local_path, url, filename, len(image_bytes)
63
 
64
  except Exception as e:
65
- print(f"[Error] Image upload failed: {e}")
66
  traceback.print_exc()
67
  return None, None, None, 0
68
 
 
46
  # Save and upload image to HF
47
  # -----------------------------------------------------
48
  def save_and_upload_image(image_b64: str, hf_token: str):
49
+ """Decodes a base64 image, saves it locally, and uploads to Hugging Face Hub."""
50
  try:
51
  image_bytes = base64.b64decode(image_b64)
52
+ size_bytes = len(image_bytes)
53
+
54
+ # Ensure the /tmp directory exists
55
  os.makedirs("/tmp", exist_ok=True)
56
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
57
  local_path = f"/tmp/robot_img_{timestamp}.jpg"
 
60
  f.write(image_bytes)
61
 
62
  filename = f"robot_{timestamp}.jpg"
63
+
64
+ # Corrected Hugging Face Hub upload
65
+ from huggingface_hub import HfApi
66
+ api = HfApi()
67
+ api.upload_file(
68
+ path_or_fileobj=local_path,
69
+ path_in_repo=filename,
70
+ repo_id=HF_DATASET_REPO,
71
+ repo_type="dataset",
72
+ token=hf_token
73
+ )
74
+
75
  url = f"https://huggingface.co/datasets/{HF_DATASET_REPO}/resolve/main/{filename}"
76
+ return local_path, url, filename, size_bytes
77
 
78
  except Exception as e:
79
+ print(f"[Error] during image upload: {e}")
80
  traceback.print_exc()
81
  return None, None, None, 0
82