Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,9 @@ from sklearn.preprocessing import StandardScaler
|
|
| 7 |
import gradio as gr
|
| 8 |
import traceback
|
| 9 |
import logging
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
# Set up logging
|
| 12 |
logging.basicConfig(level=logging.ERROR)
|
| 13 |
|
| 14 |
class TrendAnalysisAgent:
|
|
@@ -28,6 +29,13 @@ class AnomalyDetectionAgent:
|
|
| 28 |
iso_forest = IsolationForest(contamination=0.1, random_state=42)
|
| 29 |
anomalies = iso_forest.fit_predict(data_scaled)
|
| 30 |
return anomalies == -1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
class FeatureExtractionAgent:
|
| 33 |
def extract(self, data):
|
|
@@ -93,20 +101,24 @@ def analyze_time_series(data, forecast_steps):
|
|
| 93 |
agentic_rag = AgenticRAG()
|
| 94 |
trend, seasonality, anomalies, features, forecast, insight = agentic_rag.process(data, forecast_steps)
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
except Exception as e:
|
| 105 |
error_msg = f"An error occurred: {str(e)}\n{traceback.format_exc()}"
|
| 106 |
logging.error(error_msg)
|
| 107 |
-
return
|
| 108 |
-
"Error": error_msg
|
| 109 |
-
}
|
| 110 |
|
| 111 |
iface = gr.Interface(
|
| 112 |
fn=analyze_time_series,
|
|
@@ -121,7 +133,7 @@ iface = gr.Interface(
|
|
| 121 |
gr.JSON(label="Features"),
|
| 122 |
gr.Plot(label="Forecast"),
|
| 123 |
gr.Textbox(label="Insight"),
|
| 124 |
-
gr.Textbox(label="Error"
|
| 125 |
],
|
| 126 |
title="Agentic RAG Time Series Analysis",
|
| 127 |
description="Enter a comma-separated list of numbers representing your time series data, and specify the number of steps to forecast."
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
import traceback
|
| 9 |
import logging
|
| 10 |
+
import matplotlib.pyplot as plt
|
| 11 |
+
|
| 12 |
|
|
|
|
| 13 |
logging.basicConfig(level=logging.ERROR)
|
| 14 |
|
| 15 |
class TrendAnalysisAgent:
|
|
|
|
| 29 |
iso_forest = IsolationForest(contamination=0.1, random_state=42)
|
| 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):
|
|
|
|
| 101 |
agentic_rag = AgenticRAG()
|
| 102 |
trend, seasonality, anomalies, features, forecast, insight = agentic_rag.process(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,
|
| 111 |
+
seasonality_plot,
|
| 112 |
+
anomalies_plot,
|
| 113 |
+
features.to_dict(orient='records')[0],
|
| 114 |
+
forecast_plot,
|
| 115 |
+
insight,
|
| 116 |
+
"" # Empty string for the error output
|
| 117 |
+
)
|
| 118 |
except Exception as e:
|
| 119 |
error_msg = f"An error occurred: {str(e)}\n{traceback.format_exc()}"
|
| 120 |
logging.error(error_msg)
|
| 121 |
+
return (None, None, None, None, None, "", error_msg)
|
|
|
|
|
|
|
| 122 |
|
| 123 |
iface = gr.Interface(
|
| 124 |
fn=analyze_time_series,
|
|
|
|
| 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."
|