Spaces:
Build error
Build error
talexm commited on
Commit ·
5398274
1
Parent(s): 756b14b
update app p.4
Browse files
app.py
CHANGED
|
@@ -4,16 +4,36 @@ 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 |
-
#
|
| 10 |
blockchain_logger = BlockchainLogger()
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
|
@@ -21,70 +41,77 @@ def get_drive_service():
|
|
| 21 |
drive_service = get_drive_service()
|
| 22 |
|
| 23 |
def upload_to_google_drive(file_path, folder_id):
|
| 24 |
-
"""Uploads a file to Google Drive
|
| 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
|
| 35 |
-
metadata = {
|
| 36 |
-
"file_name": file_name,
|
| 37 |
-
"tags": tags,
|
| 38 |
-
"album": album
|
| 39 |
-
}
|
| 40 |
block_details = blockchain_logger.log_data(metadata)
|
| 41 |
return block_details
|
| 42 |
|
| 43 |
-
# Streamlit
|
| 44 |
-
st.title("Memora:
|
| 45 |
-
st.subheader("Securely upload and
|
| 46 |
|
| 47 |
# File Upload
|
| 48 |
-
uploaded_files = st.file_uploader(
|
| 49 |
-
"Upload your photos or videos", accept_multiple_files=True, type=['jpg', 'jpeg', 'png', 'mp4', 'avi']
|
| 50 |
-
)
|
| 51 |
|
| 52 |
if uploaded_files:
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
for uploaded_file in uploaded_files:
|
| 55 |
# Save file locally
|
| 56 |
-
file_path = os.path.join(
|
|
|
|
| 57 |
with open(file_path, "wb") as f:
|
| 58 |
f.write(uploaded_file.getbuffer())
|
| 59 |
-
st.success(f"File
|
| 60 |
|
| 61 |
-
# Display
|
| 62 |
-
st.write(f"File Name: {uploaded_file.name}")
|
| 63 |
if uploaded_file.type.startswith('image'):
|
| 64 |
image = Image.open(uploaded_file)
|
| 65 |
st.image(image, caption=uploaded_file.name, use_column_width=True)
|
| 66 |
|
| 67 |
# Metadata Input
|
| 68 |
-
album = st.text_input(f"Album for {uploaded_file.name}",
|
| 69 |
-
tags = st.text_input(f"Tags for {uploaded_file.name} (comma-separated)",
|
| 70 |
|
| 71 |
-
# Log Metadata
|
| 72 |
if st.button(f"Log Metadata for {uploaded_file.name}"):
|
| 73 |
metadata = log_metadata(uploaded_file.name, tags.split(','), album)
|
| 74 |
-
st.write(f"Metadata logged successfully!
|
| 75 |
|
| 76 |
# Upload to Google Drive
|
| 77 |
if folder_id and st.button(f"Upload {uploaded_file.name} to Google Drive"):
|
| 78 |
try:
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
| 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()
|
| 87 |
-
if is_valid
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|
| 10 |
+
# Blockchain Logger
|
| 11 |
blockchain_logger = BlockchainLogger()
|
| 12 |
|
| 13 |
+
# Neo4j Handler
|
| 14 |
+
class Neo4jHandler:
|
| 15 |
+
def __init__(self, uri, user, password):
|
| 16 |
+
self.driver = GraphDatabase.driver(uri, auth=(user, password))
|
| 17 |
|
| 18 |
+
def close(self):
|
| 19 |
+
self.driver.close()
|
| 20 |
+
|
| 21 |
+
def log_transaction(self, file_name, drive_id, blockchain_hash):
|
| 22 |
+
"""Log transactions to Neo4j."""
|
| 23 |
+
with self.driver.session() as session:
|
| 24 |
+
session.write_transaction(self._create_transaction_node, file_name, drive_id, blockchain_hash)
|
| 25 |
+
|
| 26 |
+
@staticmethod
|
| 27 |
+
def _create_transaction_node(tx, file_name, drive_id, blockchain_hash):
|
| 28 |
+
tx.run(
|
| 29 |
+
"""
|
| 30 |
+
MERGE (t:Transaction {file_name: $file_name, drive_id: $drive_id, blockchain_hash: $blockchain_hash})
|
| 31 |
+
RETURN t
|
| 32 |
+
""",
|
| 33 |
+
file_name=file_name, drive_id=drive_id, blockchain_hash=blockchain_hash
|
| 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)
|
|
|
|
| 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."""
|
| 53 |
+
metadata = {"file_name": file_name, "tags": tags, "album": album}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
block_details = blockchain_logger.log_data(metadata)
|
| 55 |
return block_details
|
| 56 |
|
| 57 |
+
# Streamlit Layout
|
| 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 |
|
| 64 |
if uploaded_files:
|
| 65 |
+
# Neo4j Setup
|
| 66 |
+
neo4j_handler = Neo4jHandler(
|
| 67 |
+
uri="neo4j+s://0ca71b10.databases.neo4j.io",
|
| 68 |
+
user="neo4j",
|
| 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("uploads", uploaded_file.name)
|
| 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}")
|
| 80 |
|
| 81 |
+
# Display file details
|
|
|
|
| 82 |
if uploaded_file.type.startswith('image'):
|
| 83 |
image = Image.open(uploaded_file)
|
| 84 |
st.image(image, caption=uploaded_file.name, use_column_width=True)
|
| 85 |
|
| 86 |
# Metadata Input
|
| 87 |
+
album = st.text_input(f"Album for {uploaded_file.name}", "Default Album")
|
| 88 |
+
tags = st.text_input(f"Tags for {uploaded_file.name} (comma-separated)", "")
|
| 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
|
| 107 |
if st.button("Validate Blockchain Integrity"):
|
| 108 |
is_valid = blockchain_logger.is_blockchain_valid()
|
| 109 |
+
st.write("Blockchain Integrity:", "Valid ✅" if is_valid else "Invalid ❌")
|
| 110 |
+
|
| 111 |
+
# Query System
|
| 112 |
+
st.subheader("Query Files")
|
| 113 |
+
query = st.text_input("Enter your query (e.g., 'Good comedy')")
|
| 114 |
+
if st.button("Search"):
|
| 115 |
+
# Simulating query processing
|
| 116 |
+
result = {"response": f"Mock result for '{query}'"}
|
| 117 |
+
st.write(f"Query Result: {result['response']}")
|