talexm commited on
Commit
759c15a
β€’
1 Parent(s): 3ec398b

adjustments

Browse files
Files changed (1) hide show
  1. app.py +43 -49
app.py CHANGED
@@ -4,34 +4,40 @@ from pathlib import Path
4
  from PIL import Image
5
  from rag_sec.document_search_system import DocumentSearchSystem
6
  from chainguard.blockchain_logger import BlockchainLogger
7
- from rag_sec.document_search_system import main
8
- import streamlit as st
9
- from rag_sec.document_retriver import DocumentRetriever
10
 
11
  # Blockchain Logger
12
  blockchain_logger = BlockchainLogger()
13
 
14
  # Initialize DocumentSearchSystem
15
- home_dir = Path(os.getenv("HOME", "/"))
16
- data_dir = home_dir / "data-sets/aclImdb/train"
17
- system = DocumentSearchSystem(
18
- neo4j_uri="neo4j+s://0ca71b10.databases.neo4j.io",
19
- neo4j_user="neo4j",
20
- neo4j_password="HwGDOxyGS1-79nLeTiX5bx5ohoFSpvHCmTv8IRgt-lY"
21
- )
22
-
23
- system.retriever.load_documents()
 
 
 
 
 
 
 
24
 
25
  # Directory for storing uploaded files
26
  UPLOAD_DIR = "uploaded_files"
27
  os.makedirs(UPLOAD_DIR, exist_ok=True)
28
 
29
  # Streamlit Layout
30
- st.title("Memora: Secure File Upload with Blockchain & Neo4j")
31
  st.subheader("Securely upload, organize, and query your files")
32
 
33
- # File Upload
34
- uploaded_files = st.file_uploader("Upload your files", accept_multiple_files=True, type=['jpg', 'jpeg', 'png', 'mp4', 'avi'])
 
 
35
 
36
  if uploaded_files:
37
  for uploaded_file in uploaded_files:
@@ -41,7 +47,7 @@ if uploaded_files:
41
  f.write(uploaded_file.getbuffer())
42
  st.success(f"File saved locally: {file_path}")
43
 
44
- # Display file details
45
  if uploaded_file.type.startswith('image'):
46
  image = Image.open(uploaded_file)
47
  st.image(image, caption=uploaded_file.name, use_column_width=True)
@@ -50,14 +56,14 @@ if uploaded_files:
50
  album = st.text_input(f"Album for {uploaded_file.name}", "Default Album")
51
  tags = st.text_input(f"Tags for {uploaded_file.name} (comma-separated)", "")
52
 
53
- # Log Metadata
54
  if st.button(f"Log Metadata for {uploaded_file.name}"):
55
  metadata = {"file_name": uploaded_file.name, "tags": tags.split(','), "album": album}
56
  blockchain_details = blockchain_logger.log_data(metadata)
57
  blockchain_hash = blockchain_details.get("block_hash", "N/A")
58
 
59
- # Use Neo4jHandler from DocumentSearchSystem to log transaction
60
- system.neo4j_handler.log_transaction(uploaded_file.name, file_path, blockchain_hash)
61
  st.write(f"Metadata logged successfully! Blockchain Details: {blockchain_details}")
62
 
63
  # Blockchain Integrity Validation
@@ -65,45 +71,33 @@ if st.button("Validate Blockchain Integrity"):
65
  is_valid = blockchain_logger.is_blockchain_valid()
66
  st.write("Blockchain Integrity:", "Valid βœ…" if is_valid else "Invalid ❌")
67
 
68
- # Query System
69
- st.subheader("Query Files")
70
-
71
- # Initialize DocumentRetriever
72
- retriever = DocumentRetriever()
73
-
74
- @st.cache_resource
75
- def load_retriever():
76
- """Load documents into the retriever."""
77
- retriever.load_documents()
78
- return retriever
79
- # Load the retriever and documents
80
- st.write("Loading documents...")
81
- retriever = load_retriever()
82
- st.write("Documents successfully loaded!")
83
-
84
- # Streamlit UI
85
- st.title("Document Search App")
86
- st.subheader("Enter a query to search for related documents")
87
 
88
  # Query Input
89
  query = st.text_input("Enter your query (e.g., 'sports news', 'machine learning')")
90
 
91
  if st.button("Search"):
92
  if query:
93
- # Retrieve documents
94
- results = retriever.retrieve(query)
95
- if results == ["Document retrieval is not initialized."]:
96
- st.error("Document retrieval is not initialized. Please reload the app.")
97
- elif not results:
 
 
 
 
 
 
 
 
98
  st.warning("No relevant documents found for your query.")
99
- else:
100
- st.success(f"Found {len(results)} relevant document(s).")
101
- for idx, doc in enumerate(results, start=1):
102
- st.write(f"### Document {idx}")
103
- st.write(doc[:500]) # Display first 500 characters of each document
104
  else:
105
  st.warning("Please enter a query to search.")
106
 
107
  # Debugging Section
108
  if st.checkbox("Show Debug Information"):
109
- st.write(f"Total documents loaded: {len(retriever.documents)}")
 
4
  from PIL import Image
5
  from rag_sec.document_search_system import DocumentSearchSystem
6
  from chainguard.blockchain_logger import BlockchainLogger
 
 
 
7
 
8
  # Blockchain Logger
9
  blockchain_logger = BlockchainLogger()
10
 
11
  # Initialize DocumentSearchSystem
12
+ @st.cache_resource
13
+ def initialize_system():
14
+ """Initialize the DocumentSearchSystem and load documents."""
15
+ home_dir = Path(os.getenv("HOME", "/"))
16
+ system = DocumentSearchSystem(
17
+ neo4j_uri="neo4j+s://0ca71b10.databases.neo4j.io",
18
+ neo4j_user="neo4j",
19
+ neo4j_password="HwGDOxyGS1-79nLeTiX5bx5ohoFSpvHCmTv8IRgt-lY"
20
+ )
21
+ system.retriever.load_documents()
22
+ return system
23
+
24
+ # Initialize and load system
25
+ st.write("Initializing the Document Search System...")
26
+ system = initialize_system()
27
+ st.success("System initialized and documents loaded!")
28
 
29
  # Directory for storing uploaded files
30
  UPLOAD_DIR = "uploaded_files"
31
  os.makedirs(UPLOAD_DIR, exist_ok=True)
32
 
33
  # Streamlit Layout
34
+ st.title("Memora: Secure File Upload and Search with Blockchain & Neo4j")
35
  st.subheader("Securely upload, organize, and query your files")
36
 
37
+ # File Upload Section
38
+ uploaded_files = st.file_uploader(
39
+ "Upload your files", accept_multiple_files=True, type=['jpg', 'jpeg', 'png', 'mp4', 'avi']
40
+ )
41
 
42
  if uploaded_files:
43
  for uploaded_file in uploaded_files:
 
47
  f.write(uploaded_file.getbuffer())
48
  st.success(f"File saved locally: {file_path}")
49
 
50
+ # Display uploaded file details
51
  if uploaded_file.type.startswith('image'):
52
  image = Image.open(uploaded_file)
53
  st.image(image, caption=uploaded_file.name, use_column_width=True)
 
56
  album = st.text_input(f"Album for {uploaded_file.name}", "Default Album")
57
  tags = st.text_input(f"Tags for {uploaded_file.name} (comma-separated)", "")
58
 
59
+ # Log Metadata and Transaction
60
  if st.button(f"Log Metadata for {uploaded_file.name}"):
61
  metadata = {"file_name": uploaded_file.name, "tags": tags.split(','), "album": album}
62
  blockchain_details = blockchain_logger.log_data(metadata)
63
  blockchain_hash = blockchain_details.get("block_hash", "N/A")
64
 
65
+ # Use Neo4jHandler from DocumentSearchSystem to log the transaction
66
+ system.neo4j_handler.log_relationships(uploaded_file.name, tags, blockchain_hash, [album])
67
  st.write(f"Metadata logged successfully! Blockchain Details: {blockchain_details}")
68
 
69
  # Blockchain Integrity Validation
 
71
  is_valid = blockchain_logger.is_blockchain_valid()
72
  st.write("Blockchain Integrity:", "Valid βœ…" if is_valid else "Invalid ❌")
73
 
74
+ # Document Search Section
75
+ st.subheader("Search Documents")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
  # Query Input
78
  query = st.text_input("Enter your query (e.g., 'sports news', 'machine learning')")
79
 
80
  if st.button("Search"):
81
  if query:
82
+ # Process query through the DocumentSearchSystem
83
+ result = system.process_query(query)
84
+ if result["status"] == "success":
85
+ st.success(f"Query processed successfully!")
86
+ st.write("### Query Response:")
87
+ st.write(result["response"])
88
+ st.write("### Retrieved Documents:")
89
+ for idx, doc in enumerate(result["retrieved_documents"], start=1):
90
+ st.write(f"**Document {idx}:**")
91
+ st.write(doc[:500]) # Display the first 500 characters
92
+ st.write("### Blockchain Details:")
93
+ st.json(result["blockchain_details"])
94
+ elif result["status"] == "no_results":
95
  st.warning("No relevant documents found for your query.")
96
+ elif result["status"] == "rejected":
97
+ st.error(result["message"])
 
 
 
98
  else:
99
  st.warning("Please enter a query to search.")
100
 
101
  # Debugging Section
102
  if st.checkbox("Show Debug Information"):
103
+ st.write(f"Total documents loaded: {len(system.retriever.documents)}")