Spaces:
Runtime error
Runtime error
Commit ·
7a826c8
1
Parent(s): d96ee2f
access key patch
Browse files- app.py +9 -1
- index.html +49 -26
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from fastapi import FastAPI, File, UploadFile, Form, BackgroundTasks, HTTPException
|
| 2 |
from fastapi.responses import HTMLResponse, JSONResponse
|
| 3 |
from pathlib import Path
|
| 4 |
from typing import List
|
|
@@ -20,6 +20,9 @@ STATUS_FOLDER = Path("status")
|
|
| 20 |
UPLOAD_FOLDER.mkdir(exist_ok=True)
|
| 21 |
STATUS_FOLDER.mkdir(exist_ok=True)
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
def save_progress(status_file, filename, status):
|
| 24 |
"""Helper function to update progress to a status file."""
|
| 25 |
with status_file.open("a", encoding="utf-8") as f:
|
|
@@ -64,9 +67,14 @@ async def main():
|
|
| 64 |
@app.post("/upload")
|
| 65 |
async def upload_files(
|
| 66 |
background_tasks: BackgroundTasks,
|
|
|
|
| 67 |
destination_folder: str = Form(...),
|
| 68 |
files: List[UploadFile] = File(...)
|
| 69 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
temp_folder_path = UPLOAD_FOLDER / uuid.uuid4().hex
|
| 71 |
temp_folder_path.mkdir(parents=True, exist_ok=True)
|
| 72 |
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, Form, BackgroundTasks, HTTPException, Depends
|
| 2 |
from fastapi.responses import HTMLResponse, JSONResponse
|
| 3 |
from pathlib import Path
|
| 4 |
from typing import List
|
|
|
|
| 20 |
UPLOAD_FOLDER.mkdir(exist_ok=True)
|
| 21 |
STATUS_FOLDER.mkdir(exist_ok=True)
|
| 22 |
|
| 23 |
+
# Access key for upload authorization
|
| 24 |
+
ACCESS_KEY = os.getenv("PASSWORD")
|
| 25 |
+
|
| 26 |
def save_progress(status_file, filename, status):
|
| 27 |
"""Helper function to update progress to a status file."""
|
| 28 |
with status_file.open("a", encoding="utf-8") as f:
|
|
|
|
| 67 |
@app.post("/upload")
|
| 68 |
async def upload_files(
|
| 69 |
background_tasks: BackgroundTasks,
|
| 70 |
+
access_key: str = Form(...),
|
| 71 |
destination_folder: str = Form(...),
|
| 72 |
files: List[UploadFile] = File(...)
|
| 73 |
):
|
| 74 |
+
# Verify access key
|
| 75 |
+
if access_key != ACCESS_KEY:
|
| 76 |
+
raise HTTPException(status_code=403, detail="Invalid access key")
|
| 77 |
+
|
| 78 |
temp_folder_path = UPLOAD_FOLDER / uuid.uuid4().hex
|
| 79 |
temp_folder_path.mkdir(parents=True, exist_ok=True)
|
| 80 |
|
index.html
CHANGED
|
@@ -8,6 +8,9 @@
|
|
| 8 |
<body>
|
| 9 |
<h2>Upload Files to Hugging Face Repository</h2>
|
| 10 |
<form id="uploadForm" enctype="multipart/form-data">
|
|
|
|
|
|
|
|
|
|
| 11 |
<label for="destination_folder">Destination Folder:</label>
|
| 12 |
<input type="text" id="destination_folder" name="destination_folder" required>
|
| 13 |
|
|
@@ -19,39 +22,59 @@
|
|
| 19 |
|
| 20 |
<div id="progress">
|
| 21 |
<h3>Upload Progress:</h3>
|
| 22 |
-
<pre id="
|
|
|
|
| 23 |
</div>
|
| 24 |
|
| 25 |
<script>
|
| 26 |
document.getElementById("uploadForm").onsubmit = async function(event) {
|
| 27 |
event.preventDefault();
|
|
|
|
| 28 |
const formData = new FormData(this);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
method: "POST",
|
| 33 |
-
body: formData
|
| 34 |
-
});
|
| 35 |
-
const result = await response.json();
|
| 36 |
-
const statusId = result.status_id;
|
| 37 |
-
|
| 38 |
-
// Start polling for progress
|
| 39 |
-
const progressText = document.getElementById("progressText");
|
| 40 |
-
progressText.textContent = "Starting upload...";
|
| 41 |
-
|
| 42 |
-
const intervalId = setInterval(async () => {
|
| 43 |
-
const progressResponse = await fetch(`/progress/${statusId}`);
|
| 44 |
-
if (progressResponse.ok) {
|
| 45 |
-
const progressResult = await progressResponse.json();
|
| 46 |
-
progressText.textContent = progressResult.progress.join("");
|
| 47 |
-
} else {
|
| 48 |
-
clearInterval(intervalId);
|
| 49 |
-
progressText.textContent += "\nUpload completed or status not found.";
|
| 50 |
-
}
|
| 51 |
-
}, 2000);
|
| 52 |
-
} catch (error) {
|
| 53 |
-
document.getElementById("progressText").textContent = "Upload failed.";
|
| 54 |
-
}
|
| 55 |
};
|
| 56 |
</script>
|
| 57 |
</body>
|
|
|
|
| 8 |
<body>
|
| 9 |
<h2>Upload Files to Hugging Face Repository</h2>
|
| 10 |
<form id="uploadForm" enctype="multipart/form-data">
|
| 11 |
+
<label for="access_key">Access Key:</label>
|
| 12 |
+
<input type="password" id="access_key" name="access_key" required>
|
| 13 |
+
|
| 14 |
<label for="destination_folder">Destination Folder:</label>
|
| 15 |
<input type="text" id="destination_folder" name="destination_folder" required>
|
| 16 |
|
|
|
|
| 22 |
|
| 23 |
<div id="progress">
|
| 24 |
<h3>Upload Progress:</h3>
|
| 25 |
+
<pre id="clientProgressText">Client to Server: No upload in progress...</pre>
|
| 26 |
+
<pre id="serverProgressText">Server to Storage: No upload in progress...</pre>
|
| 27 |
</div>
|
| 28 |
|
| 29 |
<script>
|
| 30 |
document.getElementById("uploadForm").onsubmit = async function(event) {
|
| 31 |
event.preventDefault();
|
| 32 |
+
|
| 33 |
const formData = new FormData(this);
|
| 34 |
+
const clientProgressText = document.getElementById("clientProgressText");
|
| 35 |
+
const serverProgressText = document.getElementById("serverProgressText");
|
| 36 |
+
|
| 37 |
+
// Reset progress text
|
| 38 |
+
clientProgressText.textContent = "Client to Server: Starting upload...";
|
| 39 |
+
serverProgressText.textContent = "Server to Storage: No upload in progress...";
|
| 40 |
+
|
| 41 |
+
// Create XMLHttpRequest to track client-to-server upload progress
|
| 42 |
+
const xhr = new XMLHttpRequest();
|
| 43 |
+
xhr.open("POST", "/upload", true);
|
| 44 |
+
|
| 45 |
+
// Add a progress event listener to track the upload to the server
|
| 46 |
+
xhr.upload.onprogress = function(event) {
|
| 47 |
+
if (event.lengthComputable) {
|
| 48 |
+
const percentComplete = (event.loaded / event.total) * 100;
|
| 49 |
+
clientProgressText.textContent = `Client to Server: ${percentComplete.toFixed(2)}% uploaded`;
|
| 50 |
+
}
|
| 51 |
+
};
|
| 52 |
+
|
| 53 |
+
// Handle server response
|
| 54 |
+
xhr.onload = async function() {
|
| 55 |
+
if (xhr.status === 200) {
|
| 56 |
+
const result = JSON.parse(xhr.responseText);
|
| 57 |
+
const statusId = result.status_id;
|
| 58 |
+
|
| 59 |
+
// Start polling for server-to-storage progress
|
| 60 |
+
serverProgressText.textContent = "Server to Storage: Starting upload...";
|
| 61 |
+
const intervalId = setInterval(async () => {
|
| 62 |
+
const progressResponse = await fetch(`/progress/${statusId}`);
|
| 63 |
+
if (progressResponse.ok) {
|
| 64 |
+
const progressResult = await progressResponse.json();
|
| 65 |
+
serverProgressText.textContent = progressResult.progress.join("");
|
| 66 |
+
} else {
|
| 67 |
+
clearInterval(intervalId);
|
| 68 |
+
serverProgressText.textContent += "\nServer to Storage: Upload completed or status not found.";
|
| 69 |
+
}
|
| 70 |
+
}, 2000);
|
| 71 |
+
} else {
|
| 72 |
+
clientProgressText.textContent = "Client to Server: Upload failed.";
|
| 73 |
+
}
|
| 74 |
+
};
|
| 75 |
|
| 76 |
+
// Send the request
|
| 77 |
+
xhr.send(formData);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
};
|
| 79 |
</script>
|
| 80 |
</body>
|