pranit144 commited on
Commit
e29e711
Β·
verified Β·
1 Parent(s): c918d7e

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +332 -0
  2. error_classifier.pkl +3 -0
app.py ADDED
@@ -0,0 +1,332 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import joblib
4
+ import matplotlib.pyplot as plt
5
+ import numpy as np
6
+
7
+ # Page configuration
8
+ st.set_page_config(
9
+ page_title="API Error Predictor",
10
+ page_icon="⚠️",
11
+ layout="wide",
12
+ initial_sidebar_state="expanded"
13
+ )
14
+
15
+ # Custom CSS for better styling
16
+ st.markdown("""
17
+ <style>
18
+ .main-header {
19
+ font-size: 2.5rem;
20
+ font-weight: 700;
21
+ margin-bottom: 1rem;
22
+ }
23
+ .sub-header {
24
+ font-size: 1.5rem;
25
+ font-weight: 600;
26
+ margin-top: 1rem;
27
+ }
28
+ .info-box {
29
+ background-color: #f8f9fa;
30
+ padding: 1rem;
31
+ border-radius: 0.5rem;
32
+ margin-bottom: 1rem;
33
+ }
34
+ .prediction-box-success {
35
+ background-color: #d4edda;
36
+ color: #155724;
37
+ padding: 1rem;
38
+ border-radius: 0.5rem;
39
+ margin-bottom: 1rem;
40
+ text-align: center;
41
+ }
42
+ .prediction-box-error {
43
+ background-color: #f8d7da;
44
+ color: #721c24;
45
+ padding: 1rem;
46
+ border-radius: 0.5rem;
47
+ margin-bottom: 1rem;
48
+ text-align: center;
49
+ }
50
+ .sidebar-header {
51
+ font-size: 1.2rem;
52
+ font-weight: 600;
53
+ margin-bottom: 0.5rem;
54
+ }
55
+ .metric-container {
56
+ background-color: #e9ecef;
57
+ padding: 1rem;
58
+ border-radius: 0.5rem;
59
+ margin-bottom: 1rem;
60
+ }
61
+ </style>
62
+ """, unsafe_allow_html=True)
63
+
64
+
65
+ # Load trained model
66
+ @st.cache_resource
67
+ def load_model():
68
+ return joblib.load("error_classifier.pkl")
69
+
70
+
71
+ model = load_model()
72
+
73
+ # Header section
74
+ col1, col2 = st.columns([5, 1])
75
+ with col1:
76
+ st.markdown('<div class="main-header">⚠️ API Error Prediction System</div>', unsafe_allow_html=True)
77
+ st.markdown("""
78
+ <div class="info-box">
79
+ This tool predicts whether an API call will result in an error based on request metrics and parameters.
80
+ Use the sidebar to adjust input parameters and see real-time predictions.
81
+ </div>
82
+ """, unsafe_allow_html=True)
83
+
84
+ with col2:
85
+ st.image("https://via.placeholder.com/150", width=100) # Replace with your logo if available
86
+
87
+ # Sidebar for input parameters
88
+ st.sidebar.markdown('<div class="sidebar-header">πŸ”§ Input Parameters</div>', unsafe_allow_html=True)
89
+
90
+ # Group related parameters
91
+ with st.sidebar.expander("API Configuration", expanded=True):
92
+ # API ID dropdown with colored icons
93
+ api_options = {
94
+ "OrderProcessor": "πŸ›’",
95
+ "AuthService": "πŸ”",
96
+ "ProductCatalog": "πŸ“š",
97
+ "PaymentGateway": "πŸ’³"
98
+ }
99
+ api_id = st.selectbox(
100
+ "API Service",
101
+ options=list(api_options.keys()),
102
+ format_func=lambda x: f"{api_options[x]} {x}"
103
+ )
104
+ api_id_mapping = {"OrderProcessor": 2, "AuthService": 0, "ProductCatalog": 1, "PaymentGateway": 3}
105
+ api_id_encoded = api_id_mapping[api_id]
106
+
107
+ # Environment dropdown with descriptions
108
+ env_options = {
109
+ "production-useast1": "Production (US East)",
110
+ "staging": "Staging Environment"
111
+ }
112
+ env = st.selectbox(
113
+ "Environment",
114
+ options=list(env_options.keys()),
115
+ format_func=lambda x: env_options[x]
116
+ )
117
+ env_mapping = {"production-useast1": 1, "staging": 0}
118
+ env_encoded = env_mapping[env]
119
+
120
+ # Performance metrics with tooltips and better ranges
121
+ with st.sidebar.expander("Performance Metrics", expanded=True):
122
+ latency_ms = st.slider(
123
+ "Latency (ms)",
124
+ min_value=0.0,
125
+ max_value=100.0,
126
+ value=10.0,
127
+ step=0.1,
128
+ help="Response time in milliseconds"
129
+ )
130
+
131
+ bytes_transferred = st.slider(
132
+ "Bytes Transferred",
133
+ min_value=0,
134
+ max_value=15000,
135
+ value=300,
136
+ help="Amount of data transferred in the request/response"
137
+ )
138
+
139
+ hour_slider = st.slider(
140
+ "Hour of Day",
141
+ min_value=0,
142
+ max_value=23,
143
+ value=14,
144
+ help="The hour when the request is made (0-23)"
145
+ )
146
+ # Convert hour to more readable format
147
+ hour_of_day = hour_slider
148
+ hour_display = f"{hour_slider}:00" + (" AM" if hour_slider < 12 else " PM")
149
+ st.caption(f"Selected time: {hour_display}")
150
+
151
+ # Resource usage
152
+ with st.sidebar.expander("Resource Usage", expanded=True):
153
+ simulated_cpu_cost = st.slider(
154
+ "CPU Cost",
155
+ min_value=0.0,
156
+ max_value=50.0,
157
+ value=10.0,
158
+ step=0.1,
159
+ help="Simulated CPU utilization cost"
160
+ )
161
+
162
+ simulated_memory_mb = st.slider(
163
+ "Memory Usage (MB)",
164
+ min_value=0.0,
165
+ max_value=100.0,
166
+ value=25.0,
167
+ step=0.1,
168
+ help="Simulated memory usage in megabytes"
169
+ )
170
+
171
+ # Add a reset button
172
+ if st.sidebar.button("Reset Parameters"):
173
+ st.experimental_rerun()
174
+
175
+ # Prepare input DataFrame
176
+ input_df = pd.DataFrame([[
177
+ api_id_encoded, env_encoded, latency_ms, bytes_transferred, hour_of_day,
178
+ simulated_cpu_cost, simulated_memory_mb
179
+ ]], columns=[
180
+ 'api_id', 'env', 'latency_ms', 'bytes_transferred',
181
+ 'hour_of_day', 'simulated_cpu_cost', 'simulated_memory_mb'
182
+ ])
183
+
184
+ # Get prediction
185
+ prediction = model.predict(input_df)[0]
186
+ probability = model.predict_proba(input_df)[0][1]
187
+
188
+ # Main content area
189
+ st.markdown('<div class="sub-header">🧠 Prediction Results</div>', unsafe_allow_html=True)
190
+
191
+ # Display prediction in two columns
192
+ col1, col2 = st.columns(2)
193
+
194
+ with col1:
195
+ # Show prediction with better styling
196
+ if prediction == 0:
197
+ st.markdown(f"""
198
+ <div class="prediction-box-success">
199
+ <h2>βœ… No Error Predicted</h2>
200
+ <p>The API call is likely to succeed</p>
201
+ <h3>Confidence: {(1 - probability) * 100:.1f}%</h3>
202
+ </div>
203
+ """, unsafe_allow_html=True)
204
+ else:
205
+ st.markdown(f"""
206
+ <div class="prediction-box-error">
207
+ <h2>🚫 Error Predicted</h2>
208
+ <p>The API call is likely to fail</p>
209
+ <h3>Confidence: {probability * 100:.1f}%</h3>
210
+ </div>
211
+ """, unsafe_allow_html=True)
212
+
213
+ with col2:
214
+ # Create a gauge chart for probability visualization
215
+ fig, ax = plt.subplots(figsize=(4, 3))
216
+
217
+ # Create gauge
218
+ gauge_colors = [(0.2, 0.8, 0.2), (0.8, 0.8, 0.2), (0.8, 0.2, 0.2)]
219
+ cmap = plt.cm.RdYlGn_r
220
+ norm = plt.Normalize(0, 1)
221
+
222
+ theta = np.linspace(0.75 * np.pi, 0.25 * np.pi, 100)
223
+ r = 0.5
224
+ x = r * np.cos(theta)
225
+ y = r * np.sin(theta)
226
+
227
+ ax.plot(x, y, 'k', linewidth=3)
228
+
229
+ # Needle
230
+ needle_theta = 0.75 * np.pi - probability * 0.5 * np.pi
231
+ needle_x = [0, r * 0.8 * np.cos(needle_theta)]
232
+ needle_y = [0, r * 0.8 * np.sin(needle_theta)]
233
+ ax.plot(needle_x, needle_y, 'r', linewidth=2)
234
+ ax.add_patch(plt.Circle((0, 0), radius=0.05, color='darkred'))
235
+
236
+ # Add labels
237
+ ax.text(-0.5, -0.1, "Low", fontsize=9)
238
+ ax.text(0, 0.35, "Medium", fontsize=9)
239
+ ax.text(0.5, -0.1, "High", fontsize=9)
240
+ ax.text(0, -0.3, f"Error Probability: {probability:.2f}", fontsize=10, ha='center', fontweight='bold')
241
+
242
+ # Format plot
243
+ ax.set_aspect('equal')
244
+ ax.axis('off')
245
+ st.pyplot(fig)
246
+
247
+ # Display feature importance
248
+ st.markdown('<div class="sub-header">πŸ“Š Feature Analysis</div>', unsafe_allow_html=True)
249
+
250
+ # Create three columns for metrics
251
+ col1, col2, col3 = st.columns(3)
252
+
253
+ with col1:
254
+ st.markdown("""
255
+ <div class="metric-container">
256
+ <h4>API Service</h4>
257
+ <p>{} {}</p>
258
+ </div>
259
+ """.format(api_options[api_id], api_id), unsafe_allow_html=True)
260
+
261
+ with col2:
262
+ st.markdown("""
263
+ <div class="metric-container">
264
+ <h4>Environment</h4>
265
+ <p>{}</p>
266
+ </div>
267
+ """.format(env_options[env]), unsafe_allow_html=True)
268
+
269
+ with col3:
270
+ st.markdown("""
271
+ <div class="metric-container">
272
+ <h4>Time of Day</h4>
273
+ <p>{}</p>
274
+ </div>
275
+ """.format(hour_display), unsafe_allow_html=True)
276
+
277
+ # Performance metrics
278
+ col1, col2, col3 = st.columns(3)
279
+
280
+ with col1:
281
+ st.markdown("""
282
+ <div class="metric-container">
283
+ <h4>Latency</h4>
284
+ <p>{} ms</p>
285
+ </div>
286
+ """.format(latency_ms), unsafe_allow_html=True)
287
+
288
+ with col2:
289
+ st.markdown("""
290
+ <div class="metric-container">
291
+ <h4>CPU Cost</h4>
292
+ <p>{}</p>
293
+ </div>
294
+ """.format(simulated_cpu_cost), unsafe_allow_html=True)
295
+
296
+ with col3:
297
+ st.markdown("""
298
+ <div class="metric-container">
299
+ <h4>Memory Usage</h4>
300
+ <p>{} MB</p>
301
+ </div>
302
+ """.format(simulated_memory_mb), unsafe_allow_html=True)
303
+
304
+ # Input data inspector
305
+ with st.expander("πŸ” View Raw Input Data"):
306
+ # Create a more readable table
307
+ display_df = pd.DataFrame({
308
+ 'Feature': ['API Service', 'Environment', 'Latency (ms)', 'Bytes Transferred',
309
+ 'Hour of Day', 'CPU Cost', 'Memory (MB)'],
310
+ 'Value': [api_id, env, latency_ms, bytes_transferred,
311
+ hour_of_day, simulated_cpu_cost, simulated_memory_mb],
312
+ 'Encoded Value': [api_id_encoded, env_encoded, latency_ms, bytes_transferred,
313
+ hour_of_day, simulated_cpu_cost, simulated_memory_mb]
314
+ })
315
+
316
+ st.dataframe(display_df, use_container_width=True)
317
+
318
+ # Help section
319
+ with st.expander("❓ How to Use This Tool"):
320
+ st.markdown("""
321
+ ### Instructions
322
+ 1. **Adjust Parameters**: Use the sidebar sliders and dropdowns to set your API parameters
323
+ 2. **View Prediction**: The prediction updates automatically when you change any parameter
324
+ 3. **Analyze Results**: Look at the gauge chart and feature metrics to understand factors affecting the prediction
325
+
326
+ ### About the Model
327
+ This tool uses a machine learning model trained on historical API call data to predict whether a call with the given parameters will result in an error.
328
+ """)
329
+
330
+ # Footer
331
+ st.markdown("---")
332
+ st.markdown("API Error Prediction Tool | Developed for DevOps Team", unsafe_allow_html=True)
error_classifier.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2a8481236d45ef6cde666c1d1e314c3aab6f6a282252639193b81e9ab078e0db
3
+ size 808921