SuryaT1 commited on
Commit
aeea6cb
·
verified ·
1 Parent(s): d86cc11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -129
app.py CHANGED
@@ -1,129 +1,129 @@
1
- import gradio as gr
2
- import re
3
- import cv2
4
- import pytesseract
5
- import numpy as np
6
- from pytesseract import Output
7
- from PIL import Image
8
-
9
- # Define keywords for highlighting
10
- KEYWORDS = ["fatal", "unreachable", "ssh", "publickey", "psrp", "5985", "python", "sudo", "8081",
11
- "umask", "storage", "ssl/tls", "powershell", "authenticate"]
12
-
13
- ERROR_TYPES = {
14
- "ssh": "PORT ISSUE",
15
- "publickey": "KEYS ISSUE",
16
- "psrp": "PORT 5985 ISSUE",
17
- "5985": "PORT 5985 ISSUE",
18
- "python": "PYTHON ERROR",
19
- "sudo": "SUDO ISSUE",
20
- "8081": "PORT 8081 ISSUE",
21
- "umask": "UMASK77 ISSUE",
22
- "storage": "STORAGE ISSUE",
23
- "ssl/tls": "SSL/TLS ISSUE",
24
- "powershell": "POWERSHELL ISSUE",
25
- "authenticate": "AUTHENTICATION ISSUE"
26
- }
27
-
28
- ERROR_DOCS = {
29
- "KEYS ISSUE": "https://github.com/suryaT1/Documents/blob/main/keys-issue.md",
30
- "PORT ISSUE": "https://github.com/suryaT1/Documents/blob/main/port-issue.md",
31
- "PORT 5985 ISSUE": "https://github.com/suryaT1/Documents/blob/main/port5985-doc.md",
32
- "PYTHON ERROR": "https://github.com/suryaT1/Documents/blob/main/python-issue.md",
33
- "SUDO ISSUE": "https://github.com/suryaT1/Documents/blob/main/sudoers-issue.md",
34
- "PORT 8081 ISSUE": "https://github.com/suryaT1/Documents/blob/main/port8081-issue.md",
35
- "UMASK77 ISSUE": "https://github.com/suryaT1/Documents/blob/main/umask-issue.md",
36
- "STORAGE ISSUE": "https://github.com/suryaT1/Documents/blob/main/storage-issue.md",
37
- "SSL/TLS ISSUE": "https://github.com/suryaT1/Documents/blob/main/ssl_tls-issue.md",
38
- "POWERSHELL ISSUE": "https://github.com/suryaT1/Documents/blob/main/powershell-issue.md",
39
- "AUTHENTICATION ISSUE": "https://github.com/authentication-doc",
40
- }
41
-
42
- DEVICE_NAME_PATTERN = r'\[([A-Za-z0-9@.\-]+)(?:\s*->\s*[A-Za-z0-9@.\-]+)?\]'
43
-
44
- # Function to highlight keywords in text
45
- def highlight_keywords(text):
46
- for keyword in KEYWORDS:
47
- text = re.sub(rf'\b{keyword}\b', f'<b style="color:red;">{keyword}</b>', text, flags=re.IGNORECASE)
48
- return text
49
-
50
- # Extract text from an image
51
- def extract_text_from_image(image_path):
52
- image = Image.open(image_path)
53
- image_cv = np.array(image)
54
- image_cv = cv2.cvtColor(image_cv, cv2.COLOR_RGB2BGR)
55
- extracted_text = pytesseract.image_to_string(image_cv)
56
- return extracted_text.strip()
57
-
58
- # Label and detect error types in text
59
- def analyze_text(text):
60
- labeled_entities = []
61
- detected_errors = set()
62
- device_name = "Unknown"
63
-
64
- # Check for keywords
65
- for keyword in KEYWORDS:
66
- if keyword in text.lower():
67
- labeled_entities.append((keyword, "KEYWORD"))
68
-
69
- # Detect error types
70
- for key, error_type in ERROR_TYPES.items():
71
- if key in text.lower():
72
- detected_errors.add(error_type)
73
-
74
- # Extract device name
75
- device_match = re.search(DEVICE_NAME_PATTERN, text)
76
- if device_match:
77
- device_name = device_match.group(1)
78
- labeled_entities.append((device_name, "DEVICE"))
79
-
80
- return labeled_entities, detected_errors, device_name
81
-
82
- # Process the input (image or text)
83
- def process_input(image, text):
84
- extracted_text = ""
85
-
86
- if image is not None:
87
- extracted_text = extract_text_from_image(image)
88
- elif text.strip():
89
- extracted_text = text.strip()
90
-
91
- if not extracted_text:
92
- return "No text detected!", "", ""
93
-
94
- labeled_entities, detected_errors, device_name = analyze_text(extracted_text)
95
-
96
- # Highlighted text output
97
- highlighted_text = highlight_keywords(extracted_text)
98
-
99
- # Issue detection and documentation links
100
- logs_info = [f"Device: {device_name}, Issue: {error}" for error in detected_errors]
101
- docs_links = [f'<a href="{ERROR_DOCS[error]}" target="_blank">{error} Documentation</a>' for error in detected_errors if error in ERROR_DOCS]
102
-
103
- logs_html = "<br>".join(logs_info)
104
- docs_html = "<br>".join(docs_links)
105
-
106
- return highlighted_text, logs_html, docs_html
107
-
108
- # Gradio Interface
109
- with gr.Blocks() as demo:
110
- gr.Markdown("# Ansible Log Annotator")
111
- gr.Markdown("Upload a log image or enter log text below to extract and analyze errors.")
112
-
113
- with gr.Row():
114
- image_input = gr.File(type="filepath", label="Upload Log Image (PNG, JPG)")
115
- text_input = gr.Textbox(label="Or Paste Log Text", lines=5)
116
-
117
- with gr.Row():
118
- analyze_button = gr.Button("Analyze")
119
-
120
- with gr.Row():
121
- output_highlighted = gr.HTML(label="Highlighted Log")
122
- with gr.Row():
123
- output_logs = gr.HTML(label="Detected Issues")
124
- with gr.Row():
125
- output_docs = gr.HTML(label="Documentation Links")
126
-
127
- analyze_button.click(process_input, inputs=[image_input, text_input], outputs=[output_highlighted, output_logs, output_docs])
128
-
129
- demo.launch()
 
1
+ import gradio as gr
2
+ import re
3
+ import cv2
4
+ import pytesseract
5
+ import numpy as np
6
+ from pytesseract import Output
7
+ from PIL import Image
8
+
9
+ # Define keywords for highlighting
10
+ KEYWORDS = ["fatal", "unreachable", "ssh", "publickey", "psrp", "5985", "python", "sudo", "8081",
11
+ "umask", "storage", "ssl/tls", "powershell", "authenticate"]
12
+
13
+ ERROR_TYPES = {
14
+ "ssh": "PORT ISSUE",
15
+ "publickey": "KEYS ISSUE",
16
+ "psrp": "PORT 5985 ISSUE",
17
+ "5985": "PORT 5985 ISSUE",
18
+ "python": "PYTHON ERROR",
19
+ "sudo": "SUDO ISSUE",
20
+ "8081": "PORT 8081 ISSUE",
21
+ "umask": "UMASK77 ISSUE",
22
+ "storage": "STORAGE ISSUE",
23
+ "ssl/tls": "SSL/TLS ISSUE",
24
+ "powershell": "POWERSHELL ISSUE",
25
+ "authenticate": "AUTHENTICATION ISSUE"
26
+ }
27
+
28
+ ERROR_DOCS = {
29
+ "KEYS ISSUE": "https://github.com/suryaT1/Documents/blob/main/keys-issue.md",
30
+ "PORT ISSUE": "https://github.com/suryaT1/Documents/blob/main/port-issue.md",
31
+ "PORT 5985 ISSUE": "https://github.com/suryaT1/Documents/blob/main/port5985-doc.md",
32
+ "PYTHON ERROR": "https://github.com/suryaT1/Documents/blob/main/python-issue.md",
33
+ "SUDO ISSUE": "https://github.com/suryaT1/Documents/blob/main/sudoers-issue.md",
34
+ "PORT 8081 ISSUE": "https://github.com/suryaT1/Documents/blob/main/port8081-issue.md",
35
+ "UMASK77 ISSUE": "https://github.com/suryaT1/Documents/blob/main/umask-issue.md",
36
+ "STORAGE ISSUE": "https://github.com/suryaT1/Documents/blob/main/storage-issue.md",
37
+ "SSL/TLS ISSUE": "https://github.com/suryaT1/Documents/blob/main/ssl_tls-issue.md",
38
+ "POWERSHELL ISSUE": "https://github.com/suryaT1/Documents/blob/main/powershell-issue.md",
39
+ "AUTHENTICATION ISSUE": "https://github.com/authentication-doc",
40
+ }
41
+
42
+ DEVICE_NAME_PATTERN = r'\[([A-Za-z0-9@.\-]+)(?:\s*->\s*[A-Za-z0-9@.\-]+)?\]'
43
+
44
+ # Function to highlight keywords in text
45
+ def highlight_keywords(text):
46
+ for keyword in KEYWORDS:
47
+ text = re.sub(rf'\b{keyword}\b', f'<b style="color:red;">{keyword}</b>', text, flags=re.IGNORECASE)
48
+ return text
49
+
50
+ # Extract text from an image
51
+ def extract_text_from_image(image_path):
52
+ image = Image.open(image_path)
53
+ image_cv = np.array(image)
54
+ image_cv = cv2.cvtColor(image_cv, cv2.COLOR_RGB2BGR)
55
+ extracted_text = pytesseract.image_to_string(image_cv)
56
+ return extracted_text.strip()
57
+
58
+ # Label and detect error types in text
59
+ def analyze_text(text):
60
+ labeled_entities = []
61
+ detected_errors = set()
62
+ device_name = "Unknown"
63
+
64
+ # Check for keywords
65
+ for keyword in KEYWORDS:
66
+ if keyword in text.lower():
67
+ labeled_entities.append((keyword, "KEYWORD"))
68
+
69
+ # Detect error types
70
+ for key, error_type in ERROR_TYPES.items():
71
+ if key in text.lower():
72
+ detected_errors.add(error_type)
73
+
74
+ # Extract device name
75
+ device_match = re.search(DEVICE_NAME_PATTERN, text)
76
+ if device_match:
77
+ device_name = device_match.group(1)
78
+ labeled_entities.append((device_name, "DEVICE"))
79
+
80
+ return labeled_entities, detected_errors, device_name
81
+
82
+ # Process the input (image or text)
83
+ def process_input(image, text):
84
+ extracted_text = ""
85
+
86
+ if image is not None:
87
+ extracted_text = extract_text_from_image(image)
88
+ elif text.strip():
89
+ extracted_text = text.strip()
90
+
91
+ if not extracted_text:
92
+ return "No text detected!", "", ""
93
+
94
+ labeled_entities, detected_errors, device_name = analyze_text(extracted_text)
95
+
96
+ # Highlighted text output
97
+ highlighted_text = highlight_keywords(extracted_text)
98
+
99
+ # Issue detection and documentation links
100
+ logs_info = [f"Device: {device_name}, Issue: {error}" for error in detected_errors]
101
+ docs_links = [f'<a href="{ERROR_DOCS[error]}" target="_blank">{error} Documentation</a>' for error in detected_errors if error in ERROR_DOCS]
102
+
103
+ logs_html = "<br>".join(logs_info)
104
+ docs_html = "<br>".join(docs_links)
105
+
106
+ return highlighted_text, logs_html, docs_html
107
+
108
+ # Gradio Interface
109
+ with gr.Blocks() as demo:
110
+ gr.Markdown("# Ansible Log Annotator")
111
+ gr.Markdown("Upload a log image or enter log text below to extract and analyze errors.")
112
+
113
+ with gr.Row():
114
+ image_input = gr.File(type="filepath", label="Upload Log Image (PNG, JPG)")
115
+ text_input = gr.Textbox(label="Or Paste Log Text", lines=5)
116
+
117
+ with gr.Row():
118
+ analyze_button = gr.Button("Analyze")
119
+
120
+ with gr.Row():
121
+ output_highlighted = gr.HTML(label="Highlighted Log")
122
+ with gr.Row():
123
+ output_logs = gr.HTML(label="Detected Issues")
124
+ with gr.Row():
125
+ output_docs = gr.HTML(label="Documentation Links")
126
+
127
+ analyze_button.click(process_input, inputs=[image_input, text_input], outputs=[output_highlighted, output_logs, output_docs])
128
+
129
+ demo.launch()