awacke1's picture
Update app.py
8e725cb verified
raw
history blame
4.29 kB
import streamlit as st
import pandas as pd
from datetime import datetime
import os
import threading
import time
from PIL import Image
from io import BytesIO
import aiofiles
import asyncio
import base64
# 🌟πŸ”₯ Initialize session state like a galactic DJ spinning tracks!
if 'file_history' not in st.session_state:
st.session_state['file_history'] = []
if 'auto_capture_running' not_in st.session_state:
st.session_state['auto_capture_running'] = False
if 'cam_file' not_in st.session_state:
st.session_state['cam_file'] = None
# πŸ“œπŸ’Ύ Save to history like a time-traveling scribe! | πŸ“…βœ¨ save_to_history("πŸ–ΌοΈ Image", "pic.jpg") - Stamps a pic in the history books like a boss!
def save_to_history(file_type, file_path):
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
st.session_state['file_history'].append({
"Timestamp": timestamp,
"Type": file_type,
"Path": file_path
})
# πŸ“Έβ° Auto-capture every 10 secs like a sneaky shutterbug!
def auto_capture():
if st.session_state['auto_capture_running'] and 'cam_img' in st.session_state:
cam_img = st.session_state['cam_img']
if cam_img:
filename = f"auto_snap_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
with open(filename, "wb") as f:
f.write(cam_img.getvalue())
save_to_history("πŸ–ΌοΈ Image", filename)
threading.Timer(10, auto_capture).start()
# πŸŽ›οΈ Sidebar config like a spaceship control panel!
with st.sidebar:
st.header("πŸŽšοΈπŸ“Έ Snap Shack")
if st.button("⏰ Start Auto-Snap"):
st.session_state['auto_capture_running'] = True
auto_capture()
if st.button("⏹️ Stop Auto-Snap"):
st.session_state['auto_capture_running'] = False
# πŸ“‚ Sidebar file outline with emoji flair!
st.subheader("πŸ“œ Snap Stash")
if st.session_state['file_history']:
images = [f for f in st.session_state['file_history'] if f['Type'] == "πŸ–ΌοΈ Image"]
if images:
st.write("πŸ–ΌοΈ Images")
for img in images:
st.write(f"- {img['Path']} @ {img['Timestamp']}")
else:
st.write("πŸ•³οΈ Empty Stash!")
# 🌍🎨 Main UI kicks off like a cosmic art show!
st.title("πŸ“Έ Snap Craze")
# πŸ“ΈπŸ“· Camera snap zone!
st.header("πŸ“ΈπŸŽ₯ Snap Zone")
cam_img = st.camera_input("πŸ“· Snap It!", key="cam")
if cam_img:
filename = f"cam_snap_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
if st.session_state['cam_file'] and os.path.exists(st.session_state['cam_file']):
os.remove(st.session_state['cam_file'])
with open(filename, "wb") as f:
f.write(cam_img.getvalue())
st.session_state['cam_file'] = filename
st.session_state['cam_img'] = cam_img # Store for auto-capture
save_to_history("πŸ–ΌοΈ Image", filename)
st.image(Image.open(filename), caption="Latest Snap", use_column_width=True)
# πŸ“‚ Upload zone like a media drop party!
st.header("πŸ“₯πŸŽ‰ Drop Zone")
uploaded_files = st.file_uploader("πŸ“Έ Toss Pics", accept_multiple_files=True, type=['jpg', 'png'])
if uploaded_files:
for uploaded_file in uploaded_files:
file_path = f"uploaded_{uploaded_file.name}"
with open(file_path, "wb") as f:
f.write(uploaded_file.read())
save_to_history("πŸ–ΌοΈ Image", file_path)
# πŸ–ΌοΈ Gallery like a media circus!
st.header("πŸŽͺ Snap Show")
if st.session_state['file_history']:
images = [f for f in st.session_state['file_history'] if f['Type'] == "πŸ–ΌοΈ Image"]
if images:
st.subheader("πŸ–ΌοΈ Pic Parade")
cols = st.columns(3)
for i, img in enumerate(images):
with cols[i % 3]:
st.image(img['Path'], caption=img['Path'], use_column_width=True)
st.markdown(f'<a href="data:image/jpeg;base64,{base64.b64encode(open(img["Path"], "rb").read()).decode()}" download="{os.path.basename(img["Path"])}">πŸ“₯ Snag It!</a>', unsafe_allow_html=True)
else:
st.write("🚫 No snaps yet!")
# πŸ“œ History log like a time machine!
st.header("⏳ Snap Saga")
if st.session_state['file_history']:
df = pd.DataFrame(st.session_state['file_history'])
st.dataframe(df)
else:
st.write("πŸ•³οΈ Nothing snapped yet!")