Spaces:
Build error
Build error
talexm
commited on
Commit
·
0fa01ec
1
Parent(s):
c928e3d
update
Browse files
app.py
CHANGED
|
@@ -1,9 +1,6 @@
|
|
| 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 neo4j import GraphDatabase
|
| 8 |
from chainguard.blockchain_logger import BlockchainLogger
|
| 9 |
|
|
@@ -18,35 +15,21 @@ class Neo4jHandler:
|
|
| 18 |
def close(self):
|
| 19 |
self.driver.close()
|
| 20 |
|
| 21 |
-
def log_transaction(self, file_name,
|
| 22 |
"""Log transactions to Neo4j."""
|
| 23 |
with self.driver.session() as session:
|
| 24 |
-
session.write_transaction(self._create_transaction_node, file_name,
|
| 25 |
|
| 26 |
@staticmethod
|
| 27 |
-
def _create_transaction_node(tx, file_name,
|
| 28 |
tx.run(
|
| 29 |
"""
|
| 30 |
-
MERGE (t:Transaction {file_name: $file_name,
|
| 31 |
RETURN t
|
| 32 |
""",
|
| 33 |
-
file_name=file_name,
|
| 34 |
)
|
| 35 |
|
| 36 |
-
# Google Drive Integration
|
| 37 |
-
def get_drive_service():
|
| 38 |
-
creds = Credentials.from_authorized_user_file("credentials.json", scopes=["https://www.googleapis.com/auth/drive.file"])
|
| 39 |
-
return build('drive', 'v3', credentials=creds)
|
| 40 |
-
|
| 41 |
-
drive_service = get_drive_service()
|
| 42 |
-
|
| 43 |
-
def upload_to_google_drive(file_path, folder_id):
|
| 44 |
-
"""Uploads a file to Google Drive."""
|
| 45 |
-
file_metadata = {'name': os.path.basename(file_path), 'parents': [folder_id]}
|
| 46 |
-
media = MediaFileUpload(file_path, resumable=True)
|
| 47 |
-
file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
|
| 48 |
-
return file.get('id')
|
| 49 |
-
|
| 50 |
# Metadata Logging
|
| 51 |
def log_metadata(file_name, tags, album):
|
| 52 |
"""Log metadata to Chagu blockchain."""
|
|
@@ -58,6 +41,10 @@ def log_metadata(file_name, tags, album):
|
|
| 58 |
st.title("Memora: Secure File Upload with Blockchain & Neo4j")
|
| 59 |
st.subheader("Securely upload, organize, and query your files")
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
# File Upload
|
| 62 |
uploaded_files = st.file_uploader("Upload your files", accept_multiple_files=True, type=['jpg', 'jpeg', 'png', 'mp4', 'avi'])
|
| 63 |
|
|
@@ -69,11 +56,9 @@ if uploaded_files:
|
|
| 69 |
password="HwGDOxyGS1-79nLeTiX5bx5ohoFSpvHCmTv8IRgt-lY"
|
| 70 |
)
|
| 71 |
|
| 72 |
-
folder_id = st.text_input("Enter Google Drive Folder ID", "")
|
| 73 |
for uploaded_file in uploaded_files:
|
| 74 |
# Save file locally
|
| 75 |
-
file_path = os.path.join(
|
| 76 |
-
os.makedirs("uploads", exist_ok=True)
|
| 77 |
with open(file_path, "wb") as f:
|
| 78 |
f.write(uploaded_file.getbuffer())
|
| 79 |
st.success(f"File saved locally: {file_path}")
|
|
@@ -89,18 +74,10 @@ if uploaded_files:
|
|
| 89 |
|
| 90 |
if st.button(f"Log Metadata for {uploaded_file.name}"):
|
| 91 |
metadata = log_metadata(uploaded_file.name, tags.split(','), album)
|
|
|
|
|
|
|
| 92 |
st.write(f"Metadata logged successfully! Blockchain Details: {metadata}")
|
| 93 |
|
| 94 |
-
# Upload to Google Drive
|
| 95 |
-
if folder_id and st.button(f"Upload {uploaded_file.name} to Google Drive"):
|
| 96 |
-
try:
|
| 97 |
-
drive_id = upload_to_google_drive(file_path, folder_id)
|
| 98 |
-
blockchain_hash = metadata.get('block_hash', 'N/A')
|
| 99 |
-
neo4j_handler.log_transaction(uploaded_file.name, drive_id, blockchain_hash)
|
| 100 |
-
st.success(f"File uploaded to Google Drive: File ID {drive_id}")
|
| 101 |
-
except Exception as e:
|
| 102 |
-
st.error(f"Failed to upload {uploaded_file.name} to Google Drive: {str(e)}")
|
| 103 |
-
|
| 104 |
neo4j_handler.close()
|
| 105 |
|
| 106 |
# Blockchain Integrity Validation
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
| 4 |
from neo4j import GraphDatabase
|
| 5 |
from chainguard.blockchain_logger import BlockchainLogger
|
| 6 |
|
|
|
|
| 15 |
def close(self):
|
| 16 |
self.driver.close()
|
| 17 |
|
| 18 |
+
def log_transaction(self, file_name, file_path, blockchain_hash):
|
| 19 |
"""Log transactions to Neo4j."""
|
| 20 |
with self.driver.session() as session:
|
| 21 |
+
session.write_transaction(self._create_transaction_node, file_name, file_path, blockchain_hash)
|
| 22 |
|
| 23 |
@staticmethod
|
| 24 |
+
def _create_transaction_node(tx, file_name, file_path, blockchain_hash):
|
| 25 |
tx.run(
|
| 26 |
"""
|
| 27 |
+
MERGE (t:Transaction {file_name: $file_name, file_path: $file_path, blockchain_hash: $blockchain_hash})
|
| 28 |
RETURN t
|
| 29 |
""",
|
| 30 |
+
file_name=file_name, file_path=file_path, blockchain_hash=blockchain_hash
|
| 31 |
)
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
# Metadata Logging
|
| 34 |
def log_metadata(file_name, tags, album):
|
| 35 |
"""Log metadata to Chagu blockchain."""
|
|
|
|
| 41 |
st.title("Memora: Secure File Upload with Blockchain & Neo4j")
|
| 42 |
st.subheader("Securely upload, organize, and query your files")
|
| 43 |
|
| 44 |
+
# Directory for storing uploaded files
|
| 45 |
+
UPLOAD_DIR = "uploaded_files"
|
| 46 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 47 |
+
|
| 48 |
# File Upload
|
| 49 |
uploaded_files = st.file_uploader("Upload your files", accept_multiple_files=True, type=['jpg', 'jpeg', 'png', 'mp4', 'avi'])
|
| 50 |
|
|
|
|
| 56 |
password="HwGDOxyGS1-79nLeTiX5bx5ohoFSpvHCmTv8IRgt-lY"
|
| 57 |
)
|
| 58 |
|
|
|
|
| 59 |
for uploaded_file in uploaded_files:
|
| 60 |
# Save file locally
|
| 61 |
+
file_path = os.path.join(UPLOAD_DIR, uploaded_file.name)
|
|
|
|
| 62 |
with open(file_path, "wb") as f:
|
| 63 |
f.write(uploaded_file.getbuffer())
|
| 64 |
st.success(f"File saved locally: {file_path}")
|
|
|
|
| 74 |
|
| 75 |
if st.button(f"Log Metadata for {uploaded_file.name}"):
|
| 76 |
metadata = log_metadata(uploaded_file.name, tags.split(','), album)
|
| 77 |
+
blockchain_hash = metadata.get('block_hash', 'N/A')
|
| 78 |
+
neo4j_handler.log_transaction(uploaded_file.name, file_path, blockchain_hash)
|
| 79 |
st.write(f"Metadata logged successfully! Blockchain Details: {metadata}")
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
neo4j_handler.close()
|
| 82 |
|
| 83 |
# Blockchain Integrity Validation
|