GitHub Actions commited on
Commit
e7181fd
·
1 Parent(s): aaf5034

Sync App from main repo

Browse files
Files changed (1) hide show
  1. app.py +51 -27
app.py CHANGED
@@ -38,11 +38,32 @@ classification_selected = st.selectbox("Classification type", classification)
38
 
39
  model_name = f"{model_selected} {classification_selected}"
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  st.markdown("""Upload a CSV file with single heartbeat (csv with 180 points) or load from available examples
42
  """)
43
 
44
  # Option to upload or load a file
45
- option = st.radio("Choose input method", ("Load example file", "Upload CSV file", "Upload Apple Watch ECG CSV file"))
46
 
47
  if option == "Load example file":
48
  # Load example files from Hugging Face dataset
@@ -57,6 +78,8 @@ if option == "Load example file":
57
  uploaded_file.name = example_selected # Set a name attribute to mimic the uploaded file
58
  df = pd.read_csv(uploaded_file)
59
  # st.write("Loaded Data:", df)
 
 
60
 
61
  elif option == "Upload CSV file":
62
  # File uploader
@@ -65,8 +88,10 @@ elif option == "Upload CSV file":
65
  if uploaded_file is not None:
66
  df = pd.read_csv(uploaded_file)
67
  # st.write("Uploaded Data:", df)
 
 
68
 
69
- elif option == "Upload Apple Watch ECG CSV file":
70
  # File uploader
71
  st.write("DISCLAIMER: this is an experimental feature, and the results may not be accurate. This should not be used as professional medical advice.")
72
  uploaded_file = st.file_uploader("Upload a CSV file", type="csv")
@@ -76,29 +101,28 @@ elif option == "Upload Apple Watch ECG CSV file":
76
  df = pd.read_csv(uploaded_file)
77
  # st.write("Uploaded Data:", df)
78
 
79
- # Visualize data
80
- if 'df' in locals():
81
- st.write("Visualized Data:")
82
- fig, ax = plt.subplots(figsize=(10, 6))
83
- df.iloc[0].plot(ax=ax)
84
- st.pyplot(fig)
85
 
86
- if st.button("Predict"):
87
- model = models[model_name]
88
-
89
- # Reset the file pointer to the beginning
90
- uploaded_file.seek(0)
91
-
92
- # Call the API with the file directly
93
- response = requests.post(
94
- f"https://fabriciojm-hadt-api.hf.space/predict?model_name={model}",
95
- files={"filepath_csv": (uploaded_file.name, uploaded_file, "text/csv")}
96
- )
97
-
98
- if response.status_code == 200:
99
- prediction = response.json()["prediction"]
100
- st.write(f"Prediction using {model_name}:")
101
- for i, p in enumerate(prediction):
102
- st.write(f"Beat {i+1}: {beat_labels[p]} (class {p})") # {beat_labels[prediction]} (class {prediction}) heartbeat
103
- else:
104
- st.error(f"Error: {response.json().get('detail', 'Unknown error')}")
 
 
 
 
 
 
38
 
39
  model_name = f"{model_selected} {classification_selected}"
40
 
41
+ def visualize_single(df, st):
42
+ st.write("Visualized Data:")
43
+ fig, ax = plt.subplots(figsize=(10, 6))
44
+ df.iloc[0].plot(ax=ax)
45
+ st.pyplot(fig)
46
+
47
+ # This function will be used when the API is capable of returning extracted beats
48
+ # def visualize_multiple(beats, st):
49
+ # st.write("Visualized Data:")
50
+ # if len(beats) % 4 != 0:
51
+ # nrows = len(beats) // 4 + 1
52
+ # else:
53
+ # nrows = len(beats) // 4
54
+ # fig, axs = plt.subplots(nrows, 4, figsize=(10, nrows*2.5))
55
+ # for i, beat in enumerate(beats):
56
+ # axs.flatten()[i].plot(beat)
57
+ # # delete last plots if not used
58
+ # for j in range(len(beats)%4):
59
+ # fig.delaxes(axs.flatten()[-j-1])
60
+ # st.pyplot(fig)
61
+
62
  st.markdown("""Upload a CSV file with single heartbeat (csv with 180 points) or load from available examples
63
  """)
64
 
65
  # Option to upload or load a file
66
+ option = st.radio("Choose input method", ("Load example file", "Upload CSV file", "Upload Apple Watch ECG CSV file (EXPERIMENTAL)"))
67
 
68
  if option == "Load example file":
69
  # Load example files from Hugging Face dataset
 
78
  uploaded_file.name = example_selected # Set a name attribute to mimic the uploaded file
79
  df = pd.read_csv(uploaded_file)
80
  # st.write("Loaded Data:", df)
81
+ if 'df' in locals():
82
+ visualize_single(df, st)
83
 
84
  elif option == "Upload CSV file":
85
  # File uploader
 
88
  if uploaded_file is not None:
89
  df = pd.read_csv(uploaded_file)
90
  # st.write("Uploaded Data:", df)
91
+ if 'df' in locals():
92
+ visualize_single(df, st)
93
 
94
+ elif option == "Upload Apple Watch ECG CSV file (EXPERIMENTAL)":
95
  # File uploader
96
  st.write("DISCLAIMER: this is an experimental feature, and the results may not be accurate. This should not be used as professional medical advice.")
97
  uploaded_file = st.file_uploader("Upload a CSV file", type="csv")
 
101
  df = pd.read_csv(uploaded_file)
102
  # st.write("Uploaded Data:", df)
103
 
 
 
 
 
 
 
104
 
105
+ if st.button("Predict"):
106
+ model = models[model_name]
107
+
108
+ # Reset the file pointer to the beginning
109
+ uploaded_file.seek(0)
110
+
111
+ # Call the API with the file directly
112
+ base_url = "https://fabriciojm-hadt-api.hf.space/predict"
113
+ if option == "Upload Apple Watch ECG CSV file (EXPERIMENTAL)":
114
+ base_url += "_multibeats"
115
+ print(f"Request url: {base_url}?model_name={model}")
116
+ response = requests.post(
117
+ f"{base_url}?model_name={model}",
118
+ files={"filepath_csv": (uploaded_file.name, uploaded_file, "text/csv")}
119
+ )
120
+
121
+ if response.status_code == 200:
122
+ prediction = response.json()["prediction"]
123
+ st.write(f"Prediction using {model_name}:")
124
+ for i, p in enumerate(prediction):
125
+ st.write(f"Beat {i+1}: {beat_labels[p]} (class {p})") # {beat_labels[prediction]} (class {prediction}) heartbeat
126
+ else:
127
+ st.error(f"Error: {response.json().get('detail', 'Unknown error')}")
128
+