from io import BytesIO import gradio as gr import pandas as pd import requests from matplotlib.image import imread from ast import literal_eval ALFRED_URL = "http://106.254.240.186:30007" # ALFRED_URL = "http://192.168.1.11:30007" selected_columns = ["ecg_id", "patient_id", "age", "sex", "scp_codes", "report"] ptbxl_df = pd.read_csv("./res/ptbxl_database.csv") ptbxl_df = ptbxl_df[selected_columns] def get_ecg_id_from_dataframe(df: pd.DataFrame, evt: gr.SelectData): gr.Info( "The analysis of the selected ECG will take about 1 to 2 minutes. Please wait patiently.", duration=15, ) return evt.row_value[0] def get_ecg_image_from_dataframe(ecg_id): response = requests.post( f"{ALFRED_URL}/hf_demo/ptbxl_to_image", params={"ecg_id": ecg_id}, timeout=600 ) response.raise_for_status() return imread(BytesIO(response.content)) def get_alfred_from_dataframe(ecg_id): response = requests.post( f"{ALFRED_URL}/hf_demo/alfred", params={"ecg_id": ecg_id}, timeout=600 ) response.raise_for_status() return literal_eval(response.text) with gr.Blocks() as demo: with gr.Row(): ecg_id_output = gr.Textbox( "It takes about 10 seconds to load the PTB-XL table. Please wait for a moment. \nWhen you select the ECG to analyze from the PTB-XL table below, Alfred will begin the analysis.", label="Information", lines=2, ) with gr.Row(): gr_df = gr.Dataframe( value=ptbxl_df, interactive=False, max_height=200, label="All PTB-XL v1.3.0 data (https://physionet.org/content/ptb-xl/1.0.3/ptbxl_database.csv). You can refer to the following URL(https://physionet.org/content/ptb-xl/1.0.3/scp_statements.csv) to understand what 'scp_codes' represent.", ) with gr.Row(): ecg_id_output = gr.Textbox(label="The selected ecg_id is") with gr.Row(): ecg_viewer = gr.Image(interactive=False, label="The selected ecg is") with gr.Row(): alfred_result = gr.Markdown( value="", label="Alfred said that", min_height=300, container=True ) gr_df.select(fn=get_ecg_id_from_dataframe, inputs=gr_df, outputs=ecg_id_output) ecg_id_output.change( fn=get_ecg_image_from_dataframe, inputs=ecg_id_output, outputs=ecg_viewer ) ecg_id_output.change( fn=get_alfred_from_dataframe, inputs=ecg_id_output, outputs=alfred_result ) demo.launch()