Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
172 lines (139 loc) Β· 5.73 KB
/
Copy pathapp.py
File metadata and controls
172 lines (139 loc) Β· 5.73 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import streamlit as st
import pandas as pd
import plotly.express as px
import joblib
import numpy as np
import os
import json
# --- PAGE CONFIG ---
st.set_page_config(
page_title="LogAI Diagnostics (XGBoost)",
layout="wide",
page_icon="π‘οΈ",
initial_sidebar_state="expanded"
)
# --- CUSTOM CSS ---
st.markdown("""
<style>
.stApp { background-color: #0e1117; color: #ffffff; }
.main-header { font-size: 2.5rem; color: #60a5fa; font-weight: 700; }
section[data-testid="stSidebar"] { background-color: #262730; }
</style>
""", unsafe_allow_html=True)
# --- LOAD MODELS ---
@st.cache_resource
def load_models():
vectorizer = joblib.load("tfidf-xgboost/model_v3/tfidf_vectorizer.joblib")
source_model = joblib.load("tfidf-xgboost/model_v3/xgb_source_model.joblib")
content_model = joblib.load("tfidf-xgboost/model_v3/xgb_content_model.joblib")
with open("tfidf-xgboost/model_v3/xgb_label_maps.json") as f:
label_maps = joblib.load(f) if f.name.endswith(".joblib") else json.load(f)
return vectorizer, source_model, content_model, label_maps
# --- SIDEBAR ---
with st.sidebar:
st.title("π‘οΈ LogAI Controls")
st.info("Model: TF-IDF + XGBoost")
required_files = [
"tfidf-xgboost/model_v3/tfidf_vectorizer.joblib",
"tfidf-xgboost/model_v3/xgb_source_model.joblib",
"tfidf-xgboost/model_v3/xgb_content_model.joblib",
"tfidf-xgboost/model_v3/xgb_label_maps.json"
]
missing = [f for f in required_files if not os.path.exists(f)]
if missing:
st.error(f"β Missing files: {missing}")
st.stop()
else:
st.success("β
Models Loaded")
st.markdown("---")
conf_threshold = st.slider("Confidence Threshold", 0.0, 1.0, 0.5)
# --- MAIN ---
st.markdown('<div class="main-header">π‘οΈ LogAI Diagnostics</div>', unsafe_allow_html=True)
st.markdown("### Intelligent Log Classification (XGBoost)")
vectorizer, src_model, cnt_model, label_maps = load_models()
# --- FILE UPLOAD ---
uploaded_file = st.file_uploader(
"π Upload Log File (CSV / TXT / LOG / JSON)",
type=["csv", "txt", "log", "json"]
)
if uploaded_file:
# --- READ FILE ---
try:
if uploaded_file.name.endswith(".csv"):
df = pd.read_csv(uploaded_file)
elif uploaded_file.name.endswith(".json"):
df = pd.read_json(uploaded_file)
else:
lines = uploaded_file.read().decode("utf-8").splitlines()
df = pd.DataFrame(lines, columns=["content"])
df.columns = [c.lower() for c in df.columns]
if "content" not in df.columns:
df.rename(columns={df.columns[-1]: "content"}, inplace=True)
except Exception as e:
st.error(f"File read error: {e}")
st.stop()
st.write(f"**Loaded {len(df):,} logs**")
st.dataframe(df.head(5), width='content')
# --- RUN INFERENCE ---
if st.button("π Run AI Analysis", type="primary"):
with st.spinner("Running SVM inference..."):
X = vectorizer.transform(df["content"].astype(str))
# --- SOURCE PREDICTION ---
if hasattr(src_model, "predict_proba"):
src_probs = src_model.predict_proba(X)
else:
df_scores = src_model.decision_function(X)
src_probs = np.exp(df_scores) / np.exp(df_scores).sum(axis=1, keepdims=True)
# --- CONTENT PREDICTION ---
if hasattr(cnt_model, "predict_proba"):
cnt_probs = cnt_model.predict_proba(X)
else:
df_scores = cnt_model.decision_function(X)
cnt_probs = np.exp(df_scores) / np.exp(df_scores).sum(axis=1, keepdims=True)
src_idx = np.argmax(src_probs, axis=1)
cnt_idx = np.argmax(cnt_probs, axis=1)
src_conf = np.max(src_probs, axis=1)
cnt_conf = np.max(cnt_probs, axis=1)
df["Source_Type"] = [
label_maps["source"][str(i)] if c >= conf_threshold else "LowConfidence"
for i, c in zip(src_idx, src_conf)
]
df["Content_Type"] = [
label_maps["content"][str(i)] if c >= conf_threshold else "LowConfidence"
for i, c in zip(cnt_idx, cnt_conf)
]
df["Source_Confidence"] = src_conf.round(3)
df["Content_Confidence"] = cnt_conf.round(3)
st.success("β
Analysis Complete")
# --- METRICS ---
m1, m2, m3 = st.columns(3)
m1.metric("Total Logs", len(df))
m2.metric("Source Types", df["Source_Type"].nunique())
m3.metric("Content Types", df["Content_Type"].nunique())
# --- VISUALS ---
c1, c2 = st.columns(2)
with c1:
st.subheader("π Source Distribution")
src_counts = df["Source_Type"].value_counts().reset_index()
src_counts.columns = ["Source", "Count"]
fig1 = px.pie(src_counts, values="Count", names="Source", hole=0.4)
fig1.update_layout(template="plotly_dark")
st.plotly_chart(fig1, width='content')
with c2:
st.subheader("π Content Distribution")
cnt_counts = df["Content_Type"].value_counts().reset_index()
cnt_counts.columns = ["Content", "Count"]
fig2 = px.bar(cnt_counts, x="Count", y="Content", orientation="h")
fig2.update_layout(template="plotly_dark")
st.plotly_chart(fig2, width='content')
# --- TABLE ---
st.subheader("π Classified Logs")
st.dataframe(df, width='content')
# --- DOWNLOAD ---
st.download_button(
"π₯ Download Results",
df.to_csv(index=False).encode("utf-8"),
"logai_results.csv",
"text/csv",
type="primary"
)