File size: 3,044 Bytes
53906e9
2bd30ff
 
439aa56
2bd30ff
079382a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0a8df3
079382a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53906e9
079382a
 
c0a8df3
4dd45c9
079382a
 
 
 
 
 
 
 
 
4dd45c9
c0a8df3
079382a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

import os
from pathlib import Path
import gradio as gr

# ------------------------------------------------------
# STORAGE
# ------------------------------------------------------
STORAGE = Path("file_storage")
STORAGE.mkdir(exist_ok=True)

# ------------------------------------------------------
# PARSER
# ------------------------------------------------------
def parse_logs(text):
    lines = text.splitlines()

    errors = [l for l in lines if "ERROR" in l.upper() or "CRITICAL" in l.upper()]
    warnings = [l for l in lines if "WARNING" in l.upper()]

    summary = f"Lines: {len(lines)} | Errors: {len(errors)} | Warnings: {len(warnings)}"

    return summary, "\n".join(errors), "\n".join(warnings)


# ------------------------------------------------------
# UPLOAD HANDLER
# ------------------------------------------------------
def upload_file(file):
    if file is None:
        return "No file uploaded.", []

    text = file.read().decode("utf-8", errors="ignore")

    # Save it
    path = STORAGE / file.name
    with open(path, "w", encoding="utf-8") as f:
        f.write(text)

    # Update dropdown list
    file_list = [p.name for p in STORAGE.iterdir()]

    return f"Uploaded: {file.name}", file_list


# ------------------------------------------------------
# ANALYZE HANDLER
# ------------------------------------------------------
def analyze_file(filename):
    if not filename:
        return "No file selected.", "", ""

    path = STORAGE / filename
    if not path.exists():
        return "File not found.", "", ""

    text = path.read_text(encoding="utf-8", errors="ignore")

    summary, errors, warnings = parse_logs(text)

    # ALWAYS return text; never empty components
    return summary, errors if errors else "No errors found.", warnings if warnings else "No warnings found."


# ------------------------------------------------------
# BUILD UI
# ------------------------------------------------------
with gr.Blocks() as demo:

    gr.Markdown("## **AgentOps MCP — Log Analyzer (Stable Version)**")

    with gr.Row():
        with gr.Column(scale=1):
            upload = gr.File(label="Upload TXT Log File")
            upload_out = gr.Textbox(label="Upload Status")

            dropdown = gr.Dropdown(choices=[], label="Select File to Analyze")

            btn_refresh = gr.Button("Refresh File List")
            btn_analyze = gr.Button("Analyze File")

        with gr.Column(scale=2):
            summary_box = gr.Textbox(label="Summary", lines=3)
            errors_box = gr.Textbox(label="Errors", lines=10)
            warnings_box = gr.Textbox(label="Warnings", lines=10)

    # ----- Events -----
    upload.upload(fn=upload_file, inputs=upload, outputs=[upload_out, dropdown])

    btn_refresh.click(
        fn=lambda: [p.name for p in STORAGE.iterdir()],
        outputs=dropdown
    )

    btn_analyze.click(
        fn=analyze_file,
        inputs=dropdown,
        outputs=[summary_box, errors_box, warnings_box]
    )

demo.launch(server_name="0.0.0.0", server_port=7860)