MCP_AgentX / app.py
Pontonkid's picture
Update app.py
53906e9 verified
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)