File size: 3,376 Bytes
f548caf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06266a4
f548caf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
489927d
f548caf
 
 
 
 
 
 
06266a4
f548caf
06266a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f548caf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
489927d
89f66fd
 
 
 
489927d
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os
import json
import shutil
import tempfile
from datetime import datetime
from flask import Flask, request, jsonify
from huggingface_hub import login, HfApi
from werkzeug.utils import secure_filename

app = Flask(__name__)

# Load environment variables
HF_TOKEN = os.getenv("HF_TOKEN")
REPO_ID = os.getenv("HF_REPO_ID")

# Initialize Hugging Face API
login(token=HF_TOKEN)
api = HfApi()

@app.route('/upload', methods=['POST'])
def upload_image():
    try:
        # Check if required data is present
        if 'image' not in request.files or 'guid' not in request.form or 'ip' not in request.form:
            return jsonify({"error": "Missing image, guid, or ip"}), 400

        image = request.files['image']
        guid = secure_filename(request.form['guid'])  # Sanitize GUID
        ip = request.form['ip']

        # Validate image
        if not image or not image.filename:
            return jsonify({"error": "No valid image provided"}), 400

        # Create temporary directory
        with tempfile.TemporaryDirectory() as temp_dir:
            # Create images folder
            temp_image_dir = os.path.join(temp_dir, "images")
            os.makedirs(temp_image_dir, exist_ok=True)

            # Save image with guid.jpg extension
            temp_image_path = os.path.join(temp_image_dir, f"{guid}.jpg")
            image.save(temp_image_path)

            # Create metadata
            path_in_repo = f"images/{guid}.jpg"
            metadata = {
                "file_name": path_in_repo,
                "guid": guid,
                "ip": ip,
                "upload_timestamp": datetime.utcnow().isoformat(),
            }

            # Path for temporary metadata file
            metadata_file = os.path.join(temp_dir, "metadata.jsonl")

            # Try to download existing metadata.jsonl from the repository
            try:
                api.hf_hub_download(
                    repo_id=REPO_ID,
                    filename="metadata.jsonl",
                    repo_type="dataset",
                    local_dir=temp_dir,
                )
                # Read existing metadata and append new entry
                with open(metadata_file, "r") as f:
                    existing_metadata = f.readlines()
            except Exception:
                # If metadata.jsonl doesn't exist, start with an empty list
                existing_metadata = []

            # Append new metadata
            with open(metadata_file, "w") as f:
                # Write existing metadata back
                for line in existing_metadata:
                    f.write(line)
                # Append new metadata
                f.write(json.dumps(metadata) + "\n")

            # Upload to Hugging Face
            api.upload_folder(
                folder_path=temp_dir,
                path_in_repo="",
                repo_id=REPO_ID,
                repo_type="dataset"
            )

        return jsonify({"message": "Image and metadata uploaded successfully"}), 200

    except Exception as e:
        return jsonify({"error": str(e)}), 500

if __name__ == '__main__':
    print('''curl -X POST https://<your-space-name>.hf.space/upload \\
          -F "image=@/path/to/image.jpg" \\
          -F "guid=test-guid" \\
          -F "ip=127.0.0.1"''')
    port = int(os.environ.get("PORT", 7860))
    app.run(host="0.0.0.0", port=port)