znacer commited on
Commit
c82570a
·
1 Parent(s): aafd0ea
Files changed (2) hide show
  1. app.py +120 -1
  2. app/ui.py +0 -90
app.py CHANGED
@@ -5,9 +5,17 @@ run: `gradio main.py` to run the application
5
 
6
  import sys
7
 
 
8
  from loguru import logger
9
 
10
- from app.ui import demo
 
 
 
 
 
 
 
11
 
12
  logger.add(
13
  sys.stdout,
@@ -16,7 +24,118 @@ logger.add(
16
  level="INFO",
17
  colorize=True,
18
  )
 
 
 
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  if __name__ == "__main__":
22
  demo.launch(mcp_server=True)
 
5
 
6
  import sys
7
 
8
+ import gradio as gr
9
  from loguru import logger
10
 
11
+ from app.service import (
12
+ analyze_trends,
13
+ connect_database,
14
+ detect_anomalies,
15
+ generate_analysis_report,
16
+ list_available_metrics,
17
+ query_timeseries,
18
+ )
19
 
20
  logger.add(
21
  sys.stdout,
 
24
  level="INFO",
25
  colorize=True,
26
  )
27
+ example_sensor = "temperature"
28
+ example_start = "2019-06-15T02:54:00"
29
+ example_end = "2019-06-17T02:54:00"
30
 
31
+ with gr.Blocks() as demo:
32
+ gr.Markdown("# TimescaleDB Time Series Analyzer API (Gradio)")
33
+ with gr.Tab("Connect DB"):
34
+ connect_btn = gr.Button("Connect to TimescaleDB")
35
+ connect_out = gr.Textbox(label="Connection Result")
36
+ connect_btn.click(
37
+ fn=connect_database,
38
+ inputs=[],
39
+ outputs=connect_out,
40
+ )
41
+ with gr.Tab("List Metrics"):
42
+ list_btn = gr.Button("List Available Metrics")
43
+ list_out = gr.Textbox(label="Metrics")
44
+ list_btn.click(
45
+ fn=list_available_metrics,
46
+ inputs=[],
47
+ outputs=list_out,
48
+ )
49
+ with gr.Tab("Query Timeseries"):
50
+ sensor_id = gr.Textbox(label="Sensor ID", value=example_sensor)
51
+ start_time = gr.Textbox(label="Start Time (ISO)", value=example_start)
52
+ end_time = gr.Textbox(label="End Time (ISO)", value=example_end)
53
+ query_btn = gr.Button("Query")
54
+ query_out = gr.Textbox(label="Query Result")
55
+ query_btn.click(
56
+ fn=query_timeseries,
57
+ inputs=[sensor_id, start_time, end_time],
58
+ outputs=query_out,
59
+ )
60
+ with gr.Tab("Detect Anomalies"):
61
+ sensor_id2 = gr.Textbox(label="Sensor ID", value=example_sensor)
62
+ start_time2 = gr.Textbox(label="Start Time (ISO)", value=example_start)
63
+ end_time2 = gr.Textbox(label="End Time (ISO)", value=example_end)
64
+ algorithm = gr.Radio(
65
+ label="Algorithm",
66
+ choices=["zscore", "isolation_forest"],
67
+ value="zscore",
68
+ )
69
+ threshold = gr.Number(label="Z-Score Threshold", value=2.0)
70
+ contamination = gr.Number(
71
+ label="Isolation Forest Contamination",
72
+ value=0.1,
73
+ minimum=0.01,
74
+ maximum=0.5,
75
+ step=0.01,
76
+ )
77
+ anomaly_btn = gr.Button("Detect")
78
+ anomaly_out = gr.Textbox(label="Anomaly Result")
79
+ anomaly_btn.click(
80
+ fn=detect_anomalies,
81
+ inputs=[
82
+ sensor_id2,
83
+ start_time2,
84
+ end_time2,
85
+ threshold,
86
+ algorithm,
87
+ contamination,
88
+ ],
89
+ outputs=anomaly_out,
90
+ )
91
+ with gr.Tab("Analyze Trends"):
92
+ sensor_id3 = gr.Textbox(label="Sensor ID", value=example_sensor)
93
+ start_time3 = gr.Textbox(label="Start Time (ISO)", value=example_start)
94
+ end_time3 = gr.Textbox(label="End Time (ISO)", value=example_end)
95
+ trend_btn = gr.Button("Analyze")
96
+ trend_out = gr.Textbox(label="Trend Result")
97
+ trend_btn.click(
98
+ fn=analyze_trends,
99
+ inputs=[sensor_id3, start_time3, end_time3],
100
+ outputs=trend_out,
101
+ )
102
+ with gr.Tab("Generate Report"):
103
+ sensor_id4 = gr.Textbox(label="Sensor ID", value=example_sensor)
104
+ start_time4 = gr.Textbox(label="Start Time (ISO)", value=example_start)
105
+ end_time4 = gr.Textbox(label="End Time (ISO)", value=example_end)
106
+ include_anomalies = gr.Checkbox(label="Include Anomalies", value=True)
107
+ include_trends = gr.Checkbox(label="Include Trends", value=True)
108
+ user_question = gr.Textbox(label="User Question", value="")
109
+ anomaly_algorithm = gr.Radio(
110
+ label="Anomaly Detection Algorithm",
111
+ choices=["zscore", "isolation_forest"],
112
+ value="zscore",
113
+ )
114
+ anomaly_threshold = gr.Number(label="Z-Score Threshold", value=2.0)
115
+ anomaly_contamination = gr.Number(
116
+ label="Isolation Forest Contamination",
117
+ value=0.1,
118
+ minimum=0.01,
119
+ maximum=0.5,
120
+ step=0.01,
121
+ )
122
+ report_btn = gr.Button("Generate Report")
123
+ report_out = gr.Markdown(label="Report")
124
+ report_btn.click(
125
+ fn=generate_analysis_report,
126
+ inputs=[
127
+ sensor_id4,
128
+ start_time4,
129
+ end_time4,
130
+ include_anomalies,
131
+ include_trends,
132
+ user_question,
133
+ anomaly_algorithm,
134
+ anomaly_threshold,
135
+ anomaly_contamination,
136
+ ],
137
+ outputs=report_out,
138
+ )
139
 
140
  if __name__ == "__main__":
141
  demo.launch(mcp_server=True)
app/ui.py DELETED
@@ -1,90 +0,0 @@
1
- """Definition of the Gradio UI."""
2
-
3
- import gradio as gr
4
-
5
- from app.service import (
6
- analyze_trends,
7
- connect_database,
8
- detect_anomalies,
9
- generate_analysis_report,
10
- list_available_metrics,
11
- query_timeseries,
12
- )
13
-
14
- example_sensor = "temperature"
15
- example_start = "2019-06-15T02:54:00"
16
- example_end = "2019-06-17T02:54:00"
17
-
18
- with gr.Blocks() as demo:
19
- gr.Markdown("# TimescaleDB Time Series Analyzer API (Gradio)")
20
- with gr.Tab("Connect DB"):
21
- connect_btn = gr.Button("Connect to TimescaleDB")
22
- connect_out = gr.Textbox(label="Connection Result")
23
- connect_btn.click(
24
- fn=connect_database,
25
- inputs=[],
26
- outputs=connect_out,
27
- )
28
- with gr.Tab("List Metrics"):
29
- list_btn = gr.Button("List Available Metrics")
30
- list_out = gr.Textbox(label="Metrics")
31
- list_btn.click(
32
- fn=list_available_metrics,
33
- inputs=[],
34
- outputs=list_out,
35
- )
36
- with gr.Tab("Query Timeseries"):
37
- sensor_id = gr.Textbox(label="Sensor ID", value=example_sensor)
38
- start_time = gr.Textbox(label="Start Time (ISO)", value=example_start)
39
- end_time = gr.Textbox(label="End Time (ISO)", value=example_end)
40
- query_btn = gr.Button("Query")
41
- query_out = gr.Textbox(label="Query Result")
42
- query_btn.click(
43
- fn=query_timeseries,
44
- inputs=[sensor_id, start_time, end_time],
45
- outputs=query_out,
46
- )
47
- with gr.Tab("Detect Anomalies"):
48
- sensor_id2 = gr.Textbox(label="Sensor ID", value=example_sensor)
49
- start_time2 = gr.Textbox(label="Start Time (ISO)", value=example_start)
50
- end_time2 = gr.Textbox(label="End Time (ISO)", value=example_end)
51
- threshold = gr.Number(label="Threshold", value=2.0)
52
- anomaly_btn = gr.Button("Detect")
53
- anomaly_out = gr.Textbox(label="Anomaly Result")
54
- anomaly_btn.click(
55
- fn=detect_anomalies,
56
- inputs=[sensor_id2, start_time2, end_time2, threshold],
57
- outputs=anomaly_out,
58
- )
59
- with gr.Tab("Analyze Trends"):
60
- sensor_id3 = gr.Textbox(label="Sensor ID", value=example_sensor)
61
- start_time3 = gr.Textbox(label="Start Time (ISO)", value=example_start)
62
- end_time3 = gr.Textbox(label="End Time (ISO)", value=example_end)
63
- trend_btn = gr.Button("Analyze")
64
- trend_out = gr.Textbox(label="Trend Result")
65
- trend_btn.click(
66
- fn=analyze_trends,
67
- inputs=[sensor_id3, start_time3, end_time3],
68
- outputs=trend_out,
69
- )
70
- with gr.Tab("Generate Report"):
71
- sensor_id4 = gr.Textbox(label="Sensor ID", value=example_sensor)
72
- start_time4 = gr.Textbox(label="Start Time (ISO)", value=example_start)
73
- end_time4 = gr.Textbox(label="End Time (ISO)", value=example_end)
74
- include_anomalies = gr.Checkbox(label="Include Anomalies", value=True)
75
- include_trends = gr.Checkbox(label="Include Trends", value=True)
76
- user_question = gr.Textbox(label="User Question", value="")
77
- report_btn = gr.Button("Generate Report")
78
- report_out = gr.Markdown(label="Report")
79
- report_btn.click(
80
- fn=generate_analysis_report,
81
- inputs=[
82
- sensor_id4,
83
- start_time4,
84
- end_time4,
85
- include_anomalies,
86
- include_trends,
87
- user_question,
88
- ],
89
- outputs=report_out,
90
- )