chagu-demo / app.py
talexm
update
0fa01ec
raw
history blame
3.51 kB
import streamlit as st
import os
from PIL import Image
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, file_path, blockchain_hash):
"""Log transactions to Neo4j."""
with self.driver.session() as session:
session.write_transaction(self._create_transaction_node, file_name, file_path, blockchain_hash)
@staticmethod
def _create_transaction_node(tx, file_name, file_path, blockchain_hash):
tx.run(
"""
MERGE (t:Transaction {file_name: $file_name, file_path: $file_path, blockchain_hash: $blockchain_hash})
RETURN t
""",
file_name=file_name, file_path=file_path, blockchain_hash=blockchain_hash
)
# 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")
# Directory for storing uploaded files
UPLOAD_DIR = "uploaded_files"
os.makedirs(UPLOAD_DIR, exist_ok=True)
# 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"
)
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 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)
blockchain_hash = metadata.get('block_hash', 'N/A')
neo4j_handler.log_transaction(uploaded_file.name, file_path, blockchain_hash)
st.write(f"Metadata logged successfully! Blockchain Details: {metadata}")
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']}")