Spaces:
Sleeping
Sleeping
Combine 2 apps
Browse files
app.py
CHANGED
@@ -1,171 +1,28 @@
|
|
1 |
-
from pathlib import Path
|
2 |
-
|
3 |
import streamlit as st
|
4 |
-
|
5 |
-
import pandas as pd
|
6 |
-
import os
|
7 |
-
from rag_sec.document_search_system import DocumentSearchSystem
|
8 |
-
from chainguard.blockchain_logger import BlockchainLogger
|
9 |
-
from PIL import Image
|
10 |
-
from itertools import cycle
|
11 |
-
|
12 |
-
# Blockchain Logger
|
13 |
-
blockchain_logger = BlockchainLogger()
|
14 |
-
|
15 |
-
# Directory for storing uploaded files
|
16 |
-
UPLOAD_DIR = "uploaded_files"
|
17 |
-
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
18 |
-
|
19 |
-
# Initialize DocumentSearchSystem
|
20 |
-
@st.cache_resource
|
21 |
-
def initialize_system():
|
22 |
-
"""Initialize the DocumentSearchSystem and load documents."""
|
23 |
-
system = DocumentSearchSystem(
|
24 |
-
neo4j_uri="neo4j+s://0ca71b10.databases.neo4j.io",
|
25 |
-
neo4j_user="neo4j",
|
26 |
-
neo4j_password="HwGDOxyGS1-79nLeTiX5bx5ohoFSpvHCmTv8IRgt-lY"
|
27 |
-
)
|
28 |
-
system.retriever.load_documents()
|
29 |
-
return system
|
30 |
-
|
31 |
-
# Initialize the system
|
32 |
-
system = initialize_system()
|
33 |
-
|
34 |
-
st.title("Memora: Secure File Upload and Search with Blockchain & Neo4j")
|
35 |
-
st.subheader("Personalized news and global updates at your fingertips")
|
36 |
-
# File Upload Section
|
37 |
-
uploaded_files = st.file_uploader("Upload your files", accept_multiple_files=True, type=['jpg', 'jpeg', 'png', 'mp4', 'avi'])
|
38 |
-
|
39 |
-
if uploaded_files:
|
40 |
-
for uploaded_file in uploaded_files:
|
41 |
-
# Save file locally
|
42 |
-
file_path = os.path.join(UPLOAD_DIR, uploaded_file.name)
|
43 |
-
with open(file_path, "wb") as f:
|
44 |
-
f.write(uploaded_file.getbuffer())
|
45 |
-
st.success(f"File saved locally: {file_path}")
|
46 |
-
|
47 |
-
# Display uploaded file details
|
48 |
-
if uploaded_file.type.startswith('image'):
|
49 |
-
image = Image.open(uploaded_file)
|
50 |
-
st.image(image, caption=uploaded_file.name, use_column_width=True)
|
51 |
-
|
52 |
-
# Metadata Input
|
53 |
-
album = st.text_input(f"Album for {uploaded_file.name}", "Default Album")
|
54 |
-
tags = st.text_input(f"Tags for {uploaded_file.name} (comma-separated)", "")
|
55 |
-
|
56 |
-
# Log Metadata and Transaction
|
57 |
-
if st.button(f"Log Metadata for {uploaded_file.name}"):
|
58 |
-
metadata = {"file_name": uploaded_file.name, "tags": tags.split(','), "album": album}
|
59 |
-
blockchain_details = blockchain_logger.log_data(metadata)
|
60 |
-
blockchain_hash = blockchain_details.get("block_hash", "N/A")
|
61 |
-
|
62 |
-
# Use Neo4jHandler from DocumentSearchSystem to log the transaction
|
63 |
-
system.neo4j_handler.log_relationships(uploaded_file.name, tags, blockchain_hash, [album])
|
64 |
-
st.write(f"Metadata logged successfully! Blockchain Details: {blockchain_details}")
|
65 |
-
|
66 |
-
# Blockchain Integrity Validation
|
67 |
-
if st.button("Validate Blockchain Integrity"):
|
68 |
-
is_valid = blockchain_logger.is_blockchain_valid()
|
69 |
-
st.write("Blockchain Integrity:", "Valid β
" if is_valid else "Invalid β")
|
70 |
-
|
71 |
-
# Document Search Section
|
72 |
-
st.subheader("Search Documents")
|
73 |
-
|
74 |
-
# Google Search: User-Specific News
|
75 |
-
st.subheader("1. Latest News About You")
|
76 |
-
user_name = st.text_input("Enter your name or handle to search for recent news", value="Talex Maxim")
|
77 |
-
|
78 |
-
if st.button("Search News About Me"):
|
79 |
-
if user_name:
|
80 |
-
st.write(f"Searching Google for news about **{user_name}**...")
|
81 |
-
try:
|
82 |
-
results = list(search(user_name, num_results=5))
|
83 |
-
if results:
|
84 |
-
st.success(f"Top {len(results)} results for '{user_name}':")
|
85 |
-
user_news_data = {"URL": results}
|
86 |
-
df_user_news = pd.DataFrame(user_news_data)
|
87 |
-
st.dataframe(df_user_news)
|
88 |
-
else:
|
89 |
-
st.warning("No recent news found about you.")
|
90 |
-
except Exception as e:
|
91 |
-
st.error(f"An error occurred during the search: {str(e)}")
|
92 |
-
else:
|
93 |
-
st.warning("Please enter your name or handle to search.")
|
94 |
-
|
95 |
-
# Google Search: Global News Categories
|
96 |
-
categories = ["Technology", "Sports", "Politics", "Entertainment", "Science"]
|
97 |
-
|
98 |
-
st.title("Global News Insights")
|
99 |
-
|
100 |
-
# News Results Dictionary
|
101 |
-
news_results = {}
|
102 |
-
|
103 |
-
try:
|
104 |
-
# Fetch News for Each Category
|
105 |
-
for category in categories:
|
106 |
-
try:
|
107 |
-
news_results[category] = list(search(f"latest {category} news", num_results=3))
|
108 |
-
except Exception as e:
|
109 |
-
news_results[category] = [f"Error fetching news: {str(e)}"]
|
110 |
-
|
111 |
-
# Display Results with Styled Buttons
|
112 |
-
for category, articles in news_results.items():
|
113 |
-
st.subheader(f"{category} News")
|
114 |
-
cols = st.columns(3) # Create 3 columns for the layout
|
115 |
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
st.markdown(
|
120 |
-
f"""
|
121 |
-
<div style="padding: 10px; border: 1px solid #ccc; border-radius: 5px; margin: 10px; text-align: center;">
|
122 |
-
<a href="{article}" target="_blank" style="text-decoration: none;">
|
123 |
-
<button style="background-color: #c4ccc8; color: white; border: none; padding: 10px 20px; text-align: center; display: inline-block; font-size: 16px; border-radius: 5px;">
|
124 |
-
{category}-{idx + 1}
|
125 |
-
</button>
|
126 |
-
</a>
|
127 |
-
</div>
|
128 |
-
""",
|
129 |
-
unsafe_allow_html=True,
|
130 |
-
)
|
131 |
-
else:
|
132 |
-
st.warning(f"Could not fetch news for **{category}**.")
|
133 |
-
except Exception as e:
|
134 |
-
st.error(f"An unexpected error occurred: {str(e)}")
|
135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
# for idx, article in enumerate(articles, start=1):
|
141 |
-
# st.write(f"{idx}. [Read here]({article})")
|
142 |
-
# except Exception as e:
|
143 |
-
# st.error(f"An error occurred while fetching global news: {str(e)}")
|
144 |
|
145 |
-
|
146 |
-
|
147 |
-
|
|
|
148 |
|
149 |
-
if st.button("Search Documents"):
|
150 |
-
if query:
|
151 |
-
result = system.process_query(query)
|
152 |
-
if result["status"] == "success":
|
153 |
-
st.success(f"Query processed successfully!")
|
154 |
-
st.write("### Query Response:")
|
155 |
-
st.write(result["response"])
|
156 |
-
st.write("### Retrieved Documents:")
|
157 |
-
for idx, doc in enumerate(result["retrieved_documents"], start=1):
|
158 |
-
st.write(f"**Document {idx}:**")
|
159 |
-
st.write(doc[:500]) # Display the first 500 characters
|
160 |
-
st.write("### Blockchain Details:")
|
161 |
-
st.json(result["blockchain_details"])
|
162 |
-
elif result["status"] == "no_results":
|
163 |
-
st.warning("No relevant documents found for your query.")
|
164 |
-
elif result["status"] == "rejected":
|
165 |
-
st.error(result["message"])
|
166 |
-
else:
|
167 |
-
st.warning("Please enter a query to search.")
|
168 |
|
169 |
-
|
170 |
-
|
171 |
-
st.write(f"Total documents loaded: {len(system.retriever.documents)}")
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import importlib.util
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
# Define the paths to the other apps
|
5 |
+
FIRST_APP_PATH = "app-memora.py"
|
6 |
+
SECOND_APP_PATH = "app-news-content.py"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
def run_script(script_path):
|
9 |
+
"""
|
10 |
+
Dynamically load and execute a Python script's main() function.
|
11 |
+
"""
|
12 |
+
# Load the module from the script path
|
13 |
+
spec = importlib.util.spec_from_file_location("module.name", script_path)
|
14 |
+
module = importlib.util.module_from_spec(spec)
|
15 |
+
spec.loader.exec_module(module)
|
16 |
|
17 |
+
def main():
|
18 |
+
st.sidebar.title("Navigation")
|
19 |
+
app_choice = st.sidebar.radio("Choose an App:", ["Home (Secure File System)", "Dynamic News Summarizer"])
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
if app_choice == "Home (Secure File System)":
|
22 |
+
run_script(FIRST_APP_PATH)
|
23 |
+
elif app_choice == "Dynamic News Summarizer":
|
24 |
+
run_script(SECOND_APP_PATH)
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
if __name__ == "__main__":
|
28 |
+
main()
|
|