import streamlit as st import os from pathlib import Path from PIL import Image from rag_sec.document_search_system import DocumentSearchSystem from chainguard.blockchain_logger import BlockchainLogger from rag_sec.document_search_system import main # Blockchain Logger blockchain_logger = BlockchainLogger() # Initialize DocumentSearchSystem home_dir = Path(os.getenv("HOME", "/")) data_dir = home_dir / "data-sets/aclImdb/train" system = DocumentSearchSystem( neo4j_uri="neo4j+s://0ca71b10.databases.neo4j.io", neo4j_user="neo4j", neo4j_password="HwGDOxyGS1-79nLeTiX5bx5ohoFSpvHCmTv8IRgt-lY" ) system.retriever.load_documents() # Directory for storing uploaded files UPLOAD_DIR = "uploaded_files" os.makedirs(UPLOAD_DIR, exist_ok=True) # 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: 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)", "") # Log Metadata if st.button(f"Log Metadata for {uploaded_file.name}"): metadata = {"file_name": uploaded_file.name, "tags": tags.split(','), "album": album} blockchain_details = blockchain_logger.log_data(metadata) blockchain_hash = blockchain_details.get("block_hash", "N/A") # Use Neo4jHandler from DocumentSearchSystem to log transaction system.neo4j_handler.log_transaction(uploaded_file.name, file_path, blockchain_hash) st.write(f"Metadata logged successfully! Blockchain Details: {blockchain_details}") # 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") system = main() # Initialize system with Neo4j and load documents # Query Input query = st.text_input("Enter your query", placeholder="E.g., 'Good comedy'") if st.button("Search"): if query: # Process the query result = system.process_query(query) # Display the results st.write("Query Status:", result.get("status")) st.write("Query Response:", result.get("response")) if "retrieved_documents" in result: st.write("Retrieved Documents:") for doc in result["retrieved_documents"]: st.markdown(f"- {doc}") if "blockchain_details" in result: st.write("Blockchain Details:") st.json(result["blockchain_details"]) if result.get("status") == "rejected": st.error(f"Query Blocked: {result.get('message')}") else: st.warning("Please enter a query to search.")