talexm commited on
Commit
3e269ec
1 Parent(s): ae1fcbd

update app

Browse files
Files changed (1) hide show
  1. app.py +30 -48
app.py CHANGED
@@ -1,61 +1,35 @@
1
  import streamlit as st
2
  import os
 
3
  from PIL import Image
4
- from neo4j import GraphDatabase
5
  from chainguard.blockchain_logger import BlockchainLogger
6
 
7
  # Blockchain Logger
8
  blockchain_logger = BlockchainLogger()
9
 
10
- # Neo4j Handler
11
- class Neo4jHandler:
12
- def __init__(self, uri, user, password):
13
- self.driver = GraphDatabase.driver(uri, auth=(user, password))
 
 
 
 
 
14
 
15
- def close(self):
16
- self.driver.close()
17
-
18
- def log_transaction(self, file_name, file_path, blockchain_hash):
19
- """Log transactions to Neo4j."""
20
- with self.driver.session() as session:
21
- session.write_transaction(self._create_transaction_node, file_name, file_path, blockchain_hash)
22
-
23
- @staticmethod
24
- def _create_transaction_node(tx, file_name, file_path, blockchain_hash):
25
- tx.run(
26
- """
27
- MERGE (t:Transaction {file_name: $file_name, file_path: $file_path, blockchain_hash: $blockchain_hash})
28
- RETURN t
29
- """,
30
- file_name=file_name, file_path=file_path, blockchain_hash=blockchain_hash
31
- )
32
-
33
- # Metadata Logging
34
- def log_metadata(file_name, tags, album):
35
- """Log metadata to Chagu blockchain."""
36
- metadata = {"file_name": file_name, "tags": tags, "album": album}
37
- block_details = blockchain_logger.log_data(metadata)
38
- return block_details
39
 
40
  # Streamlit Layout
41
  st.title("Memora: Secure File Upload with Blockchain & Neo4j")
42
  st.subheader("Securely upload, organize, and query your files")
43
 
44
- # Directory for storing uploaded files
45
- UPLOAD_DIR = "uploaded_files"
46
- os.makedirs(UPLOAD_DIR, exist_ok=True)
47
-
48
  # File Upload
49
  uploaded_files = st.file_uploader("Upload your files", accept_multiple_files=True, type=['jpg', 'jpeg', 'png', 'mp4', 'avi'])
50
 
51
  if uploaded_files:
52
- # Neo4j Setup
53
- neo4j_handler = Neo4jHandler(
54
- uri="neo4j+s://0ca71b10.databases.neo4j.io",
55
- user="neo4j",
56
- password="XXX"
57
- )
58
-
59
  for uploaded_file in uploaded_files:
60
  # Save file locally
61
  file_path = os.path.join(UPLOAD_DIR, uploaded_file.name)
@@ -72,13 +46,15 @@ if uploaded_files:
72
  album = st.text_input(f"Album for {uploaded_file.name}", "Default Album")
73
  tags = st.text_input(f"Tags for {uploaded_file.name} (comma-separated)", "")
74
 
 
75
  if st.button(f"Log Metadata for {uploaded_file.name}"):
76
- metadata = log_metadata(uploaded_file.name, tags.split(','), album)
77
- blockchain_hash = metadata.get('block_hash', 'N/A')
78
- neo4j_handler.log_transaction(uploaded_file.name, file_path, blockchain_hash)
79
- st.write(f"Metadata logged successfully! Blockchain Details: {metadata}")
80
 
81
- neo4j_handler.close()
 
 
82
 
83
  # Blockchain Integrity Validation
84
  if st.button("Validate Blockchain Integrity"):
@@ -89,6 +65,12 @@ if st.button("Validate Blockchain Integrity"):
89
  st.subheader("Query Files")
90
  query = st.text_input("Enter your query (e.g., 'Good comedy')")
91
  if st.button("Search"):
92
- # Simulating query processing
93
- result = {"response": f"Mock result for '{query}'"}
94
- st.write(f"Query Result: {result['response']}")
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import os
3
+ 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
 
8
  # Blockchain Logger
9
  blockchain_logger = BlockchainLogger()
10
 
11
+ # Initialize DocumentSearchSystem
12
+ home_dir = Path(os.getenv("HOME", "/"))
13
+ data_dir = home_dir / "data-sets/aclImdb/train"
14
+ system = DocumentSearchSystem(
15
+ neo4j_uri="neo4j+s://0ca71b10.databases.neo4j.io",
16
+ neo4j_user="neo4j",
17
+ neo4j_password="HwGDOxyGS1-79nLeTiX5bx5ohoFSpvHCmTv8IRgt-lY"
18
+ )
19
+ system.retriever.load_documents(data_dir)
20
 
21
+ # Directory for storing uploaded files
22
+ UPLOAD_DIR = "uploaded_files"
23
+ os.makedirs(UPLOAD_DIR, exist_ok=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  # Streamlit Layout
26
  st.title("Memora: Secure File Upload with Blockchain & Neo4j")
27
  st.subheader("Securely upload, organize, and query your files")
28
 
 
 
 
 
29
  # File Upload
30
  uploaded_files = st.file_uploader("Upload your files", accept_multiple_files=True, type=['jpg', 'jpeg', 'png', 'mp4', 'avi'])
31
 
32
  if uploaded_files:
 
 
 
 
 
 
 
33
  for uploaded_file in uploaded_files:
34
  # Save file locally
35
  file_path = os.path.join(UPLOAD_DIR, uploaded_file.name)
 
46
  album = st.text_input(f"Album for {uploaded_file.name}", "Default Album")
47
  tags = st.text_input(f"Tags for {uploaded_file.name} (comma-separated)", "")
48
 
49
+ # Log Metadata
50
  if st.button(f"Log Metadata for {uploaded_file.name}"):
51
+ metadata = {"file_name": uploaded_file.name, "tags": tags.split(','), "album": album}
52
+ blockchain_details = blockchain_logger.log_data(metadata)
53
+ blockchain_hash = blockchain_details.get("block_hash", "N/A")
 
54
 
55
+ # Use Neo4jHandler from DocumentSearchSystem to log transaction
56
+ system.neo4j_handler.log_transaction(uploaded_file.name, file_path, blockchain_hash)
57
+ st.write(f"Metadata logged successfully! Blockchain Details: {blockchain_details}")
58
 
59
  # Blockchain Integrity Validation
60
  if st.button("Validate Blockchain Integrity"):
 
65
  st.subheader("Query Files")
66
  query = st.text_input("Enter your query (e.g., 'Good comedy')")
67
  if st.button("Search"):
68
+ result = system.process_query(query)
69
+ st.write("Query Status:", result.get("status"))
70
+ st.write("Query Response:", result.get("response"))
71
+ if "retrieved_documents" in result:
72
+ st.write("Retrieved Documents:", result["retrieved_documents"])
73
+ if "blockchain_details" in result:
74
+ st.write("Blockchain Details:", result["blockchain_details"])
75
+ if result.get("status") == "rejected":
76
+ st.error(f"Query Blocked: {result.get('message')}")