Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,6 +18,7 @@ from g4f.client import Client
|
|
| 18 |
|
| 19 |
import tempfile
|
| 20 |
from huggingface_hub import HfApi, login
|
|
|
|
| 21 |
|
| 22 |
client = Client()
|
| 23 |
|
|
@@ -426,6 +427,18 @@ def make_text():
|
|
| 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
|
|
@@ -433,6 +446,7 @@ def upload_image_dataset():
|
|
| 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
|
|
@@ -442,7 +456,11 @@ def upload_image_dataset():
|
|
| 442 |
|
| 443 |
try:
|
| 444 |
# Download the image
|
| 445 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
response.raise_for_status()
|
| 447 |
|
| 448 |
# Create a temporary file
|
|
@@ -450,6 +468,9 @@ def upload_image_dataset():
|
|
| 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,
|
|
@@ -458,9 +479,6 @@ def upload_image_dataset():
|
|
| 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)
|
|
|
|
| 18 |
|
| 19 |
import tempfile
|
| 20 |
from huggingface_hub import HfApi, login
|
| 21 |
+
import threading
|
| 22 |
|
| 23 |
client = Client()
|
| 24 |
|
|
|
|
| 427 |
os.unlink(temp_file_path)
|
| 428 |
|
| 429 |
|
| 430 |
+
def delete_file_after_delay(file_path, delay_seconds):
|
| 431 |
+
def delete_file():
|
| 432 |
+
time.sleep(delay_seconds)
|
| 433 |
+
try:
|
| 434 |
+
os.unlink(file_path)
|
| 435 |
+
print(f"Temporary file {file_path} deleted after {delay_seconds} seconds.")
|
| 436 |
+
except Exception as e:
|
| 437 |
+
print(f"Error deleting temporary file {file_path}: {str(e)}")
|
| 438 |
+
|
| 439 |
+
thread = threading.Thread(target=delete_file)
|
| 440 |
+
thread.start()
|
| 441 |
+
|
| 442 |
@app.route('/upload-image-dataset', methods=['POST'])
|
| 443 |
def upload_image_dataset():
|
| 444 |
data = request.json
|
|
|
|
| 446 |
file_name = data.get('fileName')
|
| 447 |
repo_id = data.get('repoId')
|
| 448 |
repo_type = data.get('repoType', 'dataset')
|
| 449 |
+
is_forwarded = data.get('is_forwarded', False)
|
| 450 |
|
| 451 |
if not url or not file_name or not repo_id:
|
| 452 |
return jsonify({"error": "Parameters 'url', 'fileName', and 'repoId' are required"}), 400
|
|
|
|
| 456 |
|
| 457 |
try:
|
| 458 |
# Download the image
|
| 459 |
+
headers = {}
|
| 460 |
+
if is_forwarded:
|
| 461 |
+
headers['Authorization'] = f'Bearer {HF_TOKEN}'
|
| 462 |
+
|
| 463 |
+
response = requests.get(url, headers=headers)
|
| 464 |
response.raise_for_status()
|
| 465 |
|
| 466 |
# Create a temporary file
|
|
|
|
| 468 |
temp_file.write(response.content)
|
| 469 |
temp_file_path = temp_file.name
|
| 470 |
|
| 471 |
+
# Schedule file deletion after 1 minute
|
| 472 |
+
delete_file_after_delay(temp_file_path, 60)
|
| 473 |
+
|
| 474 |
# Upload the file to Hugging Face
|
| 475 |
api.upload_file(
|
| 476 |
path_or_fileobj=temp_file_path,
|
|
|
|
| 479 |
repo_type=repo_type,
|
| 480 |
)
|
| 481 |
|
|
|
|
|
|
|
|
|
|
| 482 |
return jsonify({
|
| 483 |
"message": f"File '{file_name}' uploaded successfully to {repo_id} ({repo_type})",
|
| 484 |
"size": len(response.content)
|