Spaces:
Sleeping
Sleeping
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() | |