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)