Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -55,63 +55,57 @@ def run_inference():
|
|
55 |
print("Script 'inference.sh' has started running.")
|
56 |
except subprocess.CalledProcessError as e:
|
57 |
print(f"Error while running the script: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
if uploaded_zip_file is not None:
|
60 |
-
try:
|
61 |
install_dependencies()
|
62 |
-
|
63 |
temp_dir = "/home/user/app/C2C/temp_dicom_dir"
|
64 |
-
|
65 |
os.makedirs(temp_dir, exist_ok=True)
|
66 |
-
full_path = os.path.abspath(temp_dir)
|
67 |
-
st.success("Zip file uploaded successfully")
|
68 |
|
69 |
-
|
70 |
with zipfile.ZipFile(uploaded_zip_file, "r") as zip_ref:
|
71 |
zip_ref.extractall(temp_dir)
|
72 |
|
73 |
dicom_files = [os.path.join(temp_dir, f) for f in os.listdir(temp_dir) if f.endswith(".dcm")]
|
74 |
-
dicom_files.sort()
|
75 |
|
76 |
# Anonymize DICOM files
|
77 |
for file_path in dicom_files:
|
78 |
ds = pydicom.dcmread(file_path)
|
79 |
-
|
80 |
# Anonymize personal information
|
81 |
if 'PatientName' in ds:
|
82 |
ds.PatientName = 'Anonymized'
|
83 |
if 'PatientID' in ds:
|
84 |
ds.PatientID = '00000000'
|
85 |
-
|
86 |
-
print("Anonymized")
|
87 |
-
|
88 |
ds.save_as(file_path)
|
89 |
-
|
90 |
except Exception as e:
|
91 |
st.error(f"Error: {str(e)}")
|
92 |
|
93 |
if st.button("Analyze"):
|
94 |
-
|
95 |
st.success("Analysis started (expected time: 5 mins)")
|
96 |
run_inference()
|
97 |
|
98 |
-
|
99 |
outputs_directory = "/home/user/app/C2C/outputs"
|
|
|
|
|
|
|
|
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
subdirectories = [subdir for subdir in os.listdir(outputs_directory)
|
104 |
-
if os.path.isdir(os.path.join(outputs_directory, subdir))]
|
105 |
-
|
106 |
first_subdirectory = subdirectories[0] if subdirectories else None
|
107 |
|
108 |
if first_subdirectory:
|
109 |
subdirectory_path = os.path.join(outputs_directory, first_subdirectory)
|
110 |
temp_dicom_dir_path = os.path.join(subdirectory_path, "temp_dicom_dir")
|
111 |
|
112 |
-
dicom_subdirectories = [subdir for subdir in os.listdir(temp_dicom_dir_path)
|
113 |
-
if os.path.isdir(os.path.join(temp_dicom_dir_path, subdir))]
|
114 |
-
|
115 |
first_dicom_subdirectory = dicom_subdirectories[0] if dicom_subdirectories else None
|
116 |
|
117 |
if first_dicom_subdirectory:
|
@@ -135,4 +129,4 @@ if st.button("Analyze"):
|
|
135 |
else:
|
136 |
st.warning("No subdirectories or folders found inside 'temp_dicom_dir'.")
|
137 |
else:
|
138 |
-
st.warning("No subdirectories or folders found inside the 'outputs' directory.")
|
|
|
55 |
print("Script 'inference.sh' has started running.")
|
56 |
except subprocess.CalledProcessError as e:
|
57 |
print(f"Error while running the script: {e}")
|
58 |
+
def zip_dir(directory, zip_filename):
|
59 |
+
with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
60 |
+
for root, dirs, files in os.walk(directory):
|
61 |
+
for file in files:
|
62 |
+
zipf.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), os.path.join(directory, '..')))
|
63 |
+
|
64 |
+
uploaded_zip_file = st.file_uploader("Upload a .zip file", type=["zip"])
|
65 |
|
66 |
if uploaded_zip_file is not None:
|
67 |
+
try:
|
68 |
install_dependencies()
|
69 |
+
|
70 |
temp_dir = "/home/user/app/C2C/temp_dicom_dir"
|
|
|
71 |
os.makedirs(temp_dir, exist_ok=True)
|
|
|
|
|
72 |
|
|
|
73 |
with zipfile.ZipFile(uploaded_zip_file, "r") as zip_ref:
|
74 |
zip_ref.extractall(temp_dir)
|
75 |
|
76 |
dicom_files = [os.path.join(temp_dir, f) for f in os.listdir(temp_dir) if f.endswith(".dcm")]
|
77 |
+
dicom_files.sort()
|
78 |
|
79 |
# Anonymize DICOM files
|
80 |
for file_path in dicom_files:
|
81 |
ds = pydicom.dcmread(file_path)
|
|
|
82 |
# Anonymize personal information
|
83 |
if 'PatientName' in ds:
|
84 |
ds.PatientName = 'Anonymized'
|
85 |
if 'PatientID' in ds:
|
86 |
ds.PatientID = '00000000'
|
|
|
|
|
|
|
87 |
ds.save_as(file_path)
|
|
|
88 |
except Exception as e:
|
89 |
st.error(f"Error: {str(e)}")
|
90 |
|
91 |
if st.button("Analyze"):
|
|
|
92 |
st.success("Analysis started (expected time: 5 mins)")
|
93 |
run_inference()
|
94 |
|
|
|
95 |
outputs_directory = "/home/user/app/C2C/outputs"
|
96 |
+
if os.path.exists(outputs_directory):
|
97 |
+
zip_filename = "/home/user/app/C2C/outputs.zip"
|
98 |
+
zip_dir(outputs_directory, zip_filename)
|
99 |
+
st.markdown(f"Download your outputs [here](/{zip_filename})")
|
100 |
|
101 |
+
subdirectories = [subdir for subdir in os.listdir(outputs_directory) if os.path.isdir(os.path.join(outputs_directory, subdir))]
|
|
|
|
|
|
|
|
|
102 |
first_subdirectory = subdirectories[0] if subdirectories else None
|
103 |
|
104 |
if first_subdirectory:
|
105 |
subdirectory_path = os.path.join(outputs_directory, first_subdirectory)
|
106 |
temp_dicom_dir_path = os.path.join(subdirectory_path, "temp_dicom_dir")
|
107 |
|
108 |
+
dicom_subdirectories = [subdir for subdir in os.listdir(temp_dicom_dir_path) if os.path.isdir(os.path.join(temp_dicom_dir_path, subdir))]
|
|
|
|
|
109 |
first_dicom_subdirectory = dicom_subdirectories[0] if dicom_subdirectories else None
|
110 |
|
111 |
if first_dicom_subdirectory:
|
|
|
129 |
else:
|
130 |
st.warning("No subdirectories or folders found inside 'temp_dicom_dir'.")
|
131 |
else:
|
132 |
+
st.warning("No subdirectories or folders found inside the 'outputs' directory.")
|