Update app.py
Browse files
app.py
CHANGED
|
@@ -13,10 +13,9 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 13 |
device_map="cpu"
|
| 14 |
)
|
| 15 |
|
| 16 |
-
# ----- Trend detection
|
| 17 |
def detect_trend(values):
|
| 18 |
diffs = np.diff(values)
|
| 19 |
-
|
| 20 |
if all(d > 0 for d in diffs):
|
| 21 |
return "INCREASING"
|
| 22 |
elif all(d < 0 for d in diffs):
|
|
@@ -24,19 +23,12 @@ def detect_trend(values):
|
|
| 24 |
else:
|
| 25 |
return "MIXED"
|
| 26 |
|
| 27 |
-
# -----
|
| 28 |
def detect_anomaly(values):
|
| 29 |
mean = np.mean(values)
|
| 30 |
std = np.std(values)
|
| 31 |
-
|
| 32 |
-
anomalies =
|
| 33 |
-
for i, v in enumerate(values):
|
| 34 |
-
if abs(v - mean) > 2 * std:
|
| 35 |
-
anomalies.append((i, v))
|
| 36 |
-
|
| 37 |
-
if len(anomalies) == 0:
|
| 38 |
-
return "No anomalies detected"
|
| 39 |
-
return str(anomalies)
|
| 40 |
|
| 41 |
# ----- LLM explanation -----
|
| 42 |
def explanation(entity, values, trend):
|
|
@@ -45,36 +37,36 @@ You are a KPI analysis expert.
|
|
| 45 |
The entity is: {entity}
|
| 46 |
The values are: {values}
|
| 47 |
The detected trend is: {trend}
|
| 48 |
-
|
| 49 |
Explain in simple words why the trend is {trend}.
|
| 50 |
"""
|
| 51 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 52 |
outputs = model.generate(**inputs, max_new_tokens=150)
|
| 53 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 54 |
|
| 55 |
-
# -----
|
| 56 |
-
def
|
|
|
|
| 57 |
try:
|
| 58 |
values = [float(x.strip()) for x in value_string.split(",")]
|
| 59 |
except:
|
| 60 |
-
|
|
|
|
| 61 |
|
| 62 |
trend = detect_trend(values)
|
| 63 |
anomaly = detect_anomaly(values)
|
| 64 |
exp = explanation(entity, values, trend)
|
| 65 |
|
| 66 |
-
|
| 67 |
-
π **Entity:** {entity}
|
| 68 |
-
π **Trend:** {trend}
|
| 69 |
-
β οΈ **Anomalies:** {anomaly}
|
| 70 |
-
|
| 71 |
π§ **Explanation:**
|
| 72 |
{exp}
|
| 73 |
"""
|
| 74 |
|
| 75 |
# ----- UI -----
|
| 76 |
with gr.Blocks() as demo:
|
| 77 |
-
gr.Markdown("# π KPI
|
| 78 |
|
| 79 |
entity = gr.Textbox(label="KPI Name (example: volte_rate)")
|
| 80 |
values = gr.Textbox(label="Values (comma separated, example: 10,11,13,14,15)")
|
|
@@ -82,6 +74,10 @@ with gr.Blocks() as demo:
|
|
| 82 |
|
| 83 |
run_btn = gr.Button("Analyze")
|
| 84 |
|
| 85 |
-
run_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
demo.launch()
|
|
|
|
| 13 |
device_map="cpu"
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# ----- Trend detection -----
|
| 17 |
def detect_trend(values):
|
| 18 |
diffs = np.diff(values)
|
|
|
|
| 19 |
if all(d > 0 for d in diffs):
|
| 20 |
return "INCREASING"
|
| 21 |
elif all(d < 0 for d in diffs):
|
|
|
|
| 23 |
else:
|
| 24 |
return "MIXED"
|
| 25 |
|
| 26 |
+
# ----- Anomaly detection -----
|
| 27 |
def detect_anomaly(values):
|
| 28 |
mean = np.mean(values)
|
| 29 |
std = np.std(values)
|
| 30 |
+
anomalies = [(i, v) for i, v in enumerate(values) if abs(v - mean) > 2 * std]
|
| 31 |
+
return "No anomalies detected" if len(anomalies) == 0 else str(anomalies)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# ----- LLM explanation -----
|
| 34 |
def explanation(entity, values, trend):
|
|
|
|
| 37 |
The entity is: {entity}
|
| 38 |
The values are: {values}
|
| 39 |
The detected trend is: {trend}
|
|
|
|
| 40 |
Explain in simple words why the trend is {trend}.
|
| 41 |
"""
|
| 42 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 43 |
outputs = model.generate(**inputs, max_new_tokens=150)
|
| 44 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 45 |
|
| 46 |
+
# ----- Wrapper with loading message -----
|
| 47 |
+
def analyze_with_spinner(entity, value_string):
|
| 48 |
+
yield "β³ **Analyzing... please wait...**"
|
| 49 |
try:
|
| 50 |
values = [float(x.strip()) for x in value_string.split(",")]
|
| 51 |
except:
|
| 52 |
+
yield "β Error: Please enter numbers separated by commas"
|
| 53 |
+
return
|
| 54 |
|
| 55 |
trend = detect_trend(values)
|
| 56 |
anomaly = detect_anomaly(values)
|
| 57 |
exp = explanation(entity, values, trend)
|
| 58 |
|
| 59 |
+
yield f"""
|
| 60 |
+
π **Entity:** {entity}
|
| 61 |
+
π **Trend:** {trend}
|
| 62 |
+
β οΈ **Anomalies:** {anomaly}
|
|
|
|
| 63 |
π§ **Explanation:**
|
| 64 |
{exp}
|
| 65 |
"""
|
| 66 |
|
| 67 |
# ----- UI -----
|
| 68 |
with gr.Blocks() as demo:
|
| 69 |
+
gr.Markdown("# π KPI Analyzer")
|
| 70 |
|
| 71 |
entity = gr.Textbox(label="KPI Name (example: volte_rate)")
|
| 72 |
values = gr.Textbox(label="Values (comma separated, example: 10,11,13,14,15)")
|
|
|
|
| 74 |
|
| 75 |
run_btn = gr.Button("Analyze")
|
| 76 |
|
| 77 |
+
run_btn.click(
|
| 78 |
+
fn=analyze_with_spinner,
|
| 79 |
+
inputs=[entity, values],
|
| 80 |
+
outputs=output,
|
| 81 |
+
)
|
| 82 |
|
| 83 |
demo.launch()
|