Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,124 +1,100 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import os
|
| 3 |
-
import datetime
|
| 4 |
-
import json
|
| 5 |
from pathlib import Path
|
| 6 |
-
from reportlab.lib.pagesizes import letter
|
| 7 |
-
from reportlab.pdfgen import canvas
|
| 8 |
import gradio as gr
|
| 9 |
|
| 10 |
-
# -----------------------
|
| 11 |
-
#
|
| 12 |
-
# -----------------------
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
# -----------------------
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
return
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
c.drawString(40,750,f"Incident Report - {filename}")
|
| 55 |
-
c.setFont("Helvetica",10)
|
| 56 |
-
c.drawString(40,730,f"Generated: {datetime.datetime.utcnow().isoformat()}")
|
| 57 |
-
y=700
|
| 58 |
-
c.drawString(40,y,"Summary:")
|
| 59 |
-
c.drawString(60,y-20,summary[:1000])
|
| 60 |
-
y-=60
|
| 61 |
-
c.drawString(40,y,"Issues:")
|
| 62 |
-
for it in issues:
|
| 63 |
-
y-=14
|
| 64 |
-
c.drawString(60,y,it[:120])
|
| 65 |
-
if y<80:
|
| 66 |
-
c.showPage()
|
| 67 |
-
y=740
|
| 68 |
-
y-=20
|
| 69 |
-
c.drawString(40,y,"Suggestions:")
|
| 70 |
-
for s in suggestions:
|
| 71 |
-
y-=14
|
| 72 |
-
c.drawString(60,y,f"- {s[:100]}")
|
| 73 |
-
if y<80:
|
| 74 |
-
c.showPage()
|
| 75 |
-
y=740
|
| 76 |
-
c.save()
|
| 77 |
-
return str(out)
|
| 78 |
-
|
| 79 |
-
# -----------------------
|
| 80 |
-
# GRADIO UI
|
| 81 |
-
# -----------------------
|
| 82 |
-
def refresh_files():
|
| 83 |
-
return [f.name for f in FILE_STORAGE.iterdir() if f.is_file()]
|
| 84 |
-
|
| 85 |
-
def upload_file(file_obj):
|
| 86 |
-
if file_obj is None:
|
| 87 |
-
return refresh_files()
|
| 88 |
-
filepath = FILE_STORAGE / file_obj.name
|
| 89 |
-
content = file_obj.read()
|
| 90 |
-
if isinstance(content, bytes):
|
| 91 |
-
content = content.decode("utf-8", errors="ignore")
|
| 92 |
-
filepath.write_text(content, encoding="utf-8")
|
| 93 |
-
return refresh_files()
|
| 94 |
-
|
| 95 |
-
def analyze_selected(filename):
|
| 96 |
if not filename:
|
| 97 |
-
return "
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
with gr.Blocks() as demo:
|
| 103 |
-
gr.Markdown("## AgentOps MCP - Log Analysis")
|
| 104 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
with gr.Column(scale=2):
|
| 106 |
-
upload = gr.File(label="Upload log file", file_types=[".txt"])
|
| 107 |
-
btn_upload = gr.Button("Upload File")
|
| 108 |
-
files_dropdown = gr.Dropdown(choices=refresh_files(), label="Files available")
|
| 109 |
-
btn_refresh = gr.Button("Refresh files")
|
| 110 |
-
btn_analyze = gr.Button("Analyze selected file")
|
| 111 |
-
with gr.Column(scale=3):
|
| 112 |
summary_box = gr.Textbox(label="Summary", lines=3)
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
from pathlib import Path
|
|
|
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
# ------------------------------------------------------
|
| 6 |
+
# STORAGE
|
| 7 |
+
# ------------------------------------------------------
|
| 8 |
+
STORAGE = Path("file_storage")
|
| 9 |
+
STORAGE.mkdir(exist_ok=True)
|
| 10 |
+
|
| 11 |
+
# ------------------------------------------------------
|
| 12 |
+
# PARSER
|
| 13 |
+
# ------------------------------------------------------
|
| 14 |
+
def parse_logs(text):
|
| 15 |
+
lines = text.splitlines()
|
| 16 |
+
|
| 17 |
+
errors = [l for l in lines if "ERROR" in l.upper() or "CRITICAL" in l.upper()]
|
| 18 |
+
warnings = [l for l in lines if "WARNING" in l.upper()]
|
| 19 |
+
|
| 20 |
+
summary = f"Lines: {len(lines)} | Errors: {len(errors)} | Warnings: {len(warnings)}"
|
| 21 |
+
|
| 22 |
+
return summary, "\n".join(errors), "\n".join(warnings)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# ------------------------------------------------------
|
| 26 |
+
# UPLOAD HANDLER
|
| 27 |
+
# ------------------------------------------------------
|
| 28 |
+
def upload_file(file):
|
| 29 |
+
if file is None:
|
| 30 |
+
return "No file uploaded.", []
|
| 31 |
+
|
| 32 |
+
text = file.read().decode("utf-8", errors="ignore")
|
| 33 |
+
|
| 34 |
+
# Save it
|
| 35 |
+
path = STORAGE / file.name
|
| 36 |
+
with open(path, "w", encoding="utf-8") as f:
|
| 37 |
+
f.write(text)
|
| 38 |
+
|
| 39 |
+
# Update dropdown list
|
| 40 |
+
file_list = [p.name for p in STORAGE.iterdir()]
|
| 41 |
+
|
| 42 |
+
return f"Uploaded: {file.name}", file_list
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# ------------------------------------------------------
|
| 46 |
+
# ANALYZE HANDLER
|
| 47 |
+
# ------------------------------------------------------
|
| 48 |
+
def analyze_file(filename):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
if not filename:
|
| 50 |
+
return "No file selected.", "", ""
|
| 51 |
+
|
| 52 |
+
path = STORAGE / filename
|
| 53 |
+
if not path.exists():
|
| 54 |
+
return "File not found.", "", ""
|
| 55 |
+
|
| 56 |
+
text = path.read_text(encoding="utf-8", errors="ignore")
|
| 57 |
+
|
| 58 |
+
summary, errors, warnings = parse_logs(text)
|
| 59 |
+
|
| 60 |
+
# ALWAYS return text; never empty components
|
| 61 |
+
return summary, errors if errors else "No errors found.", warnings if warnings else "No warnings found."
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# ------------------------------------------------------
|
| 65 |
+
# BUILD UI
|
| 66 |
+
# ------------------------------------------------------
|
| 67 |
+
with gr.Blocks(theme="soft") as demo:
|
| 68 |
+
|
| 69 |
+
gr.Markdown("## **AgentOps MCP — Log Analyzer (Stable Version)**")
|
| 70 |
|
|
|
|
|
|
|
| 71 |
with gr.Row():
|
| 72 |
+
with gr.Column(scale=1):
|
| 73 |
+
upload = gr.File(label="Upload TXT Log File")
|
| 74 |
+
upload_out = gr.Textbox(label="Upload Status")
|
| 75 |
+
|
| 76 |
+
dropdown = gr.Dropdown(choices=[], label="Select File to Analyze")
|
| 77 |
+
|
| 78 |
+
btn_refresh = gr.Button("Refresh File List")
|
| 79 |
+
btn_analyze = gr.Button("Analyze File")
|
| 80 |
+
|
| 81 |
with gr.Column(scale=2):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
summary_box = gr.Textbox(label="Summary", lines=3)
|
| 83 |
+
errors_box = gr.Textbox(label="Errors", lines=10)
|
| 84 |
+
warnings_box = gr.Textbox(label="Warnings", lines=10)
|
| 85 |
+
|
| 86 |
+
# ----- Events -----
|
| 87 |
+
upload.upload(fn=upload_file, inputs=upload, outputs=[upload_out, dropdown])
|
| 88 |
+
|
| 89 |
+
btn_refresh.click(
|
| 90 |
+
fn=lambda: [p.name for p in STORAGE.iterdir()],
|
| 91 |
+
outputs=dropdown
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
btn_analyze.click(
|
| 95 |
+
fn=analyze_file,
|
| 96 |
+
inputs=dropdown,
|
| 97 |
+
outputs=[summary_box, errors_box, warnings_box]
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|