Spaces:
Sleeping
Sleeping
refactor: call apis sequencially
Browse files
app.py
CHANGED
|
@@ -19,7 +19,7 @@ ptbxl_df = ptbxl_df[selected_columns]
|
|
| 19 |
with gr.Blocks() as demo:
|
| 20 |
with gr.Row():
|
| 21 |
gr.Textbox(
|
| 22 |
-
"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.",
|
| 23 |
label="Information",
|
| 24 |
lines=2,
|
| 25 |
)
|
|
@@ -44,40 +44,35 @@ with gr.Blocks() as demo:
|
|
| 44 |
|
| 45 |
def get_ecg_id_from_dataframe(df: pd.DataFrame, evt: gr.SelectData):
|
| 46 |
gr.Info(
|
| 47 |
-
"The analysis of the selected ECG may take up to
|
| 48 |
duration=15,
|
| 49 |
)
|
| 50 |
return {ecg_id_output: evt.row_value[0], gr_df: gr.Dataframe(visible=False)}
|
| 51 |
|
| 52 |
-
def
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
f"{ALFRED_URL}/hf_demo/ptbxl_to_image",
|
| 55 |
params={"ecg_id": ecg_id},
|
| 56 |
timeout=600,
|
| 57 |
)
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
ecg_viewer: imread(BytesIO(response.content)),
|
| 61 |
-
gr_df: gr.Dataframe(visible=True),
|
| 62 |
-
}
|
| 63 |
|
| 64 |
-
|
| 65 |
-
response = requests.post(
|
| 66 |
-
f"{ALFRED_URL}/hf_demo/alfred", params={"ecg_id": ecg_id}, timeout=600
|
| 67 |
-
)
|
| 68 |
-
response.raise_for_status()
|
| 69 |
-
return literal_eval(response.text)
|
| 70 |
|
| 71 |
gr_df.select(
|
| 72 |
fn=get_ecg_id_from_dataframe, inputs=gr_df, outputs=[ecg_id_output, gr_df]
|
| 73 |
)
|
| 74 |
ecg_id_output.change(
|
| 75 |
-
fn=
|
| 76 |
inputs=ecg_id_output,
|
| 77 |
-
outputs=[ecg_viewer, gr_df],
|
| 78 |
-
)
|
| 79 |
-
ecg_id_output.change(
|
| 80 |
-
fn=get_alfred_from_dataframe, inputs=ecg_id_output, outputs=alfred_result
|
| 81 |
)
|
| 82 |
|
| 83 |
demo.launch()
|
|
|
|
| 19 |
with gr.Blocks() as demo:
|
| 20 |
with gr.Row():
|
| 21 |
gr.Textbox(
|
| 22 |
+
"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.",
|
| 23 |
label="Information",
|
| 24 |
lines=2,
|
| 25 |
)
|
|
|
|
| 44 |
|
| 45 |
def get_ecg_id_from_dataframe(df: pd.DataFrame, evt: gr.SelectData):
|
| 46 |
gr.Info(
|
| 47 |
+
"The analysis of the selected ECG may take up to 5 minutes. Please wait patiently.",
|
| 48 |
duration=15,
|
| 49 |
)
|
| 50 |
return {ecg_id_output: evt.row_value[0], gr_df: gr.Dataframe(visible=False)}
|
| 51 |
|
| 52 |
+
def get_analysis_and_image(ecg_id):
|
| 53 |
+
alfred_response = requests.post(
|
| 54 |
+
f"{ALFRED_URL}/hf_demo/alfred", params={"ecg_id": ecg_id}, timeout=600
|
| 55 |
+
)
|
| 56 |
+
alfred_response.raise_for_status()
|
| 57 |
+
alfred_result = literal_eval(alfred_response.text)
|
| 58 |
+
|
| 59 |
+
image_response = requests.post(
|
| 60 |
f"{ALFRED_URL}/hf_demo/ptbxl_to_image",
|
| 61 |
params={"ecg_id": ecg_id},
|
| 62 |
timeout=600,
|
| 63 |
)
|
| 64 |
+
image_response.raise_for_status()
|
| 65 |
+
ecg_image = imread(BytesIO(image_response.content))
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
return [alfred_result, ecg_image, gr.Dataframe(visible=True)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
gr_df.select(
|
| 70 |
fn=get_ecg_id_from_dataframe, inputs=gr_df, outputs=[ecg_id_output, gr_df]
|
| 71 |
)
|
| 72 |
ecg_id_output.change(
|
| 73 |
+
fn=get_analysis_and_image,
|
| 74 |
inputs=ecg_id_output,
|
| 75 |
+
outputs=[alfred_result, ecg_viewer, gr_df],
|
|
|
|
|
|
|
|
|
|
| 76 |
)
|
| 77 |
|
| 78 |
demo.launch()
|