COLTO50 commited on
Commit
a977ddc
·
verified ·
1 Parent(s): 28e7179

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -76
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 with advanced styling
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
- .progress-bar {
97
- height: 6px;
98
- background-color: #2b2b2b;
99
- border-radius: 3px;
100
- margin-top: 12px;
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
- # Constants
112
- MAX_FILE_SIZE = 10 * 1024 * 1024 * 1024 # 10GB in bytes
113
 
114
- def save_uploaded_file(uploaded_file):
115
- try:
116
- if uploaded_file.size > MAX_FILE_SIZE:
117
- st.error(f"File size exceeds the 10GB limit. Please upload a smaller file.")
118
- return False
119
-
120
- save_path = Path("uploaded_videos")
121
- save_path.mkdir(exist_ok=True)
122
- file_path = save_path / uploaded_file.name
123
-
124
- total_size = uploaded_file.size
125
- chunk_size = 1024 * 1024 # 1MB chunks
126
- uploaded_size = 0
127
-
128
- progress_bar = st.progress(0)
129
- status_text = st.empty()
130
-
131
- with file_path.open("wb") as f:
132
- while True:
133
- chunk = uploaded_file.read(chunk_size)
134
- if not chunk:
135
- break
136
- f.write(chunk)
137
- uploaded_size += len(chunk)
138
- progress = min(uploaded_size / total_size, 1.0)
139
- progress_bar.progress(int(progress * 100))
140
- status_text.text(f"Uploading... {int(progress * 100)}%")
141
-
142
- progress_bar.empty()
143
- status_text.empty()
144
- return True
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
- uploaded_file = st.file_uploader("", type=['mp4', 'mkv', 'avi', 'mov', 'mpeg4'], key="video_uploader")
173
-
174
- upload_placeholder = st.empty()
175
- with upload_placeholder.container():
176
- st.markdown("""
177
- <div class="upload-zone">
178
- <h3>Drag and drop your video here</h3>
179
- <p>or click to select files</p>
180
- <p>Supported formats: MP4, MKV, AVI, MOV, MPEG4</p>
181
- <p>Maximum file size: 10GB</p>
182
- </div>
183
- """, unsafe_allow_html=True)
184
-
185
- if uploaded_file is not None:
186
- upload_placeholder.empty()
187
- st.write(f"Uploading: {uploaded_file.name}")
188
- file_size_mb = uploaded_file.size / (1024 * 1024)
189
- file_details = {
190
- "FileName": uploaded_file.name,
191
- "FileType": uploaded_file.type,
192
- "FileSize": f"{file_size_mb:.2f} MB"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
  }
194
- st.write(file_details)
195
 
196
- if file_size_mb > 10 * 1024: # If file size is greater than 10GB
197
- st.error("File size exceeds the 10GB limit. Please upload a smaller file.")
198
- elif save_uploaded_file(uploaded_file):
199
- st.success(f"File {uploaded_file.name} successfully uploaded!")
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