MohammedAH commited on
Commit
8953837
·
verified ·
1 Parent(s): 3714867

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +331 -26
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  from transformers import pipeline
3
  import re
4
 
5
- def anomalies_detector(logs: str) -> list[str]:
6
  """
7
  Detect anomalies in software logs using a Hugging Face transformer model.
8
  This function uses a specialized model trained to identify unusual patterns
@@ -12,44 +12,349 @@ def anomalies_detector(logs: str) -> list[str]:
12
  - Security-related events
13
  - Performance anomalies
14
  - Unexpected behavior patterns
15
-
16
  Args:
17
  logs (str): The input text containing log entries
18
-
19
  Returns:
20
- list[tuple[int, str]]: List of tuples containing (line_number, anomalous_text)
21
  """
22
- # Initialize the text classification pipeline with a proper classification model
23
- classifier = pipeline("text-classification",
24
- model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
25
-
 
 
 
 
26
 
27
  # Split logs into lines
28
- log_lines = logs.split('\n')
29
- anomalies = []
 
 
30
 
31
  # Process each line
 
 
 
 
32
  for line_num, line in enumerate(log_lines, 1):
33
- if not line.strip(): # Skip empty lines
34
- continue
 
35
 
36
- # Get classification result
37
- results = classifier(line)
38
-
39
-
40
- for log, res in zip(logs, results):
41
- anomalies.append(f"{log} => {res}")
42
- return anomalies
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
- # Create a standard Gradio interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  demo = gr.Interface(
46
  fn=anomalies_detector,
47
- inputs="textbox",
48
- outputs="text",
49
- title="Log Anomaly Detector",
50
- description="Enter log entries to detect anomalous patterns using BERT Model. The system will identify unusual patterns, errors, and potential issues in your logs."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  )
52
 
53
- # Launch both the Gradio web interface and the MCP server
54
  if __name__ == "__main__":
55
- demo.launch(mcp_server=True, share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from transformers import pipeline
3
  import re
4
 
5
+ def anomalies_detector(logs: str) -> str:
6
  """
7
  Detect anomalies in software logs using a Hugging Face transformer model.
8
  This function uses a specialized model trained to identify unusual patterns
 
12
  - Security-related events
13
  - Performance anomalies
14
  - Unexpected behavior patterns
15
+
16
  Args:
17
  logs (str): The input text containing log entries
18
+
19
  Returns:
20
+ str: Formatted analysis results with detected anomalies
21
  """
22
+ if not logs or not logs.strip():
23
+ return "⚠️ No log data provided. Please enter log entries to analyze."
24
+
25
+ # Initialize the text classification pipeline
26
+ classifier = pipeline(
27
+ "text-classification",
28
+ model="distilbert/distilbert-base-uncased-finetuned-sst-2-english"
29
+ )
30
 
31
  # Split logs into lines
32
+ log_lines = [line for line in logs.split('\n') if line.strip()]
33
+
34
+ if not log_lines:
35
+ return "⚠️ No valid log entries found."
36
 
37
  # Process each line
38
+ anomalies = []
39
+ total_lines = len(log_lines)
40
+ negative_count = 0
41
+
42
  for line_num, line in enumerate(log_lines, 1):
43
+ try:
44
+ # Get classification result
45
+ result = classifier(line[:512])[0] # Limit to 512 chars for model
46
 
47
+ # Consider "NEGATIVE" sentiment as potential anomaly
48
+ if result['label'] == 'NEGATIVE' and result['score'] > 0.7:
49
+ anomalies.append({
50
+ 'line': line_num,
51
+ 'text': line,
52
+ 'confidence': result['score']
53
+ })
54
+ negative_count += 1
55
+ except Exception as e:
56
+ continue
57
+
58
+ # Format output
59
+ output = f"📊 **Analysis Summary**\n"
60
+ output += f"{'='*60}\n\n"
61
+ output += f"📝 Total log entries analyzed: **{total_lines}**\n"
62
+ output += f"🔍 Potential anomalies detected: **{negative_count}**\n"
63
+ output += f"✅ Health rate: **{((total_lines - negative_count) / total_lines * 100):.1f}%**\n\n"
64
+
65
+ if anomalies:
66
+ output += f"⚠️ **Detected Anomalies:**\n"
67
+ output += f"{'-'*60}\n\n"
68
+ for idx, anomaly in enumerate(anomalies, 1):
69
+ output += f"**#{idx} | Line {anomaly['line']}** (Confidence: {anomaly['confidence']:.2%})\n"
70
+ output += f"```\n{anomaly['text']}\n```\n\n"
71
+ else:
72
+ output += "✨ **No significant anomalies detected!**\n"
73
+ output += "Your logs appear to be healthy.\n"
74
+
75
+ return output
76
+
77
+
78
+ # Custom CSS for dark purple and white theme
79
+ custom_css = """
80
+ :root {
81
+ --primary-purple: #6B46C1;
82
+ --dark-purple: #553C9A;
83
+ --light-purple: #9F7AEA;
84
+ --deep-purple: #3C2A5E;
85
+ --white: #FFFFFF;
86
+ --off-white: #F7FAFC;
87
+ --light-gray: #E2E8F0;
88
+ }
89
+
90
+ /* Main background */
91
+ .gradio-container {
92
+ background: linear-gradient(135deg, var(--deep-purple) 0%, var(--dark-purple) 50%, var(--primary-purple) 100%) !important;
93
+ font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif !important;
94
+ }
95
+
96
+ /* Header styling */
97
+ .contain {
98
+ background: rgba(255, 255, 255, 0.98) !important;
99
+ border-radius: 20px !important;
100
+ box-shadow: 0 20px 60px rgba(107, 70, 193, 0.3) !important;
101
+ padding: 2rem !important;
102
+ margin: 2rem auto !important;
103
+ max-width: 1400px !important;
104
+ }
105
+
106
+ /* Title styling */
107
+ h1 {
108
+ background: linear-gradient(135deg, var(--primary-purple), var(--light-purple)) !important;
109
+ -webkit-background-clip: text !important;
110
+ -webkit-text-fill-color: transparent !important;
111
+ background-clip: text !important;
112
+ font-weight: 800 !important;
113
+ font-size: 2.5rem !important;
114
+ margin-bottom: 0.5rem !important;
115
+ text-align: center !important;
116
+ }
117
+
118
+ /* Description text */
119
+ .prose p {
120
+ color: var(--dark-purple) !important;
121
+ font-size: 1.1rem !important;
122
+ text-align: center !important;
123
+ margin-bottom: 1.5rem !important;
124
+ }
125
+
126
+ /* Input/Output boxes */
127
+ .input-text, .output-text, textarea {
128
+ border: 2px solid var(--light-purple) !important;
129
+ border-radius: 12px !important;
130
+ background: var(--white) !important;
131
+ font-family: 'Fira Code', 'Courier New', monospace !important;
132
+ font-size: 0.95rem !important;
133
+ transition: all 0.3s ease !important;
134
+ }
135
+
136
+ .input-text:focus, textarea:focus {
137
+ border-color: var(--primary-purple) !important;
138
+ box-shadow: 0 0 0 3px rgba(107, 70, 193, 0.1) !important;
139
+ outline: none !important;
140
+ }
141
+
142
+ /* Labels */
143
+ label {
144
+ color: var(--dark-purple) !important;
145
+ font-weight: 600 !important;
146
+ font-size: 1rem !important;
147
+ margin-bottom: 0.5rem !important;
148
+ }
149
+
150
+ /* Submit button */
151
+ .submit-button, button[id$="-submit-button"] {
152
+ background: linear-gradient(135deg, var(--primary-purple), var(--dark-purple)) !important;
153
+ color: var(--white) !important;
154
+ border: none !important;
155
+ border-radius: 12px !important;
156
+ padding: 0.75rem 2rem !important;
157
+ font-weight: 600 !important;
158
+ font-size: 1rem !important;
159
+ cursor: pointer !important;
160
+ transition: all 0.3s ease !important;
161
+ box-shadow: 0 4px 15px rgba(107, 70, 193, 0.3) !important;
162
+ }
163
+
164
+ .submit-button:hover, button[id$="-submit-button"]:hover {
165
+ transform: translateY(-2px) !important;
166
+ box-shadow: 0 6px 20px rgba(107, 70, 193, 0.4) !important;
167
+ }
168
+
169
+ /* Clear button */
170
+ .clear-button, button.secondary {
171
+ background: var(--white) !important;
172
+ color: var(--dark-purple) !important;
173
+ border: 2px solid var(--light-purple) !important;
174
+ border-radius: 12px !important;
175
+ padding: 0.75rem 2rem !important;
176
+ font-weight: 600 !important;
177
+ transition: all 0.3s ease !important;
178
+ }
179
 
180
+ .clear-button:hover, button.secondary:hover {
181
+ background: var(--light-purple) !important;
182
+ color: var(--white) !important;
183
+ transform: translateY(-2px) !important;
184
+ }
185
+
186
+ /* Output markdown styling */
187
+ .markdown-text {
188
+ background: var(--off-white) !important;
189
+ padding: 1.5rem !important;
190
+ border-radius: 12px !important;
191
+ border-left: 4px solid var(--primary-purple) !important;
192
+ }
193
+
194
+ .markdown-text code {
195
+ background: var(--white) !important;
196
+ padding: 0.2rem 0.4rem !important;
197
+ border-radius: 4px !important;
198
+ color: var(--dark-purple) !important;
199
+ border: 1px solid var(--light-purple) !important;
200
+ }
201
+
202
+ .markdown-text pre {
203
+ background: var(--white) !important;
204
+ border: 2px solid var(--light-purple) !important;
205
+ border-radius: 8px !important;
206
+ padding: 1rem !important;
207
+ }
208
+
209
+ /* Footer */
210
+ footer {
211
+ background: transparent !important;
212
+ color: var(--white) !important;
213
+ text-align: center !important;
214
+ padding: 2rem !important;
215
+ }
216
+
217
+ footer a {
218
+ color: var(--white) !important;
219
+ text-decoration: underline !important;
220
+ }
221
+
222
+ /* Examples section */
223
+ .examples {
224
+ background: var(--off-white) !important;
225
+ border-radius: 12px !important;
226
+ padding: 1rem !important;
227
+ border: 2px solid var(--light-purple) !important;
228
+ }
229
+
230
+ /* Scrollbar styling */
231
+ ::-webkit-scrollbar {
232
+ width: 10px !important;
233
+ }
234
+
235
+ ::-webkit-scrollbar-track {
236
+ background: var(--light-gray) !important;
237
+ border-radius: 5px !important;
238
+ }
239
+
240
+ ::-webkit-scrollbar-thumb {
241
+ background: var(--primary-purple) !important;
242
+ border-radius: 5px !important;
243
+ }
244
+
245
+ ::-webkit-scrollbar-thumb:hover {
246
+ background: var(--dark-purple) !important;
247
+ }
248
+ """
249
+
250
+ # Example logs for demonstration
251
+ example_logs = [
252
+ """2024-01-29 10:15:23 INFO User logged in successfully
253
+ 2024-01-29 10:15:45 INFO Database connection established
254
+ 2024-01-29 10:16:02 ERROR Failed to connect to payment gateway
255
+ 2024-01-29 10:16:15 WARN Retry attempt 1 of 3
256
+ 2024-01-29 10:16:30 ERROR Connection timeout after 30 seconds
257
+ 2024-01-29 10:16:45 CRITICAL Payment service unavailable""",
258
+
259
+ """2024-01-29 14:22:10 INFO Application started successfully
260
+ 2024-01-29 14:22:15 INFO Loading configuration files
261
+ 2024-01-29 14:22:18 INFO Server listening on port 8080
262
+ 2024-01-29 14:22:20 INFO Ready to accept connections""",
263
+
264
+ """2024-01-29 16:30:45 INFO User authentication request
265
+ 2024-01-29 16:30:46 WARN Invalid credentials provided
266
+ 2024-01-29 16:30:50 WARN Login attempt failed for user: admin
267
+ 2024-01-29 16:30:55 ERROR Multiple failed login attempts detected
268
+ 2024-01-29 16:31:00 CRITICAL Potential security breach - IP blocked"""
269
+ ]
270
+
271
+ # Create the Gradio interface
272
  demo = gr.Interface(
273
  fn=anomalies_detector,
274
+ inputs=gr.Textbox(
275
+ label="📋 Log Entries",
276
+ placeholder="Paste your software logs here (mobile app, desktop, web server, etc.)\n\nExample:\n2024-01-29 10:15:23 INFO User logged in\n2024-01-29 10:16:02 ERROR Connection failed\n2024-01-29 10:16:15 WARN Retry attempt...",
277
+ lines=15,
278
+ max_lines=25
279
+ ),
280
+ outputs=gr.Markdown(
281
+ label="🔍 Analysis Results"
282
+ ),
283
+ title="🛡️ Sentinel Log Analyzer",
284
+ description="**Enterprise-grade anomaly detection for all your software logs.** Supports mobile apps, desktop applications, web servers, and more. Powered by advanced AI to identify errors, security events, and unusual patterns in real-time.",
285
+ examples=example_logs,
286
+ css=custom_css,
287
+ theme=gr.themes.Soft(
288
+ primary_hue="purple",
289
+ secondary_hue="purple",
290
+ ),
291
+ allow_flagging="never",
292
+ analytics_enabled=False
293
  )
294
 
295
+ # Launch the application
296
  if __name__ == "__main__":
297
+ demo.launch(
298
+ server_name="0.0.0.0",
299
+ server_port=7860,
300
+ share=True,
301
+ show_error=True
302
+ )
303
+
304
+
305
+
306
+ # import gradio as gr
307
+ # from transformers import pipeline
308
+ # import re
309
+
310
+ # def anomalies_detector(logs: str) -> list[str]:
311
+ # """
312
+ # Detect anomalies in software logs using a Hugging Face transformer model.
313
+ # This function uses a specialized model trained to identify unusual patterns
314
+ # in system logs, such as:
315
+ # - Error messages
316
+ # - Unusual system states
317
+ # - Security-related events
318
+ # - Performance anomalies
319
+ # - Unexpected behavior patterns
320
+
321
+ # Args:
322
+ # logs (str): The input text containing log entries
323
+
324
+ # Returns:
325
+ # list[tuple[int, str]]: List of tuples containing (line_number, anomalous_text)
326
+ # """
327
+ # # Initialize the text classification pipeline with a proper classification model
328
+ # classifier = pipeline("text-classification",
329
+ # model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
330
+
331
+
332
+ # # Split logs into lines
333
+ # log_lines = logs.split('\n')
334
+ # anomalies = []
335
+
336
+ # # Process each line
337
+ # for line_num, line in enumerate(log_lines, 1):
338
+ # if not line.strip(): # Skip empty lines
339
+ # continue
340
+
341
+ # # Get classification result
342
+ # results = classifier(line)
343
+
344
+
345
+ # for log, res in zip(logs, results):
346
+ # anomalies.append(f"{log} => {res}")
347
+ # return anomalies
348
+
349
+ # # Create a standard Gradio interface
350
+ # demo = gr.Interface(
351
+ # fn=anomalies_detector,
352
+ # inputs="textbox",
353
+ # outputs="text",
354
+ # title="Log Anomaly Detector",
355
+ # description="Enter log entries to detect anomalous patterns using BERT Model. The system will identify unusual patterns, errors, and potential issues in your logs."
356
+ # )
357
+
358
+ # # Launch both the Gradio web interface and the MCP server
359
+ # if __name__ == "__main__":
360
+ # demo.launch(mcp_server=True, share=True)