Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,7 +30,6 @@ import gradio as gr
|
|
| 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
|
|
@@ -42,7 +41,6 @@ class LogAnalyzer:
|
|
| 42 |
|
| 43 |
|
| 44 |
def process_log_to_csv(self):
|
| 45 |
-
print("process_log_to_csv")
|
| 46 |
with open(self.log_file_path, 'r') as f:
|
| 47 |
log_content = f.read()
|
| 48 |
|
|
@@ -67,7 +65,6 @@ class LogAnalyzer:
|
|
| 67 |
print(f"Log file converted to CSV: {self.csv_file_path}")
|
| 68 |
|
| 69 |
def create_log_embeddings(self):
|
| 70 |
-
print("create_log_embeddings")
|
| 71 |
df = pd.read_csv(self.csv_file_path)
|
| 72 |
df = df.drop_duplicates()
|
| 73 |
|
|
@@ -88,8 +85,6 @@ class LogAnalyzer:
|
|
| 88 |
self.df_combined["embedding"] = self.df_combined.combined.apply(lambda x: self.embedder.encode(x))
|
| 89 |
|
| 90 |
def initialize_qdrant(self):
|
| 91 |
-
print("initialize_qdrant")
|
| 92 |
-
#drive.mount('/content/drive')
|
| 93 |
os.makedirs(self.qdrant_data_dir, exist_ok=True)
|
| 94 |
self.client = QdrantClient(path=self.qdrant_data_dir)
|
| 95 |
|
|
@@ -108,7 +103,6 @@ class LogAnalyzer:
|
|
| 108 |
|
| 109 |
|
| 110 |
def upload_embeddings_to_qdrant(self):
|
| 111 |
-
print("upload_embeddings_to_qdrant")
|
| 112 |
if self.client is not None and self.df_combined is not None:
|
| 113 |
self.client.upload_points(
|
| 114 |
collection_name="log_for_analysis",
|
|
@@ -130,7 +124,6 @@ class LogAnalyzer:
|
|
| 130 |
print("Qdrant client or embeddings not initialized. Run initialize_qdrant and create_log_embeddings first.")
|
| 131 |
|
| 132 |
def search_qdrant(self, query, n=15):
|
| 133 |
-
print("search_qdrant")
|
| 134 |
if self.client is not None and self.embedder is not None:
|
| 135 |
query_embedding = self.embedder.encode(query).tolist()
|
| 136 |
|
|
@@ -154,7 +147,6 @@ class LogAnalyzer:
|
|
| 154 |
return []
|
| 155 |
|
| 156 |
def setup_openrouter_client(self):
|
| 157 |
-
print("setup_openrouter_client")
|
| 158 |
OPEN_ROUTER_API_KEY = os.getenv('OPEN_ROUTER_API_KEY_2')
|
| 159 |
self.open_router_client = OpenAI(
|
| 160 |
base_url="https://openrouter.ai/api/v1",
|
|
@@ -162,7 +154,6 @@ class LogAnalyzer:
|
|
| 162 |
)
|
| 163 |
|
| 164 |
def generate_answer(self, query):
|
| 165 |
-
print("generate_answer")
|
| 166 |
if self.open_router_client is not None and self.client is not None and self.embedder is not None:
|
| 167 |
context = f"""
|
| 168 |
LOG ANALYSIS RESULTS:
|
|
@@ -206,13 +197,10 @@ health of the system in one word - good or critical in the same line.
|
|
| 206 |
return "Error: Log analysis components not fully initialized."
|
| 207 |
|
| 208 |
def get_results(self):
|
| 209 |
-
print("get_results")
|
| 210 |
-
"""Returns the dictionary of stored queries and their results."""
|
| 211 |
return self.results
|
| 212 |
|
| 213 |
|
| 214 |
def main_LogAnalyzer(queries):
|
| 215 |
-
print("main_LogAnalyzer")
|
| 216 |
log_file_path = 'sample.log'
|
| 217 |
csv_file_path = 'sample_log.csv'
|
| 218 |
qdrant_data_dir = 'qdrant_data'
|
|
@@ -236,11 +224,9 @@ def main_LogAnalyzer(queries):
|
|
| 236 |
|
| 237 |
class Health:
|
| 238 |
def __init__(self):
|
| 239 |
-
print("Health init")
|
| 240 |
self.health = "Good"
|
| 241 |
|
| 242 |
def HealthInspector(self,results,last_query):
|
| 243 |
-
print("HealthInspector")
|
| 244 |
# Get the answer for the last query
|
| 245 |
last_answer = results.get(last_query, "") # Use .get() for safe access
|
| 246 |
last_line = last_answer.strip().split('\n')[-1]
|
|
@@ -248,14 +234,12 @@ class Health:
|
|
| 248 |
self.health = "Critical"
|
| 249 |
|
| 250 |
def render_markdown(md_text):
|
| 251 |
-
print("render_markdown")
|
| 252 |
# Convert Markdown to HTML
|
| 253 |
html = markdown.markdown(md_text)
|
| 254 |
# Display the HTML
|
| 255 |
display(HTML(html))
|
| 256 |
|
| 257 |
def agent_LA(query):
|
| 258 |
-
print("agent_LA")
|
| 259 |
# Perform a query
|
| 260 |
queries = [
|
| 261 |
"What are the most common errors in the logs?",
|
|
@@ -273,9 +257,6 @@ def agent_LA(query):
|
|
| 273 |
markdown_out += f"## Query: {query}\n\n"
|
| 274 |
markdown_out += f"{answer}\n\n---\n\n" # Add separator between results
|
| 275 |
|
| 276 |
-
#render_markdown(markdown_output) # Pass the formatted string to render_markdown
|
| 277 |
-
#return markdown_out
|
| 278 |
-
|
| 279 |
HP = Health()
|
| 280 |
HP.HealthInspector(results,queries[-1])
|
| 281 |
health_result = ""
|
|
@@ -288,9 +269,7 @@ def agent_LA(query):
|
|
| 288 |
return final_result
|
| 289 |
|
| 290 |
|
| 291 |
-
|
| 292 |
def greet(query):
|
| 293 |
-
print("greet")
|
| 294 |
return agent_LA(query)
|
| 295 |
|
| 296 |
markdown_output = gr.Markdown()
|
|
|
|
| 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
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
def process_log_to_csv(self):
|
|
|
|
| 44 |
with open(self.log_file_path, 'r') as f:
|
| 45 |
log_content = f.read()
|
| 46 |
|
|
|
|
| 65 |
print(f"Log file converted to CSV: {self.csv_file_path}")
|
| 66 |
|
| 67 |
def create_log_embeddings(self):
|
|
|
|
| 68 |
df = pd.read_csv(self.csv_file_path)
|
| 69 |
df = df.drop_duplicates()
|
| 70 |
|
|
|
|
| 85 |
self.df_combined["embedding"] = self.df_combined.combined.apply(lambda x: self.embedder.encode(x))
|
| 86 |
|
| 87 |
def initialize_qdrant(self):
|
|
|
|
|
|
|
| 88 |
os.makedirs(self.qdrant_data_dir, exist_ok=True)
|
| 89 |
self.client = QdrantClient(path=self.qdrant_data_dir)
|
| 90 |
|
|
|
|
| 103 |
|
| 104 |
|
| 105 |
def upload_embeddings_to_qdrant(self):
|
|
|
|
| 106 |
if self.client is not None and self.df_combined is not None:
|
| 107 |
self.client.upload_points(
|
| 108 |
collection_name="log_for_analysis",
|
|
|
|
| 124 |
print("Qdrant client or embeddings not initialized. Run initialize_qdrant and create_log_embeddings first.")
|
| 125 |
|
| 126 |
def search_qdrant(self, query, n=15):
|
|
|
|
| 127 |
if self.client is not None and self.embedder is not None:
|
| 128 |
query_embedding = self.embedder.encode(query).tolist()
|
| 129 |
|
|
|
|
| 147 |
return []
|
| 148 |
|
| 149 |
def setup_openrouter_client(self):
|
|
|
|
| 150 |
OPEN_ROUTER_API_KEY = os.getenv('OPEN_ROUTER_API_KEY_2')
|
| 151 |
self.open_router_client = OpenAI(
|
| 152 |
base_url="https://openrouter.ai/api/v1",
|
|
|
|
| 154 |
)
|
| 155 |
|
| 156 |
def generate_answer(self, query):
|
|
|
|
| 157 |
if self.open_router_client is not None and self.client is not None and self.embedder is not None:
|
| 158 |
context = f"""
|
| 159 |
LOG ANALYSIS RESULTS:
|
|
|
|
| 197 |
return "Error: Log analysis components not fully initialized."
|
| 198 |
|
| 199 |
def get_results(self):
|
|
|
|
|
|
|
| 200 |
return self.results
|
| 201 |
|
| 202 |
|
| 203 |
def main_LogAnalyzer(queries):
|
|
|
|
| 204 |
log_file_path = 'sample.log'
|
| 205 |
csv_file_path = 'sample_log.csv'
|
| 206 |
qdrant_data_dir = 'qdrant_data'
|
|
|
|
| 224 |
|
| 225 |
class Health:
|
| 226 |
def __init__(self):
|
|
|
|
| 227 |
self.health = "Good"
|
| 228 |
|
| 229 |
def HealthInspector(self,results,last_query):
|
|
|
|
| 230 |
# Get the answer for the last query
|
| 231 |
last_answer = results.get(last_query, "") # Use .get() for safe access
|
| 232 |
last_line = last_answer.strip().split('\n')[-1]
|
|
|
|
| 234 |
self.health = "Critical"
|
| 235 |
|
| 236 |
def render_markdown(md_text):
|
|
|
|
| 237 |
# Convert Markdown to HTML
|
| 238 |
html = markdown.markdown(md_text)
|
| 239 |
# Display the HTML
|
| 240 |
display(HTML(html))
|
| 241 |
|
| 242 |
def agent_LA(query):
|
|
|
|
| 243 |
# Perform a query
|
| 244 |
queries = [
|
| 245 |
"What are the most common errors in the logs?",
|
|
|
|
| 257 |
markdown_out += f"## Query: {query}\n\n"
|
| 258 |
markdown_out += f"{answer}\n\n---\n\n" # Add separator between results
|
| 259 |
|
|
|
|
|
|
|
|
|
|
| 260 |
HP = Health()
|
| 261 |
HP.HealthInspector(results,queries[-1])
|
| 262 |
health_result = ""
|
|
|
|
| 269 |
return final_result
|
| 270 |
|
| 271 |
|
|
|
|
| 272 |
def greet(query):
|
|
|
|
| 273 |
return agent_LA(query)
|
| 274 |
|
| 275 |
markdown_output = gr.Markdown()
|