File size: 3,512 Bytes
7414e20
 
 
 
5dbe791
7414e20
 
 
5dbe791
 
496a669
7414e20
 
 
 
 
 
 
61f41bd
 
 
 
 
 
 
 
 
 
cb786a7
 
 
 
 
 
 
 
 
 
 
 
 
 
5a09515
cb786a7
 
 
 
5a09515
cb786a7
 
1e2bd55
cb786a7
 
 
 
1e2bd55
cb786a7
 
 
 
 
 
fbffce1
cb786a7
 
 
 
 
a5a870e
 
cb786a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e2bd55
5a09515
7414e20
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
from io import BytesIO
import gradio as gr
import pandas as pd
import requests
import os
from matplotlib.image import imread


# hf ์˜ secret ์œผ๋กœ ์„ค์ •, ๋กœ์ปฌ ํ…Œ์ŠคํŠธ์‹œ์—๋Š” ๋‚ด๋ถ€ ip ์‚ฌ์šฉ
ALFRED_URL = os.getenv("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]


with gr.Blocks() as demo:
    # with gr.Tab("Example"):
    #     with gr.Row():
    #         gr.Textbox(
    #             "The test app is currently not functioning properly due to DNS issues. Below is the output of the actual app running internally. \nIf the service is functioning properly, You can use 'App' tab.",
    #             label="Information",
    #             lines=2,
    #         )
    #     with gr.Row():
    #         gr.Image("./res/screenshot_alfred-min.png")

    with gr.Tab("App"):
        with gr.Row():
            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. \nThe analysis by Alfred may take up to 5 minutes. Please wait patiently.",
                label="Information",
                lines=3,
            )
        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", interactive=False
            )

        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
            )

        def get_ecg_id_from_dataframe(df: pd.DataFrame, evt: gr.SelectData):
            gr.Info(
                "The analysis of the selected ECG may take up to 5 minutes. Please wait patiently.",
                duration=15,
            )
            return {ecg_id_output: evt.row_value[0], gr_df: gr.Dataframe(visible=False)}

        def get_analysis_and_image(ecg_id):
            alfred_response = requests.post(
                f"{ALFRED_URL}/hf_demo/alfred", params={"ecg_id": ecg_id}, timeout=600
            )
            alfred_response.raise_for_status()
            res = alfred_response.json()
            alfred_result = res.get("answer")

            image_response = requests.post(
                f"{ALFRED_URL}/hf_demo/ptbxl_to_image",
                params={"ecg_id": ecg_id},
                timeout=600,
            )
            image_response.raise_for_status()
            ecg_image = imread(BytesIO(image_response.content))

            return [alfred_result, ecg_image, gr.Dataframe(visible=True)]

        gr_df.select(
            fn=get_ecg_id_from_dataframe, inputs=gr_df, outputs=[ecg_id_output, gr_df]
        )
        ecg_id_output.change(
            fn=get_analysis_and_image,
            inputs=ecg_id_output,
            outputs=[alfred_result, ecg_viewer, gr_df],
        )

demo.launch()