Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
-
import gradio as gr
|
| 3 |
-
from langchain_community.vectorstores import MongoDBAtlasVectorSearch
|
| 4 |
-
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 5 |
import pymongo
|
| 6 |
import logging
|
| 7 |
import nest_asyncio
|
| 8 |
from langchain.docstore.document import Document
|
| 9 |
import redis
|
| 10 |
-
import asyncio
|
| 11 |
import threading
|
| 12 |
-
import
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
nest_asyncio.apply()
|
| 16 |
-
logging.basicConfig(level=logging.INFO)
|
| 17 |
database = "AlertSimAndRemediation"
|
| 18 |
collection = "alert_embed"
|
| 19 |
stream_name = "alerts"
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
REDIS_PWD = os.getenv('REDIS_PWD')
|
| 25 |
|
| 26 |
-
#
|
| 27 |
embedding_args = {
|
| 28 |
-
"model_name": "BAAI/bge-large-en-v1.5",
|
| 29 |
-
"model_kwargs": {"device": "cpu"},
|
| 30 |
-
"encode_kwargs": {"normalize_embeddings": True}
|
| 31 |
}
|
| 32 |
embedding_model = HuggingFaceEmbeddings(**embedding_args)
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
connection = pymongo.MongoClient(MONGO_URI)
|
| 36 |
alert_collection = connection[database][collection]
|
| 37 |
|
| 38 |
# Redis connection
|
| 39 |
-
r = redis.Redis(host=REDIS_HOST, password=REDIS_PWD, port=16652)
|
| 40 |
-
|
| 41 |
-
# Global variables to store alert information
|
| 42 |
-
latest_alert = "No alerts yet."
|
| 43 |
-
alert_count = 0
|
| 44 |
|
| 45 |
# Preprocessing
|
| 46 |
-
def create_textual_description(entry_data):
|
| 47 |
entry_dict = {k.decode(): v.decode() for k, v in entry_data.items()}
|
| 48 |
|
| 49 |
category = entry_dict["Category"]
|
|
@@ -59,7 +54,7 @@ def create_textual_description(entry_data):
|
|
| 59 |
return description, entry_dict
|
| 60 |
|
| 61 |
# Saving alert doc
|
| 62 |
-
def save(entry):
|
| 63 |
vector_search = MongoDBAtlasVectorSearch.from_documents(
|
| 64 |
documents=[Document(
|
| 65 |
page_content=entry["content"],
|
|
@@ -69,55 +64,65 @@ def save(entry):
|
|
| 69 |
collection=alert_collection,
|
| 70 |
index_name="alert_index",
|
| 71 |
)
|
| 72 |
-
logging.info("
|
| 73 |
|
| 74 |
# Listening to alert stream
|
| 75 |
-
def listen_to_alerts():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
global latest_alert, alert_count
|
| 77 |
-
last_id = '$'
|
| 78 |
-
|
| 79 |
-
while True:
|
| 80 |
-
entries = r.xread({stream_name: last_id}, block=1000, count=None)
|
| 81 |
-
|
| 82 |
-
if entries:
|
| 83 |
-
stream, new_entries = entries[0]
|
| 84 |
-
|
| 85 |
-
for entry_id, entry_data in new_entries:
|
| 86 |
-
description, entry_dict = create_textual_description(entry_data)
|
| 87 |
-
save({
|
| 88 |
-
"content": description,
|
| 89 |
-
"metadata": entry_dict
|
| 90 |
-
})
|
| 91 |
-
latest_alert = description
|
| 92 |
-
alert_count += 1
|
| 93 |
-
last_id = entry_id
|
| 94 |
-
|
| 95 |
-
# Start listening to alerts in a separate thread
|
| 96 |
-
threading.Thread(target=listen_to_alerts, daemon=True).start()
|
| 97 |
-
|
| 98 |
-
# Function to get current stats
|
| 99 |
-
def get_current_stats():
|
| 100 |
return latest_alert, f"Total Alerts: {alert_count}"
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
def update_stats():
|
| 112 |
-
while True:
|
| 113 |
-
time.sleep(1) # Update every second
|
| 114 |
-
yield get_current_stats()
|
| 115 |
-
|
| 116 |
-
iface.load(update_stats, None, [latest_alert_md, alert_count_md], every=1)
|
| 117 |
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
# Launch the app
|
| 121 |
-
if __name__ == "__main__":
|
| 122 |
-
|
| 123 |
-
iface.queue().launch()
|
|
|
|
| 1 |
+
from langchain_mongodb import MongoDBAtlasVectorSearch
|
| 2 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 3 |
+
# from dotenv import load_dotenv
|
| 4 |
import os
|
|
|
|
|
|
|
|
|
|
| 5 |
import pymongo
|
| 6 |
import logging
|
| 7 |
import nest_asyncio
|
| 8 |
from langchain.docstore.document import Document
|
| 9 |
import redis
|
|
|
|
| 10 |
import threading
|
| 11 |
+
import asyncio
|
| 12 |
+
import gradio as gr
|
| 13 |
|
| 14 |
+
# config
|
| 15 |
+
# nest_asyncio.apply()
|
| 16 |
+
logging.basicConfig(level = logging.INFO)
|
| 17 |
database = "AlertSimAndRemediation"
|
| 18 |
collection = "alert_embed"
|
| 19 |
stream_name = "alerts"
|
| 20 |
|
| 21 |
+
# Global variables to store alert information
|
| 22 |
+
latest_alert = "No alerts yet."
|
| 23 |
+
alert_count = 0
|
|
|
|
| 24 |
|
| 25 |
+
# embedding model
|
| 26 |
embedding_args = {
|
| 27 |
+
"model_name" : "BAAI/bge-large-en-v1.5",
|
| 28 |
+
"model_kwargs" : {"device": "cpu"},
|
| 29 |
+
"encode_kwargs" : {"normalize_embeddings": True}
|
| 30 |
}
|
| 31 |
embedding_model = HuggingFaceEmbeddings(**embedding_args)
|
| 32 |
|
| 33 |
+
# Mongo Connection
|
| 34 |
+
connection = pymongo.MongoClient(os.environ["MONGO_URI"])
|
| 35 |
alert_collection = connection[database][collection]
|
| 36 |
|
| 37 |
# Redis connection
|
| 38 |
+
r = redis.Redis(host=os.environ['REDIS_HOST'], password=os.environ['REDIS_PWD'], port=16652)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# Preprocessing
|
| 41 |
+
async def create_textual_description(entry_data):
|
| 42 |
entry_dict = {k.decode(): v.decode() for k, v in entry_data.items()}
|
| 43 |
|
| 44 |
category = entry_dict["Category"]
|
|
|
|
| 54 |
return description, entry_dict
|
| 55 |
|
| 56 |
# Saving alert doc
|
| 57 |
+
async def save(entry):
|
| 58 |
vector_search = MongoDBAtlasVectorSearch.from_documents(
|
| 59 |
documents=[Document(
|
| 60 |
page_content=entry["content"],
|
|
|
|
| 64 |
collection=alert_collection,
|
| 65 |
index_name="alert_index",
|
| 66 |
)
|
| 67 |
+
logging.info("Alerts stored successfully!")
|
| 68 |
|
| 69 |
# Listening to alert stream
|
| 70 |
+
async def listen_to_alerts(r):
|
| 71 |
+
global latest_alert, alert_count
|
| 72 |
+
try:
|
| 73 |
+
last_id = '$'
|
| 74 |
+
|
| 75 |
+
while True:
|
| 76 |
+
entries = r.xread({stream_name: last_id}, block=0, count=None)
|
| 77 |
+
|
| 78 |
+
if entries:
|
| 79 |
+
stream, new_entries = entries[0]
|
| 80 |
+
|
| 81 |
+
for entry_id, entry_data in new_entries:
|
| 82 |
+
description, entry_dict = await create_textual_description(entry_data)
|
| 83 |
+
await save({
|
| 84 |
+
"content" : description,
|
| 85 |
+
"metadata" : entry_dict
|
| 86 |
+
})
|
| 87 |
+
print(description)
|
| 88 |
+
latest_alert = description
|
| 89 |
+
alert_count += 1
|
| 90 |
+
# Update the last ID read
|
| 91 |
+
last_id = entry_id
|
| 92 |
+
await asyncio.sleep(1)
|
| 93 |
+
|
| 94 |
+
except KeyboardInterrupt:
|
| 95 |
+
print("Exiting...")
|
| 96 |
+
|
| 97 |
+
def run_alert_listener():
|
| 98 |
+
asyncio.run(listen_to_alerts(r))
|
| 99 |
+
|
| 100 |
+
# Start the alert listener thread
|
| 101 |
+
alert_thread = threading.Thread(target=run_alert_listener)
|
| 102 |
+
alert_thread.start()
|
| 103 |
+
|
| 104 |
+
# gradio interface
|
| 105 |
+
# Gradio interface
|
| 106 |
+
def get_latest_alert():
|
| 107 |
global latest_alert, alert_count
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
return latest_alert, f"Total Alerts: {alert_count}"
|
| 109 |
|
| 110 |
+
with gr.Blocks() as app:
|
| 111 |
+
gr.Markdown("# Alert Dashboard 🔔")
|
| 112 |
+
|
| 113 |
+
with gr.Row():
|
| 114 |
+
latest_alert_box = gr.Textbox(label="Latest Alert", lines=3, interactive=False)
|
| 115 |
+
alert_count_box = gr.Textbox(label="Alert Count", interactive=False)
|
| 116 |
+
|
| 117 |
+
refresh_button = gr.Button("Refresh")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
refresh_button.click(get_latest_alert, inputs=None, outputs=[latest_alert_box, alert_count_box])
|
| 120 |
+
|
| 121 |
+
app.load(get_latest_alert, inputs=None, outputs=[latest_alert_box, alert_count_box])
|
| 122 |
+
|
| 123 |
+
# Auto-refresh every 5 seconds
|
| 124 |
+
app.load(get_latest_alert, inputs=None, outputs=[latest_alert_box, alert_count_box], every=5)
|
| 125 |
|
| 126 |
# Launch the app
|
| 127 |
+
# if __name__ == "__main__":
|
| 128 |
+
app.launch()
|
|
|