File size: 4,916 Bytes
7405474
a463e6e
 
3e269ec
a463e6e
7405474
3e269ec
7405474
 
5398274
7405474
 
3e269ec
759c15a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d94c1e
3e269ec
 
 
7405474
5398274
759c15a
5398274
7405474
a463e6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
759c15a
a463e6e
7405474
 
 
756b14b
0fa01ec
2d94c1e
 
5398274
2d94c1e
759c15a
7405474
 
 
 
 
5398274
 
7405474
759c15a
7405474
3e269ec
 
 
7405474
759c15a
 
3e269ec
5398274
2d94c1e
7405474
 
5398274
 
759c15a
 
0a4227c
6dd2090
 
 
 
 
759c15a
 
 
 
 
 
 
 
 
 
 
 
 
6dd2090
759c15a
 
0a4227c
 
6dd2090
 
 
759c15a
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
119
120
121
122
123
import streamlit as st
from googlesearch import search
import pandas as pd
from pathlib import Path
import os
from PIL import Image
from rag_sec.document_search_system import DocumentSearchSystem
from chainguard.blockchain_logger import BlockchainLogger

# Blockchain Logger
blockchain_logger = BlockchainLogger()

# Initialize DocumentSearchSystem
@st.cache_resource
def initialize_system():
    """Initialize the DocumentSearchSystem and load documents."""
    home_dir = Path(os.getenv("HOME", "/"))
    system = DocumentSearchSystem(
        neo4j_uri="neo4j+s://0ca71b10.databases.neo4j.io",
        neo4j_user="neo4j",
        neo4j_password="HwGDOxyGS1-79nLeTiX5bx5ohoFSpvHCmTv8IRgt-lY"
    )
    system.retriever.load_documents()
    return system

# Initialize and load system
st.write("Initializing the Document Search System...")
system = initialize_system()
st.success("System initialized and documents loaded!")

# Directory for storing uploaded files
UPLOAD_DIR = "uploaded_files"
os.makedirs(UPLOAD_DIR, exist_ok=True)

# Streamlit Layout
st.title("Memora: Secure File Upload and Search with Blockchain & Neo4j")
st.subheader("Securely upload, organize, and query your files")

# Google Search Section
st.subheader("Find User Information via Google Search")
search_query = st.text_input("Enter a name or topic to search on Google")
if st.button("Google Search"):
    if search_query:
        try:
            results = list(search(search_query, num_results=5))  # Fetch top 5 results
            if results:
                st.success(f"Top {len(results)} results for '{search_query}':")
                result_data = {"URL": results}
                df = pd.DataFrame(result_data)
                st.dataframe(df)
            else:
                st.warning("No results found for the search query.")
        except Exception as e:
            st.error(f"An error occurred during the search: {str(e)}")
    else:
        st.warning("Please enter a search query.")

# File Upload Section
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 uploaded 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 and Transaction
        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 the transaction
            system.neo4j_handler.log_relationships(uploaded_file.name, tags, blockchain_hash, [album])
            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 ❌")

# Document Search Section
st.subheader("Search Documents")

# Query Input
query = st.text_input("Enter your query (e.g., 'sports news', 'machine learning')")

if st.button("Search"):
    if query:
        # Process query through the DocumentSearchSystem
        result = system.process_query(query)
        if result["status"] == "success":
            st.success(f"Query processed successfully!")
            st.write("### Query Response:")
            st.write(result["response"])
            st.write("### Retrieved Documents:")
            for idx, doc in enumerate(result["retrieved_documents"], start=1):
                st.write(f"**Document {idx}:**")
                st.write(doc[:500])  # Display the first 500 characters
            st.write("### Blockchain Details:")
            st.json(result["blockchain_details"])
        elif result["status"] == "no_results":
            st.warning("No relevant documents found for your query.")
        elif result["status"] == "rejected":
            st.error(result["message"])
    else:
        st.warning("Please enter a query to search.")

# Debugging Section
if st.checkbox("Show Debug Information"):
    st.write(f"Total documents loaded: {len(system.retriever.documents)}")