arjunanand13 commited on
Commit
bcdafda
·
verified ·
1 Parent(s): 6beab5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -10
app.py CHANGED
@@ -30,12 +30,17 @@ class AnomalyDetectionAgent:
30
  anomalies = iso_forest.fit_predict(data_scaled)
31
  return anomalies == -1
32
 
33
- def plot_data(data, title):
34
- plt.figure(figsize=(10, 6))
35
- plt.plot(data)
36
- plt.title(title)
37
- plt.close()
38
- return plt
 
 
 
 
 
39
 
40
  class FeatureExtractionAgent:
41
  def extract(self, data):
@@ -103,8 +108,13 @@ def analyze_time_series(data, forecast_steps):
103
 
104
  trend_plot = plot_data(trend, "Trend")
105
  seasonality_plot = plot_data(seasonality, "Seasonality")
106
- anomalies_plot = plot_data(data * anomalies, "Anomalies")
107
- forecast_plot = plot_data(np.concatenate([data, forecast]), "Forecast")
 
 
 
 
 
108
 
109
  return (
110
  trend_plot,
@@ -120,10 +130,12 @@ def analyze_time_series(data, forecast_steps):
120
  logging.error(error_msg)
121
  return (None, None, None, None, None, "", error_msg)
122
 
 
 
123
  iface = gr.Interface(
124
  fn=analyze_time_series,
125
  inputs=[
126
- gr.Textbox(label="Enter comma-separated time series data"),
127
  gr.Number(label="Number of steps to forecast", value=5)
128
  ],
129
  outputs=[
@@ -133,7 +145,7 @@ iface = gr.Interface(
133
  gr.JSON(label="Features"),
134
  gr.Plot(label="Forecast"),
135
  gr.Textbox(label="Insight"),
136
- gr.Textbox(label="Error")
137
  ],
138
  title="Agentic RAG Time Series Analysis",
139
  description="Enter a comma-separated list of numbers representing your time series data, and specify the number of steps to forecast."
 
30
  anomalies = iso_forest.fit_predict(data_scaled)
31
  return anomalies == -1
32
 
33
+
34
+ def plot_data(data, title, anomalies=None):
35
+ fig, ax = plt.subplots(figsize=(10, 6))
36
+ ax.plot(data, label='Data')
37
+ if anomalies is not None:
38
+ anomaly_indices = np.where(anomalies)[0]
39
+ ax.scatter(anomaly_indices, data[anomaly_indices], color='red', label='Anomalies')
40
+ ax.set_title(title)
41
+ ax.legend()
42
+ plt.close(fig)
43
+ return fig
44
 
45
  class FeatureExtractionAgent:
46
  def extract(self, data):
 
108
 
109
  trend_plot = plot_data(trend, "Trend")
110
  seasonality_plot = plot_data(seasonality, "Seasonality")
111
+ anomalies_plot = plot_data(data, "Anomalies", anomalies)
112
+
113
+ full_data = np.concatenate([data, forecast])
114
+ forecast_plot = plot_data(full_data, "Forecast")
115
+ ax = forecast_plot.axes[0]
116
+ ax.axvline(x=len(data) - 1, color='r', linestyle='--', label='Forecast Start')
117
+ ax.legend()
118
 
119
  return (
120
  trend_plot,
 
130
  logging.error(error_msg)
131
  return (None, None, None, None, None, "", error_msg)
132
 
133
+ example_input = "120,125,130,140,135,145,150,160,155,165,170,180,175,185,190,200,195,205,210,220,215,225,230,240,235,245,250,260,255,265,270,280,275,285,290,300,295,305,310,320,315,325,330,340,335,345,350,360,355,365,370,380,375,385,390,400,395,405,410,420"
134
+
135
  iface = gr.Interface(
136
  fn=analyze_time_series,
137
  inputs=[
138
+ gr.Textbox(label="Enter comma-separated time series data", value=example_input),
139
  gr.Number(label="Number of steps to forecast", value=5)
140
  ],
141
  outputs=[
 
145
  gr.JSON(label="Features"),
146
  gr.Plot(label="Forecast"),
147
  gr.Textbox(label="Insight"),
148
+ gr.Textbox(label="Error", visible=False)
149
  ],
150
  title="Agentic RAG Time Series Analysis",
151
  description="Enter a comma-separated list of numbers representing your time series data, and specify the number of steps to forecast."