Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -425,6 +425,52 @@ def make_text():
|
|
425 |
# Clean up the temporary file
|
426 |
os.unlink(temp_file_path)
|
427 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
428 |
|
429 |
if __name__ == "__main__":
|
430 |
app.run(host="0.0.0.0", port=7860, debug=True)
|
|
|
425 |
# Clean up the temporary file
|
426 |
os.unlink(temp_file_path)
|
427 |
|
428 |
+
|
429 |
+
@app.route('/upload-image-dataset', methods=['POST'])
|
430 |
+
def upload_image_dataset():
|
431 |
+
data = request.json
|
432 |
+
url = data.get('url')
|
433 |
+
file_name = data.get('fileName')
|
434 |
+
repo_id = data.get('repoId')
|
435 |
+
repo_type = data.get('repoType', 'dataset')
|
436 |
+
|
437 |
+
if not url or not file_name or not repo_id:
|
438 |
+
return jsonify({"error": "Parameters 'url', 'fileName', and 'repoId' are required"}), 400
|
439 |
+
|
440 |
+
if repo_type not in ['space', 'dataset', 'model']:
|
441 |
+
return jsonify({"error": "Invalid 'repoType'. Must be 'space', 'dataset', or 'model'"}), 400
|
442 |
+
|
443 |
+
try:
|
444 |
+
# Download the image
|
445 |
+
response = requests.get(url)
|
446 |
+
response.raise_for_status()
|
447 |
+
|
448 |
+
# Create a temporary file
|
449 |
+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
450 |
+
temp_file.write(response.content)
|
451 |
+
temp_file_path = temp_file.name
|
452 |
+
|
453 |
+
# Upload the file to Hugging Face
|
454 |
+
api.upload_file(
|
455 |
+
path_or_fileobj=temp_file_path,
|
456 |
+
path_in_repo=file_name,
|
457 |
+
repo_id=repo_id,
|
458 |
+
repo_type=repo_type,
|
459 |
+
)
|
460 |
+
|
461 |
+
# Clean up the temporary file
|
462 |
+
os.unlink(temp_file_path)
|
463 |
+
|
464 |
+
return jsonify({
|
465 |
+
"message": f"File '{file_name}' uploaded successfully to {repo_id} ({repo_type})",
|
466 |
+
"size": len(response.content)
|
467 |
+
}), 200
|
468 |
+
|
469 |
+
except requests.RequestException as e:
|
470 |
+
return jsonify({"error": f"Error downloading image: {str(e)}"}), 500
|
471 |
+
except Exception as e:
|
472 |
+
return jsonify({"error": f"Error uploading file: {str(e)}"}), 500
|
473 |
+
|
474 |
|
475 |
if __name__ == "__main__":
|
476 |
app.run(host="0.0.0.0", port=7860, debug=True)
|