Nuzz23 commited on
Commit
436e1a2
·
1 Parent(s): 06e5c32

added traceback

Browse files
Files changed (1) hide show
  1. app.py +51 -34
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import gradio as gr
2
  import os
3
  import torch
@@ -14,18 +15,23 @@ OUT_PATH ="./savedPredictions/results.csv"
14
 
15
  def dataProcessing(file, timestamp_column: str = None):
16
  global chronos2
17
- if os.path.exists(OUT_PATH):
18
- os.remove(OUT_PATH)
19
-
20
  # Mostra messaggio di processing
21
  yield (
22
- gr.update(visible=True, value="⏳ Processing file, please wait..."), # processing_msg
23
- gr.update(visible=False), # plot_output
24
- gr.update(visible=False), # download_output
25
- gr.update(visible=False, value="") # errorHandler
26
  )
27
 
28
  try:
 
 
 
 
 
 
 
29
  validateData(file, timestamp_column)
30
  preProcessedData, timestamp_old, target_cols = preProcessData(file, timestamp_column)
31
  predictions, indexes = predictData(chronos2, preProcessedData, target_cols)
@@ -37,30 +43,38 @@ def dataProcessing(file, timestamp_column: str = None):
37
  df.to_csv(OUT_PATH, index=False)
38
 
39
  # Nascondi processing, mostra risultati
40
- yield (
 
41
  gr.update(visible=False), # processing_msg
42
- gr.update(visible=True, value=fig) if fig is not None else gr.update(visible=False), # plot_output
43
  gr.update(visible=True, value=OUT_PATH), # download_output
44
- gr.update(visible=False, value="") # errorHandler
45
- )
 
 
 
 
 
 
 
 
46
  except Exception as e:
47
  # Mostra errore
48
  yield (
49
  gr.update(visible=False), # processing_msg
50
- gr.update(visible=False), # plot_output
51
- gr.update(visible=False), # download_output
52
- gr.update(visible=True, value=f"Error: {e}") # errorHandler
53
  )
54
 
 
55
  if os.path.exists(OUT_PATH):
56
  os.remove(OUT_PATH)
57
 
58
-
59
  os.makedirs(os.path.dirname(OUT_PATH), exist_ok=True)
60
  chronos2 = Chronos2Pipeline.from_pretrained("amazon/chronos-2", device_map=device)
61
 
62
 
63
-
64
  with gr.Blocks(title="Time series anomaly detection with Chronos2") as demo:
65
  gr.Markdown(
66
  """
@@ -88,39 +102,42 @@ with gr.Blocks(title="Time series anomaly detection with Chronos2") as demo:
88
 
89
  with gr.Row():
90
  with gr.Column(scale=1):
91
- file_input = gr.File(label="Upload Time Series Data (CSV)", file_types=[".csv"], file_count="single", )
92
  timestamp_question = gr.Radio(
93
  label="Does your data contain a timestamp column?",
94
  choices=["Yes", "No"],
95
  value="No",
96
  interactive=True,
97
-
98
  )
99
- timestamp_column_input = gr.Textbox(label="Please specify the column name of the timestamp:", visible=False, value="", interactive=False)
 
 
 
 
 
100
 
101
  timestamp_question.change(
102
- lambda x: gr.update(visible=(x == "Yes"), interactive=(x == "Yes"), value="timestamp" if x == "Yes" else ""),
 
 
 
 
103
  inputs=timestamp_question,
104
  outputs=timestamp_column_input
105
  )
106
 
107
- detect_button = gr.Button("Detect Anomalies")
108
 
109
  with gr.Column(scale=4):
110
- plot_output = gr.Plot(label="Time Series with Detected Anomalies", visible=False)
111
- download_output = gr.File(label="Download Anomaly Detection Results (CSV)", visible=False, interactive=False)
112
- errorHandler = gr.Markdown(label="Error Messages", visible=False)
113
-
114
  processing_msg = gr.Markdown("⏳ Processing file, please wait...", visible=False)
 
 
 
115
 
116
- detect_button.click(
117
- lambda file, timestamp_question, timestamp_column:
118
- dataProcessing(
119
- file.name if file is not None else None,
120
- timestamp_column if timestamp_question == "Yes" else None
121
- ),
122
- inputs=[file_input, timestamp_question, timestamp_column_input],
123
- outputs=[processing_msg, plot_output, download_output, errorHandler]
124
- )
125
 
126
  demo.launch(share=True)
 
1
+ import traceback
2
  import gradio as gr
3
  import os
4
  import torch
 
15
 
16
  def dataProcessing(file, timestamp_column: str = None):
17
  global chronos2
18
+
 
 
19
  # Mostra messaggio di processing
20
  yield (
21
+ gr.update(visible=True), # processing_msg
22
+ gr.update(visible=False, value=None), # plot_output
23
+ gr.update(visible=False, value=None), # download_output
24
+ gr.update(visible=False) # errorHandler
25
  )
26
 
27
  try:
28
+ if os.path.exists(OUT_PATH):
29
+ os.remove(OUT_PATH)
30
+
31
+ # Converti stringa vuota in None
32
+ if timestamp_column == "":
33
+ timestamp_column = None
34
+
35
  validateData(file, timestamp_column)
36
  preProcessedData, timestamp_old, target_cols = preProcessData(file, timestamp_column)
37
  predictions, indexes = predictData(chronos2, preProcessedData, target_cols)
 
43
  df.to_csv(OUT_PATH, index=False)
44
 
45
  # Nascondi processing, mostra risultati
46
+ if fig is not None:
47
+ yield (
48
  gr.update(visible=False), # processing_msg
49
+ gr.update(visible=True, value=fig), # plot_output
50
  gr.update(visible=True, value=OUT_PATH), # download_output
51
+ gr.update(visible=False) # errorHandler
52
+ )
53
+ else:
54
+ yield (
55
+ gr.update(visible=False), # processing_msg
56
+ gr.update(visible=False, value=None), # plot_output
57
+ gr.update(visible=True, value=OUT_PATH), # download_output
58
+ gr.update(visible=False) # errorHandler
59
+ )
60
+
61
  except Exception as e:
62
  # Mostra errore
63
  yield (
64
  gr.update(visible=False), # processing_msg
65
+ gr.update(visible=False, value=None), # plot_output
66
+ gr.update(visible=False, value=None), # download_output
67
+ gr.update(visible=True, value=f"❌ **Error:** {str(e)}\n\n```\n{traceback.format_exc()}\n```") # errorHandler
68
  )
69
 
70
+
71
  if os.path.exists(OUT_PATH):
72
  os.remove(OUT_PATH)
73
 
 
74
  os.makedirs(os.path.dirname(OUT_PATH), exist_ok=True)
75
  chronos2 = Chronos2Pipeline.from_pretrained("amazon/chronos-2", device_map=device)
76
 
77
 
 
78
  with gr.Blocks(title="Time series anomaly detection with Chronos2") as demo:
79
  gr.Markdown(
80
  """
 
102
 
103
  with gr.Row():
104
  with gr.Column(scale=1):
105
+ file_input = gr.File(label="Upload Time Series Data (CSV)", file_types=[".csv"], file_count="single")
106
  timestamp_question = gr.Radio(
107
  label="Does your data contain a timestamp column?",
108
  choices=["Yes", "No"],
109
  value="No",
110
  interactive=True,
 
111
  )
112
+ timestamp_column_input = gr.Textbox(
113
+ label="Please specify the column name of the timestamp:",
114
+ visible=False,
115
+ value="",
116
+ interactive=False
117
+ )
118
 
119
  timestamp_question.change(
120
+ lambda x: gr.update(
121
+ visible=(x == "Yes"),
122
+ interactive=(x == "Yes"),
123
+ value="timestamp" if x == "Yes" else ""
124
+ ),
125
  inputs=timestamp_question,
126
  outputs=timestamp_column_input
127
  )
128
 
129
+ detect_button = gr.Button("Detect Anomalies", variant="primary")
130
 
131
  with gr.Column(scale=4):
 
 
 
 
132
  processing_msg = gr.Markdown("⏳ Processing file, please wait...", visible=False)
133
+ errorHandler = gr.Markdown(visible=False)
134
+ plot_output = gr.Plot(label="Time Series with Detected Anomalies", visible=False)
135
+ download_output = gr.File(label="Download Anomaly Detection Results (CSV)", visible=False)
136
 
137
+ detect_button.click(
138
+ fn=dataProcessing,
139
+ inputs=[file_input, timestamp_column_input],
140
+ outputs=[processing_msg, plot_output, download_output, errorHandler]
141
+ )
 
 
 
 
142
 
143
  demo.launch(share=True)