Protect against multiple concurrent downloads
Browse files- main.py +31 -17
- requirements.txt +1 -0
main.py
CHANGED
|
@@ -12,6 +12,7 @@ from csscolor import parse
|
|
| 12 |
import subprocess
|
| 13 |
import matplotlib.pyplot as plt
|
| 14 |
import urllib.parse
|
|
|
|
| 15 |
|
| 16 |
# Define constants
|
| 17 |
path = 'ptb-xl-a-large-publicly-available-electrocardiography-dataset-1.0.1/'
|
|
@@ -67,24 +68,37 @@ Link to the new site: [{site_link}]({site_link}?{urllib.parse.urlencode(st.exper
|
|
| 67 |
# Download data from kaggle
|
| 68 |
if not os.path.isfile(path + 'ptbxl_database.csv'):
|
| 69 |
placeholder = st.empty()
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
try:
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
st.stop()
|
| 89 |
|
| 90 |
|
|
|
|
| 12 |
import subprocess
|
| 13 |
import matplotlib.pyplot as plt
|
| 14 |
import urllib.parse
|
| 15 |
+
import psutil
|
| 16 |
|
| 17 |
# Define constants
|
| 18 |
path = 'ptb-xl-a-large-publicly-available-electrocardiography-dataset-1.0.1/'
|
|
|
|
| 68 |
# Download data from kaggle
|
| 69 |
if not os.path.isfile(path + 'ptbxl_database.csv'):
|
| 70 |
placeholder = st.empty()
|
| 71 |
+
already_downloading = False
|
| 72 |
+
for proc in psutil.process_iter():
|
| 73 |
+
try:
|
| 74 |
+
# Check if process name contains the given name string.
|
| 75 |
+
if "kaggle" in proc.name().lower():
|
| 76 |
+
already_downloading = True
|
| 77 |
+
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
| 78 |
+
pass
|
| 79 |
+
if not already_downloading:
|
| 80 |
try:
|
| 81 |
+
placeholder.info(
|
| 82 |
+
"**Downloading data.**\nThis may take a minute, but it only needs to be done once.", icon="⏳")
|
| 83 |
+
subprocess.run(['pip', 'uninstall', '-y', 'kaggle'])
|
| 84 |
+
subprocess.run(['pip', 'install', '--user', 'kaggle'])
|
| 85 |
+
try:
|
| 86 |
+
# Streamlit cloud
|
| 87 |
+
subprocess.run(['/home/appuser/.local/bin/kaggle', 'datasets', 'download',
|
| 88 |
+
'khyeh0719/ptb-xl-dataset', '--unzip'])
|
| 89 |
+
except:
|
| 90 |
+
# Hugging Face
|
| 91 |
+
subprocess.run(['/home/user/.local/bin/kaggle', 'datasets', 'download',
|
| 92 |
+
'khyeh0719/ptb-xl-dataset', '--unzip'])
|
| 93 |
+
placeholder.empty()
|
| 94 |
+
except Exception as error:
|
| 95 |
+
placeholder.warning(
|
| 96 |
+
"An error occurred while downloading the data. Please take a screenshot of the whole page and send to the developer.")
|
| 97 |
+
st.write(error)
|
| 98 |
+
st.stop()
|
| 99 |
+
else:
|
| 100 |
+
placeholder.info(
|
| 101 |
+
"**Downloading data.**\nPlease refresh the page after a few minutes.", icon="⏳")
|
| 102 |
st.stop()
|
| 103 |
|
| 104 |
|
requirements.txt
CHANGED
|
@@ -6,3 +6,4 @@ pandas==2.0.0
|
|
| 6 |
streamlit==1.22.0
|
| 7 |
streamlit_javascript==0.1.5
|
| 8 |
wfdb==4.1.2
|
|
|
|
|
|
| 6 |
streamlit==1.22.0
|
| 7 |
streamlit_javascript==0.1.5
|
| 8 |
wfdb==4.1.2
|
| 9 |
+
psutil==5.9.5
|