import streamlit as st import os from PIL import Image from googleapiclient.discovery import build from googleapiclient.http import MediaFileUpload from google.oauth2.credentials import Credentials from chainguard.blockchain_logger import BlockchainLogger # Initialize Blockchain Logger blockchain_logger = BlockchainLogger() # Directory for local storage of uploaded files UPLOAD_DIR = "uploads" os.makedirs(UPLOAD_DIR, exist_ok=True) # Load Google Drive API credentials def get_drive_service(): creds = Credentials.from_authorized_user_file("credentials.json", scopes=["https://www.googleapis.com/auth/drive.file"]) return build('drive', 'v3', credentials=creds) drive_service = get_drive_service() def upload_to_google_drive(file_path, folder_id): """Uploads a file to Google Drive in the specified folder.""" file_metadata = { 'name': os.path.basename(file_path), 'parents': [folder_id] } media = MediaFileUpload(file_path, resumable=True) file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute() return file.get('id') def log_metadata(file_name, tags, album): """Log photo metadata to Chagu blockchain.""" metadata = { "file_name": file_name, "tags": tags, "album": album } block_details = blockchain_logger.log_data(metadata) return block_details # Streamlit App Layout st.title("Memora: Photo & Video Uploader") st.subheader("Securely upload and organize your memories") # File Upload uploaded_files = st.file_uploader( "Upload your photos or videos", accept_multiple_files=True, type=['jpg', 'jpeg', 'png', 'mp4', 'avi'] ) if uploaded_files: folder_id = st.text_input("Enter your Google Drive Folder ID", "") for uploaded_file in uploaded_files: # Save file locally file_path = os.path.join(UPLOAD_DIR, uploaded_file.name) with open(file_path, "wb") as f: f.write(uploaded_file.getbuffer()) st.success(f"File {uploaded_file.name} saved locally in {UPLOAD_DIR}.") # Display uploaded file st.write(f"File Name: {uploaded_file.name}") if uploaded_file.type.startswith('image'): image = Image.open(uploaded_file) st.image(image, caption=uploaded_file.name, use_column_width=True) # Metadata Input album = st.text_input(f"Album for {uploaded_file.name}", value="Default Album") tags = st.text_input(f"Tags for {uploaded_file.name} (comma-separated)", value="") # Log Metadata if st.button(f"Log Metadata for {uploaded_file.name}"): metadata = log_metadata(uploaded_file.name, tags.split(','), album) st.write(f"Metadata logged successfully! Block Details: {metadata}") # Upload to Google Drive if folder_id and st.button(f"Upload {uploaded_file.name} to Google Drive"): try: drive_file_id = upload_to_google_drive(file_path, folder_id) st.success(f"File {uploaded_file.name} uploaded to Google Drive with File ID: {drive_file_id}") except Exception as e: st.error(f"Failed to upload {uploaded_file.name} to Google Drive: {str(e)}") # Blockchain Integrity Validation if st.button("Validate Blockchain Integrity"): is_valid = blockchain_logger.is_blockchain_valid() if is_valid: st.success("Blockchain Integrity: Valid ✅") else: st.error("Blockchain Integrity: Invalid ❌")