Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,4 @@
|
|
| 1 |
import os
|
| 2 |
-
import zipfile
|
| 3 |
-
import time
|
| 4 |
import streamlit as st
|
| 5 |
from PIL import Image as PILImage
|
| 6 |
from PIL import Image as pilImage
|
|
@@ -45,14 +43,15 @@ def display_images(image_collection, query_text, max_distance=None, debug=False)
|
|
| 45 |
st.write(f"URI: {uri} - Distance: {distance}")
|
| 46 |
try:
|
| 47 |
img = PILImage.open(uri)
|
| 48 |
-
img = img.resize((img.width // 2, img.height // 2)) # Resize image for better performance
|
| 49 |
st.image(img, width=300)
|
| 50 |
except Exception as e:
|
| 51 |
st.error(f"Error loading image {uri}: {e}")
|
| 52 |
else:
|
| 53 |
if debug:
|
| 54 |
st.write(f"URI: {uri} - Distance: {distance} (Filtered out)")
|
|
|
|
| 55 |
|
|
|
|
| 56 |
def display_videos_streamlit(video_collection, query_text, max_distance=None, max_results=5, debug=False):
|
| 57 |
"""
|
| 58 |
Display videos in a Streamlit app based on a query.
|
|
@@ -171,8 +170,8 @@ def format_prompt_inputs(image_collection, text_collection, video_collection, us
|
|
| 171 |
if image_candidates:
|
| 172 |
image = image_candidates[0]
|
| 173 |
with PILImage.open(image) as img:
|
| 174 |
-
img = img.resize((img.width //
|
| 175 |
-
|
| 176 |
with io.BytesIO() as output:
|
| 177 |
img.save(output, format="JPEG", quality=60)
|
| 178 |
compressed_image_data = output.getvalue()
|
|
@@ -182,56 +181,37 @@ def format_prompt_inputs(image_collection, text_collection, video_collection, us
|
|
| 182 |
inputs["image_data_1"] = ""
|
| 183 |
|
| 184 |
return inputs
|
| 185 |
-
|
|
|
|
|
|
|
| 186 |
def page_1():
|
| 187 |
-
st.title("Page 1: Upload and Process
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
# Temporary folder to store extracted files
|
| 195 |
-
temp_folder = "/tmp/extracted_files"
|
| 196 |
-
os.makedirs(temp_folder, exist_ok=True)
|
| 197 |
-
|
| 198 |
-
# Progress bar
|
| 199 |
-
progress_bar = st.progress(0)
|
| 200 |
-
status_text = st.empty()
|
| 201 |
-
|
| 202 |
-
try:
|
| 203 |
-
total_files = len(uploaded_pdf_files)
|
| 204 |
-
files_processed = 0
|
| 205 |
-
|
| 206 |
-
# Process PDF files
|
| 207 |
-
for uploaded_pdf in uploaded_pdf_files:
|
| 208 |
-
pdf_path = f"/tmp/{uploaded_pdf.name}"
|
| 209 |
-
with open(pdf_path, "wb") as f:
|
| 210 |
-
f.write(uploaded_pdf.getbuffer())
|
| 211 |
-
|
| 212 |
-
# Simulate PDF processing (replace with actual PDF processing logic)
|
| 213 |
-
files_processed += 1
|
| 214 |
-
progress_bar.progress(files_processed / total_files) # Updated progress calculation
|
| 215 |
-
status_text.text(f"Processing PDF: {uploaded_pdf.name} ({files_processed}/{total_files})")
|
| 216 |
-
|
| 217 |
-
# Call your actual PDF processing function here
|
| 218 |
-
image_collection, text_collection, video_collection = process_pdf(pdf_path)
|
| 219 |
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
st.session_state.image_collection = image_collection
|
| 222 |
st.session_state.text_collection = text_collection
|
| 223 |
st.session_state.video_collection = video_collection
|
| 224 |
|
| 225 |
-
|
| 226 |
-
|
| 227 |
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
progress_bar.progress(0)
|
| 232 |
-
status_text.text("")
|
| 233 |
-
st.error(f"Error processing files: {e}")
|
| 234 |
-
st.write(f"Error details: {e}")
|
| 235 |
|
| 236 |
def page_2():
|
| 237 |
st.title("Page 2: Query and Use Processed Collections")
|
|
@@ -258,11 +238,9 @@ def page_2():
|
|
| 258 |
display_images(image_collection, query, max_distance=1.55, debug=True)
|
| 259 |
|
| 260 |
st.markdown("### Videos")
|
| 261 |
-
display_videos_streamlit(video_collection, query_text=query, max_distance=None, max_results=5, debug=False)
|
| 262 |
frame = inputs["frame"]
|
| 263 |
-
|
| 264 |
if frame:
|
| 265 |
-
video_path = f"
|
| 266 |
if os.path.exists(video_path):
|
| 267 |
st.video(video_path)
|
| 268 |
else:
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
from PIL import Image as PILImage
|
| 4 |
from PIL import Image as pilImage
|
|
|
|
| 43 |
st.write(f"URI: {uri} - Distance: {distance}")
|
| 44 |
try:
|
| 45 |
img = PILImage.open(uri)
|
|
|
|
| 46 |
st.image(img, width=300)
|
| 47 |
except Exception as e:
|
| 48 |
st.error(f"Error loading image {uri}: {e}")
|
| 49 |
else:
|
| 50 |
if debug:
|
| 51 |
st.write(f"URI: {uri} - Distance: {distance} (Filtered out)")
|
| 52 |
+
|
| 53 |
|
| 54 |
+
|
| 55 |
def display_videos_streamlit(video_collection, query_text, max_distance=None, max_results=5, debug=False):
|
| 56 |
"""
|
| 57 |
Display videos in a Streamlit app based on a query.
|
|
|
|
| 170 |
if image_candidates:
|
| 171 |
image = image_candidates[0]
|
| 172 |
with PILImage.open(image) as img:
|
| 173 |
+
img = img.resize((img.width // 6, img.height // 6))
|
| 174 |
+
img = img.convert("L")
|
| 175 |
with io.BytesIO() as output:
|
| 176 |
img.save(output, format="JPEG", quality=60)
|
| 177 |
compressed_image_data = output.getvalue()
|
|
|
|
| 181 |
inputs["image_data_1"] = ""
|
| 182 |
|
| 183 |
return inputs
|
| 184 |
+
|
| 185 |
+
import time # To simulate delays during processing
|
| 186 |
+
|
| 187 |
def page_1():
|
| 188 |
+
st.title("Page 1: Upload and Process PDF")
|
| 189 |
+
|
| 190 |
+
uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
|
| 191 |
+
if uploaded_file:
|
| 192 |
+
pdf_path = f"/tmp/{uploaded_file.name}"
|
| 193 |
+
with open(pdf_path, "wb") as f:
|
| 194 |
+
f.write(uploaded_file.getbuffer())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
+
# Display a spinner while processing
|
| 197 |
+
with st.spinner("Processing PDF... Please wait."):
|
| 198 |
+
try:
|
| 199 |
+
# Simulate processing stages with a delay (this is just an example)
|
| 200 |
+
time.sleep(1) # Simulate a step in the processing
|
| 201 |
+
|
| 202 |
+
# Step 1: Process images, texts, and videos
|
| 203 |
+
st.text("Extracting content from PDF...")
|
| 204 |
+
image_collection, text_collection, video_collection = process_pdf(pdf_path)
|
| 205 |
st.session_state.image_collection = image_collection
|
| 206 |
st.session_state.text_collection = text_collection
|
| 207 |
st.session_state.video_collection = video_collection
|
| 208 |
|
| 209 |
+
# Simulate a delay for finalizing (if needed)
|
| 210 |
+
time.sleep(1) # Simulate final step
|
| 211 |
|
| 212 |
+
st.success("PDF processed successfully! Collections saved to session state.")
|
| 213 |
+
except Exception as e:
|
| 214 |
+
st.error(f"Error processing PDF: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
def page_2():
|
| 217 |
st.title("Page 2: Query and Use Processed Collections")
|
|
|
|
| 238 |
display_images(image_collection, query, max_distance=1.55, debug=True)
|
| 239 |
|
| 240 |
st.markdown("### Videos")
|
|
|
|
| 241 |
frame = inputs["frame"]
|
|
|
|
| 242 |
if frame:
|
| 243 |
+
video_path = f"StockVideos-CC0/{os.path.basename(frame).split('/')[0]}.mp4"
|
| 244 |
if os.path.exists(video_path):
|
| 245 |
st.video(video_path)
|
| 246 |
else:
|