Update app.py
Browse files
app.py
CHANGED
|
@@ -7,11 +7,14 @@ from PIL import Image
|
|
| 7 |
import io
|
| 8 |
import cv2
|
| 9 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Set page config
|
| 12 |
st.set_page_config(page_title="UltraVideoSpace", layout="wide", initial_sidebar_state="collapsed")
|
| 13 |
|
| 14 |
-
# Custom CSS
|
| 15 |
st.markdown("""
|
| 16 |
<style>
|
| 17 |
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;700&display=swap');
|
|
@@ -93,58 +96,49 @@ st.markdown("""
|
|
| 93 |
.upload-zone:hover {
|
| 94 |
background-color: rgba(229, 9, 20, 0.1);
|
| 95 |
}
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
border-radius:
|
| 100 |
-
|
| 101 |
-
}
|
| 102 |
-
.progress-bar-fill {
|
| 103 |
-
height: 100%;
|
| 104 |
-
background-color: #e50914;
|
| 105 |
-
border-radius: 3px;
|
| 106 |
-
transition: width 0.3s ease;
|
| 107 |
}
|
| 108 |
</style>
|
| 109 |
""", unsafe_allow_html=True)
|
| 110 |
|
| 111 |
-
#
|
| 112 |
-
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
except Exception as e:
|
| 146 |
-
st.error(f"Error saving file: {str(e)}")
|
| 147 |
-
return False
|
| 148 |
|
| 149 |
def get_uploaded_videos():
|
| 150 |
video_dir = Path("uploaded_videos")
|
|
@@ -169,37 +163,95 @@ def generate_thumbnail(video_path):
|
|
| 169 |
def main():
|
| 170 |
st.markdown("<h1 style='text-align: center;'>UltraVideoSpace</h1>", unsafe_allow_html=True)
|
| 171 |
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
}
|
| 194 |
-
st.write(file_details)
|
| 195 |
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
else:
|
| 201 |
-
st.error("Failed to upload file.")
|
| 202 |
|
|
|
|
| 203 |
st.markdown("<h2 style='text-align: center; margin-top: 40px;'>Your Video Collection</h2>", unsafe_allow_html=True)
|
| 204 |
videos = get_uploaded_videos()
|
| 205 |
|
|
|
|
| 7 |
import io
|
| 8 |
import cv2
|
| 9 |
import numpy as np
|
| 10 |
+
from fastapi import FastAPI, File, UploadFile, Form
|
| 11 |
+
from fastapi.middleware.wsgi import WSGIMiddleware
|
| 12 |
+
from starlette.responses import JSONResponse
|
| 13 |
|
| 14 |
# Set page config
|
| 15 |
st.set_page_config(page_title="UltraVideoSpace", layout="wide", initial_sidebar_state="collapsed")
|
| 16 |
|
| 17 |
+
# Custom CSS
|
| 18 |
st.markdown("""
|
| 19 |
<style>
|
| 20 |
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;700&display=swap');
|
|
|
|
| 96 |
.upload-zone:hover {
|
| 97 |
background-color: rgba(229, 9, 20, 0.1);
|
| 98 |
}
|
| 99 |
+
#file-upload-status {
|
| 100 |
+
margin-top: 20px;
|
| 101 |
+
padding: 10px;
|
| 102 |
+
border-radius: 5px;
|
| 103 |
+
background-color: rgba(0, 0, 0, 0.5);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
}
|
| 105 |
</style>
|
| 106 |
""", unsafe_allow_html=True)
|
| 107 |
|
| 108 |
+
# FastAPI app
|
| 109 |
+
app = FastAPI()
|
| 110 |
|
| 111 |
+
@app.post("/upload_chunk")
|
| 112 |
+
async def upload_chunk(file: UploadFile = File(...), chunk_number: int = Form(...), total_chunks: int = Form(...)):
|
| 113 |
+
chunk_data = await file.read()
|
| 114 |
+
save_chunk(chunk_data, file.filename, chunk_number)
|
| 115 |
+
|
| 116 |
+
if chunk_number == total_chunks - 1:
|
| 117 |
+
reassemble_file(file.filename, total_chunks)
|
| 118 |
+
|
| 119 |
+
return JSONResponse(content={"message": "Chunk uploaded successfully"})
|
| 120 |
+
|
| 121 |
+
# Wrap Streamlit app
|
| 122 |
+
streamlit_app = WSGIMiddleware(app)
|
| 123 |
+
|
| 124 |
+
def save_chunk(chunk, filename, chunk_number):
|
| 125 |
+
save_path = Path("uploaded_chunks")
|
| 126 |
+
save_path.mkdir(exist_ok=True)
|
| 127 |
+
file_path = save_path / f"{filename}.part{chunk_number}"
|
| 128 |
+
with file_path.open("wb") as f:
|
| 129 |
+
f.write(chunk)
|
| 130 |
+
|
| 131 |
+
def reassemble_file(filename, total_chunks):
|
| 132 |
+
save_path = Path("uploaded_videos")
|
| 133 |
+
save_path.mkdir(exist_ok=True)
|
| 134 |
+
file_path = save_path / filename
|
| 135 |
+
|
| 136 |
+
with file_path.open("wb") as outfile:
|
| 137 |
+
for i in range(total_chunks):
|
| 138 |
+
chunk_path = Path("uploaded_chunks") / f"{filename}.part{i}"
|
| 139 |
+
with chunk_path.open("rb") as infile:
|
| 140 |
+
outfile.write(infile.read())
|
| 141 |
+
os.remove(chunk_path)
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
def get_uploaded_videos():
|
| 144 |
video_dir = Path("uploaded_videos")
|
|
|
|
| 163 |
def main():
|
| 164 |
st.markdown("<h1 style='text-align: center;'>UltraVideoSpace</h1>", unsafe_allow_html=True)
|
| 165 |
|
| 166 |
+
st.markdown("""
|
| 167 |
+
<div class="upload-zone" id="drop-zone">
|
| 168 |
+
<h3>Drag and drop your video here</h3>
|
| 169 |
+
<p>or click to select files</p>
|
| 170 |
+
<p>Supported formats: MP4, MKV, AVI, MOV, MPEG4</p>
|
| 171 |
+
<p>No file size limit (files will be split if larger than 200MB)</p>
|
| 172 |
+
</div>
|
| 173 |
+
<div id="file-upload-status"></div>
|
| 174 |
+
""", unsafe_allow_html=True)
|
| 175 |
+
|
| 176 |
+
# File uploader and chunked upload handling
|
| 177 |
+
st.markdown("""
|
| 178 |
+
<script>
|
| 179 |
+
const dropZone = document.getElementById('drop-zone');
|
| 180 |
+
const statusDiv = document.getElementById('file-upload-status');
|
| 181 |
+
|
| 182 |
+
dropZone.addEventListener('dragover', (e) => {
|
| 183 |
+
e.preventDefault();
|
| 184 |
+
dropZone.style.backgroundColor = 'rgba(229, 9, 20, 0.1)';
|
| 185 |
+
});
|
| 186 |
+
|
| 187 |
+
dropZone.addEventListener('dragleave', (e) => {
|
| 188 |
+
e.preventDefault();
|
| 189 |
+
dropZone.style.backgroundColor = '';
|
| 190 |
+
});
|
| 191 |
+
|
| 192 |
+
dropZone.addEventListener('drop', (e) => {
|
| 193 |
+
e.preventDefault();
|
| 194 |
+
dropZone.style.backgroundColor = '';
|
| 195 |
+
const file = e.dataTransfer.files[0];
|
| 196 |
+
uploadFile(file);
|
| 197 |
+
});
|
| 198 |
+
|
| 199 |
+
dropZone.addEventListener('click', () => {
|
| 200 |
+
const input = document.createElement('input');
|
| 201 |
+
input.type = 'file';
|
| 202 |
+
input.accept = '.mp4,.mkv,.avi,.mov,.mpeg4';
|
| 203 |
+
input.onchange = (e) => {
|
| 204 |
+
const file = e.target.files[0];
|
| 205 |
+
uploadFile(file);
|
| 206 |
+
};
|
| 207 |
+
input.click();
|
| 208 |
+
});
|
| 209 |
+
|
| 210 |
+
function uploadFile(file) {
|
| 211 |
+
const chunkSize = 200 * 1024 * 1024; // 200MB
|
| 212 |
+
const totalChunks = Math.ceil(file.size / chunkSize);
|
| 213 |
+
let currentChunk = 0;
|
| 214 |
+
|
| 215 |
+
statusDiv.innerHTML = `Uploading ${file.name} (0/${totalChunks} chunks)`;
|
| 216 |
+
|
| 217 |
+
function uploadNextChunk() {
|
| 218 |
+
const start = currentChunk * chunkSize;
|
| 219 |
+
const end = Math.min(start + chunkSize, file.size);
|
| 220 |
+
const chunk = file.slice(start, end);
|
| 221 |
+
|
| 222 |
+
const formData = new FormData();
|
| 223 |
+
formData.append('file', chunk, file.name);
|
| 224 |
+
formData.append('chunk_number', currentChunk);
|
| 225 |
+
formData.append('total_chunks', totalChunks);
|
| 226 |
+
|
| 227 |
+
fetch('/upload_chunk', {
|
| 228 |
+
method: 'POST',
|
| 229 |
+
body: formData
|
| 230 |
+
})
|
| 231 |
+
.then(response => response.json())
|
| 232 |
+
.then(data => {
|
| 233 |
+
currentChunk++;
|
| 234 |
+
statusDiv.innerHTML = `Uploading ${file.name} (${currentChunk}/${totalChunks} chunks)`;
|
| 235 |
+
if (currentChunk < totalChunks) {
|
| 236 |
+
uploadNextChunk();
|
| 237 |
+
} else {
|
| 238 |
+
statusDiv.innerHTML = `${file.name} uploaded successfully!`;
|
| 239 |
+
setTimeout(() => {
|
| 240 |
+
window.location.reload();
|
| 241 |
+
}, 2000);
|
| 242 |
+
}
|
| 243 |
+
})
|
| 244 |
+
.catch(error => {
|
| 245 |
+
statusDiv.innerHTML = `Error uploading ${file.name}: ${error}`;
|
| 246 |
+
});
|
| 247 |
}
|
|
|
|
| 248 |
|
| 249 |
+
uploadNextChunk();
|
| 250 |
+
}
|
| 251 |
+
</script>
|
| 252 |
+
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
| 253 |
|
| 254 |
+
# Display uploaded videos
|
| 255 |
st.markdown("<h2 style='text-align: center; margin-top: 40px;'>Your Video Collection</h2>", unsafe_allow_html=True)
|
| 256 |
videos = get_uploaded_videos()
|
| 257 |
|