Spaces:
Build error
Build error
talexm commited on
Commit ·
756b14b
1
Parent(s): 2d94c1e
update
Browse files
app.py
CHANGED
|
@@ -1,15 +1,34 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
| 4 |
from chainguard.blockchain_logger import BlockchainLogger
|
| 5 |
|
| 6 |
# Initialize Blockchain Logger
|
| 7 |
blockchain_logger = BlockchainLogger()
|
| 8 |
|
| 9 |
-
#
|
| 10 |
UPLOAD_DIR = "uploads"
|
| 11 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def log_metadata(file_name, tags, album):
|
| 15 |
"""Log photo metadata to Chagu blockchain."""
|
|
@@ -21,7 +40,6 @@ def log_metadata(file_name, tags, album):
|
|
| 21 |
block_details = blockchain_logger.log_data(metadata)
|
| 22 |
return block_details
|
| 23 |
|
| 24 |
-
|
| 25 |
# Streamlit App Layout
|
| 26 |
st.title("Memora: Photo & Video Uploader")
|
| 27 |
st.subheader("Securely upload and organize your memories")
|
|
@@ -32,8 +50,9 @@ uploaded_files = st.file_uploader(
|
|
| 32 |
)
|
| 33 |
|
| 34 |
if uploaded_files:
|
|
|
|
| 35 |
for uploaded_file in uploaded_files:
|
| 36 |
-
# Save
|
| 37 |
file_path = os.path.join(UPLOAD_DIR, uploaded_file.name)
|
| 38 |
with open(file_path, "wb") as f:
|
| 39 |
f.write(uploaded_file.getbuffer())
|
|
@@ -54,6 +73,14 @@ if uploaded_files:
|
|
| 54 |
metadata = log_metadata(uploaded_file.name, tags.split(','), album)
|
| 55 |
st.write(f"Metadata logged successfully! Block Details: {metadata}")
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
# Blockchain Integrity Validation
|
| 58 |
if st.button("Validate Blockchain Integrity"):
|
| 59 |
is_valid = blockchain_logger.is_blockchain_valid()
|
|
@@ -61,7 +88,3 @@ if st.button("Validate Blockchain Integrity"):
|
|
| 61 |
st.success("Blockchain Integrity: Valid ✅")
|
| 62 |
else:
|
| 63 |
st.error("Blockchain Integrity: Invalid ❌")
|
| 64 |
-
if st.button("Simulate Blockchain Corruption"):
|
| 65 |
-
# Simulate corruption for testing
|
| 66 |
-
blockchain_logger.blockchain[1]['data'] = {"corrupted": "This block has been tampered with."}
|
| 67 |
-
st.warning("Blockchain corrupted for testing purposes!")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
from PIL import Image
|
| 4 |
+
from googleapiclient.discovery import build
|
| 5 |
+
from googleapiclient.http import MediaFileUpload
|
| 6 |
+
from google.oauth2.credentials import Credentials
|
| 7 |
from chainguard.blockchain_logger import BlockchainLogger
|
| 8 |
|
| 9 |
# Initialize Blockchain Logger
|
| 10 |
blockchain_logger = BlockchainLogger()
|
| 11 |
|
| 12 |
+
# Directory for local storage of uploaded files
|
| 13 |
UPLOAD_DIR = "uploads"
|
| 14 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 15 |
|
| 16 |
+
# Load Google Drive API credentials
|
| 17 |
+
def get_drive_service():
|
| 18 |
+
creds = Credentials.from_authorized_user_file("credentials.json", scopes=["https://www.googleapis.com/auth/drive.file"])
|
| 19 |
+
return build('drive', 'v3', credentials=creds)
|
| 20 |
+
|
| 21 |
+
drive_service = get_drive_service()
|
| 22 |
+
|
| 23 |
+
def upload_to_google_drive(file_path, folder_id):
|
| 24 |
+
"""Uploads a file to Google Drive in the specified folder."""
|
| 25 |
+
file_metadata = {
|
| 26 |
+
'name': os.path.basename(file_path),
|
| 27 |
+
'parents': [folder_id]
|
| 28 |
+
}
|
| 29 |
+
media = MediaFileUpload(file_path, resumable=True)
|
| 30 |
+
file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
|
| 31 |
+
return file.get('id')
|
| 32 |
|
| 33 |
def log_metadata(file_name, tags, album):
|
| 34 |
"""Log photo metadata to Chagu blockchain."""
|
|
|
|
| 40 |
block_details = blockchain_logger.log_data(metadata)
|
| 41 |
return block_details
|
| 42 |
|
|
|
|
| 43 |
# Streamlit App Layout
|
| 44 |
st.title("Memora: Photo & Video Uploader")
|
| 45 |
st.subheader("Securely upload and organize your memories")
|
|
|
|
| 50 |
)
|
| 51 |
|
| 52 |
if uploaded_files:
|
| 53 |
+
folder_id = st.text_input("Enter your Google Drive Folder ID", "")
|
| 54 |
for uploaded_file in uploaded_files:
|
| 55 |
+
# Save file locally
|
| 56 |
file_path = os.path.join(UPLOAD_DIR, uploaded_file.name)
|
| 57 |
with open(file_path, "wb") as f:
|
| 58 |
f.write(uploaded_file.getbuffer())
|
|
|
|
| 73 |
metadata = log_metadata(uploaded_file.name, tags.split(','), album)
|
| 74 |
st.write(f"Metadata logged successfully! Block Details: {metadata}")
|
| 75 |
|
| 76 |
+
# Upload to Google Drive
|
| 77 |
+
if folder_id and st.button(f"Upload {uploaded_file.name} to Google Drive"):
|
| 78 |
+
try:
|
| 79 |
+
drive_file_id = upload_to_google_drive(file_path, folder_id)
|
| 80 |
+
st.success(f"File {uploaded_file.name} uploaded to Google Drive with File ID: {drive_file_id}")
|
| 81 |
+
except Exception as e:
|
| 82 |
+
st.error(f"Failed to upload {uploaded_file.name} to Google Drive: {str(e)}")
|
| 83 |
+
|
| 84 |
# Blockchain Integrity Validation
|
| 85 |
if st.button("Validate Blockchain Integrity"):
|
| 86 |
is_valid = blockchain_logger.is_blockchain_valid()
|
|
|
|
| 88 |
st.success("Blockchain Integrity: Valid ✅")
|
| 89 |
else:
|
| 90 |
st.error("Blockchain Integrity: Invalid ❌")
|
|
|
|
|
|
|
|
|
|
|
|