Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,6 +30,7 @@ import gradio as gr
|
|
| 30 |
|
| 31 |
class LogAnalyzer:
|
| 32 |
def __init__(self, log_file_path, csv_file_path, qdrant_data_dir):
|
|
|
|
| 33 |
self.log_file_path = log_file_path
|
| 34 |
self.csv_file_path = csv_file_path
|
| 35 |
self.qdrant_data_dir = qdrant_data_dir
|
|
@@ -38,9 +39,11 @@ class LogAnalyzer:
|
|
| 38 |
self.client = None
|
| 39 |
self.open_router_client = None
|
| 40 |
self.results = {} # Dictionary to store queries and their results
|
|
|
|
| 41 |
|
| 42 |
def process_log_to_csv(self):
|
| 43 |
"""Reads a log file, converts it to CSV, and saves it."""
|
|
|
|
| 44 |
with open(self.log_file_path, 'r') as f:
|
| 45 |
log_content = f.read()
|
| 46 |
|
|
@@ -65,6 +68,7 @@ class LogAnalyzer:
|
|
| 65 |
print(f"Log file converted to CSV: {self.csv_file_path}")
|
| 66 |
|
| 67 |
def create_log_embeddings(self):
|
|
|
|
| 68 |
"""Reads CSV, creates combined text, generates embeddings, and stores in instance."""
|
| 69 |
df = pd.read_csv(self.csv_file_path)
|
| 70 |
df = df.drop_duplicates()
|
|
@@ -86,6 +90,7 @@ class LogAnalyzer:
|
|
| 86 |
self.df_combined["embedding"] = self.df_combined.combined.apply(lambda x: self.embedder.encode(x))
|
| 87 |
|
| 88 |
def initialize_qdrant(self):
|
|
|
|
| 89 |
"""Initializes Qdrant client and creates collection if it doesn't exist."""
|
| 90 |
#drive.mount('/content/drive')
|
| 91 |
os.makedirs(self.qdrant_data_dir, exist_ok=True)
|
|
@@ -106,6 +111,7 @@ class LogAnalyzer:
|
|
| 106 |
|
| 107 |
|
| 108 |
def upload_embeddings_to_qdrant(self):
|
|
|
|
| 109 |
"""Uploads embeddings from dataframe to Qdrant collection."""
|
| 110 |
if self.client is not None and self.df_combined is not None:
|
| 111 |
self.client.upload_points(
|
|
@@ -128,6 +134,7 @@ class LogAnalyzer:
|
|
| 128 |
print("Qdrant client or embeddings not initialized. Run initialize_qdrant and create_log_embeddings first.")
|
| 129 |
|
| 130 |
def search_qdrant(self, query, n=15):
|
|
|
|
| 131 |
"""Searches Qdrant for similar log entries based on query."""
|
| 132 |
if self.client is not None and self.embedder is not None:
|
| 133 |
query_embedding = self.embedder.encode(query).tolist()
|
|
@@ -152,6 +159,7 @@ class LogAnalyzer:
|
|
| 152 |
return []
|
| 153 |
|
| 154 |
def setup_openrouter_client(self):
|
|
|
|
| 155 |
"""Sets up and returns the OpenRouter client."""
|
| 156 |
OPEN_ROUTER_API_KEY = os.getenv('OPEN_ROUTER_API_KEY_2')
|
| 157 |
self.open_router_client = OpenAI(
|
|
@@ -160,6 +168,7 @@ class LogAnalyzer:
|
|
| 160 |
)
|
| 161 |
|
| 162 |
def generate_answer(self, query):
|
|
|
|
| 163 |
"""Generates an answer to the query using LLM based on Qdrant search results and stores the result."""
|
| 164 |
if self.open_router_client is not None and self.client is not None and self.embedder is not None:
|
| 165 |
context = f"""
|
|
@@ -203,17 +212,14 @@ health of the system in one word - good or critical in the same line.
|
|
| 203 |
print("Clients or embedder not initialized. Run setup_openrouter_client, initialize_qdrant, and create_log_embeddings first.")
|
| 204 |
return "Error: Log analysis components not fully initialized."
|
| 205 |
|
| 206 |
-
def render_markdown(self, md_text):
|
| 207 |
-
"""Renders markdown text as HTML."""
|
| 208 |
-
html = markdown.markdown(md_text)
|
| 209 |
-
display(HTML(html))
|
| 210 |
-
|
| 211 |
def get_results(self):
|
|
|
|
| 212 |
"""Returns the dictionary of stored queries and their results."""
|
| 213 |
return self.results
|
| 214 |
|
| 215 |
|
| 216 |
def main_LogAnalyzer(queries):
|
|
|
|
| 217 |
log_file_path = 'sample.log'
|
| 218 |
csv_file_path = 'sample_log.csv'
|
| 219 |
qdrant_data_dir = 'qdrant_data'
|
|
@@ -237,9 +243,11 @@ def main_LogAnalyzer(queries):
|
|
| 237 |
|
| 238 |
class Health:
|
| 239 |
def __init__(self):
|
|
|
|
| 240 |
self.health = "Good"
|
| 241 |
|
| 242 |
def HealthInspector(self,results,last_query):
|
|
|
|
| 243 |
# Get the answer for the last query
|
| 244 |
last_answer = results.get(last_query, "") # Use .get() for safe access
|
| 245 |
last_line = last_answer.strip().split('\n')[-1]
|
|
@@ -247,13 +255,14 @@ class Health:
|
|
| 247 |
self.health = "Critical"
|
| 248 |
|
| 249 |
def render_markdown(md_text):
|
|
|
|
| 250 |
# Convert Markdown to HTML
|
| 251 |
html = markdown.markdown(md_text)
|
| 252 |
# Display the HTML
|
| 253 |
display(HTML(html))
|
| 254 |
|
| 255 |
def agent_LA(query):
|
| 256 |
-
|
| 257 |
# Perform a query
|
| 258 |
queries = [
|
| 259 |
"What are the most common errors in the logs?",
|
|
@@ -282,6 +291,7 @@ def agent_LA(query):
|
|
| 282 |
|
| 283 |
|
| 284 |
def greet(query):
|
|
|
|
| 285 |
return agent_LA(query)
|
| 286 |
|
| 287 |
demo = gr.Interface(fn=greet, inputs="text", outputs="text", title="Log Analyzer agent")
|
|
|
|
| 30 |
|
| 31 |
class LogAnalyzer:
|
| 32 |
def __init__(self, log_file_path, csv_file_path, qdrant_data_dir):
|
| 33 |
+
print("LogAnalyzer init")
|
| 34 |
self.log_file_path = log_file_path
|
| 35 |
self.csv_file_path = csv_file_path
|
| 36 |
self.qdrant_data_dir = qdrant_data_dir
|
|
|
|
| 39 |
self.client = None
|
| 40 |
self.open_router_client = None
|
| 41 |
self.results = {} # Dictionary to store queries and their results
|
| 42 |
+
|
| 43 |
|
| 44 |
def process_log_to_csv(self):
|
| 45 |
"""Reads a log file, converts it to CSV, and saves it."""
|
| 46 |
+
print("process_log_to_csv")
|
| 47 |
with open(self.log_file_path, 'r') as f:
|
| 48 |
log_content = f.read()
|
| 49 |
|
|
|
|
| 68 |
print(f"Log file converted to CSV: {self.csv_file_path}")
|
| 69 |
|
| 70 |
def create_log_embeddings(self):
|
| 71 |
+
print("create_log_embeddings")
|
| 72 |
"""Reads CSV, creates combined text, generates embeddings, and stores in instance."""
|
| 73 |
df = pd.read_csv(self.csv_file_path)
|
| 74 |
df = df.drop_duplicates()
|
|
|
|
| 90 |
self.df_combined["embedding"] = self.df_combined.combined.apply(lambda x: self.embedder.encode(x))
|
| 91 |
|
| 92 |
def initialize_qdrant(self):
|
| 93 |
+
print("initialize_qdrant")
|
| 94 |
"""Initializes Qdrant client and creates collection if it doesn't exist."""
|
| 95 |
#drive.mount('/content/drive')
|
| 96 |
os.makedirs(self.qdrant_data_dir, exist_ok=True)
|
|
|
|
| 111 |
|
| 112 |
|
| 113 |
def upload_embeddings_to_qdrant(self):
|
| 114 |
+
print("upload_embeddings_to_qdrant")
|
| 115 |
"""Uploads embeddings from dataframe to Qdrant collection."""
|
| 116 |
if self.client is not None and self.df_combined is not None:
|
| 117 |
self.client.upload_points(
|
|
|
|
| 134 |
print("Qdrant client or embeddings not initialized. Run initialize_qdrant and create_log_embeddings first.")
|
| 135 |
|
| 136 |
def search_qdrant(self, query, n=15):
|
| 137 |
+
print("search_qdrant")
|
| 138 |
"""Searches Qdrant for similar log entries based on query."""
|
| 139 |
if self.client is not None and self.embedder is not None:
|
| 140 |
query_embedding = self.embedder.encode(query).tolist()
|
|
|
|
| 159 |
return []
|
| 160 |
|
| 161 |
def setup_openrouter_client(self):
|
| 162 |
+
print("setup_openrouter_client")
|
| 163 |
"""Sets up and returns the OpenRouter client."""
|
| 164 |
OPEN_ROUTER_API_KEY = os.getenv('OPEN_ROUTER_API_KEY_2')
|
| 165 |
self.open_router_client = OpenAI(
|
|
|
|
| 168 |
)
|
| 169 |
|
| 170 |
def generate_answer(self, query):
|
| 171 |
+
print("generate_answer")
|
| 172 |
"""Generates an answer to the query using LLM based on Qdrant search results and stores the result."""
|
| 173 |
if self.open_router_client is not None and self.client is not None and self.embedder is not None:
|
| 174 |
context = f"""
|
|
|
|
| 212 |
print("Clients or embedder not initialized. Run setup_openrouter_client, initialize_qdrant, and create_log_embeddings first.")
|
| 213 |
return "Error: Log analysis components not fully initialized."
|
| 214 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
def get_results(self):
|
| 216 |
+
print("get_results")
|
| 217 |
"""Returns the dictionary of stored queries and their results."""
|
| 218 |
return self.results
|
| 219 |
|
| 220 |
|
| 221 |
def main_LogAnalyzer(queries):
|
| 222 |
+
print("main_LogAnalyzer")
|
| 223 |
log_file_path = 'sample.log'
|
| 224 |
csv_file_path = 'sample_log.csv'
|
| 225 |
qdrant_data_dir = 'qdrant_data'
|
|
|
|
| 243 |
|
| 244 |
class Health:
|
| 245 |
def __init__(self):
|
| 246 |
+
print("Health init")
|
| 247 |
self.health = "Good"
|
| 248 |
|
| 249 |
def HealthInspector(self,results,last_query):
|
| 250 |
+
print("HealthInspector")
|
| 251 |
# Get the answer for the last query
|
| 252 |
last_answer = results.get(last_query, "") # Use .get() for safe access
|
| 253 |
last_line = last_answer.strip().split('\n')[-1]
|
|
|
|
| 255 |
self.health = "Critical"
|
| 256 |
|
| 257 |
def render_markdown(md_text):
|
| 258 |
+
print("render_markdown")
|
| 259 |
# Convert Markdown to HTML
|
| 260 |
html = markdown.markdown(md_text)
|
| 261 |
# Display the HTML
|
| 262 |
display(HTML(html))
|
| 263 |
|
| 264 |
def agent_LA(query):
|
| 265 |
+
print("agent_LA")
|
| 266 |
# Perform a query
|
| 267 |
queries = [
|
| 268 |
"What are the most common errors in the logs?",
|
|
|
|
| 291 |
|
| 292 |
|
| 293 |
def greet(query):
|
| 294 |
+
print("greet")
|
| 295 |
return agent_LA(query)
|
| 296 |
|
| 297 |
demo = gr.Interface(fn=greet, inputs="text", outputs="text", title="Log Analyzer agent")
|