Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,7 +16,7 @@ REPO_ID = os.getenv("HF_REPO_ID")
|
|
| 16 |
# Initialize Hugging Face API
|
| 17 |
login(token=HF_TOKEN)
|
| 18 |
api = HfApi()
|
| 19 |
-
|
| 20 |
@app.route('/upload', methods=['POST'])
|
| 21 |
def upload_image():
|
| 22 |
try:
|
|
@@ -51,9 +51,30 @@ def upload_image():
|
|
| 51 |
"upload_timestamp": datetime.utcnow().isoformat(),
|
| 52 |
}
|
| 53 |
|
| 54 |
-
#
|
| 55 |
metadata_file = os.path.join(temp_dir, "metadata.jsonl")
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
f.write(json.dumps(metadata) + "\n")
|
| 58 |
|
| 59 |
# Upload to Hugging Face
|
|
|
|
| 16 |
# Initialize Hugging Face API
|
| 17 |
login(token=HF_TOKEN)
|
| 18 |
api = HfApi()
|
| 19 |
+
|
| 20 |
@app.route('/upload', methods=['POST'])
|
| 21 |
def upload_image():
|
| 22 |
try:
|
|
|
|
| 51 |
"upload_timestamp": datetime.utcnow().isoformat(),
|
| 52 |
}
|
| 53 |
|
| 54 |
+
# Path for temporary metadata file
|
| 55 |
metadata_file = os.path.join(temp_dir, "metadata.jsonl")
|
| 56 |
+
|
| 57 |
+
# Try to download existing metadata.jsonl from the repository
|
| 58 |
+
try:
|
| 59 |
+
api.hf_hub_download(
|
| 60 |
+
repo_id=REPO_ID,
|
| 61 |
+
filename="metadata.jsonl",
|
| 62 |
+
repo_type="dataset",
|
| 63 |
+
local_dir=temp_dir,
|
| 64 |
+
)
|
| 65 |
+
# Read existing metadata and append new entry
|
| 66 |
+
with open(metadata_file, "r") as f:
|
| 67 |
+
existing_metadata = f.readlines()
|
| 68 |
+
except Exception:
|
| 69 |
+
# If metadata.jsonl doesn't exist, start with an empty list
|
| 70 |
+
existing_metadata = []
|
| 71 |
+
|
| 72 |
+
# Append new metadata
|
| 73 |
+
with open(metadata_file, "w") as f:
|
| 74 |
+
# Write existing metadata back
|
| 75 |
+
for line in existing_metadata:
|
| 76 |
+
f.write(line)
|
| 77 |
+
# Append new metadata
|
| 78 |
f.write(json.dumps(metadata) + "\n")
|
| 79 |
|
| 80 |
# Upload to Hugging Face
|