Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,33 +2,61 @@ import gradio as gr
|
|
| 2 |
import json
|
| 3 |
import base64
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
try:
|
| 8 |
image_b64 = payload["image_b64"]
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
|
|
|
|
| 19 |
return {
|
| 20 |
-
"
|
| 21 |
-
"
|
| 22 |
-
"
|
|
|
|
| 23 |
}
|
| 24 |
|
| 25 |
except Exception as e:
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
|
| 28 |
|
| 29 |
demo = gr.Interface(
|
| 30 |
-
fn=
|
| 31 |
-
inputs=gr.JSON(label="Input Payload (Dict format)"),
|
| 32 |
outputs=gr.JSON(label="Reply"),
|
| 33 |
api_name="predict"
|
| 34 |
)
|
|
|
|
| 2 |
import json
|
| 3 |
import base64
|
| 4 |
import os
|
| 5 |
+
from huggingface_hub import upload_file
|
| 6 |
|
| 7 |
+
# --- Configuration for Hugging Face Hub Upload ---
|
| 8 |
+
# The HF_TOKEN secret is automatically loaded into the environment by Hugging Face Spaces
|
| 9 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 10 |
+
|
| 11 |
+
# !! REPLACE THIS with your actual dataset repo ID (e.g., "Wauplin/my-image-data") !!
|
| 12 |
+
HF_DATASET_REPO = "your-username/image_uploads"
|
| 13 |
+
|
| 14 |
+
def process_and_upload(payload: dict):
|
| 15 |
+
if not HF_TOKEN:
|
| 16 |
+
return {"error": "HF_TOKEN secret not found in Space settings."}
|
| 17 |
+
|
| 18 |
try:
|
| 19 |
image_b64 = payload["image_b64"]
|
| 20 |
+
image_bytes = base64.b64decode(image_b64)
|
| 21 |
|
| 22 |
+
# 1. Save temporarily to the local ephemeral storage (/tmp) first
|
| 23 |
+
# This file will be immediately deleted after the function finishes or the space restarts
|
| 24 |
+
local_tmp_path = "/tmp/uploaded_image.jpg"
|
| 25 |
+
with open(local_tmp_path, "wb") as f:
|
| 26 |
+
f.write(image_bytes)
|
| 27 |
|
| 28 |
+
# 2. Upload the temporary file to the persistent Hugging Face Dataset Repo
|
| 29 |
+
# The path in the repo can be dynamic, e.g., using a timestamp
|
| 30 |
+
path_in_repo = f"images/uploaded_image_{len(image_bytes)}.jpg"
|
| 31 |
+
|
| 32 |
+
upload_file(
|
| 33 |
+
path_or_fileobj=local_tmp_path,
|
| 34 |
+
path_in_repo=path_in_repo,
|
| 35 |
+
repo_id=HF_DATASET_REPO,
|
| 36 |
+
token=HF_TOKEN,
|
| 37 |
+
repo_type="dataset",
|
| 38 |
+
)
|
| 39 |
|
| 40 |
+
# 3. Clean up the local temporary file
|
| 41 |
+
os.remove(local_tmp_path)
|
| 42 |
|
| 43 |
+
# 4. Return success message
|
| 44 |
return {
|
| 45 |
+
"saved_to_hf_hub": True,
|
| 46 |
+
"repo_id": HF_DATASET_REPO,
|
| 47 |
+
"path_in_repo": path_in_repo,
|
| 48 |
+
"file_size_bytes": len(image_bytes)
|
| 49 |
}
|
| 50 |
|
| 51 |
except Exception as e:
|
| 52 |
+
# Check the HF Space logs for full traceback if an error occurs
|
| 53 |
+
print(f"Upload failed: {e}")
|
| 54 |
+
return {"error": f"Failed to upload to HF Hub: {str(e)}"}
|
| 55 |
|
| 56 |
|
| 57 |
demo = gr.Interface(
|
| 58 |
+
fn=process_and_upload,
|
| 59 |
+
inputs=gr.JSON(label="Input Payload (Dict format with 'image_b64')"),
|
| 60 |
outputs=gr.JSON(label="Reply"),
|
| 61 |
api_name="predict"
|
| 62 |
)
|