Pontonkid commited on
Commit
079382a
·
verified ·
1 Parent(s): c0a8df3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -115
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
- # CONFIG
12
- # -----------------------
13
- FILE_STORAGE = Path("file_storage")
14
- FILE_STORAGE.mkdir(exist_ok=True)
15
- REPORT_DIR = Path("reports")
16
- REPORT_DIR.mkdir(exist_ok=True)
17
-
18
- # -----------------------
19
- # LOG ANALYSIS FUNCTIONS
20
- # -----------------------
21
- def parse_log_text(txt):
22
- lines = txt.splitlines()
23
- errors = [l for l in lines if "ERROR" in l or "CRITICAL" in l or "FATAL" in l]
24
- warnings = [l for l in lines if "WARNING" in l or "WARN" in l]
25
- return {"errors": errors, "warnings": warnings, "total_lines": len(lines)}
26
-
27
- def analyze_log(filename):
28
- filepath = FILE_STORAGE / filename
29
- if not filepath.exists():
30
- return {"summary":"File not found","issues":[],"suggestions":[],"plan":""}
31
-
32
- text = filepath.read_text(encoding="utf-8", errors="ignore")
33
- parsed = parse_log_text(text)
34
-
35
- summary = f"Detected {len(parsed['errors'])} errors and {len(parsed['warnings'])} warnings in {parsed['total_lines']} lines."
36
- issues = parsed['errors'][:10] + parsed['warnings'][:10]
37
- suggestions = [
38
- "Investigate top errors",
39
- "Check system configuration",
40
- "Add proper exception handling"
41
- ]
42
- plan = "\n".join([
43
- "STEP 1: Review errors",
44
- "STEP 2: Apply suggested fixes",
45
- "STEP 3: Re-run analysis"
46
- ])
47
- return {"summary": summary, "issues": issues, "suggestions": suggestions, "plan": plan}
48
-
49
- def generate_report(filename, summary, issues, suggestions):
50
- now = datetime.datetime.utcnow().strftime("%Y%m%d-%H%M%S")
51
- out = REPORT_DIR / f"incident_{filename}_{now}.pdf"
52
- c = canvas.Canvas(str(out), pagesize=letter)
53
- c.setFont("Helvetica-Bold",16)
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 "", "", "", "", None
98
- out = analyze_log(filename)
99
- report_path = generate_report(filename, out["summary"], out["issues"], out["suggestions"])
100
- return (out["summary"], "\n".join(out["issues"]), "\n".join(out["suggestions"]), out["plan"], gr.File.update(value=report_path))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- issues_box = gr.Textbox(label="Detected Issues", lines=10)
114
- suggestions_box = gr.Textbox(label="Suggestions", lines=6)
115
- plan_box = gr.Textbox(label="Agent Plan", lines=6)
116
- report_download = gr.File(label="Download PDF Report")
117
-
118
- btn_refresh.click(fn=refresh_files, outputs=files_dropdown)
119
- btn_upload.click(fn=upload_file, inputs=upload, outputs=files_dropdown)
120
- btn_analyze.click(fn=analyze_selected, inputs=files_dropdown,
121
- outputs=[summary_box, issues_box, suggestions_box, plan_box, report_download])
122
-
123
- if __name__ == "__main__":
124
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
 
 
 
 
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)