Barisha commited on
Commit
3b6661b
Β·
verified Β·
1 Parent(s): 1703744

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -23
app.py CHANGED
@@ -13,10 +13,9 @@ model = AutoModelForCausalLM.from_pretrained(
13
  device_map="cpu"
14
  )
15
 
16
- # ----- Trend detection (math-based, NOT LLM) -----
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
- # ----- Simple anomaly detection -----
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
- # ----- Main function -----
56
- def analyze(entity, value_string):
 
57
  try:
58
  values = [float(x.strip()) for x in value_string.split(",")]
59
  except:
60
- return "❌ Error: Please enter numbers separated by commas"
 
61
 
62
  trend = detect_trend(values)
63
  anomaly = detect_anomaly(values)
64
  exp = explanation(entity, values, trend)
65
 
66
- return f"""
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 Trend + Anomaly Analyzer (CPU-Friendly)")
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(fn=analyze, inputs=[entity, values], outputs=output)
 
 
 
 
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()