Spaces:
Running
Running
File size: 4,692 Bytes
7405474 756b14b 5398274 7405474 5398274 7405474 5398274 2d94c1e 5398274 756b14b 5398274 756b14b 7405474 5398274 7405474 5398274 7405474 5398274 7405474 5398274 7405474 5398274 7405474 756b14b 5398274 2d94c1e 5398274 2d94c1e 5398274 7405474 5398274 7405474 5398274 7405474 756b14b 5398274 756b14b 5398274 2d94c1e 7405474 5398274 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
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 neo4j import GraphDatabase
from chainguard.blockchain_logger import BlockchainLogger
# Blockchain Logger
blockchain_logger = BlockchainLogger()
# Neo4j Handler
class Neo4jHandler:
def __init__(self, uri, user, password):
self.driver = GraphDatabase.driver(uri, auth=(user, password))
def close(self):
self.driver.close()
def log_transaction(self, file_name, drive_id, blockchain_hash):
"""Log transactions to Neo4j."""
with self.driver.session() as session:
session.write_transaction(self._create_transaction_node, file_name, drive_id, blockchain_hash)
@staticmethod
def _create_transaction_node(tx, file_name, drive_id, blockchain_hash):
tx.run(
"""
MERGE (t:Transaction {file_name: $file_name, drive_id: $drive_id, blockchain_hash: $blockchain_hash})
RETURN t
""",
file_name=file_name, drive_id=drive_id, blockchain_hash=blockchain_hash
)
# Google Drive Integration
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."""
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')
# Metadata Logging
def log_metadata(file_name, tags, album):
"""Log metadata to Chagu blockchain."""
metadata = {"file_name": file_name, "tags": tags, "album": album}
block_details = blockchain_logger.log_data(metadata)
return block_details
# Streamlit Layout
st.title("Memora: Secure File Upload with Blockchain & Neo4j")
st.subheader("Securely upload, organize, and query your files")
# File Upload
uploaded_files = st.file_uploader("Upload your files", accept_multiple_files=True, type=['jpg', 'jpeg', 'png', 'mp4', 'avi'])
if uploaded_files:
# Neo4j Setup
neo4j_handler = Neo4jHandler(
uri="neo4j+s://0ca71b10.databases.neo4j.io",
user="neo4j",
password="HwGDOxyGS1-79nLeTiX5bx5ohoFSpvHCmTv8IRgt-lY"
)
folder_id = st.text_input("Enter Google Drive Folder ID", "")
for uploaded_file in uploaded_files:
# Save file locally
file_path = os.path.join("uploads", uploaded_file.name)
os.makedirs("uploads", exist_ok=True)
with open(file_path, "wb") as f:
f.write(uploaded_file.getbuffer())
st.success(f"File saved locally: {file_path}")
# Display file details
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}", "Default Album")
tags = st.text_input(f"Tags for {uploaded_file.name} (comma-separated)", "")
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! Blockchain Details: {metadata}")
# Upload to Google Drive
if folder_id and st.button(f"Upload {uploaded_file.name} to Google Drive"):
try:
drive_id = upload_to_google_drive(file_path, folder_id)
blockchain_hash = metadata.get('block_hash', 'N/A')
neo4j_handler.log_transaction(uploaded_file.name, drive_id, blockchain_hash)
st.success(f"File uploaded to Google Drive: File ID {drive_id}")
except Exception as e:
st.error(f"Failed to upload {uploaded_file.name} to Google Drive: {str(e)}")
neo4j_handler.close()
# Blockchain Integrity Validation
if st.button("Validate Blockchain Integrity"):
is_valid = blockchain_logger.is_blockchain_valid()
st.write("Blockchain Integrity:", "Valid β
" if is_valid else "Invalid β")
# Query System
st.subheader("Query Files")
query = st.text_input("Enter your query (e.g., 'Good comedy')")
if st.button("Search"):
# Simulating query processing
result = {"response": f"Mock result for '{query}'"}
st.write(f"Query Result: {result['response']}")
|