File size: 2,935 Bytes
0d5a7ab
9485251
 
 
 
0d5a7ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9485251
 
 
 
 
0d5a7ab
 
 
9485251
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d5a7ab
 
 
 
 
 
 
 
 
9485251
 
 
 
 
 
 
 
 
 
 
0d5a7ab
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import gradio as gr
from src import plot_time_series
from src import detect_anomalies
from src.utils import update_controls


PROJECT_HTML_PATH = "sections/project_description.html"
WELCOME_HTML_PATH = "sections/welcome_section.html"
DEMO_HTML_PATH = "sections/demo_section.html"
TRY_IT_YOURSELF_HTML_PATH = "sections/try_it_yourself.html"
CSS_FILE_PATH = "sections/styles/blocks.css"
ANOMALY_METHODS = ["LSTM", "ARIMA", "IQR"]

with open(PROJECT_HTML_PATH, "r") as file:
    project_description_html = file.read()
with open(WELCOME_HTML_PATH, "r") as file:
    welcome_html = file.read()
with open(DEMO_HTML_PATH, "r") as file:
    demo_section_html = file.read()
with open(TRY_IT_YOURSELF_HTML_PATH, "r") as file:
    try_it_yourself_html = file.read()

with open(CSS_FILE_PATH, "r") as css_file:
    blocks_css = css_file.read()

with gr.Blocks(css=blocks_css) as demo:    
    gr.HTML(project_description_html)
    gr.Markdown(welcome_html)
    gr.Markdown(demo_section_html)
    gr.HTML(try_it_yourself_html)

    file_input = gr.File(
        label="Upload Time Series CSV",
        file_types=[".csv"],
        value="examples/mpox.csv"  # Set default example file
    )
    plot_btn = gr.Button("Plot Time Series")
    plot_output = gr.Plot(label="Time Series Plot")

    with gr.Row():
        method_input = gr.Dropdown(
            choices=ANOMALY_METHODS, 
            interactive=True,
            label="Select Anomaly Detection Method", 
            value="LSTM"
        )
        k_input = gr.Slider(
            minimum=1, 
            maximum=3, 
            step=0.1, 
            label="k", 
            value=1.5
        )
        percentile_input = gr.Slider(
            minimum=0, 
            maximum=100, 
            step=1, 
            label="Percentile", 
            value=95,
            interactive=True
        )
        threshold_method_input = gr.Dropdown(
            choices=[
                "IQR on (ground truth - forecast)", 
                "IQR on |ground truth - forecast|", 
                "IQR on |ground truth - forecast|/forecast", 
                "Percentile threshold on absolute loss", 
                "Percentile threshold on raw loss"
            ],
            label="Threshold Method",
            value="IQR on (ground truth - forecast)",
            interactive=True
        )
    analyze_btn = gr.Button("Detect Anomalies")
    anomaly_output = gr.Plot(label="Anomaly Detection Results")
    
    plot_btn.click(
        fn=plot_time_series, 
        inputs=[file_input], 
        outputs=[plot_output]
    )

    method_input.change(
        fn=update_controls,
        inputs=[method_input],
        outputs=[percentile_input, threshold_method_input]
    )

    analyze_btn.click(
        fn=detect_anomalies,
        inputs=[file_input, method_input, k_input, percentile_input, threshold_method_input],
        outputs=[anomaly_output]
    )

demo.launch(show_api=False)