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 |
+
import pytesseract
|
| 6 |
+
from PIL import Image, ImageDraw
|
| 7 |
+
|
| 8 |
+
# Define keywords for highlighting
|
| 9 |
+
KEYWORDS = ["fatal", "unreachable", "ssh", "publickey", "psrp", "5985", "python", "sudo", "8081",
|
| 10 |
+
"umask", "storage", "ssl/tls", "powershell", "authenticate", "hosts", "match", "playbook"]
|
| 11 |
+
|
| 12 |
+
ERROR_TYPES = {
|
| 13 |
+
"ssh": "PORT ISSUE",
|
| 14 |
+
"publickey": "KEYS ISSUE",
|
| 15 |
+
"psrp": "PORT 5985 ISSUE",
|
| 16 |
+
"5985": "PORT 5985 ISSUE",
|
| 17 |
+
"python": "PYTHON ERROR",
|
| 18 |
+
"sudo": "SUDO ISSUE",
|
| 19 |
+
"8081": "PORT 8081 ISSUE",
|
| 20 |
+
"umask": "UMASK77 ISSUE",
|
| 21 |
+
"storage": "STORAGE ISSUE",
|
| 22 |
+
"ssl/tls": "SSL/TLS ISSUE",
|
| 23 |
+
"powershell": "POWERSHELL ISSUE",
|
| 24 |
+
"authenticate": "AUTHENTICATION ISSUE",
|
| 25 |
+
"hosts not match": "DEVICE NOT FOUND",
|
| 26 |
+
"playbook not": "PLAYBOOK NOT FOUND"
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
ERROR_DOCS = {
|
| 30 |
+
"KEYS ISSUE": "https://github.com/suryaT1/Documents/blob/main/keys-issue.md",
|
| 31 |
+
"PORT ISSUE": "https://github.com/suryaT1/Documents/blob/main/port-issue.md",
|
| 32 |
+
"PORT 5985 ISSUE": "https://github.com/suryaT1/Documents/blob/main/port5985-doc.md",
|
| 33 |
+
"PYTHON ERROR": "https://github.com/suryaT1/Documents/blob/main/python-issue.md",
|
| 34 |
+
"SUDO ISSUE": "https://github.com/suryaT1/Documents/blob/main/sudoers-issue.md",
|
| 35 |
+
"PORT 8081 ISSUE": "https://github.com/suryaT1/Documents/blob/main/port8081-issue.md",
|
| 36 |
+
"UMASK77 ISSUE": "https://github.com/suryaT1/Documents/blob/main/umask-issue.md",
|
| 37 |
+
"STORAGE ISSUE": "https://github.com/suryaT1/Documents/blob/main/storage-issue.md",
|
| 38 |
+
"SSL/TLS ISSUE": "https://github.com/suryaT1/Documents/blob/main/ssl_tls-issue.md",
|
| 39 |
+
"POWERSHELL ISSUE": "https://github.com/suryaT1/Documents/blob/main/powershell-issue.md",
|
| 40 |
+
"AUTHENTICATION ISSUE": "https://github.com/authentication-doc",
|
| 41 |
+
"DEVICE NOT FOUND": "https://github.com/suryaT1/Documents/blob/main/device-not-found.md",
|
| 42 |
+
"PLAYBOOK NOT FOUND": "https://github.com/suryaT1/Documents/blob/main/playbook-not-found.md"
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
DEVICE_NAME_PATTERN = r'\[([A-Za-z0-9@.\-]+)(?:\s*->\s*[A-Za-z0-9@.\-]+)?\]'
|
| 46 |
+
|
| 47 |
+
# Highlight errors in text
|
| 48 |
+
def highlight_keywords(text):
|
| 49 |
+
for keyword in KEYWORDS:
|
| 50 |
+
text = re.sub(rf'\b{keyword}\b', f'<b style="color:red;">{keyword}</b>', text, flags=re.IGNORECASE)
|
| 51 |
+
return text
|
| 52 |
+
|
| 53 |
+
# Highlight errors directly on the image
|
| 54 |
+
def highlight_errors_on_image(image_path):
|
| 55 |
+
image = Image.open(image_path)
|
| 56 |
+
image_cv = np.array(image)
|
| 57 |
+
image_cv = cv2.cvtColor(image_cv, cv2.COLOR_RGB2BGR)
|
| 58 |
+
|
| 59 |
+
data = pytesseract.image_to_data(image_cv, output_type=pytesseract.Output.DICT)
|
| 60 |
+
|
| 61 |
+
draw = ImageDraw.Draw(image)
|
| 62 |
+
for i, word in enumerate(data['text']):
|
| 63 |
+
if any(keyword.lower() in word.lower() for keyword in KEYWORDS):
|
| 64 |
+
x, y, w, h = data['left'][i], data['top'][i], data['width'][i], data['height'][i]
|
| 65 |
+
draw.rectangle([x, y, x + w, y + h], outline="red", width=3)
|
| 66 |
+
draw.text((x, y - 15), word, fill="red")
|
| 67 |
+
|
| 68 |
+
highlighted_image_path = "highlighted_image.png"
|
| 69 |
+
image.save(highlighted_image_path)
|
| 70 |
+
return highlighted_image_path, image
|
| 71 |
+
|
| 72 |
+
# Analyze text input
|
| 73 |
+
def analyze_text(text):
|
| 74 |
+
detected_errors = {}
|
| 75 |
+
devices = {}
|
| 76 |
+
|
| 77 |
+
device_matches = re.finditer(DEVICE_NAME_PATTERN, text)
|
| 78 |
+
for match in device_matches:
|
| 79 |
+
device = match.group(1)
|
| 80 |
+
device_text = text[match.end():].split("fatal:", 1)[0].lower()
|
| 81 |
+
found_ssh = "ssh" in device_text
|
| 82 |
+
found_publickey = "publickey" in device_text
|
| 83 |
+
|
| 84 |
+
if found_ssh and found_publickey:
|
| 85 |
+
detected_errors[device] = "KEYS ISSUE"
|
| 86 |
+
elif found_ssh:
|
| 87 |
+
detected_errors[device] = "PORT ISSUE"
|
| 88 |
+
elif "hosts" in device_text and "not" in device_text and "match" in device_text:
|
| 89 |
+
detected_errors[device] = "DEVICE NOT FOUND"
|
| 90 |
+
elif "playbook" in device_text and "not" in device_text:
|
| 91 |
+
detected_errors[device] = "PLAYBOOK NOT FOUND"
|
| 92 |
+
else:
|
| 93 |
+
for key, error_type in ERROR_TYPES.items():
|
| 94 |
+
if key in device_text:
|
| 95 |
+
detected_errors[device] = error_type
|
| 96 |
+
|
| 97 |
+
return detected_errors
|
| 98 |
+
|
| 99 |
+
# Process input
|
| 100 |
+
def process_input(image, text):
|
| 101 |
+
if image is not None:
|
| 102 |
+
highlighted_image_path, highlighted_image = highlight_errors_on_image(image)
|
| 103 |
+
extracted_text = pytesseract.image_to_string(Image.open(image))
|
| 104 |
+
highlighted_text = ""
|
| 105 |
+
elif text.strip():
|
| 106 |
+
highlighted_image = ""
|
| 107 |
+
extracted_text = text.strip()
|
| 108 |
+
highlighted_text = highlight_keywords(extracted_text)
|
| 109 |
+
else:
|
| 110 |
+
return "", "No text detected!", ""
|
| 111 |
+
|
| 112 |
+
detected_errors = analyze_text(extracted_text)
|
| 113 |
+
|
| 114 |
+
logs_info = "<br>".join(f"Device: {device}, Issue: {error}" for device, error in detected_errors.items()) if detected_errors else "No errors detected"
|
| 115 |
+
docs_links = "<br>".join(f'<a href="{ERROR_DOCS[error]}" target="_blank">{error} Documentation</a>'
|
| 116 |
+
for error in detected_errors.values() if error in ERROR_DOCS)
|
| 117 |
+
|
| 118 |
+
return highlighted_image if highlighted_image else highlighted_text, logs_info, docs_links
|
| 119 |
+
|
| 120 |
+
# Show text input or image upload
|
| 121 |
+
def show_input(choice):
|
| 122 |
+
return (gr.Textbox(visible=choice == "Text Box"),
|
| 123 |
+
gr.File(visible=choice == "Image Upload"))
|
| 124 |
+
|
| 125 |
+
# Gradio Interface
|
| 126 |
+
with gr.Blocks() as demo:
|
| 127 |
+
gr.Markdown("# Ansible Log Annotator")
|
| 128 |
+
gr.Markdown("Select input type and analyze the logs.")
|
| 129 |
+
|
| 130 |
+
choice = gr.Radio(["Text Box", "Image Upload"], label="Select Input Type")
|
| 131 |
+
text_input = gr.Textbox(label="Enter Log Text", lines=5, visible=False)
|
| 132 |
+
image_input = gr.File(type="filepath", label="Upload Log Image", visible=False)
|
| 133 |
+
analyze_button = gr.Button("Analyze", visible=False)
|
| 134 |
+
output_display = gr.Image(label="Highlighted Output", visible=False)
|
| 135 |
+
output_logs = gr.HTML(label="Detected Issues", visible=False)
|
| 136 |
+
output_docs = gr.HTML(label="Documentation Links", visible=False)
|
| 137 |
+
|
| 138 |
+
choice.change(show_input, inputs=choice, outputs=[text_input, image_input])
|
| 139 |
+
choice.change(lambda: gr.Button(visible=True), outputs=[analyze_button])
|
| 140 |
+
|
| 141 |
+
analyze_button.click(process_input,
|
| 142 |
+
inputs=[image_input, text_input],
|
| 143 |
+
outputs=[output_display, output_logs, output_docs])
|
| 144 |
+
|
| 145 |
+
output_display.visible = True
|
| 146 |
+
output_logs.visible = True
|
| 147 |
+
output_docs.visible = True
|
| 148 |
+
|
| 149 |
+
demo.launch()
|