talexm commited on
Commit
1b7191e
β€’
1 Parent(s): 8ce7f13
Files changed (1) hide show
  1. app.py +108 -44
app.py CHANGED
@@ -1,11 +1,14 @@
1
- from pathlib import Path
2
-
3
  import streamlit as st
4
- from googlesearch import search
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
 
10
  # Blockchain Logger
11
  blockchain_logger = BlockchainLogger()
@@ -14,6 +17,7 @@ blockchain_logger = BlockchainLogger()
14
  @st.cache_resource
15
  def initialize_system():
16
  """Initialize the DocumentSearchSystem and load documents."""
 
17
  system = DocumentSearchSystem(
18
  neo4j_uri="neo4j+s://0ca71b10.databases.neo4j.io",
19
  neo4j_user="neo4j",
@@ -25,56 +29,116 @@ def initialize_system():
25
  # Initialize the system
26
  system = initialize_system()
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  # Streamlit Layout
29
- st.title("Memora: Advanced Search and News Insights")
30
- st.subheader("Personalized news and global updates at your fingertips")
 
 
 
 
 
 
 
 
 
 
31
 
32
  # Google Search: User-Specific News
33
- st.subheader("1. Latest News About You")
34
- user_name = st.text_input("Enter your name or handle to search for recent news", value="Talex Maxim")
35
-
36
- if st.button("Search News About Me"):
37
- if user_name:
38
- st.write(f"Searching Google for news about **{user_name}**...")
39
- try:
40
- results = list(search(user_name, num_results=5))
41
- if results:
42
- st.success(f"Top {len(results)} results for '{user_name}':")
43
- user_news_data = {"URL": results}
44
- df_user_news = pd.DataFrame(user_news_data)
45
- st.dataframe(df_user_news)
46
- else:
47
- st.warning("No recent news found about you.")
48
- except Exception as e:
49
- st.error(f"An error occurred during the search: {str(e)}")
50
  else:
51
- st.warning("Please enter your name or handle to search.")
 
 
52
 
53
  # Google Search: Global News Categories
54
  st.subheader("2. Global News Insights")
55
  categories = ["Technology", "Sports", "Politics", "Entertainment", "Science"]
56
- news_results = {}
57
 
58
- if st.button("Fetch Global News"):
59
- try:
60
- for category in categories:
61
- st.write(f"Fetching news for **{category}**...")
62
- try:
63
- category_results = list(search(f"latest {category} news", num_results=3))
64
- news_results[category] = category_results
65
- except Exception as e:
66
- news_results[category] = [f"Error fetching news: {str(e)}"]
67
-
68
- # Display results
69
- for category, articles in news_results.items():
70
- st.write(f"### Top News in {category}:")
71
- for idx, article in enumerate(articles, start=1):
72
- st.write(f"{idx}. [Read here]({article})")
73
- except Exception as e:
74
- st.error(f"An error occurred while fetching global news: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
- # Document Search
77
- st.subheader("3. Search Documents")
78
  query = st.text_input("Enter your query (e.g., 'sports news', 'machine learning')")
79
 
80
  if st.button("Search Documents"):
 
 
 
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
+ import requests
8
+ import pandas as pd
9
+
10
+ # SerpAPI Key (Replace with your SerpAPI key)
11
+ SERPAPI_KEY = "your_serpapi_api_key"
12
 
13
  # Blockchain Logger
14
  blockchain_logger = BlockchainLogger()
 
17
  @st.cache_resource
18
  def initialize_system():
19
  """Initialize the DocumentSearchSystem and load documents."""
20
+ home_dir = Path(os.getenv("HOME", "/"))
21
  system = DocumentSearchSystem(
22
  neo4j_uri="neo4j+s://0ca71b10.databases.neo4j.io",
23
  neo4j_user="neo4j",
 
29
  # Initialize the system
30
  system = initialize_system()
31
 
32
+ # Function to Fetch News from SerpAPI
33
+ def fetch_news(query, num_results=5):
34
+ """Fetch search results using SerpAPI."""
35
+ url = "https://serpapi.com/search"
36
+ params = {
37
+ "engine": "google",
38
+ "q": query,
39
+ "api_key": SERPAPI_KEY,
40
+ "num": num_results
41
+ }
42
+ try:
43
+ response = requests.get(url, params=params)
44
+ response.raise_for_status()
45
+ search_results = response.json().get("organic_results", [])
46
+ return [{"title": result["title"], "link": result["link"]} for result in search_results]
47
+ except Exception as e:
48
+ return [{"error": f"An error occurred: {str(e)}"}]
49
+
50
+ # Mock User Information (Replace with actual Google Login integration if needed)
51
+ def get_user_info():
52
+ """Fetch or mock user details."""
53
+ return {"name": "Talex Maxim", "email": "taimax13@gmail.com"} # Replace with dynamic user info
54
+
55
+ # Directory for storing uploaded files
56
+ UPLOAD_DIR = "uploaded_files"
57
+ os.makedirs(UPLOAD_DIR, exist_ok=True)
58
+
59
  # Streamlit Layout
60
+ st.title("Memora: Advanced File Upload and News Insights")
61
+ st.subheader("Securely upload, organize, and query your files while staying informed.")
62
+
63
+ # User-Specific Information
64
+ user_info = get_user_info()
65
+ if user_info:
66
+ st.sidebar.write("### Logged in as:")
67
+ st.sidebar.write(f"**Name:** {user_info['name']}")
68
+ st.sidebar.write(f"**Email:** {user_info['email']}")
69
+ else:
70
+ st.sidebar.write("### Not Logged In")
71
+ st.sidebar.write("We invite you on the journey! Please log in with your Google account.")
72
 
73
  # Google Search: User-Specific News
74
+ if user_info:
75
+ st.subheader("1. Latest News About You")
76
+ user_name = user_info["name"]
77
+ st.write(f"Fetching latest news for **{user_name}**...")
78
+ user_news = fetch_news(user_name, num_results=5)
79
+
80
+ if user_news and "error" not in user_news[0]:
81
+ st.success(f"Top {len(user_news)} results for '{user_name}':")
82
+ user_news_df = pd.DataFrame(user_news)
83
+ st.dataframe(user_news_df)
 
 
 
 
 
 
 
84
  else:
85
+ st.error(user_news[0].get("error", "No news found."))
86
+ else:
87
+ st.warning("Please log in with your Google account to fetch personalized news.")
88
 
89
  # Google Search: Global News Categories
90
  st.subheader("2. Global News Insights")
91
  categories = ["Technology", "Sports", "Politics", "Entertainment", "Science"]
 
92
 
93
+ for category in categories:
94
+ st.write(f"Fetching news for **{category}**...")
95
+ category_results = fetch_news(f"latest {category} news", num_results=3)
96
+ if category_results and "error" not in category_results[0]:
97
+ st.success(f"Top {len(category_results)} results for '{category}':")
98
+ for idx, result in enumerate(category_results, start=1):
99
+ st.write(f"{idx}. [{result['title']}]({result['link']})")
100
+ else:
101
+ st.error(category_results[0].get("error", "No news found."))
102
+
103
+ # File Upload Section
104
+ st.subheader("3. Upload and Organize Files")
105
+
106
+ uploaded_files = st.file_uploader("Upload your files", accept_multiple_files=True, type=['jpg', 'jpeg', 'png', 'mp4', 'avi'])
107
+
108
+ if uploaded_files:
109
+ for uploaded_file in uploaded_files:
110
+ # Save file locally
111
+ file_path = os.path.join(UPLOAD_DIR, uploaded_file.name)
112
+ with open(file_path, "wb") as f:
113
+ f.write(uploaded_file.getbuffer())
114
+ st.success(f"File saved locally: {file_path}")
115
+
116
+ # Display file details
117
+ if uploaded_file.type.startswith('image'):
118
+ image = Image.open(uploaded_file)
119
+ st.image(image, caption=uploaded_file.name, use_column_width=True)
120
+
121
+ # Metadata Input
122
+ album = st.text_input(f"Album for {uploaded_file.name}", "Default Album")
123
+ tags = st.text_input(f"Tags for {uploaded_file.name} (comma-separated)", "")
124
+
125
+ # Log Metadata
126
+ if st.button(f"Log Metadata for {uploaded_file.name}"):
127
+ metadata = {"file_name": uploaded_file.name, "tags": tags.split(','), "album": album}
128
+ blockchain_details = blockchain_logger.log_data(metadata)
129
+ blockchain_hash = blockchain_details.get("block_hash", "N/A")
130
+
131
+ # Use Neo4jHandler from DocumentSearchSystem to log transaction
132
+ system.neo4j_handler.log_relationships(uploaded_file.name, album, blockchain_hash, [])
133
+ st.write(f"Metadata logged successfully! Blockchain Details: {blockchain_details}")
134
+
135
+ # Blockchain Integrity Validation
136
+ if st.button("Validate Blockchain Integrity"):
137
+ is_valid = blockchain_logger.is_blockchain_valid()
138
+ st.write("Blockchain Integrity:", "Valid βœ…" if is_valid else "Invalid ❌")
139
 
140
+ # Query System
141
+ st.subheader("4. Search Documents")
142
  query = st.text_input("Enter your query (e.g., 'sports news', 'machine learning')")
143
 
144
  if st.button("Search Documents"):