Update app.py
Browse files
app.py
CHANGED
|
@@ -183,49 +183,129 @@ def format_prompt_inputs(image_collection, text_collection, video_collection, us
|
|
| 183 |
|
| 184 |
return inputs
|
| 185 |
|
| 186 |
-
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
-
|
| 190 |
-
|
|
|
|
| 191 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
# Button to trigger processing
|
| 193 |
-
if
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
|
| 198 |
# Progress bar
|
| 199 |
progress_bar = st.progress(0)
|
| 200 |
status_text = st.empty()
|
| 201 |
|
| 202 |
try:
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
except Exception as e:
|
| 223 |
progress_bar.progress(0)
|
| 224 |
status_text.text("")
|
| 225 |
-
st.error(f"Error processing
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
|
| 230 |
def page_2():
|
| 231 |
st.title("Page 2: Query and Use Processed Collections")
|
|
|
|
| 183 |
|
| 184 |
return inputs
|
| 185 |
|
| 186 |
+
import streamlit as st
|
| 187 |
+
import zipfile
|
| 188 |
+
import os
|
| 189 |
+
import time
|
| 190 |
+
|
| 191 |
+
def unzip_file(zip_path, extract_to):
|
| 192 |
+
"""
|
| 193 |
+
Unzips a zip file to the specified directory.
|
| 194 |
+
|
| 195 |
+
Args:
|
| 196 |
+
zip_path (str): Path to the zip file.
|
| 197 |
+
extract_to (str): Directory where the contents should be extracted.
|
| 198 |
+
"""
|
| 199 |
+
try:
|
| 200 |
+
# Ensure the destination directory exists
|
| 201 |
+
os.makedirs(extract_to, exist_ok=True)
|
| 202 |
+
|
| 203 |
+
# Open the zip file
|
| 204 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 205 |
+
# Extract all the contents
|
| 206 |
+
zip_ref.extractall(extract_to)
|
| 207 |
+
return True
|
| 208 |
+
except Exception as e:
|
| 209 |
+
print(f"An error occurred: {e}")
|
| 210 |
+
return False
|
| 211 |
+
import streamlit as st
|
| 212 |
+
import zipfile
|
| 213 |
+
import os
|
| 214 |
+
import time
|
| 215 |
|
| 216 |
+
def unzip_file(zip_path, extract_to):
|
| 217 |
+
"""
|
| 218 |
+
Unzips a zip file to the specified directory.
|
| 219 |
|
| 220 |
+
Args:
|
| 221 |
+
zip_path (str): Path to the zip file.
|
| 222 |
+
extract_to (str): Directory where the contents should be extracted.
|
| 223 |
+
"""
|
| 224 |
+
try:
|
| 225 |
+
# Ensure the destination directory exists
|
| 226 |
+
os.makedirs(extract_to, exist_ok=True)
|
| 227 |
+
|
| 228 |
+
# Open the zip file
|
| 229 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 230 |
+
# Extract all the contents
|
| 231 |
+
zip_ref.extractall(extract_to)
|
| 232 |
+
return True
|
| 233 |
+
except Exception as e:
|
| 234 |
+
print(f"An error occurred: {e}")
|
| 235 |
+
return False
|
| 236 |
+
|
| 237 |
+
def process_pdf(pdf_path):
|
| 238 |
+
# Placeholder function to simulate PDF processing
|
| 239 |
+
# Replace this with actual PDF processing logic, such as extracting text, images, etc.
|
| 240 |
+
time.sleep(2) # Simulating processing delay
|
| 241 |
+
return "image_collection", "text_collection", "video_collection" # Replace with actual collections
|
| 242 |
+
|
| 243 |
+
def page_1():
|
| 244 |
+
st.title("Page 1: Upload and Process Videos and PDFs")
|
| 245 |
+
|
| 246 |
+
# File uploader for multiple zip files containing videos
|
| 247 |
+
uploaded_video_zips = st.file_uploader("Upload ZIP files containing videos", type=["zip"], accept_multiple_files=True)
|
| 248 |
+
|
| 249 |
+
# File uploader for PDF files
|
| 250 |
+
uploaded_pdf_files = st.file_uploader("Upload PDF files", type=["pdf"], accept_multiple_files=True)
|
| 251 |
+
|
| 252 |
# Button to trigger processing
|
| 253 |
+
if (uploaded_video_zips or uploaded_pdf_files) and st.button("Process Files"):
|
| 254 |
+
# Temporary folder to store extracted files
|
| 255 |
+
temp_folder = "/tmp/extracted_files"
|
| 256 |
+
os.makedirs(temp_folder, exist_ok=True)
|
| 257 |
|
| 258 |
# Progress bar
|
| 259 |
progress_bar = st.progress(0)
|
| 260 |
status_text = st.empty()
|
| 261 |
|
| 262 |
try:
|
| 263 |
+
total_files = len(uploaded_video_zips) + len(uploaded_pdf_files)
|
| 264 |
+
files_processed = 0
|
| 265 |
+
progress_step = 100 / total_files if total_files > 0 else 0
|
| 266 |
+
|
| 267 |
+
# Process video zip files
|
| 268 |
+
for uploaded_file in uploaded_video_zips:
|
| 269 |
+
zip_path = f"/tmp/{uploaded_file.name}"
|
| 270 |
+
with open(zip_path, "wb") as f:
|
| 271 |
+
f.write(uploaded_file.getbuffer())
|
| 272 |
+
|
| 273 |
+
# Extract the content from the zip file
|
| 274 |
+
folder_name = os.path.splitext(uploaded_file.name)[0]
|
| 275 |
+
extract_to = os.path.join(temp_folder, folder_name)
|
| 276 |
+
if unzip_file(zip_path, extract_to):
|
| 277 |
+
files_processed += 1
|
| 278 |
+
progress_bar.progress(files_processed * progress_step)
|
| 279 |
+
status_text.text(f"Extracting: {uploaded_file.name} ({files_processed}/{total_files})")
|
| 280 |
+
|
| 281 |
+
# Process PDF files
|
| 282 |
+
for uploaded_pdf in uploaded_pdf_files:
|
| 283 |
+
pdf_path = f"/tmp/{uploaded_pdf.name}"
|
| 284 |
+
with open(pdf_path, "wb") as f:
|
| 285 |
+
f.write(uploaded_pdf.getbuffer())
|
| 286 |
+
|
| 287 |
+
# Simulate PDF processing (replace with actual PDF processing logic)
|
| 288 |
+
files_processed += 1
|
| 289 |
+
progress_bar.progress(files_processed * progress_step)
|
| 290 |
+
status_text.text(f"Processing PDF: {uploaded_pdf.name} ({files_processed}/{total_files})")
|
| 291 |
+
|
| 292 |
+
# Call your actual PDF processing function here, e.g.
|
| 293 |
+
image_collection, text_collection, video_collection = process_pdf(pdf_path,temp_folder)
|
| 294 |
+
|
| 295 |
+
# Save collections to session state
|
| 296 |
+
st.session_state.image_collection = image_collection
|
| 297 |
+
st.session_state.text_collection = text_collection
|
| 298 |
+
st.session_state.video_collection = video_collection
|
| 299 |
+
|
| 300 |
+
# Update status after extraction and processing
|
| 301 |
+
status_text.text("Extraction and processing completed successfully!")
|
| 302 |
+
|
| 303 |
+
st.success("Videos and PDFs processed successfully! Collections saved to session state.")
|
| 304 |
|
| 305 |
except Exception as e:
|
| 306 |
progress_bar.progress(0)
|
| 307 |
status_text.text("")
|
| 308 |
+
st.error(f"Error processing files: {e}")
|
|
|
|
|
|
|
|
|
|
| 309 |
|
| 310 |
def page_2():
|
| 311 |
st.title("Page 2: Query and Use Processed Collections")
|